fix AppUpgradeChecker

androidx
Ztiany 5 years ago
parent 08c863db3b
commit 5098c61ebc
  1. 42
      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 timber.log.Timber
import java.io.File 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 { object AppUpgradeChecker {
private var successOnce = false private var successOnce = false
@ -26,6 +50,9 @@ object AppUpgradeChecker {
var isDownloading = false var isDownloading = false
private set private set
val isChecking: Boolean
get() = currentState.isLoading
private var currentState = State.noChange<UpgradeInfo>() private var currentState = State.noChange<UpgradeInfo>()
set(value) { set(value) {
field = value field = value
@ -33,7 +60,7 @@ object AppUpgradeChecker {
} }
private fun notifyStateChanged(state: State<UpgradeInfo>) { private fun notifyStateChanged(state: State<UpgradeInfo>) {
innerLiveState.postValue(state) innerLiveState.value = state
} }
private val innerLiveState = MutableLiveData<State<UpgradeInfo>>() private val innerLiveState = MutableLiveData<State<UpgradeInfo>>()
@ -54,14 +81,14 @@ object AppUpgradeChecker {
AppUpgradeChecker.upgradeInteractor = upgradeInteractor AppUpgradeChecker.upgradeInteractor = upgradeInteractor
} }
fun checkAppUpgrade(force: Boolean = false) { fun checkAppUpgrade(silence: Boolean = true) {
Timber.d("checkAppUpgrade-->currentState == $currentState") Timber.d("checkAppUpgrade-->currentState == $currentState")
/*正在检查*/ /*正在检查*/
if (currentState.isLoading) { if (currentState.isLoading) {
return return
} }
/*已经检查过了*/ /*已经检查过了*/
if (!force && successOnce) { if (silence && successOnce) {
return return
} }
/*正在下载*/ /*正在下载*/
@ -78,7 +105,6 @@ object AppUpgradeChecker {
.observeOnUI() .observeOnUI()
.subscribe( .subscribe(
{ upgradeInfo -> { upgradeInfo ->
currentState = State.success(upgradeInfo)
successOnce = true successOnce = true
processUpdateInfo(upgradeInfo) processUpdateInfo(upgradeInfo)
}, },
@ -89,19 +115,19 @@ object AppUpgradeChecker {
} }
private fun processUpdateInfo(upgradeInfo: UpgradeInfo) { private fun processUpdateInfo(upgradeInfo: UpgradeInfo) {
currentState = State.success(upgradeInfo)
if (upgradeInfo.isNewVersion) { if (upgradeInfo.isNewVersion) {
AppUpgradeChecker.upgradeInfo = upgradeInfo AppUpgradeChecker.upgradeInfo = upgradeInfo
safeContext { safeContext {
upgradeInteractor.showUpgradeDialog(it, upgradeInfo, upgradeInteractor.showUpgradeDialog(it, upgradeInfo,
onCancel = { onCancel = {
currentState = State.success() //do nothing
}, },
onConfirm = { onConfirm = {
doUpdate() doUpdate()
}) }
)
} }
} else {
currentState = State.success()
} }
} }

Loading…
Cancel
Save