diff options
author | YuutaW <17158086+Trumeet@users.noreply.github.com> | 2019-03-01 08:17:07 -0800 |
---|---|---|
committer | YuutaW <17158086+Trumeet@users.noreply.github.com> | 2019-03-01 08:17:07 -0800 |
commit | b2d08c9227953a0fd61b44672be187214a481339 (patch) | |
tree | ba1aa79610d30a7ab68659f07461aa14a2cc5306 /app/src/main/java/moe/yuuta/workmode/update | |
parent | e004e1e12ecdca2296fefde5b305c8f3bd361833 (diff) | |
download | WorkMode-b2d08c9227953a0fd61b44672be187214a481339.tar WorkMode-b2d08c9227953a0fd61b44672be187214a481339.tar.gz WorkMode-b2d08c9227953a0fd61b44672be187214a481339.tar.bz2 WorkMode-b2d08c9227953a0fd61b44672be187214a481339.zip |
refactor(app): move some logic into Lifecycle Observers
Signed-off-by: YuutaW <17158086+Trumeet@users.noreply.github.com>
Diffstat (limited to 'app/src/main/java/moe/yuuta/workmode/update')
-rw-r--r-- | app/src/main/java/moe/yuuta/workmode/update/LifecycleUpdateChecker.kt | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/app/src/main/java/moe/yuuta/workmode/update/LifecycleUpdateChecker.kt b/app/src/main/java/moe/yuuta/workmode/update/LifecycleUpdateChecker.kt new file mode 100644 index 0000000..9e2cd89 --- /dev/null +++ b/app/src/main/java/moe/yuuta/workmode/update/LifecycleUpdateChecker.kt @@ -0,0 +1,45 @@ +package moe.yuuta.workmode.update + +import android.content.Context +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent +import moe.yuuta.workmode.BuildConfig +import moe.yuuta.workmode.async.Async +import moe.yuuta.workmode.async.Callback +import moe.yuuta.workmode.async.Stoppable + +class LifecycleUpdateChecker(val context: Context, val callback: moe.yuuta.workmode.update.Callback) : LifecycleObserver { + private lateinit var mStoppable: Stoppable + + @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) + fun onCreate() { + mStoppable = Async.beginTask(UpdateChecker(), object : Callback<Update> { + override fun onStop(success: Boolean, result: Update?, e: Throwable?) { + if (result == null) return + if (result.version <= BuildConfig.VERSION_CODE) return + if (!shouldOpenGooglePlay() && !result.altUrlEnabled && !result.altUrlForce) return + val url = if (shouldOpenGooglePlay() && !result.altUrlForce) + "https://play.google.com/store/apps/details?id=${BuildConfig.APPLICATION_ID}" + else result.altUrl + this@LifecycleUpdateChecker.callback.showToast(result.name, url) + } + + private fun shouldOpenGooglePlay(): Boolean = + "com.android.vending" == this@LifecycleUpdateChecker.context.packageManager.getInstallerPackageName(BuildConfig.APPLICATION_ID) + + }) + } + + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + fun onDestroy() { + if (::mStoppable.isInitialized) { + mStoppable.stop() + } + } +} + +@FunctionalInterface +interface Callback { + fun showToast(name: String, url: String) +}
\ No newline at end of file |