Skip to content

Conversation

@hmzgtl16
Copy link

🎯 Goal

This PR adds a new Settings dialog that allows users to choose their preferred UI theme (Follow system / Dark / Light).
The selected theme is persisted with Proto DataStore and applied across the entire app, including splash screen behavior and system theme overrides.

🛠 Implementation details

  • Added Settings dialog (PokedexSettings, SettingsDialog) with a radio-button UI for theme selection.
  • Integrated Settings as a dialog destination in PokedexNavHost using DialogSceneStrategy.
  • Introduced UserData and UiTheme models.
  • Implemented PreferencesDataSource (Proto DataStore):
  • Reads stored theme preference.
  • Persists updates via setUiTheme().
  • Added UserDataRepository to expose Flow and update preferences.
  • Added SettingsViewModel to map preference flows into SettingsUiState and handle theme updates.
  • Updated MainActivity and MainActivityViewModel to:
  • Apply the correct dark/light theme.
  • Keep the splash screen visible until user preferences load.

✍️ Explain examples

Settings dialog integration in navigation

entry<PokedexScreen.Settings>(
  metadata = DialogSceneStrategy.dialog(DialogProperties(usePlatformDefaultWidth = false))
) {
  PokedexSettings()
}

Persisting selected theme

suspend fun setUiTheme(uiTheme: UiTheme) {
  userPreferences.updateData {
    it.copy { uiThemeConfig = uiTheme.asUiThemeConfigProto() }
  }
}

Applying theme in MainActivity

PokedexMain(
  darkTheme = uiState.shouldUseDarkTheme(isSystemDarkTheme = isSystemInDarkTheme())
)

- Create a new `:core:datastore` module to handle data persistence.
- Add initial setup including build configurations, test files, and an AndroidManifest.
- Update `settings.gradle.kts` to include the new module.
Integrates Proto DataStore to manage user preferences, replacing the previous dependency on `appcompat` and `material` with Protocol Buffers.

- Adds `androidx.datastore` and `protobuf` dependencies.
- Introduces a `protobuf` plugin configuration in the `core:datastore` module.
- Defines `UserPreferences` and `UiThemeConfigProto` protobuf messages to store UI theme settings and whether to use dynamic theming.
*   Creates a new `:core:common` pure Kotlin module.
*   Moves `DispatchersModule`, `PokedexAppDispatchers`, and related coroutine logic from `:core:network` to `:core:common`.
*   Adds a new `CoroutineScopesModule` to provide a singleton `CoroutineScope`.
*   Updates dependencies to support the new module structure.
- Introduce a new `core:datastore` module using Proto DataStore to manage user preferences like UI theme and dynamic colors.
- Define a `UserData` model in `core:model` to represent user settings.
- Implement `PreferencesDataSource` to handle getting and setting theme and dynamic color preferences.
- Add a Hilt module (`DataStoreModule`) to provide the `DataStore<UserPreferences>` instance.
- Update `core:data` repositories to use the new `core:common` module for dispatchers.
- Remove boilerplate test files from the `datastore` module.
Add a `SettingsRepository` to handle user settings, such as UI theme and dynamic color preferences. This repository abstracts the data source logic for fetching and updating user preferences from the `PreferencesDataSource`.
Binds the `SettingsRepository` interface to its implementation, `SettingsRepositoryImpl`, within the Hilt dependency graph.
- Introduces a new `:feature:settings` module to manage user preferences like UI theme and dynamic colors.
- Adds a `Settings` object to `PokedexScreen` for navigation.
- Implements a `PokedexSettings` composable, which displays as a dialog, to allow users to change theme settings.
- Creates a `SettingsViewModel` to handle the business logic for fetching and updating user data.
- Adds a settings icon to the `PokedexAppBar` on the home screen to navigate to the new settings dialog.
- Includes a `FakeSettingsRepository` for testing purposes.
Adds unit tests for the `core:datastore` module to ensure the reliability of user preference handling.

