2021-10-15 18:23:21 +03:00
|
|
|
package com.tarkvaraprojekt.mobileauthapp.menu
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2021-10-16 11:02:48 +03:00
|
|
|
import android.widget.Button
|
2021-10-15 18:23:21 +03:00
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.fragment.app.activityViewModels
|
|
|
|
import androidx.navigation.fragment.findNavController
|
2021-11-25 16:13:35 +02:00
|
|
|
import com.tarkvaraprojekt.mobileauthapp.MainActivity
|
2021-10-15 18:23:21 +03:00
|
|
|
import com.tarkvaraprojekt.mobileauthapp.R
|
|
|
|
import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentSettingsBinding
|
|
|
|
import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
|
|
|
|
|
2021-10-16 11:02:48 +03:00
|
|
|
/**
|
|
|
|
* This fragment allows the user to save the CAN and the PIN 1 and also to delete them if necessary.
|
|
|
|
* Should only be accessible for the user from the HomeFragment and not during the
|
|
|
|
* authentication process itself.
|
|
|
|
*/
|
2021-10-15 18:23:21 +03:00
|
|
|
class SettingsFragment : Fragment() {
|
|
|
|
|
|
|
|
private val viewModel: SmartCardViewModel by activityViewModels()
|
|
|
|
|
|
|
|
private var binding: FragmentSettingsBinding? = null
|
|
|
|
|
2021-10-16 11:02:48 +03:00
|
|
|
private var showPin: Boolean = false
|
|
|
|
|
2021-10-15 18:23:21 +03:00
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
|
|
|
binding = FragmentSettingsBinding.inflate(inflater, container, false)
|
|
|
|
return binding!!.root
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2021-10-16 11:02:48 +03:00
|
|
|
showCanField()
|
|
|
|
showPinField()
|
|
|
|
togglePinButton()
|
|
|
|
binding!!.canMenuAction.setOnClickListener { canAction() }
|
|
|
|
binding!!.pinMenuAction.setOnClickListener { pinAction() }
|
|
|
|
binding!!.pinMenuShow.setOnClickListener { togglePin() }
|
2021-10-16 13:20:55 +03:00
|
|
|
binding!!.returnButton.setOnClickListener { backToHome() }
|
2021-10-16 11:02:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method for showing the CAN field to the user and can be used to refresh the field as well.
|
|
|
|
*/
|
|
|
|
private fun showCanField() {
|
2021-10-15 18:23:21 +03:00
|
|
|
if (viewModel.userCan.length == 6) {
|
|
|
|
binding!!.canSaved.text = getString(R.string.saved_can, viewModel.userCan)
|
|
|
|
binding!!.canMenuAction.text = getString(R.string.can_delete)
|
|
|
|
} else {
|
2021-10-16 09:47:57 +03:00
|
|
|
binding!!.canSaved.text = getString(R.string.saved_can, getString(R.string.missing))
|
2021-10-15 18:23:21 +03:00
|
|
|
binding!!.canMenuAction.text = getString(R.string.can_add)
|
|
|
|
}
|
2021-10-16 11:02:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method that allows the user to delete saved CAN from the device and also to save new a CAN if
|
|
|
|
* currently there is no CAN saved.
|
|
|
|
*/
|
|
|
|
private fun canAction() {
|
|
|
|
if (viewModel.userCan.length == 6) {
|
|
|
|
viewModel.deleteCan(requireContext())
|
|
|
|
showCanField()
|
|
|
|
} else {
|
2021-11-25 16:13:35 +02:00
|
|
|
(activity as MainActivity).inMenu = false
|
2021-10-16 13:20:55 +03:00
|
|
|
val action = SettingsFragmentDirections.actionSettingsFragmentToCanFragment(saving = true)
|
2021-10-16 11:02:48 +03:00
|
|
|
findNavController().navigate(action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method for showing the PIN 1 field to the user and can be used to refresh the field as well.
|
|
|
|
* The PIN 1 is hidden by default and when it is hidden it is always shown as **** despite the
|
|
|
|
* length of the PIN 1. Can be made visible with toggle button.
|
|
|
|
*/
|
|
|
|
private fun showPinField() {
|
2021-10-16 09:47:57 +03:00
|
|
|
if (viewModel.userPin.length in 4..12) {
|
2021-10-16 11:02:48 +03:00
|
|
|
binding!!.pinMenuShow.visibility = Button.VISIBLE
|
|
|
|
if (showPin)
|
|
|
|
binding!!.pinSaved.text = getString(R.string.saved_pin, viewModel.userPin)
|
|
|
|
else
|
|
|
|
binding!!.pinSaved.text = getString(R.string.saved_pin, getString(R.string.hidden_pin))
|
2021-10-16 09:47:57 +03:00
|
|
|
binding!!.pinMenuAction.text = getString(R.string.pin1_delete)
|
|
|
|
} else {
|
2021-10-16 11:02:48 +03:00
|
|
|
binding!!.pinMenuShow.visibility = Button.GONE
|
2021-10-16 09:47:57 +03:00
|
|
|
binding!!.pinSaved.text = getString(R.string.saved_pin, getString(R.string.missing))
|
|
|
|
binding!!.pinMenuAction.text = getString(R.string.pin1_add)
|
|
|
|
}
|
2021-10-16 11:02:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method that allows the user to delete saved PIN 1 from the device and also to save a new PIN 1 if
|
|
|
|
* currently there is no PIN 1 saved.
|
|
|
|
*/
|
|
|
|
private fun pinAction() {
|
|
|
|
if (viewModel.userPin.length in 4..12) {
|
|
|
|
viewModel.deletePin(requireContext())
|
|
|
|
showPinField()
|
|
|
|
} else {
|
2021-11-25 16:13:35 +02:00
|
|
|
(activity as MainActivity).inMenu = false
|
2021-10-16 13:20:55 +03:00
|
|
|
val action = SettingsFragmentDirections.actionSettingsFragmentToPinFragment(saving = true)
|
2021-10-16 11:02:48 +03:00
|
|
|
findNavController().navigate(action)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hides the PIN 1 or makes it visible.
|
|
|
|
*/
|
|
|
|
private fun togglePin() {
|
|
|
|
showPin = !showPin
|
|
|
|
togglePinButton()
|
|
|
|
showPinField()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the text on the button that controls the visiblity of the PIN 1.
|
|
|
|
*/
|
|
|
|
private fun togglePinButton() {
|
|
|
|
if (showPin) {
|
|
|
|
binding!!.pinMenuShow.text = getString(R.string.hide)
|
|
|
|
} else {
|
|
|
|
binding!!.pinMenuShow.text = getString(R.string.show)
|
2021-10-15 18:23:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 13:20:55 +03:00
|
|
|
/**
|
|
|
|
* Navigates back to home fragment.
|
|
|
|
*/
|
|
|
|
private fun backToHome() {
|
2021-11-25 16:13:35 +02:00
|
|
|
(activity as MainActivity).inMenu = false
|
2021-10-16 13:20:55 +03:00
|
|
|
findNavController().navigate(R.id.action_settingsFragment_to_homeFragment)
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:23:21 +03:00
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
binding = null
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|