PasswordGeneratorDialogFragment: 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:45:21 +05:30
parent bd49bcfb03
commit 1d1bfbb5ad

View File

@@ -17,10 +17,11 @@ import androidx.annotation.IdRes
import androidx.appcompat.widget.AppCompatEditText import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatTextView import androidx.appcompat.widget.AppCompatTextView
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import com.github.michaelbull.result.getOrElse
import com.github.michaelbull.result.runCatching
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.R import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.pwgen.PasswordGenerator import com.zeapo.pwdstore.pwgen.PasswordGenerator
import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorException
import com.zeapo.pwdstore.pwgen.PasswordGenerator.generate import com.zeapo.pwdstore.pwgen.PasswordGenerator.generate
import com.zeapo.pwdstore.pwgen.PasswordGenerator.setPrefs import com.zeapo.pwdstore.pwgen.PasswordGenerator.setPrefs
import com.zeapo.pwdstore.pwgen.PasswordOption import com.zeapo.pwdstore.pwgen.PasswordOption
@@ -71,11 +72,11 @@ class PasswordGeneratorDialogFragment : DialogFragment() {
private fun generate(passwordField: AppCompatTextView) { private fun generate(passwordField: AppCompatTextView) {
setPreferences() setPreferences()
try { passwordField.text = runCatching {
passwordField.text = generate(requireContext().applicationContext) generate(requireContext().applicationContext)
} catch (e: PasswordGeneratorException) { }.getOrElse { e ->
Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show() Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show()
passwordField.text = "" ""
} }
} }