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/utils/Extensions.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt') diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt index 6ce45fe5..b5cb4f8c 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt @@ -18,6 +18,7 @@ import androidx.annotation.RequiresApi import androidx.appcompat.app.AlertDialog import androidx.core.content.getSystemService import androidx.fragment.app.FragmentActivity +import androidx.preference.PreferenceManager import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKey import com.github.ajalt.timberkt.d @@ -97,6 +98,9 @@ fun Context.getEncryptedPrefs(fileName: String): SharedPreferences { ) } +val Context.sharedPrefs: SharedPreferences + get() = PreferenceManager.getDefaultSharedPreferences(applicationContext) + suspend fun FragmentActivity.commitChange( message: String, finishWithResultOnEnd: Intent? = null, @@ -109,7 +113,7 @@ suspend fun FragmentActivity.commitChange( } return } - object : GitOperation(getRepositoryDirectory(this@commitChange), this@commitChange) { + object : GitOperation(getRepositoryDirectory(), this@commitChange) { override val commands = arrayOf( git.add().addFilepattern("."), git.status(), @@ -151,6 +155,6 @@ val Context.autofillManager: AutofillManager? @RequiresApi(Build.VERSION_CODES.O) get() = getSystemService() -fun FragmentActivity.isInsideRepository(file: File): Boolean { - return file.canonicalPath.contains(getRepositoryDirectory(this).canonicalPath) +fun File.isInsideRepository(): Boolean { + return canonicalPath.contains(getRepositoryDirectory().canonicalPath) } -- cgit v1.2.3