From 5ad65919e95dbe849da060a372f720983ffaa09e Mon Sep 17 00:00:00 2001 From: Han Yin Date: Fri, 11 Apr 2025 14:38:56 -0700 Subject: [PATCH] util: implement user preferences utility --- .../data/preferences/UserPreferences.kt | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 examples/llama.android/app/src/main/java/com/example/llama/revamp/data/preferences/UserPreferences.kt diff --git a/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/preferences/UserPreferences.kt b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/preferences/UserPreferences.kt new file mode 100644 index 0000000000..2b034de459 --- /dev/null +++ b/examples/llama.android/app/src/main/java/com/example/llama/revamp/data/preferences/UserPreferences.kt @@ -0,0 +1,83 @@ +package com.example.llama.revamp.data.preferences + +import android.content.Context +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.booleanPreferencesKey +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.longPreferencesKey +import androidx.datastore.preferences.preferencesDataStore +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map + +/** + * Manages user preferences for the application. + */ +class UserPreferences(private val context: Context) { + + companion object { + private val Context.dataStore: DataStore by preferencesDataStore(name = "settings") + + // Performance monitoring preferences + val PERFORMANCE_MONITORING_ENABLED = booleanPreferencesKey("performance_monitoring_enabled") + val USE_FAHRENHEIT_TEMPERATURE = booleanPreferencesKey("use_fahrenheit_temperature") + val MONITORING_INTERVAL_MS = longPreferencesKey("monitoring_interval_ms") + + // Default values + const val DEFAULT_MONITORING_INTERVAL_MS = 5000L + } + + /** + * Gets whether performance monitoring is enabled. + */ + fun isPerformanceMonitoringEnabled(): Flow { + return context.dataStore.data.map { preferences -> + preferences[PERFORMANCE_MONITORING_ENABLED] ?: true + } + } + + /** + * Sets whether performance monitoring is enabled. + */ + suspend fun setPerformanceMonitoringEnabled(enabled: Boolean) { + context.dataStore.edit { preferences -> + preferences[PERFORMANCE_MONITORING_ENABLED] = enabled + } + } + + /** + * Gets whether temperature should be displayed in Fahrenheit. + */ + fun usesFahrenheitTemperature(): Flow { + return context.dataStore.data.map { preferences -> + preferences[USE_FAHRENHEIT_TEMPERATURE] ?: false + } + } + + /** + * Sets whether temperature should be displayed in Fahrenheit. + */ + suspend fun setUseFahrenheitTemperature(useFahrenheit: Boolean) { + context.dataStore.edit { preferences -> + preferences[USE_FAHRENHEIT_TEMPERATURE] = useFahrenheit + } + } + + /** + * Gets the monitoring interval in milliseconds. + */ + fun getMonitoringInterval(): Flow { + return context.dataStore.data.map { preferences -> + preferences[MONITORING_INTERVAL_MS] ?: DEFAULT_MONITORING_INTERVAL_MS + } + } + + /** + * Sets the monitoring interval in milliseconds. + */ + suspend fun setMonitoringInterval(intervalMs: Long) { + context.dataStore.edit { preferences -> + preferences[MONITORING_INTERVAL_MS] = intervalMs + } + } +}