Also configured database and image cache that for releases they get written to user's home dir (which is important for desktop app bundles

This commit is contained in:
dankito 2024-09-20 02:43:23 +02:00
parent 5d0669c5fe
commit b384f6bc00
3 changed files with 24 additions and 6 deletions

View File

@ -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<QueryResult.AsyncValue<Unit>>, 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<QueryResult.AsyncValue<Unit>>, 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() }
}

View File

@ -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

View File

@ -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")