From 1ea0e677852806889b5bd42cff80728870aa3ac2 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sat, 23 Jul 2022 16:41:38 -0700 Subject: [PATCH] Refactor back button handling to get rid of deprecation warnings. --- .../activity/TrackDownloadActivity.kt | 3 +- .../remote/ui/shared/activity/BaseActivity.kt | 39 +++++++++++++++---- .../remote/ui/shared/activity/IBackHandler.kt | 2 +- .../remote/ui/shared/extension/Extensions.kt | 5 ++- .../remote/ui/shared/fragment/BaseFragment.kt | 2 +- .../tracks/activity/EditPlaylistActivity.kt | 20 ++++++---- 6 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/download/activity/TrackDownloadActivity.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/download/activity/TrackDownloadActivity.kt index 5d208f1b3..fb72380aa 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/download/activity/TrackDownloadActivity.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/download/activity/TrackDownloadActivity.kt @@ -96,8 +96,9 @@ class TrackDownloadActivity: BaseActivity() { cancel() } - override fun onBackPressed() { + override fun onInterceptBackButton(): Boolean { confirmCancelDialog() + return true } private fun cancel() { diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/activity/BaseActivity.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/activity/BaseActivity.kt index d6bc79def..8581abd83 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/activity/BaseActivity.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/activity/BaseActivity.kt @@ -9,6 +9,7 @@ import android.view.KeyEvent import android.view.MenuItem import android.view.View import android.view.ViewGroup +import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager @@ -29,7 +30,7 @@ import io.casey.musikcube.remote.ui.shared.mixin.RunnerMixin import io.casey.musikcube.remote.ui.shared.mixin.ViewModelMixin import io.reactivex.disposables.CompositeDisposable -abstract class BaseActivity : AppCompatActivity(), ViewModel.Provider, Runner.TaskCallbacks { +abstract class BaseActivity: AppCompatActivity(), ViewModel.Provider, Runner.TaskCallbacks { protected var disposables = CompositeDisposable() private set @@ -55,6 +56,7 @@ abstract class BaseActivity : AppCompatActivity(), ViewModel.Provider, Runner.Ta prefs = getSharedPreferences(Prefs.NAME, Context.MODE_PRIVATE) volumeControlStream = AudioManager.STREAM_MUSIC mixins.onCreate(savedInstanceState ?: Bundle()) + onBackPressedDispatcher.addCallback(this, backHandler) } override fun onStart() { @@ -97,19 +99,42 @@ abstract class BaseActivity : AppCompatActivity(), ViewModel.Provider, Runner.Ta mixins.onDestroy() } - override fun onBackPressed() { + override fun onNavigateUp(): Boolean { + if (!navigateBack()) { + finish() + } + return true + } + + private fun navigateBack(): Boolean { (top as? IBackHandler)?.let { - if (it.onBackPressed()) { - return + if (it.onInterceptBackButton()) { + return true } } - when { - fm.backStackEntryCount > 1 -> fm.popBackStack() - else -> super.onBackPressed() + if (onInterceptBackButton()) { + return true + } + + if (fm.backStackEntryCount >= 1) { + fm.popBackStack() + return true + } + + return false + } + + private val backHandler = object: OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + if (!navigateBack()) { + finish() + } } } + protected open fun onInterceptBackButton(): Boolean = false + override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { if (mixin(PlaybackMixin::class.java)?.onKeyDown(keyCode) == true) { return true diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/activity/IBackHandler.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/activity/IBackHandler.kt index 2029d18bb..015f1d12e 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/activity/IBackHandler.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/activity/IBackHandler.kt @@ -1,5 +1,5 @@ package io.casey.musikcube.remote.ui.shared.activity interface IBackHandler { - fun onBackPressed(): Boolean + fun onInterceptBackButton(): Boolean } \ No newline at end of file diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Extensions.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Extensions.kt index 8f5cdf239..e32f0ad67 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Extensions.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/extension/Extensions.kt @@ -19,6 +19,7 @@ import android.widget.CompoundButton import android.widget.EditText import android.widget.TextView import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.content.res.AppCompatResources import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.Toolbar import androidx.core.content.ContextCompat @@ -276,9 +277,9 @@ inline fun T.withTitleOverride(activity: AppCompatActi fun BaseFragment.initToolbarIfNecessary(view: View, showFilter: Boolean = true) { view.findViewById(R.id.toolbar)?.let { - it.navigationIcon = appCompatActivity.getDrawable(R.drawable.ic_back) + it.navigationIcon = AppCompatResources.getDrawable(appCompatActivity, R.drawable.ic_back) it.setNavigationOnClickListener { - appCompatActivity.onBackPressed() + appCompatActivity.onNavigateUp() } if (showFilter) { this.addFilterAction(it.menu, this as? IFilterable) diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/fragment/BaseFragment.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/fragment/BaseFragment.kt index 6eb17c791..753d2154b 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/fragment/BaseFragment.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/shared/fragment/BaseFragment.kt @@ -169,7 +169,7 @@ open class BaseFragment: Fragment(), ViewModel.Provider, IBackHandler { } } - override fun onBackPressed(): Boolean = + override fun onInterceptBackButton(): Boolean = toolbar?.collapseActionViewIfExpanded() ?: false override fun > createViewModel(): T? = null diff --git a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/tracks/activity/EditPlaylistActivity.kt b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/tracks/activity/EditPlaylistActivity.kt index 9876fdff7..3236dcb2c 100644 --- a/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/tracks/activity/EditPlaylistActivity.kt +++ b/src/musikdroid/app/src/main/java/io/casey/musikcube/remote/ui/tracks/activity/EditPlaylistActivity.kt @@ -6,6 +6,7 @@ import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem +import androidx.activity.OnBackPressedCallback import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.ItemTouchHelper @@ -45,14 +46,7 @@ class EditPlaylistActivity: BaseActivity() { adapter = EditPlaylistAdapter(viewModel, touchHelper, prefs) setupDefaultRecyclerView(recycler, adapter) setResult(RESULT_CANCELED) - } - - override fun onBackPressed() { - if (viewModel.modified) { - ConfirmDiscardChangesDialog.show(this) - return - } - super.onBackPressed() + onBackPressedDispatcher.addCallback(this, backHandler) } override fun onCreateOptionsMenu(menu: Menu): Boolean { @@ -80,6 +74,16 @@ class EditPlaylistActivity: BaseActivity() { )) } + private val backHandler = object: OnBackPressedCallback(true) { + override fun handleOnBackPressed() { + if (viewModel.modified) { + ConfirmDiscardChangesDialog.show(this@EditPlaylistActivity) + return + } + finish() + } + } + override fun > createViewModel(): T { @Suppress("unchecked_cast") return EditPlaylistViewModel(extras.getLong(EXTRA_PLAYLIST_ID, -1L)) as T