util: implement user preferences utility

This commit is contained in:
Han Yin 2025-04-11 14:38:56 -07:00
parent 46bd638c5f
commit 5ad65919e9
1 changed files with 83 additions and 0 deletions

View File

@ -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<Preferences> 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<Boolean> {
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<Boolean> {
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<Long> {
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
}
}
}