diff --git a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/AppSettings.kt b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/AppSettings.kt new file mode 100644 index 0000000..b145765 --- /dev/null +++ b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/AppSettings.kt @@ -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 = "" +) \ No newline at end of file diff --git a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt index ffad1b9..0cfc872 100644 --- a/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt +++ b/EpcQrCodeJavaFxApp/src/main/kotlin/net/codinux/banking/epcqrcode/windows/main/MainWindow.kt @@ -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? + } + } + } \ No newline at end of file