GitServerConfigActivity: use runCatching to replace exception handling

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-09-05 20:25:01 +05:30
parent 2d4c165f15
commit 361a801e47
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

View file

@ -9,7 +9,10 @@ import android.os.Handler
import android.view.View import android.view.View
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.ajalt.timberkt.e
import com.github.michaelbull.result.fold 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.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import com.zeapo.pwdstore.R 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.PasswordRepository
import com.zeapo.pwdstore.utils.snackbar import com.zeapo.pwdstore.utils.snackbar
import com.zeapo.pwdstore.utils.viewBinding import com.zeapo.pwdstore.utils.viewBinding
import java.io.IOException
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@ -116,7 +118,7 @@ class GitServerConfigActivity : BaseGitActivity() {
.setMessage(resources.getString(R.string.dialog_delete_msg, localDir.toString())) .setMessage(resources.getString(R.string.dialog_delete_msg, localDir.toString()))
.setCancelable(false) .setCancelable(false)
.setPositiveButton(R.string.dialog_delete) { dialog, _ -> .setPositiveButton(R.string.dialog_delete) { dialog, _ ->
try { runCatching {
lifecycleScope.launch { lifecycleScope.launch {
val snackbar = snackbar(message = getString(R.string.delete_directory_progress_text), length = Snackbar.LENGTH_INDEFINITE) val snackbar = snackbar(message = getString(R.string.delete_directory_progress_text), length = Snackbar.LENGTH_INDEFINITE)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
@ -131,33 +133,25 @@ class GitServerConfigActivity : BaseGitActivity() {
failure = ::finishAfterPromptOnErrorHandler, failure = ::finishAfterPromptOnErrorHandler,
) )
} }
} catch (e: IOException) { }.onFailure { e ->
// TODO Handle the exception correctly if we are unable to delete the directory...
e.printStackTrace() e.printStackTrace()
MaterialAlertDialogBuilder(this).setMessage(e.message).show() MaterialAlertDialogBuilder(this).setMessage(e.message).show()
} finally {
dialog.cancel()
} }
dialog.cancel()
} }
.setNegativeButton(R.string.dialog_do_not_delete) { dialog, _ -> .setNegativeButton(R.string.dialog_do_not_delete) { dialog, _ ->
dialog.cancel() dialog.cancel()
} }
.show() .show()
} else { } else {
try { runCatching {
// Silently delete & replace the lone .git folder if it exists // Silently delete & replace the lone .git folder if it exists
if (localDir.exists() && localDirFiles.size == 1 && localDirFiles[0].name == ".git") { if (localDir.exists() && localDirFiles.size == 1 && localDirFiles[0].name == ".git") {
try { localDir.deleteRecursively()
localDir.deleteRecursively()
} catch (e: IOException) {
e.printStackTrace()
MaterialAlertDialogBuilder(this).setMessage(e.message).show()
}
} }
} catch (e: Exception) { }.onFailure { e ->
// This is what happens when JGit fails :( e(e)
// TODO Handle the different cases of exceptions
e.printStackTrace()
MaterialAlertDialogBuilder(this).setMessage(e.message).show() MaterialAlertDialogBuilder(this).setMessage(e.message).show()
} }
lifecycleScope.launch { lifecycleScope.launch {