Skip to content

Commit

Permalink
Option to lock reader screen rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Nov 11, 2023
1 parent 73143d2 commit dd89857
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ class ReaderConfigSheet :

private val viewModel by activityViewModels<ReaderViewModel>()
private val savePageRequest = registerForActivityResult(PageSaveContract(), this)
private var orientationHelper: ScreenOrientationHelper? = null

@Inject
lateinit var orientationHelper: ScreenOrientationHelper

private lateinit var mode: ReaderMode

@Inject
Expand Down Expand Up @@ -113,7 +116,7 @@ class ReaderConfigSheet :
}

R.id.button_screen_rotate -> {
orientationHelper?.toggleOrientation()
orientationHelper.isLandscape = !orientationHelper.isLandscape
}

R.id.button_color_filter -> {
Expand All @@ -131,6 +134,10 @@ class ReaderConfigSheet :
requireViewBinding().layoutTimer.isVisible = isChecked
requireViewBinding().sliderTimer.isVisible = isChecked
}

R.id.switch_screen_lock_rotation -> {
orientationHelper.isLocked = isChecked
}
}
}

Expand Down Expand Up @@ -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)
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_screen_rotation_lock.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector
xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M16.8,2.5C16.8,1.56 17.56,0.8 18.5,0.8C19.44,0.8 20.2,1.56 20.2,2.5V3H16.8V2.5M16,9H21A1,1 0 0,0 22,8V4A1,1 0 0,0 21,3V2.5A2.5,2.5 0 0,0 18.5,0A2.5,2.5 0 0,0 16,2.5V3A1,1 0 0,0 15,4V8A1,1 0 0,0 16,9M8.47,20.5C5.2,18.94 2.86,15.76 2.5,12H1C1.5,18.16 6.66,23 12.95,23L13.61,22.97L9.8,19.15L8.47,20.5M23.25,12.77L20.68,10.2L19.27,11.61L21.5,13.83L15.83,19.5L4.5,8.17L10.17,2.5L12.27,4.61L13.68,3.2L11.23,0.75C10.64,0.16 9.69,0.16 9.11,0.75L2.75,7.11C2.16,7.7 2.16,8.65 2.75,9.23L14.77,21.25C15.36,21.84 16.31,21.84 16.89,21.25L23.25,14.89C23.84,14.3 23.84,13.35 23.25,12.77Z" />
</vector>
16 changes: 16 additions & 0 deletions app/src/main/res/layout/sheet_reader_config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@
app:drawableStartCompat="@drawable/ic_screen_rotation"
tools:visibility="visible" />

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/switch_screen_lock_rotation"
android:layout_width="match_parent"
android:layout_height="?android:listPreferredItemHeightSmall"
android:drawablePadding="?android:listPreferredItemPaddingStart"
android:ellipsize="end"
android:paddingStart="?android:listPreferredItemPaddingStart"
android:paddingEnd="?android:listPreferredItemPaddingEnd"
android:singleLine="true"
android:text="@string/lock_screen_rotation"
android:textAppearance="?attr/textAppearanceButton"
android:textColor="?colorOnSurfaceVariant"
android:visibility="gone"
app:drawableStartCompat="@drawable/ic_screen_rotation_lock"
tools:visibility="visible" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -511,4 +511,5 @@
<string name="backups_output_directory">Backups output directory</string>
<string name="last_successful_backup">Last successful backup: %s</string>
<string name="speed_value">x%.1f</string>
<string name="lock_screen_rotation">Lock screen rotation</string>
</resources>

0 comments on commit dd89857

Please sign in to comment.