diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 20:25:01 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-09-05 22:49:05 +0530 |
commit | 361a801e47f188d028eeecfc72344eb27d631492 (patch) | |
tree | 393f6edc8781b11d35c5170273f80de8f71c8cae /app/src/main/java | |
parent | 2d4c165f15a82233e391b4a465176f63875d6f2d (diff) |
GitServerConfigActivity: 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/git/GitServerConfigActivity.kt | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt index d867b4db..c18c79c6 100644 --- a/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt +++ b/app/src/main/java/com/zeapo/pwdstore/git/GitServerConfigActivity.kt @@ -9,7 +9,10 @@ import android.os.Handler import android.view.View import androidx.core.os.postDelayed import androidx.lifecycle.lifecycleScope +import com.github.ajalt.timberkt.e import com.github.michaelbull.result.fold +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.zeapo.pwdstore.R @@ -20,7 +23,6 @@ import com.zeapo.pwdstore.git.config.Protocol import com.zeapo.pwdstore.utils.PasswordRepository import com.zeapo.pwdstore.utils.snackbar import com.zeapo.pwdstore.utils.viewBinding -import java.io.IOException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -116,7 +118,7 @@ class GitServerConfigActivity : BaseGitActivity() { .setMessage(resources.getString(R.string.dialog_delete_msg, localDir.toString())) .setCancelable(false) .setPositiveButton(R.string.dialog_delete) { dialog, _ -> - try { + runCatching { lifecycleScope.launch { val snackbar = snackbar(message = getString(R.string.delete_directory_progress_text), length = Snackbar.LENGTH_INDEFINITE) withContext(Dispatchers.IO) { @@ -131,33 +133,25 @@ class GitServerConfigActivity : BaseGitActivity() { failure = ::finishAfterPromptOnErrorHandler, ) } - } catch (e: IOException) { - // TODO Handle the exception correctly if we are unable to delete the directory... + }.onFailure { e -> e.printStackTrace() MaterialAlertDialogBuilder(this).setMessage(e.message).show() - } finally { - dialog.cancel() } + dialog.cancel() } .setNegativeButton(R.string.dialog_do_not_delete) { dialog, _ -> dialog.cancel() } .show() } else { - try { + runCatching { // Silently delete & replace the lone .git folder if it exists if (localDir.exists() && localDirFiles.size == 1 && localDirFiles[0].name == ".git") { - try { - localDir.deleteRecursively() - } catch (e: IOException) { - e.printStackTrace() - MaterialAlertDialogBuilder(this).setMessage(e.message).show() - } + localDir.deleteRecursively() + } - } catch (e: Exception) { - // This is what happens when JGit fails :( - // TODO Handle the different cases of exceptions - e.printStackTrace() + }.onFailure { e -> + e(e) MaterialAlertDialogBuilder(this).setMessage(e.message).show() } lifecycleScope.launch { |