diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 04:58:32 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 22:49:02 +0530 |
commit | 85d1ef1ad1dc229e56291640b5459c77c9354f5b (patch) | |
tree | 0bad3b5068497be16769b9d4d9acd140559749e1 /app/src/main/java | |
parent | 07607e0f376113721b70c8ddd1dca7e8641c0b2d (diff) |
PasswordStore: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java')
-rw-r--r-- | app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt index 284785bf..4ad4fb91 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt @@ -36,6 +36,9 @@ import com.github.ajalt.timberkt.e import com.github.ajalt.timberkt.i import com.github.ajalt.timberkt.w 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.snackbar.Snackbar import com.google.android.material.textfield.TextInputEditText @@ -71,8 +74,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git -import org.eclipse.jgit.api.errors.GitAPIException -import org.eclipse.jgit.revwalk.RevCommit class PasswordStore : BaseGitActivity() { @@ -254,16 +255,14 @@ class PasswordStore : BaseGitActivity() { // as you specify a parent activity in AndroidManifest.xml. override fun onOptionsItemSelected(item: MenuItem): Boolean { val id = item.itemId - val intent: Intent val initBefore = MaterialAlertDialogBuilder(this) .setMessage(resources.getString(R.string.creation_dialog_text)) .setPositiveButton(resources.getString(R.string.dialog_ok), null) when (id) { R.id.user_pref -> { - try { - intent = Intent(this, UserPreference::class.java) - startActivity(intent) - } catch (e: Exception) { + runCatching { + startActivity(Intent(this, UserPreference::class.java)) + }.onFailure { e -> e.printStackTrace() } return true @@ -408,18 +407,14 @@ class PasswordStore : BaseGitActivity() { } val git = Git(repository) val relativePath = getRelativePath(fullPath, repoPath.absolutePath).substring(1) // Removes leading '/' - val iterator: Iterator<RevCommit> - iterator = try { - git.log().addPath(relativePath).call().iterator() - } catch (e: GitAPIException) { - e(e) { "getLastChangedTimestamp: GITAPIException" } - return -1 - } - if (!iterator.hasNext()) { - w { "getLastChangedTimestamp: No commits for file: $relativePath" } - return -1 - } - return iterator.next().commitTime.toLong() * 1000 + return runCatching { + val iterator = git.log().addPath(relativePath).call().iterator() + if (!iterator.hasNext()) { + w { "getLastChangedTimestamp: No commits for file: $relativePath" } + return -1 + } + iterator.next().commitTime.toLong() * 1000 + }.getOr(-1) } fun decryptPassword(item: PasswordItem) { |