diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt index 37ad743be..d5a413fb2 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/AppSettings.kt @@ -144,7 +144,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { val readerControls: Set 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) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderControl.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderControl.kt index b7de928da..da2373011 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderControl.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/prefs/ReaderControl.kt @@ -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 = EnumSet.of( + PREV_CHAPTER, NEXT_CHAPTER, SLIDER, PAGES_SHEET, + ) + } } 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 a3f6af05f..a3b02a0b9 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 @@ -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? { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderMenuBottomProvider.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderMenuBottomProvider.kt index 0decec25b..61be3f23a 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderMenuBottomProvider.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ReaderMenuBottomProvider.kt @@ -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 + } + } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScreenOrientationHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScreenOrientationHelper.kt index 546577b5a..6dc370090 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScreenOrientationHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScreenOrientationHelper.kt @@ -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 } } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/ReaderSettingsFragment.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/ReaderSettingsFragment.kt index 0a77c28b4..439180a81 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/ReaderSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/ReaderSettingsFragment.kt @@ -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(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(AppSettings.KEY_READER_BACKGROUND)?.run { diff --git a/app/src/main/res/menu/opt_reader_bottom.xml b/app/src/main/res/menu/opt_reader_bottom.xml index 21b800716..327458cf2 100644 --- a/app/src/main/res/menu/opt_reader_bottom.xml +++ b/app/src/main/res/menu/opt_reader_bottom.xml @@ -5,6 +5,14 @@ xmlns:tools="http://schemas.android.com/tools" tools:ignore="AlwaysShowAction"> + + @string/next_chapter @string/pages_slider @string/chapters_and_pages + @string/screen_orientation diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6045b9c7f..90f653468 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -798,4 +798,6 @@ Reader controls in bottom bar Chapters and pages Page switch slider + Screen rotation has been locked + Screen rotation has been unlocked