From dd898579c940d3c748769684d60780b8a6dcf526 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 11 Nov 2023 15:01:08 +0200 Subject: [PATCH] Option to lock reader screen rotation --- .../core/util/ScreenOrientationHelper.kt | 17 ++++++++--- .../reader/ui/config/ReaderConfigSheet.kt | 28 +++++++++++++++---- .../res/drawable/ic_screen_rotation_lock.xml | 11 ++++++++ .../main/res/layout/sheet_reader_config.xml | 16 +++++++++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 app/src/main/res/drawable/ic_screen_rotation_lock.xml diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ScreenOrientationHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ScreenOrientationHelper.kt index 8f062e247..79a071842 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ScreenOrientationHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ScreenOrientationHelper.kt @@ -6,13 +6,16 @@ import android.content.res.Configuration import android.database.ContentObserver import android.os.Handler import android.provider.Settings +import dagger.hilt.android.scopes.ActivityScoped import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.channels.trySendBlocking import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.onStart +import javax.inject.Inject -class ScreenOrientationHelper(private val activity: Activity) { +@ActivityScoped +class ScreenOrientationHelper @Inject constructor(private val activity: Activity) { val isAutoRotationEnabled: Boolean get() = Settings.System.getInt( @@ -31,9 +34,15 @@ class ScreenOrientationHelper(private val activity: Activity) { } } - fun toggleOrientation() { - isLandscape = !isLandscape - } + var isLocked: Boolean + get() = activity.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_LOCKED + set(value) { + activity.requestedOrientation = if (value) { + ActivityInfo.SCREEN_ORIENTATION_LOCKED + } else { + ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + } + } fun observeAutoOrientation() = callbackFlow { val observer = object : ContentObserver(Handler(activity.mainLooper)) { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/config/ReaderConfigSheet.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/config/ReaderConfigSheet.kt index 1de1f7a15..bf26e61e5 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/config/ReaderConfigSheet.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/config/ReaderConfigSheet.kt @@ -47,7 +47,10 @@ class ReaderConfigSheet : private val viewModel by activityViewModels() private val savePageRequest = registerForActivityResult(PageSaveContract(), this) - private var orientationHelper: ScreenOrientationHelper? = null + + @Inject + lateinit var orientationHelper: ScreenOrientationHelper + private lateinit var mode: ReaderMode @Inject @@ -113,7 +116,7 @@ class ReaderConfigSheet : } R.id.button_screen_rotate -> { - orientationHelper?.toggleOrientation() + orientationHelper.isLandscape = !orientationHelper.isLandscape } R.id.button_color_filter -> { @@ -131,6 +134,10 @@ class ReaderConfigSheet : requireViewBinding().layoutTimer.isVisible = isChecked requireViewBinding().sliderTimer.isVisible = isChecked } + + R.id.switch_screen_lock_rotation -> { + orientationHelper.isLocked = isChecked + } } } @@ -168,14 +175,23 @@ class ReaderConfigSheet : } private fun observeScreenOrientation() { - val helper = ScreenOrientationHelper(requireActivity()) - orientationHelper = helper - helper.observeAutoOrientation() + orientationHelper.observeAutoOrientation() .onEach { - requireViewBinding().buttonScreenRotate.isGone = it + with(requireViewBinding()) { + buttonScreenRotate.isGone = it + switchScreenLockRotation.isVisible = it + updateOrientationLockSwitch() + } }.launchIn(viewLifecycleScope) } + private fun updateOrientationLockSwitch() { + val switch = viewBinding?.switchScreenLockRotation ?: return + switch.setOnCheckedChangeListener(null) + switch.isChecked = orientationHelper.isLocked + switch.setOnCheckedChangeListener(this) + } + private fun findCallback(): Callback? { return (parentFragment as? Callback) ?: (activity as? Callback) } diff --git a/app/src/main/res/drawable/ic_screen_rotation_lock.xml b/app/src/main/res/drawable/ic_screen_rotation_lock.xml new file mode 100644 index 000000000..eec657730 --- /dev/null +++ b/app/src/main/res/drawable/ic_screen_rotation_lock.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/main/res/layout/sheet_reader_config.xml b/app/src/main/res/layout/sheet_reader_config.xml index 30dfaef37..5017ee185 100644 --- a/app/src/main/res/layout/sheet_reader_config.xml +++ b/app/src/main/res/layout/sheet_reader_config.xml @@ -48,6 +48,22 @@ app:drawableStartCompat="@drawable/ic_screen_rotation" tools:visibility="visible" /> + + Backups output directory Last successful backup: %s x%.1f + Lock screen rotation