Skip to content

Commit

Permalink
feat(settings): add app settings initializer and state loader
Browse files Browse the repository at this point in the history
relates to #35
  • Loading branch information
Patrick Hoefer committed Jan 10, 2021
1 parent 1947b37 commit 1761426
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
project.wbp
characters.txt
session.wbs
settings.cfg
default.wbp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class AppInitializer(
}

private fun initializeSettings() {
val settingsFile = File("settings.cfg")
settingsFile.createNewFile()
SettingsHolder.init()
SettingsHolder.loadToHolder()
}

private fun initializeSession() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,54 @@ 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
*/
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<AppSettings>(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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 1761426

Please sign in to comment.