Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene Dohan committed Apr 29, 2024
1 parent 52059fc commit eae25fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
13 changes: 4 additions & 9 deletions library/src/main/java/renetik/android/store/CSStore.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package renetik.android.store

import renetik.android.core.lang.CSEnvironment.app
import renetik.android.core.lang.Func
import renetik.android.core.lang.lazy.CSLazyVar.Companion.lazyVar
import renetik.android.core.logging.CSLog.logWarn
import renetik.android.event.CSEvent
import renetik.android.event.registration.CSHasChange
import renetik.android.json.obj.CSJsonObjectInterface
import renetik.android.store.extensions.operation
import renetik.android.store.type.CSFileJsonStore
import java.io.Closeable

interface CSStore : Iterable<Map.Entry<String, Any?>>, CSJsonObjectInterface, CSHasChange<CSStore> {

Expand All @@ -22,14 +20,11 @@ interface CSStore : Iterable<Map.Entry<String, Any?>>, CSJsonObjectInterface, CS
override fun onChange(function: (CSStore) -> Unit) = eventChanged.onChange(function)
val data: Map<String, Any?>

@Deprecated("Use pause")
fun bulkSave(): Closeable =
Closeable { logWarn { "Bulk save not implemented" } }

fun operation(func: Func) = logWarn { "Pause not implemented" }
fun pauseOnChange(): Boolean = false
fun resumeOnChange() = Unit

fun load(data: Map<String, Any?>)
fun reload(data: Map<String, Any?>) = bulkSave().use {
fun reload(data: Map<String, Any?>) = operation {
clear()
load(data)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ import renetik.android.store.CSStore

fun <T : CSStore> T.load(store: CSStore) = apply { load(store.data) }
fun <T : CSStore> T.reload(store: CSStore) = apply { reload(store.data) }
fun <T : CSStore> T.reload(json: String) = apply { reload(json.parseJsonMap()!!) }
fun <T : CSStore> T.reload(json: String) = apply { reload(json.parseJsonMap()!!) }

inline fun <T : CSStore, R> T.operation(func: () -> R) {
if (!pauseOnChange()) {
func(); return
}
func()
resumeOnChange()
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package renetik.android.store.type

import renetik.android.core.kotlin.unexpected
import renetik.android.core.lang.Func
import renetik.android.event.CSEvent.Companion.event
import renetik.android.json.obj.CSJsonObject
import renetik.android.store.CSStore
import java.io.Closeable

open class CSJsonObjectStore : CSJsonObject(), CSStore {
override val eventLoaded = event<CSStore>()
Expand All @@ -21,26 +18,19 @@ open class CSJsonObjectStore : CSJsonObject(), CSStore {
var isBulkSave = false
private set

@Deprecated("Use pause")
override fun bulkSave(): Closeable {
if (isBulkSave) unexpected()
isBulkSave = true
return Closeable {
isBulkSave = false
if (isBulkSaveDirty) onChanged()
override fun pauseOnChange(): Boolean {
if (!isBulkSave) {
isBulkSave = true
isBulkSaveDirty = false
return true
}
return false
}

override fun operation(func: Func) {
if (isBulkSave) {
func()
return
}
isBulkSave = true
func()
override fun resumeOnChange() {
isBulkSave = false
if (isBulkSaveDirty) onChanged()
isBulkSaveDirty = false
}
}
}

0 comments on commit eae25fc

Please sign in to comment.