Skip to content

Commit 11373ee

Browse files
committed
Refactor theme dialog into com.jkuester
1 parent 39dc32c commit 11373ee

File tree

13 files changed

+204
-95
lines changed

13 files changed

+204
-95
lines changed

app/src/main/java/com/jkuester/unlauncher/datasource/CorePreferencesCalculations.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.jkuester.unlauncher.datastore.proto.AlignmentFormat
44
import com.jkuester.unlauncher.datastore.proto.ClockType
55
import com.jkuester.unlauncher.datastore.proto.CorePreferences
66
import com.jkuester.unlauncher.datastore.proto.SearchBarPosition
7+
import com.jkuester.unlauncher.datastore.proto.Theme
78
import com.jkuester.unlauncher.datastore.proto.TimeFormat
89

910
fun toggleActivateKeyboardInDrawer() = { originalPrefs: CorePreferences ->
@@ -33,3 +34,6 @@ fun setAlignmentFormat(alignmentFormat: AlignmentFormat) = { originalPrefs: Core
3334
fun setTimeFormat(timeFormat: TimeFormat) = { originalPrefs: CorePreferences ->
3435
originalPrefs.toBuilder().setTimeFormat(timeFormat).build()
3536
}
37+
fun setTheme(theme: Theme) = { originalPrefs: CorePreferences ->
38+
originalPrefs.toBuilder().setTheme(theme).build()
39+
}

app/src/main/java/com/jkuester/unlauncher/datasource/CorePreferencesMigrations.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import androidx.datastore.migrations.SharedPreferencesMigration
66
import androidx.datastore.migrations.SharedPreferencesView
77
import com.jkuester.unlauncher.datastore.proto.ClockType
88
import com.jkuester.unlauncher.datastore.proto.CorePreferences
9+
import com.jkuester.unlauncher.datastore.proto.Theme
910
import com.jkuester.unlauncher.datastore.proto.TimeFormat
1011

1112
private const val SHARED_PREF_GROUP_NAME = "settings"
1213
private const val PREFS_SETTINGS_KEY_TIME_FORMAT = "time_format"
14+
private const val PREFS_SETTINGS_KEY_THEME = "key_theme"
1315

1416
object AddClockTypeMigration : DataMigration<CorePreferences> {
1517
override suspend fun shouldMigrate(currentData: CorePreferences) = !currentData.hasClockType()
@@ -24,13 +26,16 @@ object AddShowSearchBarMigration : DataMigration<CorePreferences> {
2426
override suspend fun cleanUp() {}
2527
}
2628

27-
fun timeFormatSharedPrefsMigration(context: Context) = SharedPreferencesMigration(
29+
fun slimLauncherSharedPrefsMigration(context: Context) = SharedPreferencesMigration(
2830
context,
2931
SHARED_PREF_GROUP_NAME,
30-
setOf(PREFS_SETTINGS_KEY_TIME_FORMAT),
32+
setOf(PREFS_SETTINGS_KEY_TIME_FORMAT, PREFS_SETTINGS_KEY_THEME),
3133
{ true },
3234
{ sharedPrefs: SharedPreferencesView, currentData: CorePreferences ->
3335
val timeFormatPref = sharedPrefs.getInt(PREFS_SETTINGS_KEY_TIME_FORMAT, 0)
34-
setTimeFormat(TimeFormat.forNumber(timeFormatPref))(currentData)
36+
val themePref = sharedPrefs.getInt(PREFS_SETTINGS_KEY_THEME, 0)
37+
currentData
38+
.let(setTimeFormat(TimeFormat.forNumber(timeFormatPref)))
39+
.let(setTheme(Theme.forNumber(themePref)))
3540
}
3641
)

app/src/main/java/com/jkuester/unlauncher/datasource/DataStoreModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ private val Context.unlauncherAppsStore: DataStore<UnlauncherApps> by dataStore(
3434
produceMigrations = { context -> listOf(SortAppsMigration, HomeAppToIndexMigration(context)) }
3535
)
3636

37-
private val Context.corePreferencesStore: DataStore<CorePreferences> by dataStore(
37+
internal val Context.corePreferencesStore: DataStore<CorePreferences> by dataStore(
3838
fileName = "core_preferences.proto",
3939
serializer = DataSerializer(CorePreferences::getDefaultInstance, CorePreferences::parseFrom),
4040
produceMigrations = { context ->
41-
listOf(AddClockTypeMigration, AddShowSearchBarMigration, timeFormatSharedPrefsMigration(context))
41+
listOf(AddClockTypeMigration, AddShowSearchBarMigration, slimLauncherSharedPrefsMigration(context))
4242
}
4343
)
4444

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.jkuester.unlauncher.dialog
2+
3+
import android.app.AlertDialog
4+
import android.app.Dialog
5+
import android.content.DialogInterface
6+
import android.os.Bundle
7+
import androidx.fragment.app.DialogFragment
8+
import com.jkuester.unlauncher.datasource.DataRepository
9+
import com.jkuester.unlauncher.datasource.setTheme
10+
import com.jkuester.unlauncher.datastore.proto.CorePreferences
11+
import com.jkuester.unlauncher.datastore.proto.Theme
12+
import com.jkuester.unlauncher.fragment.WithFragmentLifecycle
13+
import com.sduduzog.slimlauncher.R
14+
import dagger.hilt.android.AndroidEntryPoint
15+
import javax.inject.Inject
16+
17+
@AndroidEntryPoint
18+
class ThemeDialog : DialogFragment() {
19+
@Inject @WithFragmentLifecycle
20+
lateinit var corePreferencesRepo: DataRepository<CorePreferences>
21+
22+
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = AlertDialog
23+
.Builder(context)
24+
.setTitle(R.string.choose_theme_dialog_title)
25+
.setSingleChoiceItems(
26+
R.array.themes_array,
27+
corePreferencesRepo.get().theme.number,
28+
this::onSelection
29+
)
30+
.create()
31+
32+
private fun onSelection(dialogInterface: DialogInterface, i: Int) = dialogInterface
33+
.dismiss()
34+
.also { corePreferencesRepo.updateAsync(setTheme(Theme.forNumber(i))) }
35+
}

app/src/main/java/com/sduduzog/slimlauncher/MainActivity.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import android.view.View
1313
import androidx.annotation.StyleRes
1414
import androidx.appcompat.app.AppCompatActivity
1515
import androidx.constraintlayout.motion.widget.MotionLayout
16-
import androidx.datastore.core.DataStore
1716
import androidx.navigation.NavController
1817
import androidx.navigation.Navigation.findNavController
1918
import androidx.navigation.fragment.NavHostFragment
2019
import androidx.recyclerview.widget.RecyclerView
20+
import com.jkuester.unlauncher.WithActivityLifecycle
21+
import com.jkuester.unlauncher.datasource.DataRepository
22+
import com.jkuester.unlauncher.datasource.corePreferencesStore
2123
import com.jkuester.unlauncher.datastore.proto.CorePreferences
2224
import com.sduduzog.slimlauncher.utils.BaseFragment
2325
import com.sduduzog.slimlauncher.utils.HomeWatcher
@@ -33,6 +35,8 @@ import dagger.hilt.android.components.ActivityComponent
3335
import java.lang.reflect.Method
3436
import javax.inject.Inject
3537
import kotlin.math.absoluteValue
38+
import kotlinx.coroutines.flow.first
39+
import kotlinx.coroutines.runBlocking
3640

3741
@AndroidEntryPoint
3842
class MainActivity :
@@ -43,9 +47,8 @@ class MainActivity :
4347

4448
@Inject
4549
lateinit var systemUiManager: SystemUiManager
46-
47-
@Inject
48-
lateinit var corePreferencesStore: DataStore<CorePreferences>
50+
@Inject @WithActivityLifecycle
51+
lateinit var corePrefRepo: DataRepository<CorePreferences>
4952

5053
@EntryPoint
5154
@InstallIn(ActivityComponent::class)
@@ -82,7 +85,19 @@ class MainActivity :
8285
}
8386

8487
override fun onCreate(savedInstanceState: Bundle?) {
88+
// Set theme before creating activity
89+
var currentTheme = runBlocking { corePreferencesStore.data.first().theme.number }
90+
setTheme(resolveTheme(currentTheme))
91+
8592
super.onCreate(savedInstanceState)
93+
94+
corePrefRepo.observe {
95+
if (it.theme.number != currentTheme) {
96+
currentTheme = it.theme.number
97+
setTheme(resolveTheme(currentTheme))
98+
recreate()
99+
}
100+
}
86101
setContentView(R.layout.main_activity)
87102
settings = getSharedPreferences(getString(R.string.prefs_settings), MODE_PRIVATE)
88103
settings.registerOnSharedPreferenceChangeListener(this)
@@ -121,9 +136,6 @@ class MainActivity :
121136
}
122137

123138
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, s: String?) {
124-
if (s.equals(getString(R.string.prefs_settings_key_theme), true)) {
125-
recreate()
126-
}
127139
if (s.equals(getString(R.string.prefs_settings_key_toggle_status_bar), true)) {
128140
systemUiManager.setSystemUiVisibility()
129141
}
@@ -137,14 +149,9 @@ class MainActivity :
137149
wallpaperManager.onApplyThemeResource(theme, resid)
138150
}
139151

140-
override fun setTheme(resId: Int) {
141-
super.setTheme(getUserSelectedThemeRes())
142-
}
143-
144152
@StyleRes
145153
fun getUserSelectedThemeRes(): Int {
146-
settings = getSharedPreferences(getString(R.string.prefs_settings), MODE_PRIVATE)
147-
val active = settings.getInt(getString(R.string.prefs_settings_key_theme), 0)
154+
val active = runBlocking { corePreferencesStore.data.first().theme.number }
148155
return resolveTheme(active)
149156
}
150157

app/src/main/java/com/sduduzog/slimlauncher/ui/dialogs/ChangeThemeDialog.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.

app/src/main/java/com/sduduzog/slimlauncher/ui/options/OptionsFragment.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import com.jkuester.unlauncher.datasource.setKeepDeviceWallpaper
1414
import com.jkuester.unlauncher.datastore.proto.CorePreferences
1515
import com.jkuester.unlauncher.dialog.AlignmentFormatDialog
1616
import com.jkuester.unlauncher.dialog.ClockTypeDialog
17+
import com.jkuester.unlauncher.dialog.ThemeDialog
1718
import com.jkuester.unlauncher.dialog.TimeFormatDialog
1819
import com.jkuester.unlauncher.fragment.WithFragmentLifecycle
1920
import com.sduduzog.slimlauncher.R
2021
import com.sduduzog.slimlauncher.databinding.OptionsFragmentBinding
21-
import com.sduduzog.slimlauncher.ui.dialogs.ChangeThemeDialog
2222
import com.sduduzog.slimlauncher.utils.BaseFragment
2323
import com.sduduzog.slimlauncher.utils.createTitleAndSubtitleText
2424
import com.sduduzog.slimlauncher.utils.isActivityDefaultLauncher
@@ -54,8 +54,7 @@ class OptionsFragment : BaseFragment() {
5454
true
5555
}
5656
optionsFragment.optionsFragmentChangeTheme.setOnClickListener {
57-
val changeThemeDialog = ChangeThemeDialog.getThemeChooser()
58-
changeThemeDialog.showNow(childFragmentManager, "THEME_CHOOSER")
57+
ThemeDialog().showNow(childFragmentManager, null)
5958
}
6059
optionsFragment.optionsFragmentChooseTimeFormat.setOnClickListener {
6160
TimeFormatDialog().showNow(childFragmentManager, null)

app/src/main/java/com/sduduzog/slimlauncher/utils/SystemUiManager.kt

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import android.view.WindowInsetsController
1414
import android.view.WindowManager
1515
import androidx.annotation.RequiresApi
1616
import androidx.appcompat.app.AppCompatActivity
17+
import com.jkuester.unlauncher.WithActivityLifecycle
18+
import com.jkuester.unlauncher.datasource.DataRepository
19+
import com.jkuester.unlauncher.datastore.proto.CorePreferences
1720
import com.sduduzog.slimlauncher.R
1821
import dagger.Module
1922
import dagger.Provides
@@ -23,7 +26,10 @@ import dagger.hilt.android.qualifiers.ActivityContext
2326

2427
@Module
2528
@InstallIn(ActivityComponent::class)
26-
open class SystemUiManager internal constructor(internal val context: Context) {
29+
open class SystemUiManager internal constructor(
30+
internal val context: Context,
31+
internal val prefsRepo: DataRepository<CorePreferences>
32+
) {
2733
internal val window: Window = (context as Activity).window
2834
internal val settings: SharedPreferences = context.getSharedPreferences(
2935
context.getString(R.string.prefs_settings),
@@ -32,22 +38,25 @@ open class SystemUiManager internal constructor(internal val context: Context) {
3238

3339
companion object {
3440
@Provides
35-
fun createInstance(@ActivityContext context: Context): SystemUiManager {
41+
fun createInstance(
42+
@ActivityContext context: Context,
43+
@WithActivityLifecycle prefsRepo: DataRepository<CorePreferences>
44+
): SystemUiManager {
3645
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
3746
(context as Activity).window.attributes.layoutInDisplayCutoutMode =
3847
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
3948
}
4049

4150
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
42-
return LSystemUiManager(context)
51+
return LSystemUiManager(context, prefsRepo)
4352
}
4453
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
45-
return MSystemUiManager(context)
54+
return MSystemUiManager(context, prefsRepo)
4655
}
4756
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
48-
return OSystemUiManager(context)
57+
return OSystemUiManager(context, prefsRepo)
4958
}
50-
return SystemUiManager(context)
59+
return SystemUiManager(context, prefsRepo)
5160
}
5261
}
5362

@@ -96,62 +105,56 @@ open class SystemUiManager internal constructor(internal val context: Context) {
96105
return primaryColor.data
97106
}
98107

99-
internal fun isSystemUiHidden(): Boolean {
100-
return settings.getBoolean(
101-
context.getString(R.string.prefs_settings_key_toggle_status_bar),
102-
false
103-
)
104-
}
108+
internal fun isSystemUiHidden(): Boolean = settings.getBoolean(
109+
context.getString(R.string.prefs_settings_key_toggle_status_bar),
110+
false
111+
)
105112

106113
internal fun isLightModeTheme(): Boolean {
107-
val theme = settings.getInt(context.getString(R.string.prefs_settings_key_theme), 0)
114+
val theme = prefsRepo.get().theme.number
108115
val uiMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
109116
return listOf(
110117
6,
111118
3,
112119
5
113-
).contains(theme) || (theme == 0 && uiMode == Configuration.UI_MODE_NIGHT_NO)
120+
).contains(theme) ||
121+
(theme == 0 && uiMode == Configuration.UI_MODE_NIGHT_NO)
114122
}
115123

116-
private open class OSystemUiManager(context: Context) : SystemUiManager(context) {
124+
private open class OSystemUiManager(context: Context, prefsRepo: DataRepository<CorePreferences>) :
125+
SystemUiManager(context, prefsRepo) {
117126
@RequiresApi(Build.VERSION_CODES.O)
118127
override fun setSystemUiVisibility() {
119128
window.decorView.systemUiVisibility =
120129
getLightUiBarFlags() or getToggleStatusBarFlags()
121130
}
122131

123132
@RequiresApi(Build.VERSION_CODES.O)
124-
open fun getLightUiBarFlags(): Int {
125-
return if (isLightModeTheme()) {
126-
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
127-
} else {
128-
0
129-
}
133+
open fun getLightUiBarFlags(): Int = if (isLightModeTheme()) {
134+
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
135+
} else {
136+
0
130137
}
131138

132-
private fun getToggleStatusBarFlags(): Int {
133-
return View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
134-
if (isSystemUiHidden()) View.SYSTEM_UI_FLAG_FULLSCREEN else 0
135-
}
139+
private fun getToggleStatusBarFlags(): Int = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
140+
if (isSystemUiHidden()) View.SYSTEM_UI_FLAG_FULLSCREEN else 0
136141
}
137142

138-
private open class MSystemUiManager(context: Context) : OSystemUiManager(context) {
143+
private open class MSystemUiManager(context: Context, prefsRepo: DataRepository<CorePreferences>) :
144+
OSystemUiManager(context, prefsRepo) {
139145
@RequiresApi(Build.VERSION_CODES.M)
140146
override fun setSystemUiColors() {
141147
window.statusBarColor = getPrimaryColor()
142148
}
143149

144150
@RequiresApi(Build.VERSION_CODES.M)
145-
override fun getLightUiBarFlags(): Int {
146-
return if (isLightModeTheme()) View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR else 0
147-
}
151+
override fun getLightUiBarFlags(): Int = if (isLightModeTheme()) View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR else 0
148152
}
149153

150-
private class LSystemUiManager(context: Context) : MSystemUiManager(context) {
154+
private class LSystemUiManager(context: Context, prefsRepo: DataRepository<CorePreferences>) :
155+
MSystemUiManager(context, prefsRepo) {
151156
override fun setSystemUiColors() {}
152157

153-
override fun getLightUiBarFlags(): Int {
154-
return 0
155-
}
158+
override fun getLightUiBarFlags(): Int = 0
156159
}
157160
}

0 commit comments

Comments
 (0)