From ea7320a268d61730c8bbfe40eb311fba5b563726 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Wed, 28 Jun 2023 18:19:42 +0800 Subject: [PATCH] Add observer for new version --- .../com/qhy040404/libraryonetap/constant/GlobalValues.kt | 2 ++ .../ui/fragment/settings/SettingsFragment.kt | 8 +++++--- .../java/com/qhy040404/libraryonetap/utils/UpdateUtils.kt | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/qhy040404/libraryonetap/constant/GlobalValues.kt b/app/src/main/java/com/qhy040404/libraryonetap/constant/GlobalValues.kt index f77a6a86..3e697061 100644 --- a/app/src/main/java/com/qhy040404/libraryonetap/constant/GlobalValues.kt +++ b/app/src/main/java/com/qhy040404/libraryonetap/constant/GlobalValues.kt @@ -1,6 +1,7 @@ package com.qhy040404.libraryonetap.constant import androidx.core.content.edit +import androidx.lifecycle.MutableLiveData import com.qhy040404.libraryonetap.BuildConfig import com.qhy040404.libraryonetap.R import com.qhy040404.libraryonetap.utils.PackageUtils @@ -76,6 +77,7 @@ object GlobalValues { ) } var newVersion: String? = null + var newVersionLiveData: MutableLiveData = MutableLiveData() var latestApkName: String by SPDelegates(Constants.LATEST_APK_NAME, "") // Edu diff --git a/app/src/main/java/com/qhy040404/libraryonetap/ui/fragment/settings/SettingsFragment.kt b/app/src/main/java/com/qhy040404/libraryonetap/ui/fragment/settings/SettingsFragment.kt index fb07de41..18f35d8d 100644 --- a/app/src/main/java/com/qhy040404/libraryonetap/ui/fragment/settings/SettingsFragment.kt +++ b/app/src/main/java/com/qhy040404/libraryonetap/ui/fragment/settings/SettingsFragment.kt @@ -225,9 +225,11 @@ class SettingsFragment : PreferenceFragmentCompat() { } findPreference(Constants.PREF_UPDATE)?.apply { - if (GlobalValues.newVersion != null) { - summary = - R.string.upd_available.getStringAndFormat(GlobalValues.newVersion) + GlobalValues.newVersionLiveData.observe(viewLifecycleOwner) { + if (it.isNullOrEmpty().not()) { + summary = + R.string.upd_available.getStringAndFormat(GlobalValues.newVersion) + } } setOnPreferenceClickListener { lifecycleScope.launch(Dispatchers.IO) { diff --git a/app/src/main/java/com/qhy040404/libraryonetap/utils/UpdateUtils.kt b/app/src/main/java/com/qhy040404/libraryonetap/utils/UpdateUtils.kt index d521ef9c..c5306075 100644 --- a/app/src/main/java/com/qhy040404/libraryonetap/utils/UpdateUtils.kt +++ b/app/src/main/java/com/qhy040404/libraryonetap/utils/UpdateUtils.kt @@ -113,6 +113,7 @@ object UpdateUtils { .split("\r\n") GlobalValues.newVersion = versionName + GlobalValues.newVersionLiveData.postValue(versionName) val dialogBody = buildSpannedString { inSpans(