Clear all prefs at once
P1NG2WIN opened this issue · 0 comments
P1NG2WIN commented
In my app, user can log out so i must clear all saved info.
I have class AppCleaner that listens to user events and do clean
object AppCleaner: KoinComponent {
private lateinit var applicationContext: Context
private val roomDatabase by inject<Database>()
private val userLifecycle by inject<Lifecycle>(userLifecycleQualifier)
fun init(context: Context) {
applicationContext = context.applicationContext
GlobalScope.launch {
userLifecycle.stopped()
.collect {
clearCache()
clearRoom()
clearSharedPrefs()
}
}
}
private fun clearRoom() { roomDatabase.clearAllTables() }
private fun clearSharedPrefs() {
applicationContext.filesDir.parentFile?.let { root ->
File(root, "shared_prefs")
.list()
?.let { for (fileName in it) {
applicationContext.getSharedPreferences(
fileName.replace(".xml", ""),
Service.MODE_PRIVATE
).edit { clear() }
} }
}
}
private fun clearBinaryPrefs() {
TODO()
}
private fun clearCache() { applicationContext.cacheDir.deleteRecursively() }
}
Can you help me with solution that will clean all binary prefs?