Skip to content

Commit

Permalink
fix: update channel correct
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxilly committed Oct 6, 2022
1 parent 3aa88de commit 0009c23
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 51 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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"

Expand Down Expand Up @@ -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'
}
46 changes: 22 additions & 24 deletions app/src/main/kotlin/top/learningman/push/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -42,13 +39,16 @@ class SettingsActivity : AppCompatActivity() {
setPreferencesFromResource(R.xml.root_preferences, rootKey)

findPreference<Preference>("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})"
}
Expand All @@ -67,6 +67,7 @@ class SettingsActivity : AppCompatActivity() {
}
}
}
(requireActivity() as SettingsActivity).updateResult(UPDATE_USERNAME)
true
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
}
4 changes: 2 additions & 2 deletions app/src/main/kotlin/top/learningman/push/provider/Channel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/kotlin/top/learningman/push/provider/FCM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 0009c23

Please sign in to comment.