Skip to content

Commit e8d101e

Browse files
committed
Update select theme feature implementation
- package: view > preferences - cleanup
1 parent 925de3d commit e8d101e

File tree

11 files changed

+77
-29
lines changed

11 files changed

+77
-29
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ dependencies {
4141
implementation("androidx.preference:preference-ktx:1.2.1")
4242
implementation("com.google.android.material:material:1.12.0")
4343
implementation("androidx.navigation:navigation-fragment-ktx:2.8.9")
44+
implementation("androidx.datastore:datastore-preferences:1.1.4")
4445
}

app/src/main/java/com/wstxda/gsl/activity/SettingsActivity.kt

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.wstxda.gsl.activity
22

3+
import android.content.Context
34
import android.graphics.Color
45
import android.os.Build
56
import android.os.Bundle
@@ -8,13 +9,24 @@ import androidx.activity.enableEdgeToEdge
89
import androidx.appcompat.app.AppCompatActivity
910
import androidx.core.view.ViewCompat
1011
import androidx.core.view.WindowInsetsCompat
11-
import androidx.navigation.fragment.NavHostFragment
1212
import com.wstxda.gsl.R
1313
import com.wstxda.gsl.databinding.ActivitySettingsBinding
14+
import com.wstxda.gsl.fragments.preferences.ThemePreferences
15+
import com.wstxda.gsl.ui.ThemeManager
16+
import kotlinx.coroutines.flow.first
17+
import kotlinx.coroutines.runBlocking
1418

