From 100073f45eb0ade59cc0cda67c7ae763c953c7dc Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 16 Oct 2024 18:16:14 +0300 Subject: [PATCH] Reader screen orientation settings --- app/build.gradle | 4 ++-- .../kotatsu/core/prefs/AppSettings.kt | 6 +++++ .../explore/data/MangaSourcesRepository.kt | 2 ++ .../kotatsu/reader/domain/PageLoader.kt | 4 ++-- .../kotatsu/reader/ui/ReaderActivity.kt | 10 +++++--- .../ui}/ScreenOrientationHelper.kt | 11 ++++++++- .../reader/ui/config/ReaderConfigSheet.kt | 2 +- .../settings/ReaderSettingsFragment.kt | 10 ++++++++ app/src/main/res/values/arrays.xml | 6 +++++ app/src/main/res/values/strings.xml | 3 +++ app/src/main/res/xml/pref_reader.xml | 24 ++++++++++++------- 11 files changed, 64 insertions(+), 18 deletions(-) rename app/src/main/kotlin/org/koitharu/kotatsu/{core/util => reader/ui}/ScreenOrientationHelper.kt (84%) diff --git a/app/build.gradle b/app/build.gradle index 3d18a9ef2..52ab59af2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,8 +16,8 @@ android { applicationId 'org.koitharu.kotatsu' minSdk = 21 targetSdk = 35 - versionCode = 680 - versionName = '7.7-a1' + versionCode = 681 + versionName = '7.7-a2' generatedDensities = [] testInstrumentationRunner 'org.koitharu.kotatsu.HiltTestRunner' ksp { 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 591616b49..66d172ba9 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 @@ -2,6 +2,7 @@ package org.koitharu.kotatsu.core.prefs import android.content.Context import android.content.SharedPreferences +import android.content.pm.ActivityInfo import android.net.ConnectivityManager import android.net.Uri import android.os.Build @@ -119,6 +120,10 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { get() = prefs.getBoolean(KEY_READER_DOUBLE_PAGES, false) set(value) = prefs.edit { putBoolean(KEY_READER_DOUBLE_PAGES, value) } + val readerScreenOrientation: Int + get() = prefs.getString(KEY_READER_ORIENTATION, null)?.toIntOrNull() + ?: ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + val isReaderVolumeButtonsEnabled: Boolean get() = prefs.getBoolean(KEY_READER_VOLUME_BUTTONS, false) @@ -588,6 +593,7 @@ class AppSettings @Inject constructor(@ApplicationContext context: Context) { const val KEY_READER_CONTROL_LTR = "reader_taps_ltr" const val KEY_READER_FULLSCREEN = "reader_fullscreen" const val KEY_READER_VOLUME_BUTTONS = "reader_volume_buttons" + const val KEY_READER_ORIENTATION = "reader_orientation" const val KEY_TRACKER_ENABLED = "tracker_enabled" const val KEY_TRACKER_WIFI_ONLY = "tracker_wifi" const val KEY_TRACKER_FREQUENCY = "tracker_freq" diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt index 4ccdb344f..04249df34 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt @@ -12,6 +12,7 @@ import kotlinx.coroutines.channels.trySendBlocking import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onStart @@ -338,6 +339,7 @@ class MangaSourcesRepository @Inject constructor( }.map { getExternalSources() }.distinctUntilChanged() + .conflate() } private fun getExternalSources() = context.packageManager.queryIntentContentProviders( diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt index 1b3464c3c..b41462ad4 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/domain/PageLoader.kt @@ -32,8 +32,8 @@ import okio.use import org.koitharu.kotatsu.core.network.CommonHeaders import org.koitharu.kotatsu.core.network.MangaHttpClient import org.koitharu.kotatsu.core.network.imageproxy.ImageProxyInterceptor +import org.koitharu.kotatsu.core.parser.CachingMangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository -import org.koitharu.kotatsu.core.parser.ParserMangaRepository import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.util.FileSize import org.koitharu.kotatsu.core.util.RetainedLifecycleCoroutineScope @@ -93,7 +93,7 @@ class PageLoader @Inject constructor( private val edgeDetector = EdgeDetector(context) fun isPrefetchApplicable(): Boolean { - return repository is ParserMangaRepository + return repository is CachingMangaRepository && settings.isPagesPreloadEnabled && !context.isPowerSaveMode() && !isLowRam() 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 2e02fa6f7..6553b7db2 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 @@ -83,6 +83,12 @@ class ReaderActivity : @Inject lateinit var tapGridSettings: TapGridSettings + @Inject + lateinit var scrollTimerFactory: ScrollTimer.Factory + + @Inject + lateinit var screenOrientationHelper: ScreenOrientationHelper + private val idlingDetector = IdlingDetector(TimeUnit.SECONDS.toMillis(10), this) private val savePageRequest = registerForActivityResult(PageSaveContract(), this) @@ -97,9 +103,6 @@ class ReaderActivity : scrollTimer.isEnabled = value } - @Inject - lateinit var scrollTimerFactory: ScrollTimer.Factory - private lateinit var scrollTimer: ScrollTimer private lateinit var touchHelper: TapGridDispatcher private lateinit var controlDelegate: ReaderControlDelegate @@ -110,6 +113,7 @@ 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) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ScreenOrientationHelper.kt b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScreenOrientationHelper.kt similarity index 84% rename from app/src/main/kotlin/org/koitharu/kotatsu/core/util/ScreenOrientationHelper.kt rename to app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScreenOrientationHelper.kt index 79a071842..546577b5a 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ScreenOrientationHelper.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/reader/ui/ScreenOrientationHelper.kt @@ -1,4 +1,4 @@ -package org.koitharu.kotatsu.core.util +package org.koitharu.kotatsu.reader.ui import android.app.Activity import android.content.pm.ActivityInfo @@ -10,6 +10,7 @@ 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.conflate import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.onStart import javax.inject.Inject @@ -44,6 +45,13 @@ class ScreenOrientationHelper @Inject constructor(private val activity: Activity } } + fun init(orientation: Int) { + if (activity.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) { + // https://developer.android.com/reference/android/R.attr.html#screenOrientation + activity.requestedOrientation = orientation + } + } + fun observeAutoOrientation() = callbackFlow { val observer = object : ContentObserver(Handler(activity.mainLooper)) { override fun onChange(selfChange: Boolean) { @@ -59,4 +67,5 @@ class ScreenOrientationHelper @Inject constructor(private val activity: Activity }.onStart { emit(isAutoRotationEnabled) }.distinctUntilChanged() + .conflate() } 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 6aad038a9..37da96afe 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 @@ -24,7 +24,6 @@ import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.ReaderMode import org.koitharu.kotatsu.core.prefs.observeAsStateFlow import org.koitharu.kotatsu.core.ui.sheet.BaseAdaptiveSheet -import org.koitharu.kotatsu.core.util.ScreenOrientationHelper import org.koitharu.kotatsu.core.util.ext.findParentCallback import org.koitharu.kotatsu.core.util.ext.observe import org.koitharu.kotatsu.core.util.ext.showDistinct @@ -33,6 +32,7 @@ import org.koitharu.kotatsu.core.util.ext.withArgs import org.koitharu.kotatsu.databinding.SheetReaderConfigBinding import org.koitharu.kotatsu.reader.domain.PageLoader import org.koitharu.kotatsu.reader.ui.ReaderViewModel +import org.koitharu.kotatsu.reader.ui.ScreenOrientationHelper import org.koitharu.kotatsu.reader.ui.colorfilter.ColorFilterConfigActivity import org.koitharu.kotatsu.settings.SettingsActivity import javax.inject.Inject 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 d46843db9..052a1e4c1 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/ReaderSettingsFragment.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/ReaderSettingsFragment.kt @@ -2,6 +2,7 @@ package org.koitharu.kotatsu.settings import android.content.Intent import android.content.SharedPreferences +import android.content.pm.ActivityInfo import android.os.Bundle import android.view.View import androidx.preference.ListPreference @@ -38,6 +39,15 @@ class ReaderSettingsFragment : ) setDefaultValueCompat(ReaderMode.STANDARD.name) } + findPreference(AppSettings.KEY_READER_ORIENTATION)?.run { + entryValues = arrayOf( + ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED.toString(), + ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR.toString(), + ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT.toString(), + ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE.toString(), + ) + setDefaultValueCompat(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED.toString()) + } findPreference(AppSettings.KEY_READER_BACKGROUND)?.run { entryValues = ReaderBackground.entries.names() setDefaultValueCompat(ReaderBackground.DEFAULT.name) diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index da63ca086..fa5ea6aa5 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -118,4 +118,10 @@ @string/ask_every_time @string/dont_allow + + @string/system_default + @string/automatic + @string/portrait + @string/landscape + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e4184c482..8f2623743 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -754,4 +754,7 @@ Allow always Allow once Ask every time + Screen orientation + Portrait + Landscape diff --git a/app/src/main/res/xml/pref_reader.xml b/app/src/main/res/xml/pref_reader.xml index 6183b186a..778ff82a9 100644 --- a/app/src/main/res/xml/pref_reader.xml +++ b/app/src/main/res/xml/pref_reader.xml @@ -101,11 +101,24 @@ android:title="@string/fullscreen_mode" app:allowDividerAbove="true" /> + + + + + android:title="@string/reader_info_bar" + app:allowDividerAbove="true" /> - - + android:title="@string/show_pages_numbers" />x