diff options
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt')
| -rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt | 50 |
1 files changed, 29 insertions, 21 deletions
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 b274b47d..6ce45fe5 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt @@ -14,20 +14,18 @@ import android.view.View import android.view.autofill.AutofillManager import android.view.inputmethod.InputMethodManager import androidx.annotation.IdRes -import androidx.annotation.MainThread import androidx.annotation.RequiresApi import androidx.appcompat.app.AlertDialog -import androidx.appcompat.app.AppCompatActivity import androidx.core.content.getSystemService +import androidx.fragment.app.FragmentActivity import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKey import com.github.ajalt.timberkt.d import com.google.android.material.snackbar.Snackbar -import com.zeapo.pwdstore.git.GitAsyncTask -import com.zeapo.pwdstore.git.GitOperation +import com.zeapo.pwdstore.git.GitCommandExecutor +import com.zeapo.pwdstore.git.operation.GitOperation import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory import java.io.File -import org.eclipse.jgit.api.Git const val OPENPGP_PROVIDER = "org.sufficientlysecure.keychain" @@ -51,12 +49,14 @@ fun CharArray.clear() { val Context.clipboard get() = getSystemService<ClipboardManager>() -fun AppCompatActivity.snackbar( +fun FragmentActivity.snackbar( view: View = findViewById(android.R.id.content), message: String, - length: Int = Snackbar.LENGTH_SHORT -) { - Snackbar.make(view, message, length).show() + length: Int = Snackbar.LENGTH_SHORT, +): Snackbar { + val snackbar = Snackbar.make(view, message, length) + snackbar.show() + return snackbar } fun File.listFilesRecursively() = walkTopDown().filter { !it.isDirectory }.toList() @@ -97,24 +97,33 @@ fun Context.getEncryptedPrefs(fileName: String): SharedPreferences { ) } -@MainThread -fun AppCompatActivity.commitChange(message: String, finishWithResultOnEnd: Intent? = null) { +suspend fun FragmentActivity.commitChange( + message: String, + finishWithResultOnEnd: Intent? = null, + finishActivityOnEnd: Boolean = true, +) { if (!PasswordRepository.isGitRepo()) { if (finishWithResultOnEnd != null) { - setResult(AppCompatActivity.RESULT_OK, finishWithResultOnEnd) + setResult(FragmentActivity.RESULT_OK, finishWithResultOnEnd) finish() } return } object : GitOperation(getRepositoryDirectory(this@commitChange), this@commitChange) { - override fun execute() { + override val commands = arrayOf( + git.add().addFilepattern("."), + git.status(), + git.commit().setAll(true).setMessage(message), + ) + + override suspend fun execute() { d { "Comitting with message: '$message'" } - val git = Git(repository) - val task = GitAsyncTask(this@commitChange, this, finishWithResultOnEnd, silentlyExecute = true) - task.execute( - git.add().addFilepattern("."), - git.commit().setAll(true).setMessage(message) - ) + GitCommandExecutor( + this@commitChange, + this, + finishWithResultOnEnd, + finishActivityOnEnd, + ).execute() } }.execute() } @@ -124,7 +133,6 @@ fun AppCompatActivity.commitChange(message: String, finishWithResultOnEnd: Inten * view whose id is [id]. Solution based on a StackOverflow * answer: https://stackoverflow.com/a/13056259/297261 */ -@MainThread fun <T : View> AlertDialog.requestInputFocusOnView(@IdRes id: Int) { setOnShowListener { findViewById<T>(id)?.apply { @@ -143,6 +151,6 @@ val Context.autofillManager: AutofillManager? @RequiresApi(Build.VERSION_CODES.O) get() = getSystemService() -fun AppCompatActivity.isInsideRepository(file: File): Boolean { +fun FragmentActivity.isInsideRepository(file: File): Boolean { return file.canonicalPath.contains(getRepositoryDirectory(this).canonicalPath) } |
