Implemented saving and restoring app settings

This commit is contained in:
dankito 2020-11-16 01:59:15 +01:00
parent 6c21222051
commit bec5ca8f14
2 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,11 @@
package net.codinux.banking.epcqrcode.windows.main
open class AppSettings(
open var receiver: String = "",
open var iban: String = "",
open var bic: String = "",
open var amount: Double? = null,
open var reference: String = "",
open var destinationFile: String = ""
)

View File

@ -16,6 +16,7 @@ import java.io.File
import java.text.DecimalFormat
import java.text.ParsePosition
import net.codinux.banking.epcqrcode.*
import net.dankito.utils.serialization.JacksonJsonSerializer
class MainWindow : View(FX.messages["application.title"]) {
@ -25,6 +26,8 @@ class MainWindow : View(FX.messages["application.title"]) {
private val DecimalFormat = DecimalFormat( "#.0" )
private val AppSettingsFile = File("settings.json")
private val logger = LoggerFactory.getLogger(MainWindow::class.java)
}
@ -52,10 +55,15 @@ class MainWindow : View(FX.messages["application.title"]) {
private var generatedQrCodeBytes: ByteArray? = null // couldn't find no working way to get image bytes from image -> also saving pure bytes in memory (what a waste)
private val serializer = JacksonJsonSerializer()
init {
selectedDestinationFile.addListener { _, _, newValue ->
isDestinationFileSelected.value = newValue.isNotBlank()
}
deserializeAppSettings()
}
@ -217,6 +225,8 @@ class MainWindow : View(FX.messages["application.title"]) {
alert.show()
}
}
saveSettings() // TODO: actually a side effect
}
@ -235,6 +245,8 @@ class MainWindow : View(FX.messages["application.title"]) {
generatedQrCodeImage.value = generateQrCode(qrCodeContent)
currentStage?.sizeToScene()
saveSettings() // TODO: actually a side effect
}
private fun map(amount: String): Double? {
@ -253,4 +265,36 @@ class MainWindow : View(FX.messages["application.title"]) {
return null
}
private fun saveSettings() {
try {
val appSettings = AppSettings(receiver.value, iban.value, bic.value, map(amount.value), reference.value,
selectedDestinationFile.value)
serializer.serializeObject(appSettings, AppSettingsFile)
} catch (e: Exception) {
logger.error("Could not save AppSettings", e) // TODO: show message to user?
}
}
private fun deserializeAppSettings() {
try {
serializer.deserializeObject(AppSettingsFile, AppSettings::class.java)?.let { appSettings ->
receiver.value = appSettings.receiver
iban.value = appSettings.iban
bic.value = appSettings.bic
appSettings.amount?.let {
amount.value = DecimalFormat.format(it)
}
reference.value = appSettings.reference
selectedDestinationFile.value = appSettings.destinationFile
}
} catch (e: Exception) {
logger.error("Could not deserialize AppSettings", e) // TODO: show message to user?
}
}
}