feat(mindmachine): implement Android MVP app scaffold, session flows, safety gating, and tests
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package com.mindmachine.mvp.data
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.preferences.core.booleanPreferencesKey
|
||||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.intPreferencesKey
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import com.mindmachine.mvp.domain.AppSettings
|
||||
import com.mindmachine.mvp.domain.CountdownPreference
|
||||
import com.mindmachine.mvp.domain.SessionMode
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
||||
private val Context.dataStore by preferencesDataStore("mindmachine_settings")
|
||||
|
||||
class SettingsRepository(private val context: Context) {
|
||||
private object Keys {
|
||||
val safetyAcknowledged = booleanPreferencesKey("safety_ack")
|
||||
val safetyVersion = intPreferencesKey("safety_version")
|
||||
val countdownPref = stringPreferencesKey("countdown_pref")
|
||||
val defaultMode = stringPreferencesKey("default_mode")
|
||||
val showGuidance = booleanPreferencesKey("show_guidance")
|
||||
val lastPresetId = stringPreferencesKey("last_preset")
|
||||
}
|
||||
|
||||
val settings: Flow<AppSettings> = context.dataStore.data.map { p ->
|
||||
AppSettings(
|
||||
safetyAcknowledged = p[Keys.safetyAcknowledged] ?: false,
|
||||
safetyAcknowledgedVersion = p[Keys.safetyVersion] ?: 0,
|
||||
countdownPreference = runCatching { CountdownPreference.valueOf(p[Keys.countdownPref] ?: "FIVE") }.getOrDefault(CountdownPreference.FIVE),
|
||||
defaultModePreference = runCatching { SessionMode.valueOf(p[Keys.defaultMode] ?: "AUDIO_VISUAL") }.getOrDefault(SessionMode.AUDIO_VISUAL),
|
||||
showHolderGuidanceBeforeSession = p[Keys.showGuidance] ?: false,
|
||||
lastPresetId = p[Keys.lastPresetId],
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun acknowledgeSafety(version: Int) {
|
||||
context.dataStore.edit {
|
||||
it[Keys.safetyAcknowledged] = true
|
||||
it[Keys.safetyVersion] = version
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateCountdown(value: CountdownPreference) = context.dataStore.edit { it[Keys.countdownPref] = value.name }
|
||||
suspend fun updateDefaultMode(value: SessionMode) = context.dataStore.edit { it[Keys.defaultMode] = value.name }
|
||||
suspend fun updateGuidance(value: Boolean) = context.dataStore.edit { it[Keys.showGuidance] = value }
|
||||
suspend fun updateLastPreset(id: String) = context.dataStore.edit { it[Keys.lastPresetId] = id }
|
||||
}
|
||||
Reference in New Issue
Block a user