fixes in KeyValueRepo.clear

This commit is contained in:
2023-07-23 13:47:20 +06:00
parent 3a609e5b66
commit 856e657f81
12 changed files with 93 additions and 4 deletions

View File

@@ -127,7 +127,11 @@ open class MapperKeyValueRepo<FromKey, FromValue, ToKey, ToValue>(
) : KeyValueRepo<FromKey, FromValue>,
MapperRepo<FromKey, FromValue, ToKey, ToValue> by mapper,
ReadKeyValueRepo<FromKey, FromValue> by MapperReadKeyValueRepo(to, mapper),
WriteKeyValueRepo<FromKey, FromValue> by MapperWriteKeyValueRepo(to, mapper)
WriteKeyValueRepo<FromKey, FromValue> by MapperWriteKeyValueRepo(to, mapper) {
override suspend fun clear() {
to.clear()
}
}
@Suppress("NOTHING_TO_INLINE")
inline fun <FromKey, FromValue, ToKey, ToValue> KeyValueRepo<ToKey, ToValue>.withMapper(

View File

@@ -202,9 +202,14 @@ class FileWriteKeyValueRepo(
@Warning("Files watching will not correctly works on Android Platform with version of API lower than API 26")
@Suppress("DELEGATED_MEMBER_HIDES_SUPERTYPE_OVERRIDE")
class FileKeyValueRepo(
folder: File,
private val folder: File,
filesChangedProcessingScope: CoroutineScope? = null
) : KeyValueRepo<String, File>,
WriteKeyValueRepo<String, File> by FileWriteKeyValueRepo(folder, filesChangedProcessingScope),
ReadKeyValueRepo<String, File> by FileReadKeyValueRepo(folder) {
override suspend fun clear() {
withContext(Dispatchers.IO) {
folder.listFiles() ?.forEach { runCatching { it.deleteRecursively() } }
}
}
}

View File

@@ -7,6 +7,7 @@ import dev.inmo.micro_utils.pagination.*
import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.pagination.utils.reverse
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.pagination.maxPagePagination
import kotlinx.coroutines.flow.*
private val cache = HashMap<String, KeyValueStore<*>>()
@@ -159,6 +160,24 @@ class KeyValueStore<T : Any> internal constructor (
}
}
override suspend fun clear() {
val keys = mutableSetOf<String>()
doWithPagination(maxPagePagination()) {
keys(it).also {
keys.addAll(it.results)
}.nextPageIfNotEmpty()
}
val success = sharedPreferences.edit().apply {
clear()
}.commit()
if (success) {
keys.forEach {
_onValueRemovedFlow.emit(it)
}
}
}
companion object {
operator fun <T : Any> invoke(
context: Context,