From b384f6bc0000dba5bec1a8d05ca1fe1082f94146 Mon Sep 17 00:00:00 2001 From: dankito Date: Fri, 20 Sep 2024 02:43:23 +0200 Subject: [PATCH] Also configured database and image cache that for releases they get written to user's home dir (which is important for desktop app bundles --- .../persistence/BankingRepository.jvm.kt | 26 ++++++++++++++++--- .../kotlin/net/codinux/banking/ui/main.kt | 1 - .../ui/service/ImageService.desktop.kt | 3 ++- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/BankingPersistence/src/jvmMain/kotlin/net/codinux/banking/persistence/BankingRepository.jvm.kt b/BankingPersistence/src/jvmMain/kotlin/net/codinux/banking/persistence/BankingRepository.jvm.kt index f2d5d80..14f5458 100644 --- a/BankingPersistence/src/jvmMain/kotlin/net/codinux/banking/persistence/BankingRepository.jvm.kt +++ b/BankingPersistence/src/jvmMain/kotlin/net/codinux/banking/persistence/BankingRepository.jvm.kt @@ -7,16 +7,34 @@ import app.cash.sqldelight.db.SqlSchema import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver import java.io.File -actual fun createSqlDriverDriver(dbName: String, schema: SqlSchema>, version: Long): SqlDriver { - val dbDir = File("data/db").also { it.mkdirs() } - return JdbcSqliteDriver("jdbc:sqlite:data/db/$dbName").also { driver -> +val dataDirectory: File = determineDataDirectory() + +actual fun createSqlDriverDriver(dbName: String, schema: SqlSchema>, version: Long): SqlDriver { + val dbDir = File(dataDirectory, "db").also { it.mkdirs() } + val databaseFile = File(dbDir, dbName) + + return JdbcSqliteDriver("jdbc:sqlite:${databaseFile.path}").also { driver -> schema.synchronous().also { schema -> - if (File(dbDir, dbName).exists() == false) { + if (databaseFile.exists() == false) { schema.create(driver) } schema.migrate(driver, schema.version, version) } } +} + + +private fun determineDataDirectory(): File { + val currentDir = File(System.getProperty("user.dir")) + + val dataDir = if (currentDir.canWrite()) { // if the current directory is writable, use that one (the default for development) + File(currentDir, "data") + } else { // otherwise use .bankmeister dir in user's home dir (the default for releases) + val userHome = System.getProperty("user.home") + File(userHome, ".bankmeister") + } + + return dataDir.also { it.mkdirs() } } \ No newline at end of file diff --git a/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/main.kt b/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/main.kt index c7f34ff..8a4b505 100644 --- a/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/main.kt +++ b/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/main.kt @@ -12,7 +12,6 @@ import kotlinx.datetime.LocalDate import net.codinux.banking.client.model.AccountTransaction import net.codinux.banking.client.model.Amount import net.codinux.banking.persistence.InMemoryBankingRepository -import net.codinux.banking.persistence.SqliteBankingRepository import net.codinux.banking.ui.config.DI import org.jetbrains.compose.resources.painterResource diff --git a/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/service/ImageService.desktop.kt b/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/service/ImageService.desktop.kt index 52b0434..339ecba 100644 --- a/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/service/ImageService.desktop.kt +++ b/composeApp/src/desktopMain/kotlin/net/codinux/banking/ui/service/ImageService.desktop.kt @@ -2,6 +2,7 @@ package net.codinux.banking.ui.service import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.toComposeImageBitmap +import net.codinux.banking.persistence.dataDirectory import net.codinux.log.Log import org.jetbrains.skia.Image import java.io.File @@ -9,7 +10,7 @@ import java.net.URL import java.security.MessageDigest -private val cacheDir = File("data/imageCache").also { it.mkdirs() } +private val cacheDir = File(dataDirectory, "imageCache").also { it.mkdirs() } private val messageDigest = MessageDigest.getInstance("SHA-256")