Skip to content

Commit

Permalink
Fix themes requiring Restart, custom colors is still a lil bugged
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Dec 15, 2024
1 parent 451be0d commit 2390697
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 63 deletions.
37 changes: 17 additions & 20 deletions app/src/main/java/dev/brahmkshatriya/echo/EchoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,33 @@ class EchoApplication : Application(), SingletonImageLoader.Factory {

companion object {

private var theme: Int? = null

@SuppressLint("RestrictedApi")
private val onAppliedCallback = DynamicColors.OnAppliedCallback {
val theme = theme ?: return@OnAppliedCallback
ThemeUtils.applyThemeOverlay(it, theme)
}

fun applyUiChanges(app: Application, preferences: SharedPreferences) {
val theme = when (preferences.getBoolean(AMOLED_KEY, false)) {
true -> R.style.Amoled
false -> null
val mode = when (preferences.getString(THEME_KEY, "system")) {
"light" -> AppCompatDelegate.MODE_NIGHT_NO
"dark" -> AppCompatDelegate.MODE_NIGHT_YES
else -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
}
AppCompatDelegate.setDefaultNightMode(mode)

theme = if (preferences.getBoolean(AMOLED_KEY, false)) R.style.Amoled else null

val customColor = if (!preferences.getBoolean(CUSTOM_THEME_KEY, false)) null
else preferences.getInt(COLOR_KEY, -1).takeIf { it != -1 }

val builder = if (customColor != null) DynamicColorsOptions.Builder()
.setContentBasedSource(customColor)
else DynamicColorsOptions.Builder()

theme?.let {
builder.setOnAppliedCallback {
ThemeUtils.applyThemeOverlay(it, theme)
}
}

val builder = DynamicColorsOptions.Builder()
builder.setOnAppliedCallback(onAppliedCallback)
customColor?.let { builder.setContentBasedSource(it) }
DynamicColors.applyToActivitiesIfAvailable(app, builder.build())

when (preferences.getString(THEME_KEY, "system")) {
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
"dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}


fun applyLocale(sharedPref: SharedPreferences) {
val value = sharedPref.getString("language", "system") ?: "system"
val locale = if (value == "system") LocaleListCompat.getEmptyLocaleList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ class PlayerFragment : Fragment() {
fun update() {
val list = viewModel.list
adapter.submitList(list) {
val index = viewModel.currentFlow.value?.index ?: -1
val smooth = abs(index - binding.viewPager.currentItem) <= 1
binding.viewPager.setCurrentItem(index, smooth)
runCatching {
val index = viewModel.currentFlow.value?.index ?: -1
val smooth = abs(index - binding.viewPager.currentItem) <= 1
binding.viewPager.setCurrentItem(index, smooth)
}
}
if (list.isEmpty()) {
emit(uiViewModel.changeInfoState) { STATE_COLLAPSED }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SearchFragment : Fragment() {
setupTransition(view)
applyInsetsMain(binding.appBarLayout, binding.recyclerView) {
binding.quickSearchView.updatePaddingRelative(start = it.start, end = it.end)
binding.quickSearchRecyclerView.updatePaddingRelative(bottom = it.bottom)
}
applyBackPressCallback {
if (it == STATE_EXPANDED) binding.quickSearchView.hide()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.brahmkshatriya.echo.ui.settings

import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
Expand All @@ -10,9 +12,6 @@ import dev.brahmkshatriya.echo.EchoApplication.Companion.applyUiChanges
import dev.brahmkshatriya.echo.R
import dev.brahmkshatriya.echo.utils.prefs.ColorListPreference
import dev.brahmkshatriya.echo.utils.prefs.MaterialListPreference
import dev.brahmkshatriya.echo.utils.restartApp
import dev.brahmkshatriya.echo.viewmodels.SnackBar
import dev.brahmkshatriya.echo.viewmodels.SnackBar.Companion.createSnack


class LookFragment : BaseSettingsFragment() {
Expand All @@ -28,26 +27,9 @@ class LookFragment : BaseSettingsFragment() {
preferenceManager.sharedPreferencesMode = Context.MODE_PRIVATE
val preferences = preferenceManager.sharedPreferences ?: return

val message = SnackBar.Message(
getString(R.string.restart_app),
SnackBar.Action(getString(R.string.restart)) {
context.restartApp()
}
)

fun uiListener(block: (Any) -> Unit = {}) =
Preference.OnPreferenceChangeListener { _, new ->
val activity = requireActivity()
applyUiChanges(activity.application, preferences)
createSnack(message)
block(new)
true
}

val screen = preferenceManager.createPreferenceScreen(context)
preferenceScreen = screen


PreferenceCategory(context).apply {
title = getString(R.string.ui)
key = "ui"
Expand All @@ -65,7 +47,6 @@ class LookFragment : BaseSettingsFragment() {
entries = context.resources.getStringArray(R.array.themes)
entryValues = arrayOf("light", "dark", "system")
value = preferences.getString(THEME_KEY, "system")
onPreferenceChangeListener = uiListener()
addPreference(this)
}

Expand All @@ -76,16 +57,16 @@ class LookFragment : BaseSettingsFragment() {
layoutResource = R.layout.preference_switch
isIconSpaceReserved = false
setDefaultValue(false)
onPreferenceChangeListener = uiListener {
onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, it ->
screen.findPreference<Preference>(COLOR_KEY)?.isEnabled = it as Boolean
true
}
addPreference(this)
}

ColorListPreference(this@LookPreference).apply {
key = COLOR_KEY
isEnabled = preferences.getBoolean(CUSTOM_THEME_KEY, false)
listener = ColorListPreference.Listener { createSnack(message) }
addPreference(this)
}

Expand All @@ -96,7 +77,6 @@ class LookFragment : BaseSettingsFragment() {
layoutResource = R.layout.preference_switch
isIconSpaceReserved = false
setDefaultValue(false)
onPreferenceChangeListener = uiListener()
addPreference(this)
}

Expand Down Expand Up @@ -145,7 +125,6 @@ class LookFragment : BaseSettingsFragment() {
layoutResource = R.layout.preference_switch
isIconSpaceReserved = false
setDefaultValue(true)
onPreferenceChangeListener = uiListener()
addPreference(this)
}

Expand All @@ -156,11 +135,30 @@ class LookFragment : BaseSettingsFragment() {
layoutResource = R.layout.preference_switch
isIconSpaceReserved = false
setDefaultValue(true)
onPreferenceChangeListener = uiListener()
addPreference(this)
}
}
}

val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
when (key) {
THEME_KEY, CUSTOM_THEME_KEY, COLOR_KEY, AMOLED_KEY -> {
requireActivity().applyUiChanges()
}
}
}

override fun onResume() {
super.onResume()
preferenceManager.sharedPreferences!!.registerOnSharedPreferenceChangeListener(listener)
}

override fun onPause() {
super.onPause()
preferenceManager.sharedPreferences!!.unregisterOnSharedPreferenceChangeListener(
listener
)
}
}


Expand All @@ -174,5 +172,11 @@ class LookFragment : BaseSettingsFragment() {
const val SHOW_BACKGROUND = "show_background"
const val ANIMATIONS_KEY = "animations"
const val SHARED_ELEMENT_KEY = "shared_element_transitions"

fun Activity.applyUiChanges() {
val preferences = getSharedPreferences(packageName, Context.MODE_PRIVATE)
applyUiChanges(application, preferences)
recreate()
}
}
}
29 changes: 15 additions & 14 deletions app/src/main/res/layout/fragment_search.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,33 @@
<com.google.android.material.search.SearchBar
android:id="@+id/searchBar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginVertical="12dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="112dp"
android:layout_height="40dp"
android:hint="@string/search"
app:backgroundTint="?navBackground"
app:strokeWidth="1dp"
app:strokeColor="?colorSurfaceContainer"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed|snap"
app:defaultMarginsEnabled="false"
app:navigationIcon="@drawable/ic_search_outline" />
app:layout_scrollFlags="scroll|enterAlwaysCollapsed|snap"
app:navigationIcon="@drawable/ic_search_outline"
app:strokeColor="?colorSurfaceContainer"
app:strokeWidth="1dp" />

<com.google.android.material.appbar.MaterialToolbar
android:layout_gravity="end"
android:id="@+id/toolBar"
android:layout_marginTop="-64dp"
android:layout_marginHorizontal="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_marginHorizontal="8dp"
android:layout_marginTop="-64dp"
app:layout_constraintEnd_toEndOf="parent"
app:menu="@menu/top_bar_menu" />

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
style="@style/TabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TabLayout">
android:layout_height="wrap_content">

<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
Expand Down Expand Up @@ -95,15 +95,16 @@
android:id="@+id/quickSearchView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="1dp"
android:hint="@string/search"
android:elevation="0dp"
app:closeIcon="@drawable/ic_close"
app:backgroundTint="?navBackground">
app:backHandlingEnabled="true"
app:backgroundTint="?navBackground"
app:closeIcon="@drawable/ic_close">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/quickSearchRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:contentDescription="@string/search"
android:orientation="vertical"
Expand Down

0 comments on commit 2390697

Please sign in to comment.