diff --git a/app/src/main/kotlin/net/jeena/pacer/PacerViewModel.kt b/app/src/main/kotlin/net/jeena/pacer/PacerViewModel.kt index ac1a1eb..5591c6e 100644 --- a/app/src/main/kotlin/net/jeena/pacer/PacerViewModel.kt +++ b/app/src/main/kotlin/net/jeena/pacer/PacerViewModel.kt @@ -11,11 +11,12 @@ import androidx.lifecycle.AndroidViewModel class PacerViewModel(application: Application) : AndroidViewModel(application) { private val ctx = application.applicationContext + private val settings = SettingsRepository(ctx) - var bpm by mutableIntStateOf(120) + var bpm by mutableIntStateOf(settings.loadBpm()) private set - var volume by mutableFloatStateOf(1f) + var volume by mutableFloatStateOf(settings.loadVolume()) private set var isPlaying by mutableStateOf(PacerService.isRunning) @@ -33,11 +34,13 @@ class PacerViewModel(application: Application) : AndroidViewModel(application) { fun updateBpm(newBpm: Int) { bpm = newBpm + settings.saveBpm(newBpm) if (isPlaying) ctx.startService(PacerService.updateBpmIntent(ctx, newBpm)) } fun updateVolume(newVolume: Float) { volume = newVolume + settings.saveVolume(newVolume) if (isPlaying) ctx.startService(PacerService.updateVolumeIntent(ctx, newVolume)) } } diff --git a/app/src/main/kotlin/net/jeena/pacer/SettingsRepository.kt b/app/src/main/kotlin/net/jeena/pacer/SettingsRepository.kt new file mode 100644 index 0000000..6fc21e2 --- /dev/null +++ b/app/src/main/kotlin/net/jeena/pacer/SettingsRepository.kt @@ -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) +}