settings: shared reset-to-defaults, wired in maps, todo, code
- Commit
a5293791dc0baa4055a3f1375692950717b9bc97- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-21 02:41 UTC (15 days ago)
- Parents
1bcc550
No app offered reset-to-defaults despite the shared string existing. library/ui gains ResetDefaultsRow (confirm dialog, then onReset). Maps, todo and code wire it: their prefs owners re-seed defaults explicitly (their flows use mapNotNull, so plain removal would leave stale values). Code's reset removes only editor settings - the Keystore-sealed git token and git identity are never touched; maps preserves last position and consent state; todo preserves sync bookkeeping. Verified: :library:ui tests and compile for maps/todo/code.
Diff
code/src/main/java/com/vayunmathur/code/data/EditorPrefs.kt
diff --git a/code/src/main/java/com/vayunmathur/code/data/EditorPrefs.kt b/code/src/main/java/com/vayunmathur/code/data/EditorPrefs.ktindex 4d22e08114..12cd346e32 100644--- a/code/src/main/java/com/vayunmathur/code/data/EditorPrefs.kt+++ b/code/src/main/java/com/vayunmathur/code/data/EditorPrefs.kt@@ -241,6 +241,39 @@ class EditorPrefs(context: Context) { appContext.editorDataStore.edit { it[GIT_AUTHOR_EMAIL_KEY] = value } } + /**+ * Clears only the non-secret editor settings keys, so every settings+ * flow above falls back to its default (soft-wrap off, font size+ * [DEFAULT_FONT_SIZE], tab width [DEFAULT_TAB_WIDTH], theme system,+ * auto-indent / auto-close on, the rest off, editor theme default).+ *+ * Excluded, and preserved untouched:+ * - `git_token` — the Keystore-sealed secret; reset must never read,+ * clear or rewrite it (nor the other git credentials / author+ * identity keys, `git_username`, `git_author_name`,+ * `git_author_email`, which are credentials, not editor settings);+ * - workspace / user data keys: `folder_path`, `session_*`,+ * `recent_files`, `user_snippets`, `fold_state`.+ */+ suspend fun resetAll() {+ appContext.editorDataStore.edit { prefs ->+ prefs.remove(SOFT_WRAP_KEY)+ prefs.remove(FONT_SIZE_KEY)+ prefs.remove(TAB_WIDTH_KEY)+ prefs.remove(THEME_MODE_KEY)+ prefs.remove(AUTO_INDENT_KEY)+ prefs.remove(AUTO_CLOSE_KEY)+ prefs.remove(AUTO_SAVE_KEY)+ prefs.remove(TRIM_TRAILING_KEY)+ prefs.remove(FINAL_NEWLINE_KEY)+ prefs.remove(EXPERIMENTAL_EDITOR_KEY)+ prefs.remove(SHOW_WHITESPACE_KEY)+ prefs.remove(SHOW_INDENT_GUIDES_KEY)+ prefs.remove(SHOW_MINIMAP_KEY)+ prefs.remove(EDITOR_THEME_KEY)+ }+ }+ /** Editor preference defaults and DataStore keys. */ companion object { private const val KEY_ALIAS = "code_git_token_key"
code/src/main/java/com/vayunmathur/code/platform/EditorViewModel.kt
diff --git a/code/src/main/java/com/vayunmathur/code/platform/EditorViewModel.kt b/code/src/main/java/com/vayunmathur/code/platform/EditorViewModel.ktindex 761ec13ddc..f1c6fd00d9 100644--- a/code/src/main/java/com/vayunmathur/code/platform/EditorViewModel.kt+++ b/code/src/main/java/com/vayunmathur/code/platform/EditorViewModel.kt@@ -454,6 +454,30 @@ class EditorViewModel(application: Application) : AndroidViewModel(application) viewModelScope.launch { prefs.setShowMinimap(enabled) } } + /**+ * Resets the editor settings to their defaults, in memory (so the open+ * settings screen and editor redraw at once) and persisted via+ * [EditorPrefs.resetAll], which preserves git credentials — the+ * Keystore-sealed token included — and workspace/user data.+ */+ fun resetAllSettings() {+ softWrap = false+ fontSizeState.intValue = EditorPrefs.DEFAULT_FONT_SIZE+ tabWidthState.intValue = EditorPrefs.DEFAULT_TAB_WIDTH+ themeModeState.value = EditorPrefs.THEME_SYSTEM+ autoIndentState.value = true+ autoCloseBracketsState.value = true+ autoSaveState.value = false+ trimTrailingOnSaveState.value = false+ finalNewlineOnSaveState.value = false+ editorThemeState.value = EditorThemes.DEFAULT+ experimentalEditorState.value = false+ showWhitespaceState.value = false+ showIndentGuidesState.value = false+ showMinimapState.value = false+ viewModelScope.launch { prefs.resetAll() }+ }+ // ---- Find & replace (via EditorEditActions) ---- /** Commits a find/replace edit to [tab] as one undo step, bypassing smart input. */
code/src/main/java/com/vayunmathur/code/ui/SettingsPage.kt
diff --git a/code/src/main/java/com/vayunmathur/code/ui/SettingsPage.kt b/code/src/main/java/com/vayunmathur/code/ui/SettingsPage.ktindex 8543fff633..7d14497755 100644--- a/code/src/main/java/com/vayunmathur/code/ui/SettingsPage.kt+++ b/code/src/main/java/com/vayunmathur/code/ui/SettingsPage.kt@@ -30,6 +30,7 @@ import com.vayunmathur.library.ui.AlertDialog import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.MaterialTheme import com.vayunmathur.library.ui.RadioButton+import com.vayunmathur.library.ui.ResetDefaultsRow import com.vayunmathur.library.ui.SettingsDivider import com.vayunmathur.library.ui.SettingsRow import com.vayunmathur.library.ui.SettingsSection@@ -170,6 +171,10 @@ fun SettingsPage(viewModel: EditorViewModel, backStack: NavBackStack<Route>) { onClick = { backStack.add(Route.Snippets) }, ) }++ SettingsSection {+ ResetDefaultsRow(onReset = viewModel::resetAllSettings)+ } } }
library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt
diff --git a/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt b/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.ktindex 233bc2ab3e..59fa87581a 100644--- a/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt+++ b/library/ui/src/main/java/com/vayunmathur/library/ui/SettingsComponents.kt@@ -18,6 +18,7 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color+import androidx.compose.ui.res.stringResource import androidx.compose.ui.semantics.Role import androidx.compose.ui.unit.dp import com.vayunmathur.library.util.sharedText@@ -155,6 +156,44 @@ fun SettingsSwitchRow( ) } +/**+ * The reset-to-defaults row, for the bottom of a settings screen.+ *+ * Shows the shared reset row; tapping it asks for confirmation with the+ * shared [ConfirmDialog] (the AlertDialog pattern every destructive action+ * uses) and only then calls [onReset]. The caller owns what "defaults" means+ * for its app — this row deliberately knows no preference keys, so an app+ * can exclude secrets or user data from its reset without this component+ * needing to know.+ */+@Composable+fun ResetDefaultsRow(+ onReset: () -> Unit,+ modifier: Modifier = Modifier,+ supportingText: String? = null,+ enabled: Boolean = true,+) {+ var showConfirm by remember { mutableStateOf(false) }+ SettingsRow(+ title = stringResource(R.string.reset),+ modifier = modifier,+ supportingText = supportingText,+ enabled = enabled,+ onClick = { showConfirm = true },+ )+ if (showConfirm) {+ ConfirmDialog(+ title = stringResource(R.string.reset),+ message = stringResource(R.string.reset_defaults_message),+ confirmLabel = stringResource(R.string.reset),+ dismissLabel = stringResource(R.string.cancel),+ destructive = true,+ onConfirm = onReset,+ onDismiss = { showConfirm = false },+ )+ }+}+ /** * Divider between settings rows, inset past any leading icon so it lines up * with the text rather than cutting the full width.
library/ui/src/main/res/values/strings.xml
diff --git a/library/ui/src/main/res/values/strings.xml b/library/ui/src/main/res/values/strings.xmlindex d2d2dc43c2..302db834b5 100644--- a/library/ui/src/main/res/values/strings.xml+++ b/library/ui/src/main/res/values/strings.xml@@ -90,6 +90,8 @@ <string name="play">Play</string> <!-- Shared across every app. Reset to defaults. Verb, keep short. --> <string name="reset">Reset</string>+ <!-- Shared across every app. ResetDefaultsRow confirmation body: every setting goes back to its default. -->+ <string name="reset_defaults_message">All settings will go back to their defaults.</string> <!-- Shared across every app. Apply pending changes. Verb, keep short. --> <string name="apply">Apply</string> <!-- Shared across every app. Next step or item. Keep short. -->
maps/src/main/java/com/vayunmathur/maps/data/MapPreferences.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/data/MapPreferences.kt b/maps/src/main/java/com/vayunmathur/maps/data/MapPreferences.ktindex f0cdb577f7..5222a0f660 100644--- a/maps/src/main/java/com/vayunmathur/maps/data/MapPreferences.kt+++ b/maps/src/main/java/com/vayunmathur/maps/data/MapPreferences.kt@@ -1,4 +1,5 @@ package com.vayunmathur.maps.data+import com.vayunmathur.library.util.DataStoreUtils import com.vayunmathur.maps.platform.MapOverlayWorker import com.vayunmathur.maps.service.NavigationService @@ -56,6 +57,31 @@ object MapPreferences { const val DEFAULT_LAYER_SAFETY = false const val DEFAULT_LAYER_TRANSIT = false const val DEFAULT_GOOGLE_SERVICES = false+ const val DEFAULT_THEME_MODE = "system"++ /**+ * Re-seeds every user-facing setting above to its default.+ *+ * Values are written explicitly rather than removed: the settings and+ * layer flows are `mapNotNull` flows, so deleting a key would emit+ * nothing and the screens would keep showing the old value.+ *+ * Preserves, by design: [KEY_LAST_LON]/[KEY_LAST_LAT] (last known+ * position, not a setting), [KEY_GOOGLE_CONSENT_ASKED] (so reset does+ * not re-trigger the first-launch consent dialog), and every+ * non-settings key in the same store (map-overlay state, saved data).+ */+ suspend fun resetAll(store: DataStoreUtils) {+ store.setString(KEY_THEME_MODE, DEFAULT_THEME_MODE)+ store.setBoolean(KEY_VOICE_GUIDANCE, DEFAULT_VOICE_GUIDANCE)+ store.setBoolean(KEY_GLOBE, DEFAULT_GLOBE)+ store.setBoolean(KEY_MAP_AUTO_UPDATE, DEFAULT_MAP_AUTO_UPDATE)+ store.setBoolean(KEY_LAYER_TRAFFIC, DEFAULT_LAYER_TRAFFIC)+ store.setBoolean(KEY_LAYER_SATELLITE, DEFAULT_LAYER_SATELLITE)+ store.setBoolean(KEY_LAYER_SAFETY, DEFAULT_LAYER_SAFETY)+ store.setBoolean(KEY_LAYER_TRANSIT, DEFAULT_LAYER_TRANSIT)+ store.setBoolean(KEY_GOOGLE_SERVICES, DEFAULT_GOOGLE_SERVICES)+ } } /** App theme selection persisted under [MapPreferences.KEY_THEME_MODE]. */
maps/src/main/java/com/vayunmathur/maps/platform/MapSettingsViewModel.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/platform/MapSettingsViewModel.kt b/maps/src/main/java/com/vayunmathur/maps/platform/MapSettingsViewModel.ktindex 188e9f04e7..e1b5d32ed1 100644--- a/maps/src/main/java/com/vayunmathur/maps/platform/MapSettingsViewModel.kt+++ b/maps/src/main/java/com/vayunmathur/maps/platform/MapSettingsViewModel.kt@@ -97,6 +97,22 @@ class MapSettingsViewModel(application: Application) : AndroidViewModel(applicat else MapOverlayWorker.cancelAuto(context) } + /**+ * Resets every setting to its [MapPreferences] default (see+ * [MapPreferences.resetAll] for what is preserved). The default+ * auto-update value is on, so the periodic chain is re-armed here, as+ * [setMapAutoUpdate] does.+ */+ fun resetAll() =+ launch {+ MapPreferences.resetAll(ds)+ if (MapPreferences.DEFAULT_MAP_AUTO_UPDATE) {+ MapOverlayWorker.scheduleAuto(getApplication<Application>().applicationContext)+ } else {+ MapOverlayWorker.cancelAuto(getApplication<Application>().applicationContext)+ }+ }+ /** Ensure the periodic chain matches the persisted toggle (call on start). */ fun syncAutoUpdateSchedule() { val context = getApplication<Application>().applicationContext
maps/src/main/java/com/vayunmathur/maps/ui/settings/MapSettingsPage.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/ui/settings/MapSettingsPage.kt b/maps/src/main/java/com/vayunmathur/maps/ui/settings/MapSettingsPage.ktindex c28c24d624..9f008d2a67 100644--- a/maps/src/main/java/com/vayunmathur/maps/ui/settings/MapSettingsPage.kt+++ b/maps/src/main/java/com/vayunmathur/maps/ui/settings/MapSettingsPage.kt@@ -21,6 +21,7 @@ import com.vayunmathur.library.ui.ExperimentalMaterial3Api import com.vayunmathur.library.ui.IconClearNight import com.vayunmathur.library.ui.IconStar import com.vayunmathur.library.ui.IconVolumeUp+import com.vayunmathur.library.ui.ResetDefaultsRow import com.vayunmathur.library.ui.SettingsSection import com.vayunmathur.library.ui.SettingsSelectRow import com.vayunmathur.library.ui.SettingsSwitchRow@@ -140,6 +141,10 @@ fun MapSettingsPage(backStack: NavBackStack<Route>, viewModel: MapSettingsViewMo leadingContent = { IconStar() }, ) }++ SettingsSection {+ ResetDefaultsRow(onReset = { viewModel.resetAll() })+ } } } }
todo/src/main/java/com/vayunmathur/todo/data/TodoSettings.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/data/TodoSettings.kt b/todo/src/main/java/com/vayunmathur/todo/data/TodoSettings.ktindex 12c071a407..33d88274de 100644--- a/todo/src/main/java/com/vayunmathur/todo/data/TodoSettings.kt+++ b/todo/src/main/java/com/vayunmathur/todo/data/TodoSettings.kt@@ -37,6 +37,24 @@ class TodoSettings(context: Context) { suspend fun setCalendarMirror(value: Boolean) = store.setBoolean(KEY_CALENDAR_MIRROR, value) suspend fun markSynced(at: Long = System.currentTimeMillis()) = store.setLongIfGreater(KEY_LAST_SYNC, at) + /**+ * Re-seeds every user-facing setting to its default (the values the+ * flows above fall back to). Written explicitly rather than removed:+ * the string flows are `mapNotNull` flows, so deleting a key would+ * emit nothing and the screen would keep showing the old value.+ *+ * Preserves [KEY_LAST_SYNC]: that is sync bookkeeping, not a setting.+ * Tasks, lists and every other key in the shared store are untouched.+ */+ suspend fun resetAll() {+ setDefaultView(TodoView.Today)+ setDefaultSort(SortMode.MANUAL)+ setShowCompleted(true)+ setDigestEnabled(false)+ setDigestMinutes(DEFAULT_DIGEST_MINUTES)+ setCalendarMirror(false)+ }+ suspend fun digestEnabledNow(): Boolean = digestEnabled.first() suspend fun digestMinutesNow(): Long = digestMinutes.first() suspend fun calendarMirrorNow(): Boolean = calendarMirror.first()
todo/src/main/java/com/vayunmathur/todo/ui/SettingsPage.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/ui/SettingsPage.kt b/todo/src/main/java/com/vayunmathur/todo/ui/SettingsPage.ktindex acfc5eeb4c..f21fe97db4 100644--- a/todo/src/main/java/com/vayunmathur/todo/ui/SettingsPage.kt+++ b/todo/src/main/java/com/vayunmathur/todo/ui/SettingsPage.kt@@ -31,6 +31,7 @@ fun SettingsPage(backStack: NavBackStack<Route>, viewModel: TodoViewModel) { override fun setDefaultView(view: TodoView) = viewModel.updateSettings { setDefaultView(view) } override fun setDefaultSort(sort: SortMode) = viewModel.updateSettings { setDefaultSort(sort) } override fun setShowCompleted(show: Boolean) = viewModel.updateSettings { setShowCompleted(show) }+ override fun resetDefaults() = viewModel.updateSettings { resetAll() } }, extraSections = { ReminderSettingsSection(
todo/src/main/java/com/vayunmathur/todo/ui/SettingsScreen.kt
diff --git a/todo/src/main/java/com/vayunmathur/todo/ui/SettingsScreen.kt b/todo/src/main/java/com/vayunmathur/todo/ui/SettingsScreen.ktindex b3b6be7e70..347d0e9b31 100644--- a/todo/src/main/java/com/vayunmathur/todo/ui/SettingsScreen.kt+++ b/todo/src/main/java/com/vayunmathur/todo/ui/SettingsScreen.kt@@ -4,6 +4,7 @@ import androidx.compose.runtime.Composable import androidx.compose.ui.res.stringResource import com.vayunmathur.library.ui.DetailScaffold import com.vayunmathur.library.ui.ExperimentalMaterial3Api+import com.vayunmathur.library.ui.ResetDefaultsRow import com.vayunmathur.library.ui.SettingsDivider import com.vayunmathur.library.ui.SettingsSection import com.vayunmathur.library.ui.SettingsSelectRow@@ -27,6 +28,7 @@ interface SettingsActions { fun setDefaultView(view: TodoView) {} fun setDefaultSort(sort: SortMode) {} fun setShowCompleted(show: Boolean) {}+ fun resetDefaults() {} } /**@@ -74,5 +76,8 @@ fun SettingsScreen( SettingsSection(title = stringResource(R.string.settings_data)) { dataSection() }+ SettingsSection {+ ResetDefaultsRow(onReset = actions::resetDefaults)+ } } }