Screen rotation button in reader

This commit is contained in:
Koitharu
2025-02-05 15:54:29 +02:00
parent 26a2ffcbb1
commit 772fd5cc6a
9 changed files with 79 additions and 10 deletions

View File

@@ -144,7 +144,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) {
val readerControls: Set<ReaderControl>
get() = prefs.getStringSet(KEY_READER_CONTROLS, null)?.mapNotNullTo(EnumSet.noneOf(ReaderControl::class.java)) {
ReaderControl.entries.find(it)
} ?: EnumSet.allOf(ReaderControl::class.java)
} ?: ReaderControl.DEFAULT
val isOfflineCheckDisabled: Boolean
get() = prefs.getBoolean(KEY_OFFLINE_DISABLED, false)

View File

@@ -1,6 +1,15 @@
package org.koitharu.kotatsu.core.prefs
import java.util.EnumSet
enum class ReaderControl {
PREV_CHAPTER, NEXT_CHAPTER, SLIDER, PAGES_SHEET
PREV_CHAPTER, NEXT_CHAPTER, SLIDER, PAGES_SHEET, SCREEN_ROTATION;
companion object {
val DEFAULT: Set<ReaderControl> = EnumSet.of(
PREV_CHAPTER, NEXT_CHAPTER, SLIDER, PAGES_SHEET,
)
}
}

View File

