2021-09-26 14:42:38 +03:00
|
|
|
package com.tarkvaraprojekt.mobileauthapp
|
|
|
|
|
2021-10-15 22:12:06 +03:00
|
|
|
import android.app.AlertDialog
|
|
|
|
import android.content.DialogInterface
|
2021-09-26 14:42:38 +03:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2021-10-03 15:45:28 +03:00
|
|
|
import android.widget.Toast
|
2021-09-26 14:42:38 +03:00
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.fragment.app.activityViewModels
|
|
|
|
import androidx.navigation.fragment.findNavController
|
2021-10-15 18:23:21 +03:00
|
|
|
import androidx.navigation.fragment.navArgs
|
2021-09-26 14:42:38 +03:00
|
|
|
import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentCanBinding
|
|
|
|
import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fragment that deals with asking the user for six digit CAN
|
|
|
|
*/
|
|
|
|
class CanFragment : Fragment() {
|
|
|
|
|
|
|
|
private val viewModel: SmartCardViewModel by activityViewModels()
|
|
|
|
|
|
|
|
private var binding: FragmentCanBinding? = null
|
|
|
|
|
2021-10-15 18:23:21 +03:00
|
|
|
// Navigation arguments. saving = true means that we are navigating here from the settings menu and must return to the settings
|
|
|
|
private val args: CanFragmentArgs by navArgs()
|
|
|
|
|
2021-09-26 14:42:38 +03:00
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
|
|
|
binding = FragmentCanBinding.inflate(inflater, container, false)
|
|
|
|
return binding!!.root
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2021-10-15 22:12:06 +03:00
|
|
|
if (viewModel.userCan.length == 6) {
|
|
|
|
skip()
|
|
|
|
}
|
2021-10-15 18:23:21 +03:00
|
|
|
if (args.saving) {
|
|
|
|
binding!!.nextButton.text = getString(R.string.save_text)
|
|
|
|
}
|
2021-09-26 14:42:38 +03:00
|
|
|
binding!!.nextButton.setOnClickListener { goToNextFragment() }
|
|
|
|
binding!!.cancelButton.setOnClickListener { goToTheStart() }
|
|
|
|
}
|
|
|
|
|
2021-10-15 22:12:06 +03:00
|
|
|
// If CAN is already set
|
|
|
|
private fun skip() {
|
2021-10-16 11:02:48 +03:00
|
|
|
findNavController().navigate(R.id.action_canFragment_to_pinFragment)
|
2021-10-15 22:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Might need some rework, must break it up and make logic better.
|
2021-09-26 14:42:38 +03:00
|
|
|
private fun goToNextFragment() {
|
2021-10-03 15:45:28 +03:00
|
|
|
val enteredCan = binding!!.canEditText.editText?.text.toString()
|
|
|
|
if (enteredCan.length != 6) {
|
|
|
|
Toast.makeText(requireContext(), getString(R.string.length_can), Toast.LENGTH_SHORT)
|
|
|
|
.show()
|
|
|
|
} else {
|
|
|
|
viewModel.setUserCan(
|
|
|
|
binding!!.canEditText.editText?.text.toString()
|
|
|
|
)
|
2021-10-15 18:23:21 +03:00
|
|
|
if (args.saving) {
|
2021-10-15 22:12:06 +03:00
|
|
|
viewModel.storeCan(requireContext())
|
2021-10-15 18:23:21 +03:00
|
|
|
findNavController().navigate(R.id.action_canFragment_to_settingsFragment)
|
|
|
|
} else {
|
2021-10-15 22:12:06 +03:00
|
|
|
val canStoreQuestion: AlertDialog? = activity?.let { frag ->
|
|
|
|
val builder = AlertDialog.Builder(frag)
|
|
|
|
builder.apply {
|
|
|
|
setPositiveButton(R.string.save_text) { _, _ ->
|
|
|
|
viewModel.storeCan(
|
|
|
|
requireContext()
|
|
|
|
)
|
2021-10-16 11:02:48 +03:00
|
|
|
findNavController().navigate(R.id.action_canFragment_to_pinFragment)
|
2021-10-15 22:12:06 +03:00
|
|
|
}
|
|
|
|
setNegativeButton(R.string.deny_text) { _, _ ->
|
2021-10-16 11:02:48 +03:00
|
|
|
findNavController().navigate(R.id.action_canFragment_to_pinFragment)
|
2021-10-15 22:12:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
builder.setMessage(R.string.can_save_request)
|
|
|
|
builder.setTitle(R.string.save_can_title)
|
|
|
|
builder.create()
|
|
|
|
}
|
|
|
|
canStoreQuestion?.show()
|
2021-10-15 18:23:21 +03:00
|
|
|
}
|
2021-10-03 15:45:28 +03:00
|
|
|
}
|
2021-09-26 14:42:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun goToTheStart() {
|
2021-10-15 18:23:21 +03:00
|
|
|
if (args.saving) {
|
|
|
|
findNavController().navigate(R.id.action_canFragment_to_settingsFragment)
|
|
|
|
} else {
|
|
|
|
viewModel.clearUserInfo()
|
|
|
|
findNavController().navigate(R.id.action_canFragment_to_homeFragment)
|
|
|
|
}
|
2021-09-26 14:42:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
binding = null
|
|
|
|
}
|
|
|
|
}
|