weather: add settings page on the library settings system
- Commit
38bcd143c110ac6ee3ec9c64964a89469bf256da- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-05 14:36 UTC (17 hours ago)
- Parents
294a7c1
Weather had no settings page or preferences owner. Add WeatherPrefs (the app's only prefs owner, weather DataStore, SettingsDataStore with a ZipBackupFormat over the weather DB + datastore) holding the background-refresh setting, a Route.Settings destination, and ui/SettingsPage built only from the library SettingsPage DSL: a background-refresh Toggle bound through WeatherPrefs, a current-units Info row, and a Units Action opening the OS regional-preferences settings (units still follow the device locale). The import/export section comes from the host's importExport flag + ds. Wire the toggle honestly: WeatherViewModel now schedules or cancels the hourly WeatherRefreshWorker (new cancelHourlyRefresh) by collecting the setting instead of always scheduling. Entry is a settings icon in the locations top bar (a bottom-bar button displaced the locations list in tests). SettingsContent is stateless for previews; add the 7-settings screenshot preview for Route coverage. Verified: ./gradlew :weather:lint :weather:lintVitalRelease :weather:testDevUnitTest --max-workers=128 (green; lintVitalRelease is SKIPPED by this module's variant setup).
Diff
weather/AGENTS.md
diff --git a/weather/AGENTS.md b/weather/AGENTS.mdindex 9bd399ccc4..deb2809fd3 100644--- a/weather/AGENTS.md+++ b/weather/AGENTS.md@@ -55,7 +55,7 @@ receivers are a phone-only surface and early-return on a watch. ### Root — entry + navigation - com/vayunmathur/weather/MainActivity.kt - com/vayunmathur/weather/Navigation.kt-- com/vayunmathur/weather/Route.kt+- com/vayunmathur/weather/Route.kt (`Home` / `SearchLocation` / `Settings` / `WeatherMap`) ### data/ — cache + repository + refresh - com/vayunmathur/weather/data/WeatherDatabase.kt - com/vayunmathur/weather/data/WeatherRepository.kt@@ -64,6 +64,7 @@ receivers are a phone-only surface and early-return on a watch. - com/vayunmathur/weather/data/WeatherCacheStore.kt - com/vayunmathur/weather/data/WeatherRefreshWorker.kt - com/vayunmathur/weather/data/SavedLocation.kt+- com/vayunmathur/weather/data/WeatherPrefs.kt — the app's only preferences owner (`weather` DataStore, `SettingsDataStore`): background-refresh setting + backup format for the settings page ### domain/ + domain/map/ — pure formatting + map metadata (no Android) - com/vayunmathur/weather/domain/HomeLayout.kt — pure home width breakpoints (600/840dp): compact = modal drawer + single column, medium = full-width two-column forecast, expanded = bounded 280–360dp locations panel + two-column forecast; the home screen sizes itself from its available width, not the activity window; in expanded the panel is permanent, so the forecast's search bar renders without a drawer toggle - com/vayunmathur/weather/domain/WmoCode.kt@@ -97,6 +98,7 @@ receivers are a phone-only surface and early-return on a watch. - com/vayunmathur/weather/intents/GetWeatherByNameIntent.kt - com/vayunmathur/weather/intents/GetForecastIntent.kt — validated `WeatherForecastQuery` contract: named place resolves saved places first, then the catalogue (or an explicit coordinate pair); returns at most 5 days + 12 hourly summary entries, never raw series ### ui/ + ui/components/ + blocks/ — home, map, locations, metric blocks+- com/vayunmathur/weather/ui/SettingsPage.kt — settings (library `SettingsPage` DSL only) + stateless `SettingsContent` for previews - com/vayunmathur/weather/ui/HomePage.kt - com/vayunmathur/weather/ui/ForecastColumn.kt - com/vayunmathur/weather/ui/DeviceLocation.kt
weather/src/main/java/com/vayunmathur/weather/Navigation.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/Navigation.kt b/weather/src/main/java/com/vayunmathur/weather/Navigation.ktindex fd8373d1ba..08553da3d8 100644--- a/weather/src/main/java/com/vayunmathur/weather/Navigation.kt+++ b/weather/src/main/java/com/vayunmathur/weather/Navigation.kt@@ -8,6 +8,7 @@ import com.vayunmathur.weather.platform.WeatherViewModel import com.vayunmathur.weather.ui.HomePage import com.vayunmathur.weather.ui.SearchLocationPage import com.vayunmathur.weather.ui.WeatherMapPage+import com.vayunmathur.weather.ui.SettingsPage @Composable fun Navigation(viewModel: WeatherViewModel) {@@ -19,6 +20,7 @@ fun Navigation(viewModel: WeatherViewModel) { MainNavigation(backStack) { entry<Route.Home> { HomePage(backStack, viewModel, it.locationId) } entry<Route.SearchLocation>(metadata = DialogPage()) { SearchLocationPage(backStack, viewModel) }+ entry<Route.Settings> { SettingsPage(backStack, viewModel) } entry<Route.WeatherMap> { WeatherMapPage(backStack, it.latitude, it.longitude, it.isoTime, it.metric) }
weather/src/main/java/com/vayunmathur/weather/Route.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/Route.kt b/weather/src/main/java/com/vayunmathur/weather/Route.ktindex 1ca863c0f9..d3b02e3989 100644--- a/weather/src/main/java/com/vayunmathur/weather/Route.kt+++ b/weather/src/main/java/com/vayunmathur/weather/Route.kt@@ -21,6 +21,10 @@ sealed interface Route : NavKey { @Serializable data object SearchLocation : Route + /** App settings (library settings page; see ui/SettingsPage.kt). */+ @Serializable+ data object Settings : Route+ /** Immutable value describing weather map. */ @Serializable data class WeatherMap(
weather/src/main/java/com/vayunmathur/weather/data/WeatherPrefs.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/data/WeatherPrefs.kt b/weather/src/main/java/com/vayunmathur/weather/data/WeatherPrefs.ktnew file mode 100644index 0000000000..55a5b8e196--- /dev/null+++ b/weather/src/main/java/com/vayunmathur/weather/data/WeatherPrefs.kt@@ -0,0 +1,50 @@+package com.vayunmathur.weather.data++import android.content.Context+import androidx.datastore.preferences.core.booleanPreferencesKey+import androidx.datastore.preferences.core.edit+import androidx.datastore.preferences.core.emptyPreferences+import androidx.datastore.preferences.preferencesDataStore+import com.vayunmathur.library.room.SqlCipherDbCodec+import com.vayunmathur.library.ui.SettingsDataStore+import com.vayunmathur.library.util.BackupFormat+import com.vayunmathur.library.util.ZipBackupFormat+import kotlinx.coroutines.flow.Flow+import kotlinx.coroutines.flow.catch+import kotlinx.coroutines.flow.map+import java.io.IOException++private val Context.weatherDataStore by preferencesDataStore(name = "weather")++/**+ * The app's only preferences owner. The settings page binds through+ * this class (as the `ds` of the library settings page and through+ * bindings built from [backgroundRefresh] / [setBackgroundRefresh]);+ * no UI code opens the `weather` DataStore directly. A corrupt file+ * reads as the documented default (background refresh on) instead of+ * crashing, mirroring `NewsPrefs`.+ */+class WeatherPrefs(context: Context) : SettingsDataStore {+ private val appContext = context.applicationContext++ override val backupFormat: BackupFormat+ get() = ZipBackupFormat(+ dbConfigs = weatherDbConfigs(appContext),+ datastoreNames = listOf("weather"),+ dbCodec = SqlCipherDbCodec,+ )++ /** Hourly background forecast refresh; on unless the user turns it off. */+ val backgroundRefresh: Flow<Boolean> =+ appContext.weatherDataStore.data+ .catch { e -> if (e is IOException) emit(emptyPreferences()) else throw e }+ .map { it[BACKGROUND_REFRESH] ?: true }++ suspend fun setBackgroundRefresh(enabled: Boolean) {+ appContext.weatherDataStore.edit { it[BACKGROUND_REFRESH] = enabled }+ }++ private companion object {+ val BACKGROUND_REFRESH = booleanPreferencesKey("background_refresh")+ }+}
weather/src/main/java/com/vayunmathur/weather/data/WeatherRefreshWorker.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/data/WeatherRefreshWorker.kt b/weather/src/main/java/com/vayunmathur/weather/data/WeatherRefreshWorker.ktindex a327e4f638..741d005907 100644--- a/weather/src/main/java/com/vayunmathur/weather/data/WeatherRefreshWorker.kt+++ b/weather/src/main/java/com/vayunmathur/weather/data/WeatherRefreshWorker.kt@@ -59,6 +59,11 @@ class WeatherRefreshWorker( private const val TAG = "WeatherRefresh" private const val WORK_NAME = "WeatherHourlyRefresh" + /** Cancels the hourly refresh, e.g. when the user turns it off in Settings. */+ fun cancelHourlyRefresh(context: Context) {+ WorkManager.getInstance(context).cancelUniqueWork(WORK_NAME)+ }+ fun scheduleHourlyRefresh(context: Context) { // The worker only fetches small forecast bundles, so CONNECTED // (metered allowed) matches the other hourly network syncs in
weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.kt b/weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.ktindex 9f27270492..ccbcaab453 100644--- a/weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.kt+++ b/weather/src/main/java/com/vayunmathur/weather/platform/WeatherViewModel.kt@@ -10,6 +10,7 @@ import com.vayunmathur.library.ui.friendlyError import com.vayunmathur.weather.R import com.vayunmathur.weather.data.SavedLocation import com.vayunmathur.weather.data.WeatherRepository+import com.vayunmathur.weather.data.WeatherPrefs import com.vayunmathur.weather.data.WeatherRefreshWorker import com.vayunmathur.weather.data.weatherJson import com.vayunmathur.weather.domain.SelectedDateOrTime@@ -55,6 +56,17 @@ class WeatherViewModel( }, ) : AndroidViewModel(application), WeatherActions { + /** The app's only preferences owner; the settings page binds through it. */+ val prefs: WeatherPrefs = WeatherPrefs(application)++ /** Background hourly refresh setting; true until the user turns it off. */+ val backgroundRefresh: StateFlow<Boolean> = prefs.backgroundRefresh+ .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), true)++ fun setBackgroundRefresh(enabled: Boolean) {+ viewModelScope.launch { prefs.setBackgroundRefresh(enabled) }+ }+ val wearSyncState = com.vayunmathur.weather.sync.WearWeatherSync.get(application).state fun syncWearNow() = com.vayunmathur.weather.sync.WearWeatherSync.get(getApplication()).syncNow() val savedLocations: StateFlow<List<SavedLocation>?> = repository.locations@@ -96,7 +108,17 @@ class WeatherViewModel( } init {- WeatherRefreshWorker.scheduleHourlyRefresh(application)+ // The hourly worker follows the background-refresh setting: schedule+ // while it is on, cancel while it is off (the default is on).+ viewModelScope.launch {+ prefs.backgroundRefresh.collect { enabled ->+ if (enabled) {+ WeatherRefreshWorker.scheduleHourlyRefresh(application)+ } else {+ WeatherRefreshWorker.cancelHourlyRefresh(application)+ }+ }+ } com.vayunmathur.weather.sync.WearWeatherSync.get(application).start() }
weather/src/main/java/com/vayunmathur/weather/ui/LocationsPage.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/LocationsPage.kt b/weather/src/main/java/com/vayunmathur/weather/ui/LocationsPage.ktindex d8a7d3d5a6..32b138cf8d 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/LocationsPage.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/LocationsPage.kt@@ -116,6 +116,7 @@ fun LocationsPage( onClose = onClose, onSearchLocation = { backStack.add(Route.SearchLocation) }, onUseDeviceLocation = onAddCurrentLocation,+ onOpenSettings = { backStack.add(Route.Settings) }, wearStatusText = wearStatusText, ) }
weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt b/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.ktindex 2d0349b274..8e73896b06 100644--- a/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt+++ b/weather/src/main/java/com/vayunmathur/weather/ui/LocationsScreen.kt@@ -14,6 +14,7 @@ import com.vayunmathur.library.ui.IconBack import com.vayunmathur.library.ui.IconDelete import com.vayunmathur.library.ui.IconDragHandle import com.vayunmathur.library.ui.IconSearch+import com.vayunmathur.library.ui.IconSettings import com.vayunmathur.library.ui.IconButton import com.vayunmathur.library.ui.ListItem import com.vayunmathur.library.ui.ListItemDefaults@@ -79,6 +80,7 @@ fun LocationsScreen( onClose: () -> Unit = {}, onSearchLocation: () -> Unit = {}, onUseDeviceLocation: () -> Unit = {},+ onOpenSettings: () -> Unit = {}, wearStatusText: String? = null, ) { var longPressedLocation: SavedLocation? by remember { mutableStateOf(null) }@@ -128,6 +130,11 @@ fun LocationsScreen( IconBack(tint = MaterialTheme.colorScheme.onSurface) } },+ actions = {+ IconButton(onClick = onOpenSettings) {+ IconSettings(tint = MaterialTheme.colorScheme.onSurface)+ }+ }, ) }, bottomBar = {
weather/src/main/java/com/vayunmathur/weather/ui/SettingsPage.kt
diff --git a/weather/src/main/java/com/vayunmathur/weather/ui/SettingsPage.kt b/weather/src/main/java/com/vayunmathur/weather/ui/SettingsPage.ktnew file mode 100644index 0000000000..472c837de6--- /dev/null+++ b/weather/src/main/java/com/vayunmathur/weather/ui/SettingsPage.kt@@ -0,0 +1,76 @@+package com.vayunmathur.weather.ui++import androidx.compose.runtime.Composable+import androidx.compose.runtime.collectAsState+import androidx.compose.runtime.getValue+import androidx.compose.ui.platform.LocalContext+import androidx.compose.ui.res.stringResource+import com.vayunmathur.library.ui.SettingsPage as LibrarySettingsPage+import com.vayunmathur.library.ui.settingsBinding+import com.vayunmathur.library.util.NavBackStack+import com.vayunmathur.weather.R+import com.vayunmathur.weather.Route+import com.vayunmathur.weather.platform.DisplayUnits+import com.vayunmathur.weather.platform.WeatherViewModel+import com.vayunmathur.weather.platform.rememberDisplayUnits++/**+ * Weather settings, built only from the library settings system: the+ * page host plus its DSL controls, no raw rows or fields. Persistence+ * goes through [com.vayunmathur.weather.data.WeatherPrefs] (the `ds`+ * below and the toggle binding); this file opens no DataStore.+ */+@Composable+fun SettingsPage(backStack: NavBackStack<Route>, viewModel: WeatherViewModel) {+ val backgroundRefresh by viewModel.backgroundRefresh.collectAsState()+ val prefs = viewModel.prefs+ SettingsContent(+ backgroundRefresh = backgroundRefresh,+ units = rememberDisplayUnits(),+ ds = prefs,+ onBackgroundRefreshChange = viewModel::setBackgroundRefresh,+ onBack = { backStack.pop() },+ )+}++/**+ * Stateless settings body behind [SettingsPage]: every value is a+ * parameter, so Compose previews (src/screenshotTest) render the real+ * screen from literal fixtures with no ViewModel or DataStore.+ */+@Composable+internal fun SettingsContent(+ backgroundRefresh: Boolean,+ units: DisplayUnits,+ ds: com.vayunmathur.weather.data.WeatherPrefs? = null,+ onBackgroundRefreshChange: (Boolean) -> Unit = {},+ onOpenUnits: (() -> Unit)? = null,+ onBack: () -> Unit = {},+) {+ val context = LocalContext.current+ LibrarySettingsPage(+ title = stringResource(com.vayunmathur.library.ui.R.string.settings),+ onNavigateBack = onBack,+ importExport = true,+ ds = ds,+ ) {+ Section(stringResource(R.string.settings_refresh)) {+ Toggle(+ title = stringResource(R.string.settings_background_refresh),+ description = stringResource(R.string.settings_background_refresh_description),+ binding = settingsBinding(backgroundRefresh, onBackgroundRefreshChange),+ )+ }+ Section(stringResource(R.string.settings_display)) {+ Info(+ title = stringResource(R.string.settings_current_units),+ value = "${units.temperature}, ${units.wind}, ${units.pressure}",+ )+ Action(+ title = stringResource(R.string.set_units),+ description = stringResource(R.string.settings_units_description),+ onClick = onOpenUnits ?: { openRegionalUnitsSettings(context) },+ )+ }+ }+}
weather/src/main/res/values/strings.xml
diff --git a/weather/src/main/res/values/strings.xml b/weather/src/main/res/values/strings.xmlindex af9b2fca5e..bccda6903a 100644--- a/weather/src/main/res/values/strings.xml+++ b/weather/src/main/res/values/strings.xml@@ -215,4 +215,17 @@ <string name="weather_temp_format"><xliff:g id="temp" example="22">%1$d</xliff:g>°</string> <!-- Blob widget description shown in widget picker. Full sentence --> <string name="weather_blob_widget_description">Weather in an organic blob - big temperature with icon, dark teal background</string>++ <!-- Settings: refresh section heading. Noun -->+ <string name="settings_refresh">Refresh</string>+ <!-- Settings: background refresh toggle title. Noun phrase -->+ <string name="settings_background_refresh">Background refresh</string>+ <!-- Settings: background refresh toggle description. Full sentence -->+ <string name="settings_background_refresh_description">Update saved locations every hour in the background.</string>+ <!-- Settings: display section heading. Noun -->+ <string name="settings_display">Display</string>+ <!-- Settings: current units info title. Noun phrase -->+ <string name="settings_current_units">Current units</string>+ <!-- Settings: units action description. Full sentence -->+ <string name="settings_units_description">Units follow your device\'s regional preferences.</string> </resources>
weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.kt
diff --git a/weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.kt b/weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.ktindex 6d057d0f20..7193ed8258 100644--- a/weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.kt+++ b/weather/src/screenshotTest/kotlin/com/vayunmathur/weather/ui/MetadataPreviews.kt@@ -466,6 +466,18 @@ class MetadataPreviews { } } + @PreviewTest+ @Preview(name = "7-settings", device = PHONE, group = "phone", showSystemUi = true)+ @Composable+ fun Preview7Settings() {+ DynamicTheme(darkTheme = true) {+ SettingsContent(+ backgroundRefresh = true,+ units = DisplayUnits(),+ )+ }+ }+ @PreviewTest @Preview(name = "4-expanded", device = EXPANDED, group = "large-tablet", showSystemUi = true) @Composable