From 1761426a9bc7b5f8d8b0e637969b4df25a01df38 Mon Sep 17 00:00:00 2001 From: Patrick Hoefer Date: Sun, 10 Jan 2021 01:08:38 +0100 Subject: [PATCH] feat(settings): add app settings initializer and state loader relates to #35 --- .gitignore | 1 + .../ck3workbench/app/AppInitializer.kt | 4 +- .../ck3workbench/app/SettingsHolder.kt | 38 +++++++++++++++++++ .../ck3workbench/app/AppInitializerTest.kt | 9 +++++ .../app/AppShutdownServiceTest.kt | 4 +- 5 files changed, 51 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index e09185e..3a094a9 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ project.wbp characters.txt session.wbs +settings.cfg default.wbp diff --git a/src/main/kotlin/com/github/xetra11/ck3workbench/app/AppInitializer.kt b/src/main/kotlin/com/github/xetra11/ck3workbench/app/AppInitializer.kt index 085952a..68ceb66 100644 --- a/src/main/kotlin/com/github/xetra11/ck3workbench/app/AppInitializer.kt +++ b/src/main/kotlin/com/github/xetra11/ck3workbench/app/AppInitializer.kt @@ -27,8 +27,8 @@ class AppInitializer( } private fun initializeSettings() { - val settingsFile = File("settings.cfg") - settingsFile.createNewFile() + SettingsHolder.init() + SettingsHolder.loadToHolder() } private fun initializeSession() { diff --git a/src/main/kotlin/com/github/xetra11/ck3workbench/app/SettingsHolder.kt b/src/main/kotlin/com/github/xetra11/ck3workbench/app/SettingsHolder.kt index 455af2c..9f208a5 100644 --- a/src/main/kotlin/com/github/xetra11/ck3workbench/app/SettingsHolder.kt +++ b/src/main/kotlin/com/github/xetra11/ck3workbench/app/SettingsHolder.kt @@ -3,6 +3,9 @@ package com.github.xetra11.ck3workbench.app import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.json.Json +import java.nio.file.Paths /** * Holds all the app wide settings state @@ -10,9 +13,44 @@ import androidx.compose.runtime.setValue object SettingsHolder { var autosave by mutableStateOf(false) + /** + * Creates 'settings.cfg' if non exists + */ + fun init() { + val settingsFile = Paths.get("settings.cfg").toAbsolutePath().toFile() + if (!settingsFile.exists()) { + AppSettings().save() + } + } + + /** + * Convert holder state into [AppSettings] instance + * @return Instance of [AppSettings] containing the holder state + */ fun toAppSettings(): AppSettings { return AppSettings( autosave = autosave ) } + + /** + * Load settings from 'settings.cfg' file from filesystem + * @return Instance of [AppSettings] from settings.cfg file + */ + fun load(): AppSettings { + val settingsFile = Paths.get("settings.cfg").toAbsolutePath().toFile() + if (!settingsFile.exists()) { + NotificationsService.error("No settings.cfg could be found. Returning default settings") + return AppSettings() + } + return Json.decodeFromString(settingsFile.readText()) + } + + /** + * Load settings from 'settings.cfg' file from filesystem and initialize [SettingsHolder] with it + */ + fun loadToHolder() { + val appSettings = this.load() + this.autosave = appSettings.autosave + } } diff --git a/src/test/kotlin/com/github/xetra11/ck3workbench/app/AppInitializerTest.kt b/src/test/kotlin/com/github/xetra11/ck3workbench/app/AppInitializerTest.kt index e41f886..873bb96 100644 --- a/src/test/kotlin/com/github/xetra11/ck3workbench/app/AppInitializerTest.kt +++ b/src/test/kotlin/com/github/xetra11/ck3workbench/app/AppInitializerTest.kt @@ -72,6 +72,15 @@ class AppInitializerTest : ShouldSpec({ file.exists() shouldBe true } + + should("load settings and set to SettingsHolder") { + val expected = AppSettings(true) + expected.save() + + appInitializer.initialize() + + SettingsHolder.toAppSettings() shouldBe expected + } }) private fun deleteTestFiles() { diff --git a/src/test/kotlin/com/github/xetra11/ck3workbench/app/AppShutdownServiceTest.kt b/src/test/kotlin/com/github/xetra11/ck3workbench/app/AppShutdownServiceTest.kt index 0e47766..ab97817 100644 --- a/src/test/kotlin/com/github/xetra11/ck3workbench/app/AppShutdownServiceTest.kt +++ b/src/test/kotlin/com/github/xetra11/ck3workbench/app/AppShutdownServiceTest.kt @@ -4,7 +4,6 @@ import com.github.xetra11.ck3workbench.app.project.Project import com.github.xetra11.ck3workbench.app.project.save import io.kotest.core.spec.style.ShouldSpec import io.kotest.matchers.shouldBe -import kotlinx.coroutines.async import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import java.io.File @@ -56,8 +55,7 @@ class AppShutdownServiceTest : ShouldSpec({ SettingsHolder.autosave = true val settingsFile = File("settings.cfg") val expectedSettings = AppSettings(autosave = true) - val createdFile = async { settingsFile.createNewFile() } - createdFile.await() + settingsFile.createNewFile() appShutdownService.shutdown()