parent
90b61097e2
commit
75d280ad53
@ -0,0 +1,14 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
import java.io.File |
||||
|
||||
class AndroidFileWriter : FileWriter { |
||||
override fun writeFile(directory: String, filename: String, content: String) { |
||||
val dir = File(directory) |
||||
if (!dir.exists()) { |
||||
dir.mkdirs() |
||||
} |
||||
val file = File(dir, filename) |
||||
file.writeText(content) |
||||
} |
||||
} |
||||
@ -0,0 +1,5 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
actual fun createPlatformFileWriter(): FileWriter { |
||||
return AndroidFileWriter() |
||||
} |
||||
@ -0,0 +1,5 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
interface FileWriter { |
||||
fun writeFile(directory: String, filename: String, content: String) |
||||
} |
||||
@ -1,7 +1,12 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
open class ModelObject: Storable { |
||||
import kotlinx.serialization.Serializable |
||||
import kotlinx.serialization.Transient |
||||
|
||||
@Serializable |
||||
abstract class ModelObject: Storable { |
||||
|
||||
@Transient |
||||
override var store: Store? = null |
||||
|
||||
} |
||||
@ -0,0 +1,3 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
expect fun createPlatformFileWriter(): FileWriter |
||||
@ -1,7 +1,10 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
import kotlinx.serialization.Serializable |
||||
|
||||
interface Storable { |
||||
|
||||
val id: String |
||||
var store: Store? |
||||
|
||||
} |
||||
@ -1,5 +1,13 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
class Store { |
||||
class Store(val storeCenter: StoreCenter) { |
||||
|
||||
fun writeJson(filename: String, json: String) { |
||||
this.storeCenter.fileWriter.writeFile(storeCenter.directory, filename, json) |
||||
} |
||||
|
||||
fun <T : Storable> collection(filename: String): StoredCollection<T> { |
||||
return StoredCollection(this, filename) |
||||
} |
||||
|
||||
} |
||||
@ -1,7 +1,31 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
class StoredCollection<T : Storable>(var store: Store) { |
||||
import kotlinx.serialization.Serializable |
||||
import kotlinx.serialization.encodeToString |
||||
import kotlinx.serialization.json.Json |
||||
|
||||
class StoredCollection<T : Storable>(var store: Store, val filename: String) { |
||||
|
||||
var items: MutableList<T> = mutableListOf() |
||||
|
||||
fun addOrUpdate(item: T) { |
||||
val existingIndex = items.indexOfFirst { it.id == item.id } |
||||
if (existingIndex != -1) { |
||||
items[existingIndex] = item |
||||
} else { |
||||
items.add(item) |
||||
} |
||||
writeToJson() |
||||
} |
||||
|
||||
fun delete(id: String) { |
||||
items.removeAll { it.id == id } |
||||
writeToJson() |
||||
} |
||||
|
||||
private fun writeToJson() { |
||||
val json = Json.encodeToString(items) |
||||
store.writeJson(filename, json) |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,33 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
import kotlinx.cinterop.ExperimentalForeignApi |
||||
import platform.Foundation.NSFileManager |
||||
import platform.Foundation.NSString |
||||
import platform.Foundation.NSUTF8StringEncoding |
||||
import platform.Foundation.create |
||||
import platform.Foundation.writeToFile |
||||
|
||||
class IosFileWriter : FileWriter { |
||||
@OptIn(ExperimentalForeignApi::class) |
||||
override fun writeFile(directory: String, filename: String, content: String) { |
||||
val fileManager = NSFileManager.defaultManager |
||||
|
||||
// Create directory if it doesn't exist |
||||
fileManager.createDirectoryAtPath( |
||||
directory, |
||||
withIntermediateDirectories = true, |
||||
attributes = null, |
||||
error = null |
||||
) |
||||
|
||||
// Write file |
||||
val filePath = "$directory/$filename" |
||||
val nsString = NSString.create(string = content) |
||||
nsString.writeToFile( |
||||
filePath, |
||||
atomically = true, |
||||
encoding = NSUTF8StringEncoding, |
||||
error = null |
||||
) |
||||
} |
||||
} |
||||
@ -0,0 +1,5 @@ |
||||
package app.padelclub.lestorage |
||||
|
||||
actual fun createPlatformFileWriter(): FileWriter { |
||||
return IosFileWriter() |
||||
} |
||||
Loading…
Reference in new issue