Skip to content

Commit

Permalink
Add option to pause sync connectors without removing them
Browse files Browse the repository at this point in the history
  • Loading branch information
d4rken committed Aug 30, 2024
1 parent cea80d6 commit d5a4a3d
Show file tree
Hide file tree
Showing 21 changed files with 424 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types

inline fun <reified T> moshiReader(
moshi: Moshi,
Expand Down Expand Up @@ -34,4 +35,72 @@ inline fun <reified T : Any?> DataStore<Preferences>.createValue(
key = stringPreferencesKey(key),
reader = moshiReader(moshi, defaultValue),
writer = moshiWriter(moshi),
)


inline fun <reified T> moshiSetReader(
moshi: Moshi,
defaultValue: Set<T> = emptySet(),
): (Any?) -> Set<T> {
val type = Types.newParameterizedType(Set::class.java, T::class.java)
val adapter = moshi.adapter<Set<T>>(type)
return { rawValue ->
rawValue as String?
rawValue?.let { adapter.fromJson(it) } ?: defaultValue
}
}

inline fun <reified T> moshiSetWriter(
moshi: Moshi,
): (Set<T>?) -> Any? {
val type = Types.newParameterizedType(Set::class.java, T::class.java)
val adapter = moshi.adapter<Set<T>>(type)
return { newValue: Set<T>? ->
newValue.let { adapter.toJson(it) }
}
}

inline fun <reified T : Any> DataStore<Preferences>.createSetValue(
key: String,
defaultValue: Set<T> = emptySet(),
moshi: Moshi,
) = DataStoreValue(
dataStore = this,
key = stringPreferencesKey(key),
reader = moshiSetReader(moshi, defaultValue),
writer = moshiSetWriter(moshi),
)


inline fun <reified T> moshiListReader(
moshi: Moshi,
defaultValue: List<T>,
): (Any?) -> List<T> {
val type = Types.newParameterizedType(List::class.java, T::class.java)
val adapter = moshi.adapter<List<T>>(type)
return { rawValue ->
rawValue as String?
rawValue?.let { adapter.fromJson(it) } ?: defaultValue
}
}

inline fun <reified T> moshiListWriter(
moshi: Moshi,
): (List<T>) -> Any? {
val type = Types.newParameterizedType(List::class.java, T::class.java)
val adapter = moshi.adapter<List<T>>(type)
return { newValue: List<T> ->
newValue.let { adapter.toJson(it) }
}
}

inline fun <reified T : Any> DataStore<Preferences>.createListValue(
key: String,
defaultValue: List<T> = emptyList(),
moshi: Moshi,
) = DataStoreValue(
dataStore = this,
key = stringPreferencesKey(key),
reader = moshiListReader(moshi, defaultValue),
writer = moshiListWriter(moshi),
)
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,80 @@ class DataStoreMoshiExtensionsTest : BaseTest() {
}
}

@Test
fun `reading and writing sets`() = runTest {
val testStore = createDataStore(this)

val testData = TestGson(string = "teststring")

testStore.createSetValue<TestGson>(
key = "testKey",
defaultValue = emptySet(),
moshi = Moshi.Builder().build()
).apply {
flow.first() shouldBe emptySet()
testStore.data.first()[stringPreferencesKey(keyName)] shouldBe null

update {
setOf<TestGson>() + testData + testData
}
testStore.data.first()[stringPreferencesKey(keyName)]!!.toComparableJson() shouldBe """
[
{
"string":"teststring",
"boolean":true,
"float":1.0,
"int":1,
"long":1
}
]
""".toComparableJson()

flow.first() shouldBe setOf(testData)
flow.first().contains(testData) shouldBe true
}
}

@Test
fun `reading and writing lists`() = runTest {
val testStore = createDataStore(this)

val testData = TestGson(string = "teststring")

testStore.createListValue<TestGson>(
key = "testKey",
defaultValue = emptyList(),
moshi = Moshi.Builder().build()
).apply {
flow.first() shouldBe emptyList()
testStore.data.first()[stringPreferencesKey(keyName)] shouldBe null

update {
listOf<TestGson>() + testData + testData
}
testStore.data.first()[stringPreferencesKey(keyName)]!!.toComparableJson() shouldBe """
[
{
"string":"teststring",
"boolean":true,
"float":1.0,
"int":1,
"long":1
}, {
"string":"teststring",
"boolean":true,
"float":1.0,
"int":1,
"long":1
}
]
""".toComparableJson()

flow.first() shouldBe listOf(testData, testData)
flow.first().contains(testData) shouldBe true
}
}

@Test
fun `reading and writing using autocreated reader and writer`() = runTest {
val testStore = createDataStore(this)
Expand Down
Loading

0 comments on commit d5a4a3d

Please sign in to comment.