1519
class SettingsActivity : AppCompatActivity() {
20+
1621
private val binding by lazy { ActivitySettingsBinding.inflate(layoutInflater) }
1722

23+
override fun attachBaseContext(newBase: Context?) {
24+
super.attachBaseContext(newBase)
25+
val themePreferences = ThemePreferences(newBase!!)
26+
val savedTheme = runBlocking { themePreferences.themeFlow.first() }
27+
ThemeManager.applyTheme(savedTheme)
28+
}
29+
1830
override fun onCreate(savedInstanceState: Bundle?) {
1931
super.onCreate(savedInstanceState)
2032
setContentView(binding.root)
@@ -25,26 +37,19 @@ class SettingsActivity : AppCompatActivity() {
2537
setSupportActionBar(binding.toolbar)
2638
supportActionBar?.setDisplayHomeAsUpEnabled(false)
2739

28-
if (savedInstanceState == null) {
29-
val navHostFragment =
30-
supportFragmentManager.findFragmentById(R.id.nav_host_container) as NavHostFragment
31-
val navController = navHostFragment.navController
32-
navController.setGraph(R.navigation.nav_settings)
33-
}
34-
35-
ViewCompat.setOnApplyWindowInsetsListener(binding.navHostContainer) { v, insets ->
36-
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
37-
v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom * 2)
40+
ViewCompat.setOnApplyWindowInsetsListener(binding.navHostContainer) { view, insets ->
41+
val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
42+
view.setPadding(
43+
systemBarsInsets.left, 0, systemBarsInsets.right, systemBarsInsets.bottom * 2
44+
)
3845
insets
3946
}
4047
}
4148

4249
private fun enableEdgeToEdgeNoContrast() {
4350
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
4451
enableEdgeToEdge(
45-
navigationBarStyle = SystemBarStyle.auto(
46-
Color.TRANSPARENT, Color.TRANSPARENT
47-
)
52+
navigationBarStyle = SystemBarStyle.auto(Color.TRANSPARENT, Color.TRANSPARENT)
4853
)
4954
window.isNavigationBarContrastEnforced = false
5055
}

app/src/main/java/com/wstxda/gsl/fragments/SettingsFragment.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ import androidx.preference.PreferenceFragmentCompat
1414
import androidx.preference.SwitchPreferenceCompat
1515
import com.wstxda.gsl.R
1616
import com.wstxda.gsl.activity.SettingsActivity
17-
import com.wstxda.gsl.fragments.view.DigitalAssistantPreference
17+
import com.wstxda.gsl.fragments.preferences.DigitalAssistantPreference
18+
import com.wstxda.gsl.fragments.preferences.ThemePreferences
1819
import com.wstxda.gsl.shortcuts.*
1920
import com.wstxda.gsl.ui.DigitalAssistantSetupDialog
2021
import com.wstxda.gsl.ui.ThemeManager
2122
import com.wstxda.gsl.ui.TileManager
2223
import com.wstxda.gsl.utils.Constants
24+
import kotlinx.coroutines.flow.first
2325
import kotlinx.coroutines.launch
2426

2527
class SettingsFragment : PreferenceFragmentCompat() {
28+
private lateinit var themePreferences: ThemePreferences
2629

2730
private val digitalAssistantPreference by lazy { DigitalAssistantPreference(this) }
2831

2932
private val digitalAssistantLauncher =
30-
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { _ ->
33+
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
3134
viewLifecycleOwner.lifecycleScope.launch {
3235
val isAssistSetupDone = digitalAssistantPreference.checkDigitalAssistSetupStatus()
3336
digitalAssistantPreference.updateDigitalAssistantPreferences(isAssistSetupDone)
@@ -54,6 +57,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
5457

5558
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
5659
setPreferencesFromResource(R.xml.preferences, rootKey)
60+
themePreferences = ThemePreferences(requireContext())
61+
5762
setupInitialVisibility()
5863
setupPreferences()
5964
}
@@ -66,7 +71,6 @@ class SettingsFragment : PreferenceFragmentCompat() {
6671
private fun setupInitialVisibility() {
6772
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
6873
findPreference<Preference>(Constants.MUSIC_SEARCH_TILE_KEY)?.isVisible = false
69-
findPreference<Preference>(Constants.DIGITAL_ASSISTANT_SETUP_KEY)?.isVisible = false
7074
}
7175
}
7276

@@ -80,7 +84,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
8084

8185
private fun setupShortcutsActivityPreferences() {
8286
shortcuts.forEach { (key, activityClass) ->
83-
findPreference<SwitchPreferenceCompat>(key)?.setOnPreferenceChangeListener { _, newValue ->
87+
(findPreference<SwitchPreferenceCompat>(key))?.setOnPreferenceChangeListener { _, newValue ->
8488
toggleActivityVisibility(activityClass, newValue as Boolean)
8589
true
8690
}
@@ -106,6 +110,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
106110
private fun setupDigitalAssistantPreferences() {
107111
val isAssistSetupDone = digitalAssistantPreference.checkDigitalAssistSetupStatus()
108112
digitalAssistantPreference.updateDigitalAssistantPreferences(isAssistSetupDone)
113+
109114
if (!isAssistSetupDone) {
110115
findPreference<Preference>(Constants.DIGITAL_ASSISTANT_SETUP_KEY)?.setOnPreferenceClickListener {
111116
DigitalAssistantSetupDialog.show(childFragmentManager, digitalAssistantLauncher)
@@ -115,8 +120,18 @@ class SettingsFragment : PreferenceFragmentCompat() {
115120
}
116121

117122
private fun setupThemePreference() {
118-
findPreference<ListPreference>(Constants.SELECT_THEME_KEY)?.setOnPreferenceChangeListener { _, newValue ->
119-
ThemeManager.setupTheme(newValue as String)
123+
val themePreference = findPreference<ListPreference>("select_theme")
124+
lifecycleScope.launch {
125+
val currentTheme = themePreferences.themeFlow.first()
126+
themePreference?.value = currentTheme
127+
}
128+
129+
themePreference?.setOnPreferenceChangeListener { _, newValue ->
130+
val selectedTheme = newValue as String
131+
lifecycleScope.launch {
132+
themePreferences.saveTheme(selectedTheme)
133+
ThemeManager.applyTheme(selectedTheme)
134+
}
120135
true
121136
}
122137
}

app/src/main/java/com/wstxda/gsl/fragments/view/DigitalAssistantPreference.kt renamed to app/src/main/java/com/wstxda/gsl/fragments/preferences/DigitalAssistantPreference.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.wstxda.gsl.fragments.view
1+
package com.wstxda.gsl.fragments.preferences
22

33
import android.content.Context
44
import android.os.Build
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.wstxda.gsl.fragments.preferences
2+
3+
import android.content.Context
4+
import androidx.datastore.preferences.core.edit
5+
import androidx.datastore.preferences.core.stringPreferencesKey
6+
import androidx.datastore.preferences.preferencesDataStore
7+
import com.wstxda.gsl.utils.Constants
8+
import kotlinx.coroutines.flow.Flow
9+
import kotlinx.coroutines.flow.map
10+
11+
private val Context.dataStore by preferencesDataStore(name = "theme_preferences")
12+
13+
class ThemePreferences(private val context: Context) {
14+
15+
companion object {
16+
private val THEME_KEY = stringPreferencesKey("theme_key")
17+
}
18+
19+
val themeFlow: Flow<String> = context.dataStore.data.map { preferences ->
20+
preferences[THEME_KEY] ?: Constants.THEME_SYSTEM
21+
}
22+
23+
suspend fun saveTheme(theme: String) {
24+
context.dataStore.edit { preferences ->
25+
preferences[THEME_KEY] = when (theme) {
26+
Constants.THEME_LIGHT, Constants.THEME_DARK, Constants.THEME_SYSTEM -> theme
27+
else -> Constants.THEME_SYSTEM
28+
}
29+
}
30+
}
31+
}

app/src/main/java/com/wstxda/gsl/fragments/view/UpdaterPreference.kt renamed to app/src/main/java/com/wstxda/gsl/fragments/preferences/UpdaterPreference.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.wstxda.gsl.fragments.view
1+
package com.wstxda.gsl.fragments.preferences
22

33
import android.content.Context
44
import android.util.AttributeSet

app/src/main/java/com/wstxda/gsl/ui/DigitalAssistantSetupDialog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import androidx.fragment.app.DialogFragment
1010
import androidx.preference.PreferenceFragmentCompat
1111
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1212
import com.wstxda.gsl.databinding.AssistantSetupDialogBinding
13-
import com.wstxda.gsl.fragments.view.DigitalAssistantPreference
13+
import com.wstxda.gsl.fragments.preferences.DigitalAssistantPreference
1414
import com.wstxda.gsl.utils.Constants
1515

1616
class DigitalAssistantSetupDialog : DialogFragment() {

app/src/main/java/com/wstxda/gsl/ui/ThemeManager.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import androidx.appcompat.app.AppCompatDelegate
44
import com.wstxda.gsl.utils.Constants
55

66
object ThemeManager {
7-
fun setupTheme(theme: String) {
7+
fun applyTheme(theme: String) {
88
when (theme) {
99
Constants.THEME_LIGHT -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
1010
Constants.THEME_DARK -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
1111
Constants.THEME_SYSTEM -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
12-
else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
1312
}
1413
}
1514
}

app/src/main/java/com/wstxda/gsl/utils/Constants.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ object Constants {
1212
const val DIGITAL_ASSISTANT_SETUP_KEY = "digital_assistant_setup"
1313
const val DIGITAL_ASSISTANT_DIALOG_TAG = "DigitalAssistantSetupDialog"
1414
const val GITHUB_RELEASE_URL = "https://api.github.com/repos/WSTxda/Google-Shortcuts-Launcher/releases/latest"
15-
const val SELECT_THEME_KEY = "select_theme"
1615
const val THEME_SYSTEM = "system"
1716
const val THEME_LIGHT = "light"
1817
const val THEME_DARK = "dark"

app/src/main/res/values/arrays.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
</string-array>
1414

1515
<string-array name="digital_assistant_shortcut_entries">
16-
<!--<item>@string/assistant</item>-->
1716
<item>@string/files</item>
1817
<item>@string/games</item>
1918
<item>@string/lens</item>
@@ -24,7 +23,6 @@
2423
</string-array>
2524

2625
<string-array name="digital_assistant_shortcut_values">
27-
<!--<item>assistant_shortcut</item>-->
2826
<item>files_shortcut</item>
2927
<item>games_shortcut</item>
3028
<item>lens_shortcut</item>

0 commit comments

Comments
 (0)