From 3dace243e4b0fe1378e2d81c46fb70cb236e6ae5 Mon Sep 17 00:00:00 2001 From: Fabian Henneke Date: Thu, 6 Aug 2020 10:58:20 +0200 Subject: Refactor uses of applicationContext and startActivityForResult (#997) * Refactor uses of applicationContext and startActivityForResult This commit applies three types of refactoring: 1. Remove context argument from PasswordRepository companion functions by relying on Application.instance. 2. Introduce a sharedPrefs extension function on Context that returns the default SharedPreferences for the applicationContext. 3. Use OpenDocument() and OpenDocumentTree() contracts. * Drop toPasswordItem argument --- app/src/main/java/com/zeapo/pwdstore/Application.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/Application.kt') diff --git a/app/src/main/java/com/zeapo/pwdstore/Application.kt b/app/src/main/java/com/zeapo/pwdstore/Application.kt index 611ccfe0..c3b9a90d 100644 --- a/app/src/main/java/com/zeapo/pwdstore/Application.kt +++ b/app/src/main/java/com/zeapo/pwdstore/Application.kt @@ -15,27 +15,25 @@ import com.github.ajalt.timberkt.Timber.DebugTree import com.github.ajalt.timberkt.Timber.plant import com.zeapo.pwdstore.git.config.setUpBouncyCastleForSshj import com.zeapo.pwdstore.utils.PreferenceKeys +import com.zeapo.pwdstore.utils.sharedPrefs @Suppress("Unused") class Application : android.app.Application(), SharedPreferences.OnSharedPreferenceChangeListener { - private var prefs: SharedPreferences? = null - override fun onCreate() { super.onCreate() instance = this - prefs = PreferenceManager.getDefaultSharedPreferences(this) - if (BuildConfig.ENABLE_DEBUG_FEATURES || prefs?.getBoolean(PreferenceKeys.ENABLE_DEBUG_LOGGING, false) == - true) { + if (BuildConfig.ENABLE_DEBUG_FEATURES || + sharedPrefs.getBoolean(PreferenceKeys.ENABLE_DEBUG_LOGGING, false)) { plant(DebugTree()) } - prefs?.registerOnSharedPreferenceChangeListener(this) + sharedPrefs.registerOnSharedPreferenceChangeListener(this) setNightMode() setUpBouncyCastleForSshj() } override fun onTerminate() { - prefs?.unregisterOnSharedPreferenceChangeListener(this) + sharedPrefs.unregisterOnSharedPreferenceChangeListener(this) super.onTerminate() } @@ -46,7 +44,7 @@ class Application : android.app.Application(), SharedPreferences.OnSharedPrefere } private fun setNightMode() { - AppCompatDelegate.setDefaultNightMode(when (prefs?.getString(PreferenceKeys.APP_THEME, getString(R.string.app_theme_def))) { + AppCompatDelegate.setDefaultNightMode(when (sharedPrefs.getString(PreferenceKeys.APP_THEME, getString(R.string.app_theme_def))) { "light" -> MODE_NIGHT_NO "dark" -> MODE_NIGHT_YES "follow_system" -> MODE_NIGHT_FOLLOW_SYSTEM -- cgit v1.2.3