Clearing shared prefs fixed

This commit is contained in:
Milan Cerovsky 2025-04-24 14:21:02 +02:00
parent 1b423b8367
commit bf47f5bbfb
1 changed files with 12 additions and 0 deletions

View File

@ -11,9 +11,11 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
@ -32,6 +34,8 @@ class AndroidPreferenceProvider private constructor(
private val sharedPreferences: SharedPreferences,
private val dispatcher: CoroutineDispatcher
) : PreferenceProvider {
private val clearPipeline = MutableSharedFlow<Unit>()
private val mutex = Mutex()
/*
* Implementation note: EncryptedSharedPreferences are not thread-safe, so this implementation
@ -119,6 +123,8 @@ class AndroidPreferenceProvider private constructor(
editor.clear()
clearPipeline.emit(Unit)
return@withContext editor.commit()
}
@ -131,6 +137,12 @@ class AndroidPreferenceProvider private constructor(
}
sharedPreferences.registerOnSharedPreferenceChangeListener(listener)
this.launch {
clearPipeline.collect {
send(Unit)
}
}
// Kickstart the emissions
trySend(Unit)