From bed52c7b86b0e6a0f40f4e646e17b913d090379e Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Sat, 3 Jul 2021 03:28:24 -0400 Subject: [PATCH] Splitting Light and Dark Theme setting Soi you can stop complaining now --- .../ui/setting/SettingsGeneralController.kt | 20 ++- .../tachiyomi/ui/setting/ThemePreference.kt | 132 ++++++++++++------ app/src/main/res/layout/theme_item.xml | 2 +- app/src/main/res/layout/themes_preference.xml | 42 +++++- app/src/main/res/values/strings.xml | 2 + 5 files changed, 139 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index 4db5d83224..80ec95f73d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt @@ -19,7 +19,8 @@ class SettingsGeneralController : SettingsController() { private val isUpdaterEnabled = BuildConfig.INCLUDE_UPDATER - var lastThemeX: Int? = null + var lastThemeXLight: Int? = null + var lastThemeXDark: Int? = null var themePreference: ThemePreference? = null override fun setupPreferenceScreen(screen: PreferenceScreen) = screen.apply { titleRes = R.string.general @@ -90,12 +91,13 @@ class SettingsGeneralController : SettingsController() { } preferenceCategory { - titleRes = R.string.display + titleRes = R.string.app_theme themePreference = themePreference { key = "theme_preference" titleRes = R.string.app_theme - lastScrollPostion = lastThemeX + lastScrollPostionLight = lastThemeXLight + lastScrollPostionDark = lastThemeXDark summary = if (preferences.nightMode() .get() == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM ) { @@ -124,7 +126,8 @@ class SettingsGeneralController : SettingsController() { activity?.recreate() } else { preferences.nightMode().set(context.appDelegateNightMode()) - themePreference?.fastAdapter?.notifyDataSetChanged() + themePreference?.fastAdapterLight?.notifyDataSetChanged() + themePreference?.fastAdapterDark?.notifyDataSetChanged() } true } @@ -270,13 +273,16 @@ class SettingsGeneralController : SettingsController() { } override fun onSaveViewState(view: View, outState: Bundle) { - outState.putInt(::lastThemeX.name, themePreference?.lastScrollPostion ?: 0) + outState.putInt(::lastThemeXLight.name, themePreference?.lastScrollPostionLight ?: 0) + outState.putInt(::lastThemeXDark.name, themePreference?.lastScrollPostionDark ?: 0) super.onSaveInstanceState(outState) } override fun onRestoreViewState(view: View, savedViewState: Bundle) { super.onRestoreViewState(view, savedViewState) - lastThemeX = savedViewState.getInt(::lastThemeX.name) - themePreference?.lastScrollPostion = lastThemeX + lastThemeXLight = savedViewState.getInt(::lastThemeXLight.name) + lastThemeXDark = savedViewState.getInt(::lastThemeXDark.name) + themePreference?.lastScrollPostionLight = lastThemeXLight + themePreference?.lastScrollPostionDark = lastThemeXDark } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ThemePreference.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ThemePreference.kt index 9044ccc1b9..552b29cc5d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ThemePreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/ThemePreference.kt @@ -27,88 +27,128 @@ import eu.kanade.tachiyomi.util.system.appDelegateNightMode import eu.kanade.tachiyomi.util.system.dpToPx import eu.kanade.tachiyomi.util.system.isInNightMode import uy.kohesive.injekt.injectLazy +import kotlin.math.max class ThemePreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : Preference(context, attrs) { - var fastAdapter: FastAdapter - private val itemAdapter = ItemAdapter() - private var selectExtension: SelectExtension + var fastAdapterLight: FastAdapter + var fastAdapterDark: FastAdapter + private val itemAdapterLight = ItemAdapter() + private val itemAdapterDark = ItemAdapter() + private var selectExtensionLight: SelectExtension + private var selectExtensionDark: SelectExtension private val preferences: PreferencesHelper by injectLazy() var activity: Activity? = null - var lastScrollPostion: Int? = null + var lastScrollPostionLight: Int? = null + var lastScrollPostionDark: Int? = null lateinit var binding: ThemesPreferenceBinding - val manager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) + private val managerLight = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) + private val managerDark = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) init { layoutResource = R.layout.themes_preference - fastAdapter = FastAdapter.with(itemAdapter) - fastAdapter.setHasStableIds(true) - selectExtension = fastAdapter.getSelectExtension().apply { - isSelectable = true - multiSelect = true - selectionListener = object : ISelectionListener { - override fun onSelectionChanged(item: ThemeItem, selected: Boolean) { - if (item.theme.nightMode == AppCompatDelegate.MODE_NIGHT_YES) { - preferences.darkTheme().set(item.theme) - } else { - preferences.lightTheme().set(item.theme) - } - if (!selected) { - preferences.nightMode().set(item.theme.nightMode) - } else if (preferences.nightMode() - .get() != AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM - ) { - preferences.nightMode().set(item.theme.nightMode) - } - if (( - preferences.nightMode().get() == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM && - item.theme.nightMode != context.appDelegateNightMode() - ) || - (!selected && item.theme.nightMode == context.appDelegateNightMode()) - ) { - fastAdapter.notifyDataSetChanged() - } else { - activity?.recreate() - } + fastAdapterLight = FastAdapter.with(itemAdapterLight) + fastAdapterDark = FastAdapter.with(itemAdapterDark) + fastAdapterLight.setHasStableIds(true) + fastAdapterDark.setHasStableIds(true) + selectExtensionLight = fastAdapterLight.getSelectExtension().setThemeListener() + selectExtensionDark = fastAdapterDark.getSelectExtension().setThemeListener() + val enumConstants = Themes.values() + itemAdapterLight.set(enumConstants.filter { it.nightMode == AppCompatDelegate.MODE_NIGHT_NO }.map(::ThemeItem)) + itemAdapterDark.set(enumConstants.filter { it.nightMode == AppCompatDelegate.MODE_NIGHT_YES }.map(::ThemeItem)) + isSelectable = false + } + + private fun SelectExtension.setThemeListener(): SelectExtension { + isSelectable = true + multiSelect = true + selectionListener = object : ISelectionListener { + override fun onSelectionChanged(item: ThemeItem, selected: Boolean) { + if (item.theme.nightMode == AppCompatDelegate.MODE_NIGHT_YES) { + preferences.darkTheme().set(item.theme) + } else { + preferences.lightTheme().set(item.theme) + } + if (!selected) { + preferences.nightMode().set(item.theme.nightMode) + } else if (preferences.nightMode() + .get() != AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM + ) { + preferences.nightMode().set(item.theme.nightMode) + } + if (( + preferences.nightMode().get() == AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM && + item.theme.nightMode != context.appDelegateNightMode() + ) || + (!selected && item.theme.nightMode == context.appDelegateNightMode()) + ) { + fastAdapterLight.notifyDataSetChanged() + fastAdapterDark.notifyDataSetChanged() + } else { + activity?.recreate() } } } - - val enumConstants = Themes.values() - itemAdapter.set(enumConstants.map(::ThemeItem)) - isSelectable = false + return this } override fun onBindViewHolder(holder: PreferenceViewHolder) { super.onBindViewHolder(holder) binding = ThemesPreferenceBinding.bind(holder.itemView) - binding.themePrefTitle.text = title binding.themeRecycler.setHasFixedSize(true) - binding.themeRecycler.layoutManager = manager + binding.themeRecycler.layoutManager = managerLight - binding.themeRecycler.adapter = fastAdapter + binding.themeRecycler.adapter = fastAdapterLight binding.themeRecycler.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { super.onScrolled(recyclerView, dx, dy) - lastScrollPostion = + lastScrollPostionLight = recyclerView.computeHorizontalScrollOffset() } }) - if (lastScrollPostion != null) { - val lX = lastScrollPostion!! + binding.themeRecyclerDark.setHasFixedSize(true) + binding.themeRecyclerDark.layoutManager = managerDark + + binding.themeRecyclerDark.adapter = fastAdapterDark + + binding.themeRecyclerDark.addOnScrollListener(object : RecyclerView.OnScrollListener() { + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + super.onScrolled(recyclerView, dx, dy) + lastScrollPostionDark = + recyclerView.computeHorizontalScrollOffset() + } + }) + + if (lastScrollPostionLight != null) { + val lX = lastScrollPostionLight!! (binding.themeRecycler.layoutManager as LinearLayoutManager).apply { scrollToPositionWithOffset( lX / 110.dpToPx, -lX % 110.dpToPx ) } - lastScrollPostion = binding.themeRecycler.computeHorizontalScrollOffset() + lastScrollPostionLight = binding.themeRecycler.computeHorizontalScrollOffset() } else { binding.themeRecycler.scrollToPosition( - selectExtension.selections.firstOrNull() ?: 0 + max((selectExtensionLight.selections.firstOrNull() ?: 0) - 1, 0) + ) + } + + if (lastScrollPostionDark != null) { + val lX = lastScrollPostionDark!! + (binding.themeRecyclerDark.layoutManager as LinearLayoutManager).apply { + scrollToPositionWithOffset( + lX / 110.dpToPx, + -lX % 110.dpToPx + ) + } + lastScrollPostionDark = binding.themeRecyclerDark.computeHorizontalScrollOffset() + } else { + binding.themeRecyclerDark.scrollToPosition( + max((selectExtensionDark.selections.firstOrNull() ?: 0) - 1, 0) ) } } diff --git a/app/src/main/res/layout/theme_item.xml b/app/src/main/res/layout/theme_item.xml index 5539a91c95..b37b81265e 100644 --- a/app/src/main/res/layout/theme_item.xml +++ b/app/src/main/res/layout/theme_item.xml @@ -49,7 +49,7 @@ android:layout_width="match_parent" android:layout_height="@dimen/theme_app_bar_preview" android:background="?attr/colorSecondary" - android:elevation="2dp" + android:elevation="1dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> diff --git a/app/src/main/res/layout/themes_preference.xml b/app/src/main/res/layout/themes_preference.xml index d58b5edc8c..1aaf632d0e 100644 --- a/app/src/main/res/layout/themes_preference.xml +++ b/app/src/main/res/layout/themes_preference.xml @@ -5,18 +5,18 @@ xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"> - + android:text="@string/light_theme"/> + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 69620eaa32..71e2de4a66 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -602,6 +602,8 @@ App theme + Light theme + Dark theme Light Blue Dark AMOLED Black