- Implements `UserPreferencesSerializerTest` to verify the serialization and deserialization of `UserPreferences`, including default values, correct read/write operations, and corruption handling.
- Adds `PreferencesDataSourceTest` to validate the logic for setting and retrieving UI theme and dynamic color settings.
- Introduces `InMemoryDataStore`, a test fake for `DataStore`, to facilitate testing of the data source in isolation.
Updates the `FakeSettingsRepository` to emit a default `UserData` object in its `fetchUserData` flow. This ensures that tests relying on this fake implementation have initial data to work with, setting the UI theme to `FOLLOW_SYSTEM` and `useDynamicColors` to `true`.
Adds unit tests for `SettingsRepositoryImpl` to verify its functionality.

- Implements an `InMemoryDataStore` for testing `PreferencesDataSource` without relying on the actual file system.
- Verifies that the default user data is correct upon initialization.
- Confirms that `setDynamicColors` correctly updates the `useDynamicColors` preference.
- Confirms that `setUiTheme` correctly updates the `uiTheme` preference.
- Adds the `protobuf-kotlin-lite` test dependency to support the tests.
- Updates `FakeSettingsRepository` to align with repository interface changes.
Removes the `useDynamicColors` boolean preference from the user settings. This simplifies the `UserData` model and the corresponding Proto DataStore schema, making the theme preference the sole user-configurable setting.

- Removes the `useDynamicColors` field from the `UserData` data class in the `:core:model` module.
- Updates the `user_preferences.proto` file in `:core:datastore` to remove the `use_dynamic_colors` field.
- Adjusts tests for `UserPreferencesSerializer` to reflect the removal of the dynamic color setting.
…dynamic colors

- Renames `SettingsRepository` and its related classes to `UserDataRepository` to more accurately reflect that it manages all user data, not just settings.
- Simplifies the `UserData` model and repository by removing the `useDynamicColors` option, which will be implicitly handled by Material 3.
- Removes the `fetchUserData` method in favor of directly exposing a `userData` Flow.
- Updates Hilt bindings and tests to reflect the new repository name and simplified data model.
Adds the Apache 2.0 license header to files in the `:core:datastore` module. This commit also includes minor code formatting adjustments.
Refactors the settings screen to simplify its UI and state management. This change streamlines the underlying logic by removing dynamic color theming options and dependencies on the Palette API.

- Updates `SettingsViewModel` to directly use `UserDataRepository` and a simpler `SettingsUiState` (Loading, Success, Error).
- Removes the `SettingsBackground.kt` file and its dependency on the Palette API for dynamic background colors.
- Simplifies the `PokedexSettings` and `SettingsDialog` composables by removing logic for dynamic colors and palette extraction.
- Updates UI components to use `PokedexTheme` for consistent styling.
Integrates user preferences from `UserDataRepository` to dynamically set the app's theme.

- Introduces a `MainActivityViewModel` to expose a `MainActivityUiState` which holds the `UserData`.
- The `MainActivity` now collects this state to determine whether to use a dark theme, light theme, or follow the system setting.
- The splash screen is now kept on-screen until the user data (and theme preference) is loaded.
- Updates `PokedexMain` composable to accept a `darkTheme` boolean parameter to apply the selected theme.
- Adds a dependency on the `:core:data` module in the `:app` module to access `UserDataRepository`.
Updates unit tests for `HomeRepositoryImpl` to include the newly added `onLastPageReached` lambda parameter in the `fetchPokemonList` method calls.
Updates unit tests for `HomeRepositoryImpl` to include the newly added `onLastPageReached` lambda parameter in the `fetchPokemonList` method calls.
Renames `SettingsRepository` to `UserDataRepository` for consistency and clarity. This update also refactors related code, including:

- Renaming the Hilt binding method in `DataModule` from `bindsSettingsRepository` to `bindsUserDataRepository`.
- Removing unused imports (`Build`, `ChecksSdkIntAtLeast`) from `PokedexTheme`.
- Simplifying the initial state value in `MainActivityViewModel` from `MainActivityUiState.Loading` to just `Loading`.
@hmzgtl16 hmzgtl16 requested a review from skydoves as a code owner November 27, 2025 20:43
hmzgtl16 and others added 2 commits November 29, 2025 15:11
Adds the Apache 2.0 license header to multiple files across various modules. This commit also includes minor code formatting adjustments, such as correcting trailing commas and removing unnecessary parameters, to improve consistency and readability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant