From 5098c61ebc9765d2caaa351f2d491184ad979b4d Mon Sep 17 00:00:00 2001 From: Ztiany Date: Fri, 1 Nov 2019 17:05:04 +0800 Subject: [PATCH] fix AppUpgradeChecker --- .../base/utils/upgrade/AppUpgradeChecker.kt | 42 +++++++++++++++---- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/lib_base/src/main/java/com/android/base/utils/upgrade/AppUpgradeChecker.kt b/lib_base/src/main/java/com/android/base/utils/upgrade/AppUpgradeChecker.kt index bfd0be5..9630849 100644 --- a/lib_base/src/main/java/com/android/base/utils/upgrade/AppUpgradeChecker.kt +++ b/lib_base/src/main/java/com/android/base/utils/upgrade/AppUpgradeChecker.kt @@ -19,6 +19,30 @@ import com.blankj.utilcode.util.Utils import timber.log.Timber import java.io.File +/** + * A tool for checking app upgrade. + * + * usage, when automatic check new version: + * + * ``` + * AppUpgradeChecker.checkAppUpgrade() + * ``` + * + * usage, when click to check new version: + * ``` + * if (AppUpgradeChecker.isDownloading) { + * showMessage("正在下载更新") + * return@setOnClickListener + * } + * + * AppUpgradeChecker.checkAppUpgrade(false) + * + * if (!subscribed) { + * subscribeUpgradeInfo() + * subscribed = true + * } + * ``` + */ object AppUpgradeChecker { private var successOnce = false @@ -26,6 +50,9 @@ object AppUpgradeChecker { var isDownloading = false private set + val isChecking: Boolean + get() = currentState.isLoading + private var currentState = State.noChange() set(value) { field = value @@ -33,7 +60,7 @@ object AppUpgradeChecker { } private fun notifyStateChanged(state: State) { - innerLiveState.postValue(state) + innerLiveState.value = state } private val innerLiveState = MutableLiveData>() @@ -54,14 +81,14 @@ object AppUpgradeChecker { AppUpgradeChecker.upgradeInteractor = upgradeInteractor } - fun checkAppUpgrade(force: Boolean = false) { + fun checkAppUpgrade(silence: Boolean = true) { Timber.d("checkAppUpgrade-->currentState == $currentState") /*正在检查*/ if (currentState.isLoading) { return } /*已经检查过了*/ - if (!force && successOnce) { + if (silence && successOnce) { return } /*正在下载*/ @@ -78,7 +105,6 @@ object AppUpgradeChecker { .observeOnUI() .subscribe( { upgradeInfo -> - currentState = State.success(upgradeInfo) successOnce = true processUpdateInfo(upgradeInfo) }, @@ -89,19 +115,19 @@ object AppUpgradeChecker { } private fun processUpdateInfo(upgradeInfo: UpgradeInfo) { + currentState = State.success(upgradeInfo) if (upgradeInfo.isNewVersion) { AppUpgradeChecker.upgradeInfo = upgradeInfo safeContext { upgradeInteractor.showUpgradeDialog(it, upgradeInfo, onCancel = { - currentState = State.success() + //do nothing }, onConfirm = { doUpdate() - }) + } + ) } - } else { - currentState = State.success() } }