diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt index 1b281aa..e7a478f 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/CanFragment.kt @@ -8,6 +8,7 @@ import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.core.widget.addTextChangedListener import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController @@ -44,13 +45,10 @@ class CanFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) checkIfSkip() - // If the user arrives from the settings menu then the button should say - // save instead of continue. - if (args.saving) { - binding!!.nextButton.text = getString(R.string.save_text) + binding!!.canTextField.editText?.addTextChangedListener { + checkEnteredCan() } - binding!!.nextButton.setOnClickListener { checkEnteredCan() } - binding!!.cancelButton.setOnClickListener { goToTheStart() } + binding!!.buttonCancel.setOnClickListener { goToTheStart() } } /** @@ -71,60 +69,12 @@ class CanFragment : Fragment() { findNavController().navigate(action) } - /** - * Checks whether the user has entered a 6 digit can to the input field. - * If yes then the user is allowed to continue otherwise the user is - * allowed to modify the entered can. - */ - private fun checkEnteredCan() { - val enteredCan = binding!!.canEditText.editText?.text.toString() - if (enteredCan.length == 6) { - viewModel.setUserCan(enteredCan) - if (args.saving) { - viewModel.storeCan(requireContext()) - goToTheStart() - } else { - val storeCanQuestion = getDialog() - storeCanQuestion?.show() - } - } else { - Toast.makeText(requireContext(), getString(R.string.length_can), Toast.LENGTH_SHORT) - .show() - } - } - - /** - * Builds a dialog that asks the user whether the entered CAN should be saved - * on the device or not. - */ - private fun getDialog(): AlertDialog? { - return activity?.let { frag -> - val builder = AlertDialog.Builder(frag) - builder.apply { - // If response is positive then save the CAN on the device. - setPositiveButton(R.string.save_text) { _, _ -> - viewModel.storeCan( - requireContext() - ) - goToTheNextFragment() - } - setNegativeButton(R.string.deny_text) { _, _ -> - goToTheNextFragment() - } - } - builder.setMessage(R.string.can_save_request) - builder.setTitle(R.string.save_can_title) - builder.create() - } - } - /** * Navigates the user back to the start depending on where the user arrived. * If the user arrived from the settings menu then the start is the settings menu * not the HomeFragment. */ private fun goToTheStart() { - // TODO: Needs special handling when the app is launched with intent. Temporary solution at the moment. if (args.saving) { findNavController().navigate(R.id.action_canFragment_to_settingsFragment) } else if (args.auth || args.mobile) { @@ -140,6 +90,24 @@ class CanFragment : Fragment() { } } + /** + * Checks whether the user has entered a 6 digit can to the input field. + * If yes then the user is allowed to continue otherwise the user is + * allowed to modify the entered can. + */ + private fun checkEnteredCan() { + val enteredCan = binding!!.canTextField.editText?.text.toString() + if (enteredCan.length == 6) { + viewModel.setUserCan(enteredCan) + viewModel.storeCan(requireContext()) //Maybe storeCan should always automatically call setUserCan method as well because these methods usually are used together + if (args.saving) { + goToTheStart() + } else { + goToTheNextFragment() + } + } + } + override fun onDestroy() { super.onDestroy() binding = null diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt index e1888f4..4819fb2 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt @@ -53,6 +53,7 @@ class HomeFragment : Fragment() { } val mobile = requireActivity().intent.getBooleanExtra("mobile", false) if (auth || mobile){ + try { if (mobile) { // We use !! because we want an exception when something is not right. @@ -73,6 +74,7 @@ class HomeFragment : Fragment() { requireActivity().setResult(AppCompatActivity.RESULT_CANCELED, resultIntent) requireActivity().finish() } + goToTheNextFragment(true, mobile) } binding!!.beginButton.setOnClickListener { goToTheNextFragment() } diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt index 638d88e..f205c38 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/MainActivity.kt @@ -40,6 +40,7 @@ class MainActivity : AppCompatActivity() { R.id.menu_settings_option -> { if (menuAvailable) { navigationController.navigate(R.id.action_homeFragment_to_settingsFragment) + menuAvailable = false true } else { Toast.makeText(this, getString(R.string.unavailable), Toast.LENGTH_SHORT).show() diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt index 86dbbc1..a0df1f3 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/PinFragment.kt @@ -32,6 +32,9 @@ class PinFragment : Fragment() { // not require PIN 1 so it is not necessary to ask it. private val args: PinFragmentArgs by navArgs() + // TODO: Should be persistent and read when launching the app + private var saveToggle = true + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -44,26 +47,22 @@ class PinFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) checkIfSkip() - // If the user arrives from the settings menu then the button says - // save instead of continue. + // Switch should be not visible when user is in savings mode if (args.saving) { - binding!!.nextButton.text = getString(R.string.save_text) - } - binding!!.nextButton.setOnClickListener { checkEnteredPin() } - binding!!.cancelButton.setOnClickListener { goToTheStart() } - } - - /** - * Checks if the current fragment can be skipped or not. - * If the user has PIN 1 saved on the device or PIN 1 is not required - * then the PIN 1 won't be asked. - */ - private fun checkIfSkip() { - if (args.reading) { - goToTheNextFragment() - } else if (viewModel.userPin.length in 4..12) { - goToTheNextFragment() + binding!!.savePinQuestion.visibility = View.GONE + binding!!.saveLayout.visibility = View.GONE + } else { + binding!!.saveSwitch.setOnCheckedChangeListener { _, isChecked -> + if (isChecked) { + binding!!.saveStatus.text = getString(R.string.pin_save_on) + } else { + binding!!.saveStatus.text = getString(R.string.pin_save_off) + } + saveToggle = !saveToggle + } } + binding!!.buttonContinue.setOnClickListener { checkEnteredPin() } + binding!!.buttonCancel.setOnClickListener { goToTheStart() } } /** @@ -74,60 +73,13 @@ class PinFragment : Fragment() { findNavController().navigate(action) } - /** - * Checks whether the user has entered a PIN 1 with length between [4, 12] in the - * input field. If yes then the user is allowed to continue otherwise the user is - * allowed to modify the entered PIN 1. - */ - private fun checkEnteredPin() { - val enteredPin = binding!!.pinEditText.editText?.text.toString() - if (enteredPin.length in 4..12) { - viewModel.setUserPin(enteredPin) - if (args.saving) { - viewModel.storePin(requireContext()) - goToTheStart() - } else { - val storePinQuestion = getDialog() - storePinQuestion?.show() - } - } else { - Toast.makeText(requireContext(), getString(R.string.length_pin), Toast.LENGTH_SHORT) - .show() - } - } - - /** - * Builds a dialog that asks the user whether the entered PIN 1 should be saved - * on the device or not. - */ - private fun getDialog(): AlertDialog? { - return activity?.let { frag -> - val builder = AlertDialog.Builder(frag) - builder.apply { - // If response is positive save the PIN 1 on the device. - setPositiveButton(R.string.save_text) { _, _ -> - viewModel.storePin( - requireContext() - ) - goToTheNextFragment() - } - setNegativeButton(R.string.deny_text) { _, _ -> - goToTheNextFragment() - } - } - builder.setMessage(R.string.pin_save_request) - builder.setTitle(R.string.save_pin_title) - builder.create() - } - } - /** * Returns user to the start. If the user arrived from the settings menu then the start is * settings menu not the HomeFragment. */ private fun goToTheStart() { if (args.saving) { - findNavController().navigate(R.id.action_canFragment_to_settingsFragment) + findNavController().navigate(R.id.action_pinFragment_to_settingsFragment) } else if (args.auth || args.mobile) { if (args.mobile) { val resultIntent = Intent() @@ -141,6 +93,45 @@ class PinFragment : Fragment() { } } + /** + * Checks if the current fragment can be skipped or not. + * If the user has PIN 1 saved on the device or PIN 1 is not required + * then the PIN 1 won't be asked. + * + * NOTE: maybe args.reading can be removed after changing the nav_graph + */ + private fun checkIfSkip() { + if (args.reading) { + goToTheNextFragment() + } else if (viewModel.userPin.length in 4..12) { + goToTheNextFragment() + } + } + + /** + * Checks whether the user has entered a PIN 1 with length between [4, 12] in the + * input field. If yes then the user is allowed to continue otherwise the user is + * allowed to modify the entered PIN 1. + */ + private fun checkEnteredPin() { + val enteredPin = binding!!.pinTextField.editText?.text.toString() + if (enteredPin.length in 4..12) { + viewModel.setUserPin(enteredPin) + if (args.saving) { + viewModel.storePin(requireContext()) + goToTheStart() + } else { + if (saveToggle) { + viewModel.storePin(requireContext()) + } + goToTheNextFragment() + } + } else { + Toast.makeText(requireContext(), getString(R.string.length_pin), Toast.LENGTH_SHORT) + .show() + } + } + override fun onDestroy() { super.onDestroy() binding = null diff --git a/MobileAuthApp/app/src/main/res/layout/fragment_can.xml b/MobileAuthApp/app/src/main/res/layout/fragment_can.xml index f7bcd1e..1fe0ebd 100644 --- a/MobileAuthApp/app/src/main/res/layout/fragment_can.xml +++ b/MobileAuthApp/app/src/main/res/layout/fragment_can.xml @@ -4,82 +4,57 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:padding="24dp" - tools:context=".CanFragment"> + android:padding="@dimen/padding" + tools:context=".MainActivity"> - + app:layout_constraintTop_toTopOf="parent"/> - + + + android:textSize="@dimen/regular_text" + android:fontFamily="sans-serif" + android:inputType="number" + android:singleLine="true" + /> - - - - - - - - - - - +