Skip to content

Commit

Permalink
feat(session): add logic to load character state from file
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Hoefer committed Dec 18, 2020
1 parent c5d7baa commit 6cd0157
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions project.wbp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"characters":[]}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class SessionManager(
val projectData = Json.encodeToString(project)
projectFile.writeText(projectData)
NotificationsService.notify("Project ${project.name} has been initialized")
} else {
NotificationsService.notify("Loading project file...")
val projectFromFile = Json.decodeFromString<Project>(projectFile.readText())
StateManager.characters.addAll(projectFromFile.characters)
NotificationsService.notify("Project file loaded")
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.window.Menu
import androidx.compose.ui.window.MenuBar
import androidx.compose.ui.window.MenuItem
import com.github.xetra11.ck3workbench.app.DialogManager
import com.github.xetra11.ck3workbench.app.SessionManager
import com.github.xetra11.ck3workbench.app.notifications.NotificationPanel
import com.github.xetra11.ck3workbench.app.ui.MainUiComponents
import com.github.xetra11.ck3workbench.app.view.DialogView
Expand All @@ -29,6 +30,17 @@ import javax.swing.SwingUtilities.invokeLater

fun main() = invokeLater {
lateinit var window: AppWindow
val sessionManager = SessionManager()

AppManager.setEvents(
onAppStart = {
sessionManager.initialize()
},
onAppExit = {
sessionManager.onExit()
}
)

val toggleDialog = mutableStateOf(false)
val windowSize = mutableStateOf(IntSize.Zero)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.xetra11.ck3workbench.app

import com.github.xetra11.ck3workbench.module.character.CharacterTemplate
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -62,4 +63,24 @@ class SessionManagerTest : ShouldSpec({
projectFromFile.characters shouldBe expectedCharacters
}

should("load character state from file into state manager") {
val projectFile = File("test.wbp")

sessionManager.initialize()

StateManager.characters.addAll(
listOf(
CharacterTemplate.DEFAULT_CHARACTER,
CharacterTemplate.DEFAULT_CHARACTER,
CharacterTemplate.DEFAULT_CHARACTER
)
)

sessionManager.onExit()
StateManager.characters.clear()

sessionManager.initialize()

StateManager.characters shouldHaveSize 3
}
})

0 comments on commit 6cd0157

Please sign in to comment.