Skip to content

Commit f0ed07c

Browse files
authored
Update PreferencesRepository.kt
1 parent 5009179 commit f0ed07c

1 file changed

Lines changed: 77 additions & 0 deletions

File tree

source-code/app/src/main/java/com/hackeros/app/data/parser/repository/PreferencesRepository.kt

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.datastore.core.DataStore
55
import androidx.datastore.preferences.core.Preferences
66
import androidx.datastore.preferences.core.booleanPreferencesKey
77
import androidx.datastore.preferences.core.edit
8+
import androidx.datastore.preferences.core.longPreferencesKey
89
import androidx.datastore.preferences.core.stringPreferencesKey
910
import androidx.datastore.preferences.core.stringSetPreferencesKey
1011
import androidx.datastore.preferences.preferencesDataStore
@@ -38,6 +39,23 @@ class PreferencesRepository(private val context: Context) {
3839
// Tracks which app version the "What's new" dialog was last shown for, so it only
3940
// appears once per update (not on every launch).
4041
val LAST_SEEN_APP_VERSION_KEY = stringPreferencesKey("hackeros_last_seen_app_version")
42+
43+
// Whether the Documentation / Games Store sections are shown at all (both default on).
44+
val DOCS_SECTION_ENABLED_KEY = booleanPreferencesKey("hackeros_docs_section_enabled")
45+
val GAMES_STORE_SECTION_ENABLED_KEY = booleanPreferencesKey("hackeros_games_store_section_enabled")
46+
47+
// Offline cache for the Games Store catalog, same pattern as releases/gallery.
48+
val CACHED_GAMES_STORE_JSON_KEY = stringPreferencesKey("hackeros_cached_games_store_json")
49+
50+
// Offline cache for the raw documentation JS source, so a previously-loaded page can
51+
// still be parsed and shown fully offline.
52+
val CACHED_DOC_JS_KEY = stringPreferencesKey("hackeros_cached_doc_js")
53+
54+
// User-defined custom theme colors (v0.6). Stored as ARGB Long hex values, same
55+
// representation AppTheme itself uses internally.
56+
val CUSTOM_THEME_PRIMARY_KEY = longPreferencesKey("hackeros_custom_theme_primary")
57+
val CUSTOM_THEME_BACKGROUND_KEY = longPreferencesKey("hackeros_custom_theme_background")
58+
val CUSTOM_THEME_CARD_KEY = longPreferencesKey("hackeros_custom_theme_card")
4159
}
4260

4361
val themeFlow: Flow<ThemeId> = context.dataStore.data.map { prefs ->
@@ -130,4 +148,63 @@ class PreferencesRepository(private val context: Context) {
130148
suspend fun saveLastKnownVersion(version: String) {
131149
context.dataStore.edit { it[LAST_KNOWN_VERSION_KEY] = version }
132150
}
151+
152+
// --- Section visibility -----------------------------------------------------------------
153+
154+
val docsSectionEnabledFlow: Flow<Boolean> = context.dataStore.data.map { prefs ->
155+
prefs[DOCS_SECTION_ENABLED_KEY] ?: true
156+
}
157+
158+
val gamesStoreSectionEnabledFlow: Flow<Boolean> = context.dataStore.data.map { prefs ->
159+
prefs[GAMES_STORE_SECTION_ENABLED_KEY] ?: true
160+
}
161+
162+
suspend fun saveDocsSectionEnabled(enabled: Boolean) {
163+
context.dataStore.edit { it[DOCS_SECTION_ENABLED_KEY] = enabled }
164+
}
165+
166+
suspend fun saveGamesStoreSectionEnabled(enabled: Boolean) {
167+
context.dataStore.edit { it[GAMES_STORE_SECTION_ENABLED_KEY] = enabled }
168+
}
169+
170+
// --- Games Store offline cache ----------------------------------------------------------
171+
172+
val cachedGamesStoreJsonFlow: Flow<String?> = context.dataStore.data.map { prefs ->
173+
prefs[CACHED_GAMES_STORE_JSON_KEY]
174+
}
175+
176+
suspend fun saveCachedGamesStoreJson(json: String) {
177+
context.dataStore.edit { it[CACHED_GAMES_STORE_JSON_KEY] = json }
178+
}
179+
180+
// --- Documentation offline cache --------------------------------------------------------
181+
182+
val cachedDocJsFlow: Flow<String?> = context.dataStore.data.map { prefs ->
183+
prefs[CACHED_DOC_JS_KEY]
184+
}
185+
186+
suspend fun saveCachedDocJs(js: String) {
187+
context.dataStore.edit { it[CACHED_DOC_JS_KEY] = js }
188+
}
189+
190+
// --- Custom theme (v0.6) ----------------------------------------------------------------
191+
192+
data class CustomThemeColors(val primary: Long, val background: Long, val card: Long)
193+
194+
val customThemeColorsFlow: Flow<CustomThemeColors?> = context.dataStore.data.map { prefs ->
195+
val primary = prefs[CUSTOM_THEME_PRIMARY_KEY]
196+
val background = prefs[CUSTOM_THEME_BACKGROUND_KEY]
197+
val card = prefs[CUSTOM_THEME_CARD_KEY]
198+
if (primary != null && background != null && card != null) {
199+
CustomThemeColors(primary, background, card)
200+
} else null
201+
}
202+
203+
suspend fun saveCustomThemeColors(primary: Long, background: Long, card: Long) {
204+
context.dataStore.edit {
205+
it[CUSTOM_THEME_PRIMARY_KEY] = primary
206+
it[CUSTOM_THEME_BACKGROUND_KEY] = background
207+
it[CUSTOM_THEME_CARD_KEY] = card
208+
}
209+
}
133210
}

0 commit comments

Comments
 (0)