From 0df23ab87870b87190a3942585da834eff5ed97a Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sat, 4 Sep 2021 21:06:56 +0700 Subject: [PATCH] Tablet UI override (#5830) * Tablet UI override * Tablet UI advanced pref --- .../data/preference/PreferenceKeys.kt | 2 ++ .../data/preference/PreferenceValues.kt | 6 +++++ .../data/preference/PreferencesHelper.kt | 6 +++++ .../ui/base/activity/BaseRxActivity.kt | 6 +++++ .../ui/base/activity/BaseThemedActivity.kt | 6 +++++ .../ui/setting/SettingsAdvancedController.kt | 26 +++++++++++++++++++ .../util/system/ContextExtensions.kt | 25 +++++++++++++++++- app/src/main/res/values/strings.xml | 1 + 8 files changed, 77 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 3770a3f0dd..9a4784caa6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -223,6 +223,8 @@ object PreferenceKeys { const val incognitoMode = "incognito_mode" + const val tabletUiMode = "tablet_ui_mode" + fun trackUsername(syncId: Int) = "pref_mangasync_username_$syncId" fun trackPassword(syncId: Int) = "pref_mangasync_password_$syncId" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt index 60192114e2..d210f6ec58 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceValues.kt @@ -51,4 +51,10 @@ object PreferenceValues { LOW(31), LOWEST(47) } + + enum class TabletUiMode { + ALWAYS, + LANDSCAPE, + NEVER + } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index c1b1fc25bd..9a77030f8c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -17,6 +17,7 @@ import eu.kanade.tachiyomi.ui.library.setting.SortDirectionSetting import eu.kanade.tachiyomi.ui.library.setting.SortModeSetting import eu.kanade.tachiyomi.ui.reader.setting.OrientationType import eu.kanade.tachiyomi.ui.reader.setting.ReadingModeType +import eu.kanade.tachiyomi.util.system.isTablet import eu.kanade.tachiyomi.widget.ExtendedNavigationView import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.onEach @@ -317,6 +318,11 @@ class PreferencesHelper(val context: Context) { fun incognitoMode() = flowPrefs.getBoolean(Keys.incognitoMode, false) + fun tabletUiMode() = flowPrefs.getEnum( + Keys.tabletUiMode, + if (context.applicationContext.isTablet()) Values.TabletUiMode.ALWAYS else Values.TabletUiMode.NEVER + ) + fun setChapterSettingsDefault(manga: Manga) { prefs.edit { putInt(Keys.defaultChapterFilterByRead, manga.readFilter) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt index f7ab4f104a..63738ccad1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseRxActivity.kt @@ -1,9 +1,11 @@ package eu.kanade.tachiyomi.ui.base.activity +import android.content.Context import android.os.Bundle import androidx.viewbinding.ViewBinding import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter import eu.kanade.tachiyomi.ui.security.SecureActivityDelegate +import eu.kanade.tachiyomi.util.system.prepareTabletUiContext import nucleus.view.NucleusAppCompatActivity abstract class BaseRxActivity> : NucleusAppCompatActivity

() { @@ -13,6 +15,10 @@ abstract class BaseRxActivity> : NucleusA lateinit var binding: VB + override fun attachBaseContext(newBase: Context) { + super.attachBaseContext(newBase.prepareTabletUiContext()) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt index 5e75acd81c..48a46ce9f2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseThemedActivity.kt @@ -1,16 +1,22 @@ package eu.kanade.tachiyomi.ui.base.activity +import android.content.Context import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.preference.PreferenceValues import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.util.system.prepareTabletUiContext import uy.kohesive.injekt.injectLazy abstract class BaseThemedActivity : AppCompatActivity() { val preferences: PreferencesHelper by injectLazy() + override fun attachBaseContext(newBase: Context) { + super.attachBaseContext(newBase.prepareTabletUiContext()) + } + override fun onCreate(savedInstanceState: Bundle?) { applyAppTheme(preferences) super.onCreate(savedInstanceState) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt index c6e6af932f..eb51c35917 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAdvancedController.kt @@ -15,6 +15,7 @@ import eu.kanade.tachiyomi.data.cache.ChapterCache import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.library.LibraryUpdateService import eu.kanade.tachiyomi.data.library.LibraryUpdateService.Target +import eu.kanade.tachiyomi.data.preference.PreferenceValues import eu.kanade.tachiyomi.network.NetworkHelper import eu.kanade.tachiyomi.network.PREF_DOH_ADGUARD import eu.kanade.tachiyomi.network.PREF_DOH_CLOUDFLARE @@ -25,7 +26,9 @@ import eu.kanade.tachiyomi.util.CrashLogUtil import eu.kanade.tachiyomi.util.lang.launchIO import eu.kanade.tachiyomi.util.lang.withUIContext import eu.kanade.tachiyomi.util.preference.defaultValue +import eu.kanade.tachiyomi.util.preference.entriesRes import eu.kanade.tachiyomi.util.preference.intListPreference +import eu.kanade.tachiyomi.util.preference.listPreference import eu.kanade.tachiyomi.util.preference.onChange import eu.kanade.tachiyomi.util.preference.onClick import eu.kanade.tachiyomi.util.preference.preference @@ -33,6 +36,7 @@ import eu.kanade.tachiyomi.util.preference.preferenceCategory import eu.kanade.tachiyomi.util.preference.summaryRes import eu.kanade.tachiyomi.util.preference.switchPreference import eu.kanade.tachiyomi.util.preference.titleRes +import eu.kanade.tachiyomi.util.system.isTablet import eu.kanade.tachiyomi.util.system.powerManager import eu.kanade.tachiyomi.util.system.toast import uy.kohesive.injekt.injectLazy @@ -182,6 +186,28 @@ class SettingsAdvancedController : SettingsController() { onClick { LibraryUpdateService.start(context, target = Target.TRACKING) } } } + + preferenceCategory { + titleRes = R.string.pref_category_display + + listPreference { + key = Keys.tabletUiMode + titleRes = R.string.pref_tablet_ui_mode + summary = "%s" + entriesRes = arrayOf(R.string.lock_always, R.string.landscape, R.string.lock_never) + entryValues = PreferenceValues.TabletUiMode.values().map { it.name }.toTypedArray() + defaultValue = if (activity!!.applicationContext.isTablet()) { + PreferenceValues.TabletUiMode.ALWAYS + } else { + PreferenceValues.TabletUiMode.NEVER + }.name + + onChange { + activity?.toast(R.string.requires_app_restart) + true + } + } + } } private fun clearChapterCache() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt index bc7ac01bbd..84787a21d3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/ContextExtensions.kt @@ -41,6 +41,7 @@ import androidx.core.graphics.red import androidx.core.net.toUri import androidx.localbroadcastmanager.content.LocalBroadcastManager import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.PreferenceValues import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.base.activity.BaseThemedActivity import eu.kanade.tachiyomi.util.lang.truncateCenter @@ -50,6 +51,8 @@ import uy.kohesive.injekt.api.get import java.io.File import kotlin.math.roundToInt +private const val TABLET_UI_MIN_SCREEN_WIDTH_DP = 720 + /** * Display a toast in this context. * @@ -305,7 +308,27 @@ fun Context.createFileInCacheDir(name: String): File { * We consider anything with a width of >= 720dp as a tablet, i.e. with layouts in layout-sw720dp. */ fun Context.isTablet(): Boolean { - return resources.configuration.smallestScreenWidthDp >= 720 + return resources.configuration.smallestScreenWidthDp >= TABLET_UI_MIN_SCREEN_WIDTH_DP +} + +fun Context.prepareTabletUiContext(): Context { + val configuration = resources.configuration + val expected = when (Injekt.get().tabletUiMode().get()) { + PreferenceValues.TabletUiMode.ALWAYS -> true + PreferenceValues.TabletUiMode.LANDSCAPE -> configuration.orientation == Configuration.ORIENTATION_LANDSCAPE + PreferenceValues.TabletUiMode.NEVER -> false + } + if (configuration.smallestScreenWidthDp >= TABLET_UI_MIN_SCREEN_WIDTH_DP != expected) { + val overrideConf = Configuration() + overrideConf.setTo(configuration) + overrideConf.smallestScreenWidthDp = if (expected) { + overrideConf.smallestScreenWidthDp.coerceAtLeast(TABLET_UI_MIN_SCREEN_WIDTH_DP) + } else { + overrideConf.smallestScreenWidthDp.coerceAtMost(TABLET_UI_MIN_SCREEN_WIDTH_DP - 1) + } + return createConfigurationContext(overrideConf) + } + return this } /** diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3b33d7905f..ea8a372da9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -469,6 +469,7 @@ Battery optimization is already disabled Couldn\'t open device settings Some manufacturers have additional app restrictions that kill background services. This website has more info on how to fix it. + Tablet UI Website