From b8db4c81d81009f373ba5f50d95474bbb60c1b13 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Fri, 27 Oct 2023 16:44:40 +0300 Subject: [PATCH] Handle up navigation from reader --- .../koitharu/kotatsu/core/ui/BaseActivity.kt | 21 +++++++++++++------ .../koitharu/kotatsu/core/util/ext/Android.kt | 2 +- .../koitharu/kotatsu/main/ui/MainActivity.kt | 3 ++- .../kotatsu/reader/ui/ReaderActivity.kt | 6 ++++++ 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BaseActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BaseActivity.kt index 8e4765c9b..bb27f8a59 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BaseActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/ui/BaseActivity.kt @@ -6,7 +6,6 @@ import android.graphics.Color import android.os.Build import android.os.Bundle import android.view.KeyEvent -import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.annotation.CallSuper @@ -96,11 +95,10 @@ abstract class BaseActivity : insetsDelegate.onViewCreated(binding.root) } - override fun onOptionsItemSelected(item: MenuItem) = if (item.itemId == android.R.id.home) { - onBackPressedDispatcher.onBackPressed() - // TODO: navigateUpTo - true - } else super.onOptionsItemSelected(item) + override fun onSupportNavigateUp(): Boolean { + dispatchNavigateUp() + return true + } override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { if (BuildConfig.DEBUG && keyCode == KeyEvent.KEYCODE_VOLUME_UP) { @@ -151,6 +149,17 @@ abstract class BaseActivity : window.statusBarColor = defaultStatusBarColor } + protected open fun dispatchNavigateUp() { + val upIntent = parentActivityIntent + if (upIntent != null) { + if (!navigateUpTo(upIntent)) { + startActivity(upIntent) + } + } else { + finishAfterTransition() + } + } + private fun putDataToExtras(intent: Intent?) { intent?.putExtra(EXTRA_DATA, intent.data) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt index d5af3b6b3..92bca0fd4 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Android.kt @@ -83,7 +83,7 @@ fun ActivityResultLauncher.tryLaunch( e.printStackTraceDebug() }.isSuccess -fun SharedPreferences.observe() = callbackFlow { +fun SharedPreferences.observe(): Flow = callbackFlow { val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key -> trySendBlocking(key) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainActivity.kt index 1c6f5db40..131b93b5f 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/main/ui/MainActivity.kt @@ -183,7 +183,8 @@ class MainActivity : BaseActivity(), AppBarOwner, BottomNav override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) { android.R.id.home -> if (isSearchOpened()) { - super.onOptionsItemSelected(item) + closeSearchCallback.handleOnBackPressed() + true } else { viewBinding.searchView.requestFocus() true diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt index c3b7fad89..42ded6f1c 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderActivity.kt @@ -52,6 +52,7 @@ import org.koitharu.kotatsu.core.util.ext.postDelayed import org.koitharu.kotatsu.core.util.ext.setValueRounded import org.koitharu.kotatsu.core.util.ext.zipWithPrevious import org.koitharu.kotatsu.databinding.ActivityReaderBinding +import org.koitharu.kotatsu.details.ui.DetailsActivity import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.MangaChapter import org.koitharu.kotatsu.reader.ui.config.ReaderConfigSheet @@ -147,6 +148,11 @@ class ReaderActivity : } } + override fun getParentActivityIntent(): Intent? { + val manga = viewModel.manga?.toManga() ?: return null + return DetailsActivity.newIntent(this, manga) + } + override fun onUserInteraction() { super.onUserInteraction() scrollTimer.onUserInteraction()