xxfast / KStore

A tiny Kotlin multiplatform library that assists in saving and restoring objects to and from disk using kotlinx.coroutines, kotlinx.serialisation and kotlinx.io
https://xxfast.github.io/KStore/
Apache License 2.0
470 stars 15 forks source link

Stores on desktop targets requires the folder to be created ahead of file writes/reads #103

Open xxfast opened 3 months ago

xxfast commented 3 months ago

Creating a store for android/ios is fairly straightforward

//android
val filesDir: String = androidContext().filesDir.path
storeOf(file = "$filesDir/countries.json".toPath(), default = emptyList())

// ios
val filesDir: String? = NSFileManager.defaultManager.DocumentDirectory?.relativePath
requireNotNull(filesDir) { "Document directory not found" }
storeOf(file = "$filesDir/countries.json".toPath(), default = emptyList())

However, for desktop we need this extra step

// desktop
val filesDir: String = AppDirsFactory.getInstance()
            .getUserDataDir(PACKAGE_NAME, VERSION, AUTHOR)

FILE_SYSTEM.createDirectories(filesDir.toPath())

storeOf(file = "$filesDir/countries.json".toPath(), default = emptyList())

Need to think of an API to bridge this gap