feat(US-11): persist BPM and volume with SharedPreferences

SettingsRepository wraps SharedPreferences with typed save/load
for BPM (default 120) and volume (default 1.0). PacerViewModel
loads saved values on init and saves on every change.
This commit is contained in:
Jeena 2026-03-09 12:05:03 +00:00
parent c646709438
commit d296f2bfec
2 changed files with 31 additions and 2 deletions

View file

@ -11,11 +11,12 @@ import androidx.lifecycle.AndroidViewModel
class PacerViewModel(application: Application) : AndroidViewModel(application) { class PacerViewModel(application: Application) : AndroidViewModel(application) {
private val ctx = application.applicationContext private val ctx = application.applicationContext
private val settings = SettingsRepository(ctx)
var bpm by mutableIntStateOf(120) var bpm by mutableIntStateOf(settings.loadBpm())
private set private set
var volume by mutableFloatStateOf(1f) var volume by mutableFloatStateOf(settings.loadVolume())
private set private set
var isPlaying by mutableStateOf(PacerService.isRunning) var isPlaying by mutableStateOf(PacerService.isRunning)
@ -33,11 +34,13 @@ class PacerViewModel(application: Application) : AndroidViewModel(application) {
fun updateBpm(newBpm: Int) { fun updateBpm(newBpm: Int) {
bpm = newBpm bpm = newBpm
settings.saveBpm(newBpm)
if (isPlaying) ctx.startService(PacerService.updateBpmIntent(ctx, newBpm)) if (isPlaying) ctx.startService(PacerService.updateBpmIntent(ctx, newBpm))
} }
fun updateVolume(newVolume: Float) { fun updateVolume(newVolume: Float) {
volume = newVolume volume = newVolume
settings.saveVolume(newVolume)
if (isPlaying) ctx.startService(PacerService.updateVolumeIntent(ctx, newVolume)) if (isPlaying) ctx.startService(PacerService.updateVolumeIntent(ctx, newVolume))
} }
} }

View file

@ -0,0 +1,26 @@
package net.jeena.pacer
import android.content.Context
class SettingsRepository(context: Context) {
companion object {
const val KEY_BPM = "bpm"
const val KEY_VOLUME = "volume"
private const val PREFS_NAME = "pacer_settings"
}
private val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
fun saveBpm(bpm: Int) {
prefs.edit().putInt(KEY_BPM, bpm).apply()
}
fun saveVolume(volume: Float) {
prefs.edit().putFloat(KEY_VOLUME, volume).apply()
}
fun loadBpm(): Int = prefs.getInt(KEY_BPM, 120)
fun loadVolume(): Float = prefs.getFloat(KEY_VOLUME, 1f)
}