From 85b8f3f6153a2325abb2d89fed20da3d71f4ea74 Mon Sep 17 00:00:00 2001
From: MM20 <15646950+MM2-0@users.noreply.github.com>
Date: Tue, 25 Jan 2022 21:53:19 +0100
Subject: [PATCH] Migrate wallpaper preferences
---
i18n/src/main/res/values-de/strings.xml | 2 +-
.../de/mm20/launcher2/preferences/Defaults.kt | 1 +
preferences/src/main/proto/settings.proto | 1 +
.../launcher2/ui/launcher/LauncherActivity.kt | 20 ++++++++---
.../ui/launcher/LauncherActivityVM.kt | 33 +++++++++++++++++--
.../appearance/AppearanceSettingsScreen.kt | 26 +++++++++++++--
.../appearance/AppearanceSettingsScreenVM.kt | 17 ++++++++++
ui/src/main/res/layout/activity_launcher.xml | 5 ---
8 files changed, 90 insertions(+), 15 deletions(-)
diff --git a/i18n/src/main/res/values-de/strings.xml b/i18n/src/main/res/values-de/strings.xml
index 2cfeae02..2ab22b18 100644
--- a/i18n/src/main/res/values-de/strings.xml
+++ b/i18n/src/main/res/values-de/strings.xml
@@ -280,7 +280,7 @@
HERE
Fünfeck
Hintergrund
- Hintergrundbild setzen
+ Hintergrundbild auswählen
Hintergrund dimmen
Hintergrundbild bei Verwendung von dunklen Desings abdunkeln
Plaketten
diff --git a/preferences/src/main/java/de/mm20/launcher2/preferences/Defaults.kt b/preferences/src/main/java/de/mm20/launcher2/preferences/Defaults.kt
index e3897eca..be3e5834 100644
--- a/preferences/src/main/java/de/mm20/launcher2/preferences/Defaults.kt
+++ b/preferences/src/main/java/de/mm20/launcher2/preferences/Defaults.kt
@@ -9,6 +9,7 @@ fun createFactorySettings(context: Context): Settings {
.newBuilder()
.setTheme(Settings.AppearanceSettings.Theme.System)
.setColorScheme(Settings.AppearanceSettings.ColorScheme.Default)
+ .setDimWallpaper(false)
.build()
)
.setWeather(
diff --git a/preferences/src/main/proto/settings.proto b/preferences/src/main/proto/settings.proto
index 9be2089d..a0f904c2 100644
--- a/preferences/src/main/proto/settings.proto
+++ b/preferences/src/main/proto/settings.proto
@@ -17,6 +17,7 @@ message Settings {
BlackAndWhite = 1;
}
ColorScheme color_scheme = 6;
+ bool dim_wallpaper = 7;
}
AppearanceSettings appearance = 2;
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherActivity.kt b/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherActivity.kt
index 5ac1452a..d317b7ef 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherActivity.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherActivity.kt
@@ -2,6 +2,7 @@ package de.mm20.launcher2.ui.launcher
import android.app.WallpaperManager
import android.content.Intent
+import android.content.res.Configuration
import android.os.Bundle
import android.view.*
import androidx.activity.viewModels
@@ -13,6 +14,7 @@ import com.afollestad.materialdialogs.callbacks.onDismiss
import com.afollestad.materialdialogs.customview.customView
import de.mm20.launcher2.icons.DynamicIconController
import de.mm20.launcher2.icons.IconRepository
+import de.mm20.launcher2.ktx.dp
import de.mm20.launcher2.legacy.helper.ActivityStarter
import de.mm20.launcher2.preferences.LauncherPreferences
import de.mm20.launcher2.ui.R
@@ -38,9 +40,23 @@ class LauncherActivity : BaseActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)
+ viewModel.setDarkMode(resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES)
+
binding = ActivityLauncherBinding.inflate(LayoutInflater.from(this))
setContentView(binding.root)
+ viewModel.dimBackground.observe(this) { dim ->
+ window.attributes = window.attributes.also {
+ if (dim) {
+ it.dimAmount = 0.3f
+ it.flags = it.flags or WindowManager.LayoutParams.FLAG_DIM_BEHIND
+ } else {
+ it.dimAmount = 0f
+ it.flags = it.flags and WindowManager.LayoutParams.FLAG_DIM_BEHIND.inv()
+ }
+ }
+ }
+
var editFavoritesDialog: MaterialDialog? = null
viewModel.isEditFavoritesShown.observe(this) {
if (it) {
@@ -83,10 +99,6 @@ class LauncherActivity : BaseActivity() {
}
}
- if (LauncherPreferences.instance.dimWallpaper) {
- binding.dimWallpaper.setBackgroundColor(getColor(R.color.wallpaper_dim))
- }
-
val dynamicIconController: DynamicIconController by inject()
lifecycle.addObserver(dynamicIconController)
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherActivityVM.kt b/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherActivityVM.kt
index 9322ea29..11665ce3 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherActivityVM.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/launcher/LauncherActivityVM.kt
@@ -2,11 +2,40 @@ package de.mm20.launcher2.ui.launcher
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
+import androidx.lifecycle.asLiveData
+import androidx.lifecycle.viewModelScope
+import de.mm20.launcher2.preferences.LauncherDataStore
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.combine
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.launch
+import org.koin.core.component.KoinComponent
+import org.koin.core.component.inject
+
+class LauncherActivityVM : ViewModel(), KoinComponent {
+ private val dataStore: LauncherDataStore by inject()
-class LauncherActivityVM : ViewModel() {
val isHiddenItemsShown = MutableLiveData(false)
val isEditFavoritesShown = MutableLiveData(false)
- val dimBackground = MutableLiveData(false)
+
+ private var isDarkInMode = MutableStateFlow(false)
+
+ val dimBackground = combine(
+ dataStore.data.map { it.appearance.dimWallpaper },
+ isDarkInMode
+ ) { dim, darkMode ->
+ dim && darkMode
+ }.asLiveData()
+
+ init {
+ viewModelScope.launch {
+
+ }
+ }
+
+ fun setDarkMode(darkMode: Boolean) {
+ isDarkInMode.value = darkMode
+ }
fun showEditFavorites() {
isEditFavoritesShown.value = true
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/settings/appearance/AppearanceSettingsScreen.kt b/ui/src/main/java/de/mm20/launcher2/ui/settings/appearance/AppearanceSettingsScreen.kt
index 2dbec317..e28ee44e 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/settings/appearance/AppearanceSettingsScreen.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/settings/appearance/AppearanceSettingsScreen.kt
@@ -1,19 +1,20 @@
package de.mm20.launcher2.ui.settings.appearance
+import androidx.appcompat.app.AppCompatActivity
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
+import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.viewmodel.compose.viewModel
import de.mm20.launcher2.preferences.Settings.AppearanceSettings.ColorScheme
import de.mm20.launcher2.preferences.Settings.AppearanceSettings.Theme
import de.mm20.launcher2.ui.R
-import de.mm20.launcher2.ui.component.preferences.ListPreference
-import de.mm20.launcher2.ui.component.preferences.PreferenceCategory
-import de.mm20.launcher2.ui.component.preferences.PreferenceScreen
+import de.mm20.launcher2.ui.component.preferences.*
@Composable
fun AppearanceSettingsScreen() {
val viewModel: AppearanceSettingsScreenVM = viewModel()
+ val context = LocalContext.current
PreferenceScreen(title = stringResource(id = R.string.preference_screen_appearance)) {
item {
PreferenceCategory {
@@ -58,6 +59,25 @@ fun AppearanceSettingsScreen() {
}
)
}
+
+ PreferenceCategory(stringResource(id = R.string.preference_category_wallpaper)) {
+ Preference(
+ title = stringResource(R.string.wallpaper),
+ summary = stringResource(R.string.preference_wallpaper_summary),
+ onClick = {
+ viewModel.openWallpaperChooser(context as AppCompatActivity)
+ }
+ )
+ val dimWallpaper by viewModel.dimWallpaper.observeAsState()
+ SwitchPreference(
+ title = stringResource(R.string.preference_dim_wallpaper),
+ summary = stringResource(R.string.preference_dim_wallpaper_summary),
+ value = dimWallpaper == true,
+ onValueChanged = {
+ viewModel.setDimWallpaper(it)
+ }
+ )
+ }
}
}
}
\ No newline at end of file
diff --git a/ui/src/main/java/de/mm20/launcher2/ui/settings/appearance/AppearanceSettingsScreenVM.kt b/ui/src/main/java/de/mm20/launcher2/ui/settings/appearance/AppearanceSettingsScreenVM.kt
index 1d4f4c85..0fa2fc37 100644
--- a/ui/src/main/java/de/mm20/launcher2/ui/settings/appearance/AppearanceSettingsScreenVM.kt
+++ b/ui/src/main/java/de/mm20/launcher2/ui/settings/appearance/AppearanceSettingsScreenVM.kt
@@ -1,5 +1,7 @@
package de.mm20.launcher2.ui.settings.appearance
+import android.content.Intent
+import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asLiveData
import androidx.lifecycle.viewModelScope
@@ -46,4 +48,19 @@ class AppearanceSettingsScreenVM : ViewModel(), KoinComponent {
}
}
}
+
+ val dimWallpaper = dataStore.data.map { it.appearance.dimWallpaper }.asLiveData()
+ fun setDimWallpaper(dimWallpaper: Boolean) {
+ viewModelScope.launch {
+ dataStore.updateData {
+ it.toBuilder()
+ .setAppearance(it.appearance.toBuilder().setDimWallpaper(dimWallpaper))
+ .build()
+ }
+ }
+ }
+
+ fun openWallpaperChooser(context: AppCompatActivity) {
+ context.startActivity(Intent.createChooser(Intent(Intent.ACTION_SET_WALLPAPER), null))
+ }
}
\ No newline at end of file
diff --git a/ui/src/main/res/layout/activity_launcher.xml b/ui/src/main/res/layout/activity_launcher.xml
index 47b0b61e..c3b324aa 100644
--- a/ui/src/main/res/layout/activity_launcher.xml
+++ b/ui/src/main/res/layout/activity_launcher.xml
@@ -5,11 +5,6 @@
android:layout_height="match_parent"
android:clipChildren="false">
-
-