Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear all prefs at once #67

Open
P1NG2WIN opened this issue Feb 6, 2021 · 0 comments
Open

Clear all prefs at once #67

P1NG2WIN opened this issue Feb 6, 2021 · 0 comments

Comments

@P1NG2WIN
Copy link

P1NG2WIN commented Feb 6, 2021

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant