From b2d08c9227953a0fd61b44672be187214a481339 Mon Sep 17 00:00:00 2001 From: YuutaW <17158086+Trumeet@users.noreply.github.com> Date: Fri, 1 Mar 2019 08:17:07 -0800 Subject: refactor(app): move some logic into Lifecycle Observers Signed-off-by: YuutaW <17158086+Trumeet@users.noreply.github.com> --- .../yuuta/workmode/LifecycleUIUpdateReceiver.kt | 36 ++++++++++ .../main/java/moe/yuuta/workmode/MainActivity.kt | 76 +++++++++------------- .../workmode/update/LifecycleUpdateChecker.kt | 45 +++++++++++++ 3 files changed, 110 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/moe/yuuta/workmode/LifecycleUIUpdateReceiver.kt create mode 100644 app/src/main/java/moe/yuuta/workmode/update/LifecycleUpdateChecker.kt (limited to 'app/src/main/java') diff --git a/app/src/main/java/moe/yuuta/workmode/LifecycleUIUpdateReceiver.kt b/app/src/main/java/moe/yuuta/workmode/LifecycleUIUpdateReceiver.kt new file mode 100644 index 0000000..63cebc6 --- /dev/null +++ b/app/src/main/java/moe/yuuta/workmode/LifecycleUIUpdateReceiver.kt @@ -0,0 +1,36 @@ +package moe.yuuta.workmode + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.LifecycleObserver +import androidx.lifecycle.OnLifecycleEvent +import moe.yuuta.workmode.access.AccessorStarter + +class LifecycleUIUpdateReceiver(val context: Context, val callback: Callback) : LifecycleObserver { + + @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) + fun onCreate() { + val filter = IntentFilter(AccessorStarter.ACTION_UPDATE_UI_STATE) + filter.addAction(AccessorStarter.ACTION_UPDATE_UI_PROGRESS) + context.registerReceiver(mUIUpdateReceiver, filter) + } + + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + fun onDestroy() { + context.unregisterReceiver(mUIUpdateReceiver) + } + + interface Callback { + fun updateUI(intent: Intent) + } + + private val mUIUpdateReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context?, intent: Intent?) { + if (intent == null) return + callback.updateUI(intent) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/moe/yuuta/workmode/MainActivity.kt b/app/src/main/java/moe/yuuta/workmode/MainActivity.kt index 792e948..ed93f38 100644 --- a/app/src/main/java/moe/yuuta/workmode/MainActivity.kt +++ b/app/src/main/java/moe/yuuta/workmode/MainActivity.kt @@ -1,7 +1,10 @@ package moe.yuuta.workmode import android.app.Activity -import android.content.* +import android.content.ActivityNotFoundException +import android.content.ComponentName +import android.content.Context +import android.content.Intent import android.content.pm.PackageManager import android.graphics.drawable.Drawable import android.net.Uri @@ -35,12 +38,11 @@ import moe.yuuta.workmode.suspend.SuspendTile import moe.yuuta.workmode.suspend.data.ListMode import moe.yuuta.workmode.suspend.data.Status import moe.yuuta.workmode.suspend.data.SuspendedStorage -import moe.yuuta.workmode.update.Update -import moe.yuuta.workmode.update.UpdateChecker +import moe.yuuta.workmode.update.LifecycleUpdateChecker import moe.yuuta.workmode.utils.Utils import java.util.stream.Collectors -class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View.OnClickListener, LicenseCheckerCallback { +class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View.OnClickListener, LicenseCheckerCallback, moe.yuuta.workmode.update.Callback, LifecycleUIUpdateReceiver.Callback { private val logger: Logger = XLog.tag("MainActivity").build() companion object { @@ -74,10 +76,8 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View mAdapter = Adapter() recyclerView.adapter = mAdapter displayUI() - val filter = IntentFilter(AccessorStarter.ACTION_UPDATE_UI_STATE) - filter.addAction(AccessorStarter.ACTION_UPDATE_UI_PROGRESS) - registerReceiver(mUIUpdateReceiver, filter) - scheduleUpdateChecking() + lifecycle.addObserver(LifecycleUIUpdateReceiver(this, this)) + lifecycle.addObserver(LifecycleUpdateChecker(this, this)) lifecycle.addObserver(GPL(this, lifecycle, this)) setProgressUI(false) } @@ -133,7 +133,6 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View mStoppableGroup.stop() mAdapter.destroy() if (mSortDisplayStoppable != null) (mSortDisplayStoppable as Stoppable).stop() - unregisterReceiver(mUIUpdateReceiver) super.onDestroy() } @@ -203,46 +202,16 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View }) } - private val mUIUpdateReceiver = object : BroadcastReceiver() { - override fun onReceive(context: Context?, intent: Intent?) { - if (intent == null) return - when (intent.action) { - AccessorStarter.ACTION_UPDATE_UI_STATE -> { - displayUI() - } - AccessorStarter.ACTION_UPDATE_UI_PROGRESS -> { - logger.d("Updating progress from receiver") - setProgressUI(intent.getBooleanExtra(AccessorStarter.EXTRA_SHOW_PROGRESS, false)) - } + override fun updateUI(intent: Intent) { + when (intent.action) { + AccessorStarter.ACTION_UPDATE_UI_STATE -> { + displayUI() } - } - } - - private fun scheduleUpdateChecking() { - mStoppableGroup.add(Async.beginTask(UpdateChecker(), object : Callback { - 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 - Snackbar.make(findViewById(android.R.id.content), - getString(R.string.update_available, - result.name), - Snackbar.LENGTH_LONG) - .setAction(R.string.view) { - val url = if (shouldOpenGooglePlay() && !result.altUrlForce) - "https://play.google.com/store/apps/details?id=${BuildConfig.APPLICATION_ID}" - else result.altUrl - try { - startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) - } catch (ignored: ActivityNotFoundException) {} - } - .show() + AccessorStarter.ACTION_UPDATE_UI_PROGRESS -> { + logger.d("Updating progress from receiver") + setProgressUI(intent.getBooleanExtra(AccessorStarter.EXTRA_SHOW_PROGRESS, false)) } - - private fun shouldOpenGooglePlay(): Boolean = - "com.android.vending" == this@MainActivity.packageManager.getInstallerPackageName(BuildConfig.APPLICATION_ID) - - })) + } } private val mSwitchListModeListener = object : TabLayout.OnTabSelectedListener { @@ -365,6 +334,19 @@ class MainActivity : AppCompatActivity(), SwitchBar.OnSwitchChangeListener, View Policy.RETRY -> SuspendedStorage(this).reportCrack("g_p_l", "rt") } } + + override fun showToast(name: String, url: String) { + Snackbar.make(findViewById(android.R.id.content), + getString(R.string.update_available, + name), + Snackbar.LENGTH_LONG) + .setAction(R.string.view) { + try { + startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) + } catch (ignored: ActivityNotFoundException) {} + } + .show() + } } private class Adapter : RecyclerView.Adapter() { 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 { + 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 -- cgit v1.2.3