From 0009c239d29ef0730f61937b95ccc012382ac373 Mon Sep 17 00:00:00 2001 From: Zxilly Date: Thu, 6 Oct 2022 23:22:40 +0800 Subject: [PATCH] fix: update channel correct --- app/build.gradle | 4 +- .../learningman/push/activity/MainActivity.kt | 46 +++++++++---------- .../push/activity/SettingsActivity.kt | 42 +++++++++-------- .../top/learningman/push/provider/Channel.kt | 4 +- .../top/learningman/push/provider/FCM.kt | 5 -- 5 files changed, 50 insertions(+), 51 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 279f376..443799e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -147,7 +147,6 @@ dependencies { implementation "com.google.firebase:firebase-messaging-ktx" implementation "androidx.constraintlayout:constraintlayout:2.1.4" - implementation "androidx.preference:preference-ktx:1.2.0" implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2' implementation 'androidx.navigation:navigation-ui-ktx:2.5.2' @@ -164,6 +163,7 @@ dependencies { implementation "androidx.fragment:fragment-ktx:1.5.3" implementation "androidx.activity:activity-ktx:1.6.0" + implementation "androidx.preference:preference-ktx:1.2.0" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0" @@ -192,5 +192,5 @@ dependencies { implementation 'com.github.Zxilly:SetupWizardLib:master-SNAPSHOT' implementation 'com.github.XomaDev:MIUI-autostart:master-SNAPSHOT' - implementation 'com.github.Zxilly:upgrader:0.0.4' + implementation 'com.github.Zxilly:upgrader:0.0.5' } \ No newline at end of file diff --git a/app/src/main/kotlin/top/learningman/push/activity/MainActivity.kt b/app/src/main/kotlin/top/learningman/push/activity/MainActivity.kt index 261bd04..d1ba183 100644 --- a/app/src/main/kotlin/top/learningman/push/activity/MainActivity.kt +++ b/app/src/main/kotlin/top/learningman/push/activity/MainActivity.kt @@ -6,6 +6,7 @@ import android.content.res.ColorStateList import android.os.Bundle import android.util.Log import android.widget.Toast +import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen @@ -22,8 +23,6 @@ import top.learningman.push.utils.APIUtils import top.learningman.push.utils.PermissionManager class MainActivity : AppCompatActivity() { - private lateinit var currentUserID: String - private var inited = false private val repo by lazy { (application as MainApplication).repo @@ -32,6 +31,20 @@ class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding private lateinit var channel: Channel + private val startSetting = + registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + Log.d("MainActivity", "Setting Activity Result ${it.resultCode}") + if (it.resultCode and SettingsActivity.UPDATE_CHANNEL == SettingsActivity.UPDATE_CHANNEL) { + Log.d("MainActivity", "Update Channel") + val channel = AutoChannel.getInstance(context = this, nocache = true) + channel.init(context = this) + channel.setUserCallback(this, repo.getUser(), lifecycleScope) + } else if (it.resultCode and SettingsActivity.UPDATE_USERNAME == SettingsActivity.UPDATE_USERNAME) { + Log.d("MainActivity", "Update User") + channel.setUserCallback(this, repo.getUser(), lifecycleScope) + } + } + override fun onCreate(savedInstanceState: Bundle?) { installSplashScreen() super.onCreate(savedInstanceState) @@ -48,7 +61,7 @@ class MainActivity : AppCompatActivity() { setContentView(binding.root) binding.goSetting.setOnClickListener { - startActivity(Intent(this, SettingsActivity::class.java)) + startSetting.launch(Intent(this, SettingsActivity::class.java)) } binding.goHistory.setOnClickListener { @@ -61,30 +74,12 @@ class MainActivity : AppCompatActivity() { } val userid = repo.getUser() - currentUserID = userid if (userid == Repo.PREF_USER_DEFAULT) { setStatus(RegStatus.NOT_SET) - } - channel.init(this) - } - - override fun onResume() { - super.onResume() - - val userid = repo.getUser() - - if (userid == currentUserID && inited) { - return - } - - inited = true - - if (userid != Repo.PREF_USER_DEFAULT) { + } else { setStatus(RegStatus.PENDING) - channel.setUserCallback(this, userid, lifecycleScope) - lifecycleScope.launch { APIUtils.check(userid) .onSuccess { @@ -94,9 +89,12 @@ class MainActivity : AppCompatActivity() { setStatus(RegStatus.FAILED) } } - } else { - setStatus(RegStatus.NOT_SET) } + channel.init(this) + } + + override fun onResume() { + super.onResume() } private var status = RegStatus.NOT_SET diff --git a/app/src/main/kotlin/top/learningman/push/activity/SettingsActivity.kt b/app/src/main/kotlin/top/learningman/push/activity/SettingsActivity.kt index 0664e98..53f9a8b 100644 --- a/app/src/main/kotlin/top/learningman/push/activity/SettingsActivity.kt +++ b/app/src/main/kotlin/top/learningman/push/activity/SettingsActivity.kt @@ -9,21 +9,18 @@ import androidx.preference.EditTextPreference import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat +import dev.zxilly.lib.upgrader.Upgrader import dev.zxilly.notify.sdk.Client import kotlinx.coroutines.runBlocking import top.learningman.push.BuildConfig import top.learningman.push.Constant import top.learningman.push.R -import top.learningman.push.application.MainApplication import top.learningman.push.databinding.ActivitySettingsBinding import top.learningman.push.provider.AutoChannel -import top.learningman.push.provider.Channel import top.learningman.push.provider.channels import kotlin.concurrent.thread class SettingsActivity : AppCompatActivity() { - private var nextChannel: Channel? = null - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding = ActivitySettingsBinding.inflate(layoutInflater) @@ -42,13 +39,16 @@ class SettingsActivity : AppCompatActivity() { setPreferencesFromResource(R.xml.root_preferences, rootKey) findPreference("version")?.apply { - setOnPreferenceClickListener { - Log.d("SettingsActivity", "Version Clicked") - (requireActivity().application as MainApplication).upgrader?.tryUpgrade(false) - ?: let { - Toast.makeText(context, "应用内更新未生效", Toast.LENGTH_SHORT).show() - } - true + @Suppress("KotlinConstantConditions") + if (BuildConfig.FLAVOR != "free") { + setOnPreferenceClickListener { + Log.d("SettingsActivity", "Version Clicked") + Upgrader.getInstance()?.tryUpgrade(false) + ?: let { + Toast.makeText(context, "应用内更新未生效", Toast.LENGTH_SHORT).show() + } + true + } } summary = "${BuildConfig.VERSION_NAME} (${BuildConfig.VERSION_CODE})" } @@ -67,6 +67,7 @@ class SettingsActivity : AppCompatActivity() { } } } + (requireActivity() as SettingsActivity).updateResult(UPDATE_USERNAME) true } } @@ -94,10 +95,8 @@ class SettingsActivity : AppCompatActivity() { startActivity(Intent(context, SetupActivity::class.java).apply { action = SetupActivity.PERMISSION_GRANT_ACTION }) - (requireActivity() as SettingsActivity).nextChannel = next - } else { - next.init(context) } + (requireActivity() as SettingsActivity).updateResult(UPDATE_CHANNEL) true } } ?: run { @@ -109,9 +108,16 @@ class SettingsActivity : AppCompatActivity() { override fun onResume() { super.onResume() - if (nextChannel != null) { - nextChannel?.init(this) - nextChannel = null - } + } + + private var resultCode = 0 + private fun updateResult(result: Int) { + resultCode = resultCode or result + setResult(resultCode) + } + + companion object { + const val UPDATE_USERNAME = 1 shl 0 + const val UPDATE_CHANNEL = 1 shl 1 } } \ No newline at end of file diff --git a/app/src/main/kotlin/top/learningman/push/provider/Channel.kt b/app/src/main/kotlin/top/learningman/push/provider/Channel.kt index 5179465..93a38f9 100644 --- a/app/src/main/kotlin/top/learningman/push/provider/Channel.kt +++ b/app/src/main/kotlin/top/learningman/push/provider/Channel.kt @@ -54,8 +54,8 @@ class AutoChannel private constructor(channel: Channel) : Channel by channel { return instance!!.name == chan.name } - fun getInstance(context: Context, channelText: String? = null): Channel { - return if (instance != null && channelText == null) { + fun getInstance(context: Context, channelText: String? = null, nocache: Boolean = false): Channel { + return if (instance != null && channelText == null && !nocache) { instance as Channel } else { var impl: Channel? = null diff --git a/app/src/main/kotlin/top/learningman/push/provider/FCM.kt b/app/src/main/kotlin/top/learningman/push/provider/FCM.kt index ad541e8..79c38ca 100644 --- a/app/src/main/kotlin/top/learningman/push/provider/FCM.kt +++ b/app/src/main/kotlin/top/learningman/push/provider/FCM.kt @@ -11,7 +11,6 @@ import com.google.firebase.messaging.ktx.messaging import com.microsoft.appcenter.crashes.Crashes import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch -import top.learningman.push.BuildConfig import top.learningman.push.utils.APIUtils object FCM : Channel { @@ -25,10 +24,6 @@ object FCM : Channel { } override fun should(context: Context): Boolean { - if (BuildConfig.DEBUG) { - return false - } - val ga = GoogleApiAvailability.getInstance() return when (ga.isGooglePlayServicesAvailable(context)) { ConnectionResult.SUCCESS -> true