Skip to content

Clear all prefs at once #67

@P1NG2WIN

Description

@P1NG2WIN

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions