summaryrefslogtreecommitdiff
path: root/app/src/main
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2020-09-05 05:11:25 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2020-09-05 22:49:02 +0530
commit9bc1d59dd225a4b482ad8177c80bef5af946a9f0 (patch)
treec3fee1e1addb798d5e536870d8266cb8991c0709 /app/src/main
parent85d1ef1ad1dc229e56291640b5459c77c9354f5b (diff)
PasswordBuilder: use runCatching to replace exception handling
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt32
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt51
2 files changed, 40 insertions, 43 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt b/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt
index 587a5350..398a955d 100644
--- a/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/pwgenxkpwd/PasswordBuilder.kt
@@ -5,12 +5,13 @@
package com.zeapo.pwdstore.pwgenxkpwd
import android.content.Context
+import com.github.michaelbull.result.Result
+import com.github.michaelbull.result.runCatching
import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.pwgen.PasswordGenerator.PasswordGeneratorException
import com.zeapo.pwdstore.pwgen.secureRandomCharacter
import com.zeapo.pwdstore.pwgen.secureRandomElement
import com.zeapo.pwdstore.pwgen.secureRandomNumber
-import java.io.IOException
import java.util.Locale
class PasswordBuilder(ctx: Context) {
@@ -83,8 +84,7 @@ class PasswordBuilder(ctx: Context) {
}
@OptIn(ExperimentalStdlibApi::class)
- @Throws(PasswordGeneratorException::class)
- fun create(): String {
+ fun create(): Result<String, Throwable> {
val wordBank = mutableListOf<String>()
val password = StringBuilder()
@@ -94,7 +94,7 @@ class PasswordBuilder(ctx: Context) {
password.append(separator)
}
}
- try {
+ return runCatching {
val dictionary = XkpwdDictionary(context)
val words = dictionary.words
for (wordLength in minWordLength..maxWordLength) {
@@ -119,22 +119,20 @@ class PasswordBuilder(ctx: Context) {
password.append(separator)
}
}
- } catch (e: IOException) {
- throw PasswordGeneratorException("Failed generating password!")
- }
- if (numDigits != 0) {
- if (isAppendNumberSeparator) {
- password.append(separator)
+ if (numDigits != 0) {
+ if (isAppendNumberSeparator) {
+ password.append(separator)
+ }
+ password.append(generateRandomNumberSequence(numDigits))
}
- password.append(generateRandomNumberSequence(numDigits))
- }
- if (numSymbols != 0) {
- if (isAppendSymbolsSeparator) {
- password.append(separator)
+ if (numSymbols != 0) {
+ if (isAppendSymbolsSeparator) {
+ password.append(separator)
+ }
+ password.append(generateRandomSymbolSequence(numSymbols))
}
- password.append(generateRandomSymbolSequence(numSymbols))
+ password.toString()
}
- return password.toString()
}
companion object {
diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt
index 527e4c6c..274330bb 100644
--- a/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/ui/dialogs/XkPasswordGeneratorDialogFragment.kt
@@ -16,12 +16,15 @@ import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.edit
import androidx.fragment.app.DialogFragment
import com.github.ajalt.timberkt.Timber.tag
+import com.github.michaelbull.result.fold
+import com.github.michaelbull.result.getOr
+import com.github.michaelbull.result.runCatching
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.databinding.FragmentXkpwgenBinding
-import com.zeapo.pwdstore.pwgen.PasswordGenerator
import com.zeapo.pwdstore.pwgenxkpwd.CapsType
import com.zeapo.pwdstore.pwgenxkpwd.PasswordBuilder
+import com.zeapo.pwdstore.utils.getString
/** A placeholder fragment containing a simple view. */
class XkPasswordGeneratorDialogFragment : DialogFragment() {
@@ -41,19 +44,13 @@ class XkPasswordGeneratorDialogFragment : DialogFragment() {
prefs = callingActivity.getSharedPreferences("PasswordGenerator", Context.MODE_PRIVATE)
- val previousStoredCapStyle: String = try {
- prefs.getString(PREF_KEY_CAPITALS_STYLE, DEFAULT_CAPS_STYLE)!!
- } catch (e: Exception) {
- tag("xkpw").e(e)
- DEFAULT_CAPS_STYLE
- }
+ val previousStoredCapStyle: String = runCatching {
+ prefs.getString(PREF_KEY_CAPITALS_STYLE)!!
+ }.getOr(DEFAULT_CAPS_STYLE)
- val lastCapitalsStyleIndex: Int = try {
+ val lastCapitalsStyleIndex: Int = runCatching {
CapsType.valueOf(previousStoredCapStyle).ordinal
- } catch (e: Exception) {
- tag("xkpw").e(e)
- DEFAULT_CAPS_INDEX
- }
+ }.getOr(DEFAULT_CAPS_INDEX)
binding.xkCapType.setSelection(lastCapitalsStyleIndex)
binding.xkNumWords.setText(prefs.getString(PREF_KEY_NUM_WORDS, DEFAULT_NUMBER_OF_WORDS))
@@ -87,20 +84,22 @@ class XkPasswordGeneratorDialogFragment : DialogFragment() {
}
private fun makeAndSetPassword(passwordText: AppCompatTextView) {
- try {
- passwordText.text = PasswordBuilder(requireContext())
- .setNumberOfWords(Integer.valueOf(binding.xkNumWords.text.toString()))
- .setMinimumWordLength(DEFAULT_MIN_WORD_LENGTH)
- .setMaximumWordLength(DEFAULT_MAX_WORD_LENGTH)
- .setSeparator(binding.xkSeparator.text.toString())
- .appendNumbers(binding.xkNumberSymbolMask.text!!.count { c -> c == EXTRA_CHAR_PLACEHOLDER_DIGIT })
- .appendSymbols(binding.xkNumberSymbolMask.text!!.count { c -> c == EXTRA_CHAR_PLACEHOLDER_SYMBOL })
- .setCapitalization(CapsType.valueOf(binding.xkCapType.selectedItem.toString())).create()
- } catch (e: PasswordGenerator.PasswordGeneratorException) {
- Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show()
- tag("xkpw").e(e, "failure generating xkpasswd")
- passwordText.text = FALLBACK_ERROR_PASS
- }
+ PasswordBuilder(requireContext())
+ .setNumberOfWords(Integer.valueOf(binding.xkNumWords.text.toString()))
+ .setMinimumWordLength(DEFAULT_MIN_WORD_LENGTH)
+ .setMaximumWordLength(DEFAULT_MAX_WORD_LENGTH)
+ .setSeparator(binding.xkSeparator.text.toString())
+ .appendNumbers(binding.xkNumberSymbolMask.text!!.count { c -> c == EXTRA_CHAR_PLACEHOLDER_DIGIT })
+ .appendSymbols(binding.xkNumberSymbolMask.text!!.count { c -> c == EXTRA_CHAR_PLACEHOLDER_SYMBOL })
+ .setCapitalization(CapsType.valueOf(binding.xkCapType.selectedItem.toString())).create()
+ .fold(
+ success = { passwordText.text = it },
+ failure = { e ->
+ Toast.makeText(requireActivity(), e.message, Toast.LENGTH_SHORT).show()
+ tag("xkpw").e(e, "failure generating xkpasswd")
+ passwordText.text = FALLBACK_ERROR_PASS
+ },
+ )
}
private fun setPreferences() {