@@ -109,7 +109,6 @@ class ReaderActivity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(ActivityReaderBinding.inflate(layoutInflater))
screenOrientationHelper.init(settings.readerScreenOrientation)
readerManager = ReaderManager(supportFragmentManager, viewBinding.container, settings)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
touchHelper = TapGridDispatcher(this, this)
@@ -151,7 +150,9 @@ class ReaderActivity :
viewModel.isInfoBarTransparent.observe(this) { viewBinding.infoBar.drawBackground = !it }
viewModel.isInfoBarEnabled.observe(this, ::onReaderBarChanged)
viewModel.isBookmarkAdded.observe(this, MenuInvalidator(this))
viewModel.isPagesSheetEnabled.observe(this, MenuInvalidator(viewBinding.toolbarBottom))
val bottomMenuInvalidator = MenuInvalidator(viewBinding.toolbarBottom)
viewModel.isPagesSheetEnabled.observe(this, bottomMenuInvalidator)
screenOrientationHelper.observeAutoOrientation().observe(this, bottomMenuInvalidator)
viewModel.onShowToast.observeEvent(this) { msgId ->
Snackbar.make(viewBinding.container, msgId, Snackbar.LENGTH_SHORT)
.setAnchorView(viewBinding.appbarBottom)
@@ -164,7 +165,9 @@ class ReaderActivity :
viewBinding.zoomControl.isVisible = it
}
addMenuProvider(ReaderMenuTopProvider(viewModel))
viewBinding.toolbarBottom.addMenuProvider(ReaderMenuBottomProvider(this, readerManager, viewModel))
viewBinding.toolbarBottom.addMenuProvider(
ReaderMenuBottomProvider(this, readerManager, screenOrientationHelper, viewModel),
)
}
override fun getParentActivityIntent(): Intent? {

View File

@@ -3,6 +3,7 @@ package org.koitharu.kotatsu.reader.ui
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.Toast
import androidx.core.view.MenuProvider
import androidx.fragment.app.FragmentActivity
import org.koitharu.kotatsu.R
@@ -12,6 +13,7 @@ import org.koitharu.kotatsu.core.prefs.ReaderControl
class ReaderMenuBottomProvider(
private val activity: FragmentActivity,
private val readerManager: ReaderManager,
private val screenOrientationHelper: ScreenOrientationHelper,
private val viewModel: ReaderViewModel,
) : MenuProvider {
@@ -21,18 +23,39 @@ class ReaderMenuBottomProvider(
}
override fun onPrepareMenu(menu: Menu) {
val readerControls = viewModel.readerControls.value
val isPagesSheetEnabled = viewModel.content.value.pages.isNotEmpty() &&
ReaderControl.PAGES_SHEET in viewModel.readerControls.value
ReaderControl.PAGES_SHEET in readerControls
menu.findItem(R.id.action_pages_thumbs).run {
isVisible = isPagesSheetEnabled
if (isPagesSheetEnabled) {
setIcon(if (viewModel.isPagesSheetEnabled.value) R.drawable.ic_grid else R.drawable.ic_list)
}
}
menu.findItem(R.id.action_screen_rotation).run {
isVisible = ReaderControl.SCREEN_ROTATION in readerControls
when {
!isVisible -> Unit
!screenOrientationHelper.isAutoRotationEnabled -> {
setTitle(R.string.rotate_screen)
setIcon(R.drawable.ic_screen_rotation)
}
else -> {
setTitle(R.string.lock_screen_rotation)
setIcon(R.drawable.ic_screen_rotation_lock)
}
}
}
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.action_screen_rotation -> {
toggleScreenRotation()
true
}
R.id.action_pages_thumbs -> {
activity.router.showChapterPagesSheet()
true
@@ -57,4 +80,22 @@ class ReaderMenuBottomProvider(
else -> false
}
}
private fun toggleScreenRotation() = with(screenOrientationHelper) {
if (isAutoRotationEnabled) {
val newValue = !isLocked
isLocked = newValue
Toast.makeText(
activity,
if (newValue) {
R.string.screen_rotation_locked
} else {
R.string.screen_rotation_unlocked
},
Toast.LENGTH_SHORT,
).show()
} else {
isLandscape = !isLandscape
}
}
}

View File

@@ -13,10 +13,14 @@ import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.onStart
import org.koitharu.kotatsu.core.prefs.AppSettings
import javax.inject.Inject
@ActivityScoped
class ScreenOrientationHelper @Inject constructor(private val activity: Activity) {
class ScreenOrientationHelper @Inject constructor(
private val activity: Activity,
settings: AppSettings,
) {
val isAutoRotationEnabled: Boolean
get() = Settings.System.getInt(
@@ -45,10 +49,10 @@ class ScreenOrientationHelper @Inject constructor(private val activity: Activity
}
}
fun init(orientation: Int) {
init {
if (activity.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
// https://developer.android.com/reference/android/R.attr.html#screenOrientation
activity.requestedOrientation = orientation
activity.requestedOrientation = settings.readerScreenOrientation
}
}

View File

@@ -18,6 +18,7 @@ import org.koitharu.kotatsu.core.prefs.ReaderControl
import org.koitharu.kotatsu.core.prefs.ReaderMode
import org.koitharu.kotatsu.core.ui.BasePreferenceFragment
import org.koitharu.kotatsu.core.util.ext.setDefaultValueCompat
import org.koitharu.kotatsu.parsers.util.mapToSet
import org.koitharu.kotatsu.parsers.util.names
import org.koitharu.kotatsu.settings.utils.MultiSummaryProvider
import org.koitharu.kotatsu.settings.utils.PercentSummaryProvider
@@ -45,7 +46,7 @@ class ReaderSettingsFragment :
}
findPreference<MultiSelectListPreference>(AppSettings.KEY_READER_CONTROLS)?.run {
entryValues = ReaderControl.entries.names()
setDefaultValueCompat(ReaderControl.entries.names().toSet())
setDefaultValueCompat(ReaderControl.DEFAULT.mapToSet { it.name })
summaryProvider = MultiSummaryProvider(R.string.none)
}
findPreference<ListPreference>(AppSettings.KEY_READER_BACKGROUND)?.run {

View File

@@ -5,6 +5,14 @@
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="AlwaysShowAction">
<item
android:id="@+id/action_screen_rotation"
android:icon="@drawable/ic_screen_rotation"
android:title="@string/screen_orientation"
android:visible="false"
app:showAsAction="always"
tools:visible="true" />
<item
android:id="@+id/action_pages_thumbs"
android:icon="@drawable/ic_grid"

View File

@@ -129,5 +129,6 @@
<item>@string/next_chapter</item>
<item>@string/pages_slider</item>
<item>@string/chapters_and_pages</item>
<item>@string/screen_orientation</item>
</string-array>
</resources>

View File

@@ -798,4 +798,6 @@
<string name="reader_controls_in_bottom_bar">Reader controls in bottom bar</string>
<string name="chapters_and_pages">Chapters and pages</string>
<string name="pages_slider">Page switch slider</string>
<string name="screen_rotation_locked">Screen rotation has been locked</string>
<string name="screen_rotation_unlocked">Screen rotation has been unlocked</string>
</resources>