PasswordStore: use runCatching to replace exception handling

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya
2020-09-05 04:58:32 +05:30
parent 07607e0f37
commit 85d1ef1ad1

View File

@@ -36,6 +36,9 @@ import com.github.ajalt.timberkt.e
import com.github.ajalt.timberkt.i import com.github.ajalt.timberkt.i
import com.github.ajalt.timberkt.w import com.github.ajalt.timberkt.w
import com.github.michaelbull.result.fold import com.github.michaelbull.result.fold
import com.github.michaelbull.result.getOr
import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.runCatching
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
@@ -71,8 +74,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.errors.GitAPIException
import org.eclipse.jgit.revwalk.RevCommit
class PasswordStore : BaseGitActivity() { class PasswordStore : BaseGitActivity() {
@@ -254,16 +255,14 @@ class PasswordStore : BaseGitActivity() {
// as you specify a parent activity in AndroidManifest.xml. // as you specify a parent activity in AndroidManifest.xml.
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId val id = item.itemId
val intent: Intent
val initBefore = MaterialAlertDialogBuilder(this) val initBefore = MaterialAlertDialogBuilder(this)
.setMessage(resources.getString(R.string.creation_dialog_text)) .setMessage(resources.getString(R.string.creation_dialog_text))
.setPositiveButton(resources.getString(R.string.dialog_ok), null) .setPositiveButton(resources.getString(R.string.dialog_ok), null)
when (id) { when (id) {
R.id.user_pref -> { R.id.user_pref -> {
try { runCatching {
intent = Intent(this, UserPreference::class.java) startActivity(Intent(this, UserPreference::class.java))
startActivity(intent) }.onFailure { e ->
} catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
} }
return true return true
@@ -408,18 +407,14 @@ class PasswordStore : BaseGitActivity() {
} }
val git = Git(repository) val git = Git(repository)
val relativePath = getRelativePath(fullPath, repoPath.absolutePath).substring(1) // Removes leading '/' val relativePath = getRelativePath(fullPath, repoPath.absolutePath).substring(1) // Removes leading '/'
val iterator: Iterator<RevCommit> return runCatching {
iterator = try { val iterator = git.log().addPath(relativePath).call().iterator()
git.log().addPath(relativePath).call().iterator() if (!iterator.hasNext()) {
} catch (e: GitAPIException) { w { "getLastChangedTimestamp: No commits for file: $relativePath" }
e(e) { "getLastChangedTimestamp: GITAPIException" } return -1
return -1 }
} iterator.next().commitTime.toLong() * 1000
if (!iterator.hasNext()) { }.getOr(-1)
w { "getLastChangedTimestamp: No commits for file: $relativePath" }
return -1
}
return iterator.next().commitTime.toLong() * 1000
} }
fun decryptPassword(item: PasswordItem) { fun decryptPassword(item: PasswordItem) {