Skip to content

Commit

Permalink
Persist settings to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanLobbenmeier committed Mar 28, 2024
1 parent 2bc8848 commit 627477c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package de.lobbenmeier.stefan.settings.business

import de.lobbenmeier.stefan.common.business.SettingsJson
import de.lobbenmeier.stefan.updater.business.getPlatform
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlin.io.path.exists
import kotlin.io.path.readText
import kotlin.io.path.writeText
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.serialization.encodeToString

class SettingsViewModel {
private val logger = KotlinLogging.logger {}
private val platform = getPlatform()
private val settingsFile = platform.settingsFile

private val _settings = MutableStateFlow(loadSettings())
val settings: StateFlow<Settings>
get() = _settings

private fun loadSettings(): Settings {
if (settingsFile.exists()) {
logger.info { """Loading settings "$settingsFile" from $platform""" }
val settingsJson = settingsFile.readText()
return SettingsJson.decodeFromString<Settings>(settingsJson)
} else {
logger.warn { """Settings file $settingsFile not found for $platform""" }
return createEmptySettings()
}
}

fun saveSettings(settings: Settings) {
val settingsJson = SettingsJson.encodeToString(settings)
settingsFile.writeText(settingsJson)

_settings.value = settings
}

private fun createEmptySettings(): Settings = SettingsJson.decodeFromString("""{}""")
}
7 changes: 4 additions & 3 deletions src/main/kotlin/de/lobbenmeier/stefan/ui/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ import de.lobbenmeier.stefan.downloadlist.model.DownloadQueue
import de.lobbenmeier.stefan.downloadlist.ui.DownloadList
import de.lobbenmeier.stefan.downloadlist.ui.Header
import de.lobbenmeier.stefan.settings.business.Settings
import de.lobbenmeier.stefan.settings.business.loadSettings
import de.lobbenmeier.stefan.settings.business.SettingsViewModel
import de.lobbenmeier.stefan.settings.ui.SettingsUI
import de.lobbenmeier.stefan.updater.business.BinariesUpdater
import de.lobbenmeier.stefan.updater.model.Binaries
import de.lobbenmeier.stefan.updater.ui.Updater

@Composable
fun App() {
var settings by remember { mutableStateOf(loadSettings()) }
val settingsViewModel = remember { SettingsViewModel() }
val settings by settingsViewModel.settings.collectAsState()
val binariesUpdater = remember { BinariesUpdater() }
val binaries = binariesUpdater.binaries.collectAsState().value

if (binaries == null) {
Updater(binariesUpdater.downloads)
} else {
MainView(settings, { settings = it }, binaries)
MainView(settings, settingsViewModel::saveSettings, binaries)
}
}

Expand Down

0 comments on commit 627477c

Please sign in to comment.