mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-09-14 21:09:24 +00:00
fixes in KeyValueRepo.clear
This commit is contained in:
@@ -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(
|
||||
|
@@ -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() } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -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,
|
||||
|
Reference in New Issue
Block a user