@@ -41,6 +41,8 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
4141import io.github.acedroidx.frp.ui.theme.FrpTheme
4242import io.github.acedroidx.frp.ui.theme.ThemeModeKeys
4343import kotlinx.coroutines.flow.MutableStateFlow
44+ import androidx.compose.runtime.collectAsState
45+ import androidx.core.content.edit
4446
4547class SettingsActivity : ComponentActivity () {
4648 private val isStartup = MutableStateFlow (false )
@@ -61,8 +63,7 @@ class SettingsActivity : ComponentActivity() {
6163 isStartup.value = preferences.getBoolean(PreferencesKey .AUTO_START , false )
6264
6365 // 读取主题设置,默认为跟随系统
64- val rawTheme =
65- preferences.getString(PreferencesKey .THEME_MODE , ThemeModeKeys .FOLLOW_SYSTEM )
66+ val rawTheme = preferences.getString(PreferencesKey .THEME_MODE , ThemeModeKeys .FOLLOW_SYSTEM )
6667 themeMode.value = ThemeModeKeys .normalize(rawTheme)
6768
6869 // 读取 Tasker 权限设置,默认为允许
@@ -80,7 +81,7 @@ class SettingsActivity : ComponentActivity() {
8081
8182 enableEdgeToEdge()
8283 setContent {
83- val currentTheme by themeMode.collectAsStateWithLifecycle(themeMode.value.ifEmpty { ThemeModeKeys .FOLLOW_SYSTEM })
84+ val currentTheme by themeMode.collectAsStateWithLifecycle(themeMode.collectAsState(). value.ifEmpty { ThemeModeKeys .FOLLOW_SYSTEM })
8485 FrpTheme (themeMode = currentTheme) {
8586 Scaffold (topBar = {
8687 TopAppBar (title = {
@@ -110,7 +111,7 @@ class SettingsActivity : ComponentActivity() {
110111 @Composable
111112 fun SettingsContent () {
112113 val isAutoStart by isStartup.collectAsStateWithLifecycle(false )
113- val currentTheme by themeMode.collectAsStateWithLifecycle(themeMode.value.ifEmpty { ThemeModeKeys .FOLLOW_SYSTEM })
114+ val currentTheme by themeMode.collectAsStateWithLifecycle(themeMode.collectAsState(). value.ifEmpty { ThemeModeKeys .FOLLOW_SYSTEM })
114115 val isTaskerAllowed by allowTasker.collectAsStateWithLifecycle(true )
115116 val isExcludeFromRecents by excludeFromRecents.collectAsStateWithLifecycle(false )
116117 val currentQuickTileConfig by quickTileConfig.collectAsStateWithLifecycle(null )
@@ -133,9 +134,9 @@ class SettingsActivity : ComponentActivity() {
133134 title = stringResource(R .string.auto_start_switch),
134135 checked = isAutoStart,
135136 onCheckedChange = { checked ->
136- val editor = preferences.edit()
137- editor. putBoolean(PreferencesKey .AUTO_START , checked)
138- editor. apply ()
137+ preferences.edit {
138+ putBoolean(PreferencesKey .AUTO_START , checked)
139+ }
139140 isStartup.value = checked
140141 })
141142
@@ -147,9 +148,9 @@ class SettingsActivity : ComponentActivity() {
147148 currentKey = currentTheme,
148149 options = themeOptions,
149150 onValueChange = { newThemeKey ->
150- val editor = preferences.edit()
151- editor. putString(PreferencesKey .THEME_MODE , newThemeKey)
152- editor. apply ()
151+ preferences.edit {
152+ putString(PreferencesKey .THEME_MODE , newThemeKey)
153+ }
153154 themeMode.value = ThemeModeKeys .normalize(newThemeKey)
154155 })
155156
@@ -161,15 +162,15 @@ class SettingsActivity : ComponentActivity() {
161162 currentConfig = currentQuickTileConfig,
162163 configs = configs,
163164 onConfigChange = { config ->
164- val editor = preferences.edit()
165- if (config != null ) {
166- editor.putString(PreferencesKey .QUICK_TILE_CONFIG_TYPE , config.type.name)
167- editor.putString(PreferencesKey .QUICK_TILE_CONFIG_NAME , config.fileName)
168- } else {
169- editor.remove(PreferencesKey .QUICK_TILE_CONFIG_TYPE )
170- editor.remove(PreferencesKey .QUICK_TILE_CONFIG_NAME )
165+ preferences.edit {
166+ if (config != null ) {
167+ putString(PreferencesKey .QUICK_TILE_CONFIG_TYPE , config.type.name)
168+ putString(PreferencesKey .QUICK_TILE_CONFIG_NAME , config.fileName)
169+ } else {
170+ remove(PreferencesKey .QUICK_TILE_CONFIG_TYPE )
171+ remove(PreferencesKey .QUICK_TILE_CONFIG_NAME )
172+ }
171173 }
172- editor.apply ()
173174 quickTileConfig.value = config
174175 })
175176
@@ -180,9 +181,9 @@ class SettingsActivity : ComponentActivity() {
180181 title = stringResource(R .string.allow_tasker_title),
181182 checked = isTaskerAllowed,
182183 onCheckedChange = { checked ->
183- val editor = preferences.edit()
184- editor. putBoolean(PreferencesKey .ALLOW_TASKER , checked)
185- editor. apply ()
184+ preferences.edit {
185+ putBoolean(PreferencesKey .ALLOW_TASKER , checked)
186+ }
186187 allowTasker.value = checked
187188 })
188189
@@ -193,35 +194,32 @@ class SettingsActivity : ComponentActivity() {
193194 title = stringResource(R .string.exclude_from_recents_title),
194195 checked = isExcludeFromRecents,
195196 onCheckedChange = { checked ->
196- val editor = preferences.edit()
197- editor. putBoolean(PreferencesKey .EXCLUDE_FROM_RECENTS , checked)
198- editor. apply ()
197+ preferences.edit {
198+ putBoolean(PreferencesKey .EXCLUDE_FROM_RECENTS , checked)
199+ }
199200 excludeFromRecents.value = checked
200201
201202 // 立即应用设置,不需要重启
202- if (android.os.Build .VERSION .SDK_INT >= android.os.Build .VERSION_CODES .LOLLIPOP ) {
203- try {
204- val am =
205- getSystemService(ACTIVITY_SERVICE ) as android.app.ActivityManager
206- val appTasks = am.appTasks
207- android.util.Log .d(
208- " SettingsActivity" , " appTasks size: ${appTasks.size} "
209- )
210- if (appTasks.isNotEmpty()) {
211- for (task in appTasks) {
212- task.setExcludeFromRecents(checked)
213- android.util.Log .d(
214- " SettingsActivity" , " Set excludeFromRecents to $checked "
215- )
216- }
217- } else {
218- android.util.Log .w(" SettingsActivity" , " appTasks is empty" )
203+ try {
204+ val am = getSystemService(ACTIVITY_SERVICE ) as android.app.ActivityManager
205+ val appTasks = am.appTasks
206+ android.util.Log .d(
207+ " SettingsActivity" , " appTasks size: ${appTasks.size} "
208+ )
209+ if (appTasks.isNotEmpty()) {
210+ for (task in appTasks) {
211+ task.setExcludeFromRecents(checked)
212+ android.util.Log .d(
213+ " SettingsActivity" , " Set excludeFromRecents to $checked "
214+ )
219215 }
220- } catch (e: Exception ) {
221- android.util.Log .e(
222- " SettingsActivity" , " Failed to set excludeFromRecents: ${e.message} "
223- )
216+ } else {
217+ android.util.Log .w(" SettingsActivity" , " appTasks is empty" )
224218 }
219+ } catch (e: Exception ) {
220+ android.util.Log .e(
221+ " SettingsActivity" , " Failed to set excludeFromRecents: ${e.message} "
222+ )
225223 }
226224 })
227225
@@ -264,8 +262,9 @@ class SettingsActivity : ComponentActivity() {
264262 ) {
265263 var expanded by remember { mutableStateOf(false ) }
266264
267- val currentLabel = options.firstOrNull { it.first == currentKey }?.second
268- ? : stringResource(R .string.theme_mode_follow_system)
265+ val currentLabel = options.firstOrNull { it.first == currentKey }?.second ? : stringResource(
266+ R .string.theme_mode_follow_system
267+ )
269268
270269 Row (modifier = Modifier
271270 .fillMaxWidth()
0 commit comments