From 395cc4c89941c8fb26934241c31c746dae2588fd Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 19 Nov 2020 10:23:12 -0300 Subject: [PATCH] Allow user to generate new recovery code --- .../recoverycode/RecoveryCodeInputFragment.kt | 37 +++++++++++++++++++ .../layout/fragment_recovery_code_input.xml | 16 +++++++- app/src/main/res/values/strings.xml | 4 ++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/stevesoltys/seedvault/ui/recoverycode/RecoveryCodeInputFragment.kt b/app/src/main/java/com/stevesoltys/seedvault/ui/recoverycode/RecoveryCodeInputFragment.kt index abd2d8f2..15c1d5ae 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/ui/recoverycode/RecoveryCodeInputFragment.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/ui/recoverycode/RecoveryCodeInputFragment.kt @@ -1,8 +1,11 @@ package com.stevesoltys.seedvault.ui.recoverycode +import android.app.Activity.RESULT_OK +import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View +import android.view.View.GONE import android.view.View.OnFocusChangeListener import android.view.View.VISIBLE import android.view.ViewGroup @@ -12,9 +15,11 @@ import android.widget.Button import android.widget.TextView import android.widget.Toast import android.widget.Toast.LENGTH_LONG +import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult import androidx.appcompat.app.AlertDialog import androidx.constraintlayout.widget.ConstraintLayout import androidx.fragment.app.Fragment +import com.google.android.material.snackbar.Snackbar import com.google.android.material.textfield.TextInputLayout import com.stevesoltys.seedvault.R import com.stevesoltys.seedvault.isDebugBuild @@ -32,6 +37,7 @@ class RecoveryCodeInputFragment : Fragment() { private lateinit var introText: TextView private lateinit var doneButton: Button + private lateinit var newCodeButton: Button private lateinit var backView: TextView private lateinit var wordLayout1: TextInputLayout private lateinit var wordLayout2: TextInputLayout @@ -61,6 +67,7 @@ class RecoveryCodeInputFragment : Fragment() { introText = v.findViewById(R.id.introText) doneButton = v.findViewById(R.id.doneButton) + newCodeButton = v.findViewById(R.id.newCodeButton) backView = v.findViewById(R.id.backView) wordLayout1 = v.findViewById(R.id.wordLayout1) wordLayout2 = v.findViewById(R.id.wordLayout2) @@ -103,6 +110,8 @@ class RecoveryCodeInputFragment : Fragment() { editText.setAdapter(adapter) } doneButton.setOnClickListener { done() } + newCodeButton.visibility = if (forVerifyingNewCode) GONE else VISIBLE + newCodeButton.setOnClickListener { generateNewCode() } viewModel.existingCodeChecked.observeEvent(viewLifecycleOwner, LiveEventHandler { verified -> onExistingCodeChecked(verified) } @@ -172,10 +181,38 @@ class RecoveryCodeInputFragment : Fragment() { setPositiveButton(R.string.recovery_code_verification_try_again) { dialog, _ -> dialog.dismiss() } + setNegativeButton(R.string.recovery_code_verification_generate_new) { dialog, _ -> + dialog.dismiss() + } } }.show() } + private val regenRequest = registerForActivityResult(StartActivityForResult()) { + if (it.resultCode == RESULT_OK) { + parentFragmentManager.popBackStack() + Snackbar.make(requireView(), R.string.recovery_code_recreated, Snackbar.LENGTH_LONG) + .show() + } + } + + private fun generateNewCode() { + AlertDialog.Builder(requireContext()) + .setIcon(R.drawable.ic_warning) + .setTitle(R.string.recovery_code_verification_new_dialog_title) + .setMessage(R.string.recovery_code_verification_new_dialog_message) + .setPositiveButton(R.string.recovery_code_verification_generate_new) { dialog, _ -> + dialog.dismiss() + // TODO try to delete backups + val i = Intent(requireContext(), RecoveryCodeActivity::class.java) + regenRequest.launch(i) + } + .setNegativeButton(android.R.string.cancel) { dialog, _ -> + dialog.dismiss() + } + .show() + } + @Suppress("MagicNumber") private fun getWordLayout(i: Int) = when (i + 1) { 1 -> wordLayout1 diff --git a/app/src/main/res/layout/fragment_recovery_code_input.xml b/app/src/main/res/layout/fragment_recovery_code_input.xml index 09e421e5..911415eb 100644 --- a/app/src/main/res/layout/fragment_recovery_code_input.xml +++ b/app/src/main/res/layout/fragment_recovery_code_input.xml @@ -67,7 +67,7 @@ android:layout_marginBottom="8dp" android:text="@string/recovery_code_done_button" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintEnd_toStartOf="@+id/newCodeButton" app:layout_constraintStart_toStartOf="parent" /> +