Skip to content

Commit

Permalink
Remove the equality check
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyant0 committed Jul 23, 2023
1 parent ee4db04 commit 08c32f7
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 93 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ allprojects {
}
}

implementation("com.github.Kyant0:DataSaver:2023.7.2")
implementation("com.github.Kyant0:DataSaver:2023.7.3")
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all subprojects/modules.
plugins {
id("com.android.library") version "7.4.1" apply false
id("com.android.library") version "8.0.0" apply false
kotlin("android") version "1.9.0" apply false
kotlin("plugin.serialization") version "1.9.0" apply false
}
5 changes: 3 additions & 2 deletions datasaver/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.kyant.datasaver"
version = "2023.7.2"
version = "2023.7.3"

android {
namespace = "com.kyant.datasaver"
Expand All @@ -15,6 +15,7 @@ android {

defaultConfig {
minSdk = 21
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
Expand Down Expand Up @@ -51,7 +52,7 @@ afterEvaluate {
register("mavenRelease", MavenPublication::class) {
groupId = "com.kyant"
artifactId = "datasaver"
version = "2023.7.2"
version = "2023.7.3"
from(components["release"])
}
}
Expand Down
7 changes: 3 additions & 4 deletions datasaver/src/main/java/com/kyant/datasaver/DataSaver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class DataSaver<T>(
fun saveData(key: String, data: T) {
return try {
lock.write {
val file = File(path, key)
file.writeBytes(save(data))
File(path, key).writeBytes(save(data))
}
} catch (e: Exception) {
e.printStackTrace()
Expand Down Expand Up @@ -49,8 +48,8 @@ class DataSaver<T>(
companion object {
private lateinit var path: String

fun init(location: String) {
this.path = location
fun init(path: String) {
this.path = path
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ class MutableSaveableListState<T>(
private val savePolicy: SavePolicy = SavePolicy.IMMEDIATELY
) : MutableState<List<T>>, MutableList<T> {
private val list = mutableStateOf(initialValue)

override var value: List<T>
get() = list.value
set(value) {
doSetValue(value)
}

override fun component1(): List<T> = value

override fun component2(): (List<T>) -> Unit = ::doSetValue

private fun doSetValue(value: List<T>) {
list.value = value
if (savePolicy == SavePolicy.IMMEDIATELY) {
scope.launch {
dataSaver.saveData(key, value)
}
}
}

override val size: Int
get() = list.value.size

Expand Down Expand Up @@ -151,45 +171,11 @@ class MutableSaveableListState<T>(
doSetValue(elements.toList())
}

override var value: List<T>
get() = list.value
set(value) {
doSetValue(value)
}

fun saveData() {
scope.launch {
dataSaver.saveData(key, value)
}
}

fun valueChangedSinceInit() = list.value.deepEquals(initialValue)

private fun <T> List<T>.deepEquals(other: List<T>): Boolean {
if (size != other.size) return false
for (i in indices) {
if (this[i] != other[i]) return false
}
return true
}

fun remove(replacement: List<T> = initialValue) {
dataSaver.remove(key)
list.value = replacement
}

private fun doSetValue(value: List<T>) {
val oldValue = list
list.value = value
if (oldValue != value && savePolicy == SavePolicy.IMMEDIATELY) {
saveData()
}
}

override fun component1(): List<T> = value

override fun component2(): (List<T>) -> Unit = ::doSetValue

companion object {
private val scope = CoroutineScope(Dispatchers.IO)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ class MutableSaveableMapState<K, V>(
) : MutableState<Map<K, V>>, MutableMap<K, V> {
private val map = mutableStateOf(initialValue)

override var value: Map<K, V>
get() = map.value
set(value) {
doSetValue(value)
}

override fun component1(): Map<K, V> = value

override fun component2(): (Map<K, V>) -> Unit = ::doSetValue

private fun doSetValue(value: Map<K, V>) {
map.value = value
if (savePolicy == SavePolicy.IMMEDIATELY) {
scope.launch {
dataSaver.saveData(key, value)
}
}
}

override val entries: MutableSet<MutableMap.MutableEntry<K, V>>
get() = map.value.toMutableMap().entries
override val keys: MutableSet<K>
Expand Down Expand Up @@ -85,45 +104,11 @@ class MutableSaveableMapState<K, V>(
doSetValue(elements.toMap())
}

override var value: Map<K, V>
get() = map.value
set(value) {
doSetValue(value)
}

fun saveData() {
scope.launch {
dataSaver.saveData(key, value)
}
}

fun valueChangedSinceInit() = map.value.deepEquals(initialValue)

private fun <K, V> Map<K, V>.deepEquals(other: Map<K, V>): Boolean {
if (size != other.size) return false
for ((key, value) in this) {
if (value != other[key]) return false
}
return true
}

fun remove(replacement: Map<K, V> = initialValue) {
dataSaver.remove(key)
map.value = replacement
}

private fun doSetValue(value: Map<K, V>) {
val oldValue = map
map.value = value
if (oldValue != value && savePolicy == SavePolicy.IMMEDIATELY) {
saveData()
}
}

override fun component1(): Map<K, V> = value

override fun component2(): (Map<K, V>) -> Unit = ::doSetValue

companion object {
private val scope = CoroutineScope(Dispatchers.IO)
}
Expand Down
27 changes: 15 additions & 12 deletions datasaver/src/main/java/com/kyant/datasaver/MutableSaveableState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,27 @@ inline fun <reified T> mutableSaveableStateOf(
class MutableSaveableState<T>(
private val dataSaver: DataSaver<T>,
private val initialValue: T
) {
) : MutableState<T> {
private lateinit var key: String
private lateinit var state: MutableState<T>

override var value: T
get() = state.value
set(_) = error("MutableSaveableState can't be set directly, use delegated property instead")

override fun component1() = value

override fun component2() = error("MutableSaveableState can't be deconstructed, use delegated property instead")

operator fun setValue(thisObj: Any?, property: KProperty<*>, value: T) {
init(thisObj, property)
val oldValue = this.state.value
this.state.value = value
if (oldValue != value) {
if (value != null) {
value.let {
scope.launch {
dataSaver.saveData(key, it)
}
}
} else {
dataSaver.remove(key)
state.value = value
value?.let {
scope.launch {
dataSaver.saveData(key, it)
}
} ?: run {
dataSaver.remove(key)
}
}

Expand Down
5 changes: 0 additions & 5 deletions jitpack.yml

This file was deleted.

1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ dependencyResolutionManagement {
mavenCentral()
}
}

rootProject.name = "DataSaver"
include(":datasaver")

0 comments on commit 08c32f7

Please sign in to comment.