2021-09-26 14:42:38 +03:00
package com.tarkvaraprojekt.mobileauthapp
2021-10-15 22:12:06 +03:00
import android.app.AlertDialog
2021-10-26 11:42:58 +03:00
import android.content.Intent
2021-09-26 14:42:38 +03:00
import android.os.Bundle
2021-11-28 16:24:08 +02:00
import android.util.TypedValue
2021-09-26 14:42:38 +03:00
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
2021-11-28 16:24:08 +02:00
import android.widget.TextView
2021-10-03 15:45:28 +03:00
import android.widget.Toast
2021-10-26 11:42:58 +03:00
import androidx.appcompat.app.AppCompatActivity
2021-11-24 20:44:40 +02:00
import androidx.core.widget.addTextChangedListener
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-11-28 16:24:08 +02:00
import com.google.android.material.snackbar.Snackbar
2021-09-26 14:42:38 +03:00
import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentCanBinding
import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
2021-11-28 16:24:08 +02:00
import org.w3c.dom.Text
2021-09-26 14:42:38 +03:00
/ * *
2021-10-16 13:20:55 +03:00
* Fragment that deals with asking the user for a six digit CAN . If the CAN is already saved
* then the fragment is skipped immediately and if the CAN is not saved then the user
* is asked whether it should be saved for the future or not before continuing .
2021-09-26 14:42:38 +03:00
* /
class CanFragment : Fragment ( ) {
private val viewModel : SmartCardViewModel by activityViewModels ( )
2021-12-04 17:21:07 +02:00
private var _binding : FragmentCanBinding ? = null
private val binding get ( ) = _binding !!
2021-09-26 14:42:38 +03:00
2021-10-16 13:20:55 +03:00
// Navigation arguments:
// saving = true means that we are navigating here from the settings menu and must return to the settings menu.
2021-10-15 18:23:21 +03:00
private val args : CanFragmentArgs by navArgs ( )
2021-09-26 14:42:38 +03:00
override fun onCreateView (
inflater : LayoutInflater ,
container : ViewGroup ? ,
savedInstanceState : Bundle ?
) : View ? {
2021-12-04 17:21:07 +02:00
_binding = FragmentCanBinding . inflate ( inflater , container , false )
return binding . root
2021-09-26 14:42:38 +03:00
}
override fun onViewCreated ( view : View , savedInstanceState : Bundle ? ) {
super . onViewCreated ( view , savedInstanceState )
2021-10-16 13:20:55 +03:00
checkIfSkip ( )
2021-12-04 17:21:07 +02:00
binding . canTextField . editText ?. addTextChangedListener {
2021-11-24 20:44:40 +02:00
checkEnteredCan ( )
2021-10-15 18:23:21 +03:00
}
2021-12-04 17:21:07 +02:00
binding . buttonCancel . setOnClickListener { goToTheStart ( ) }
2021-09-26 14:42:38 +03:00
}
2021-10-16 13:20:55 +03:00
/ * *
* Checks if the current fragment can be skipped or not .
* If the user has CAN saved on the device there is no need to ask it again .
* /
private fun checkIfSkip ( ) {
if ( viewModel . userCan . length == 6 ) {
goToTheNextFragment ( )
}
2021-10-15 22:12:06 +03:00
}
2021-10-16 13:20:55 +03:00
/ * *
* Takes user to the next fragment , which is PinFragment .
* /
private fun goToTheNextFragment ( ) {
2021-11-27 21:16:50 +02:00
val action = CanFragmentDirections . actionCanFragmentToPinFragment ( auth = args . auth , mobile = args . mobile )
2021-10-16 13:20:55 +03:00
findNavController ( ) . navigate ( action )
}
/ * *
* 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 .
* /
2021-09-26 14:42:38 +03:00
private fun goToTheStart ( ) {
2021-10-26 11:42:58 +03:00
if ( args . saving ) {
2021-11-27 21:16:50 +02:00
if ( args . fromhome ) {
findNavController ( ) . navigate ( R . id . action _canFragment _to _homeFragment )
} else {
findNavController ( ) . navigate ( R . id . action _canFragment _to _settingsFragment )
}
2021-11-14 10:13:40 +02:00
} else if ( args . auth || args . mobile ) {
if ( args . mobile ) {
val resultIntent = Intent ( )
requireActivity ( ) . setResult ( AppCompatActivity . RESULT _CANCELED , resultIntent )
requireActivity ( ) . finish ( )
} else {
2022-01-18 00:34:45 +02:00
( activity as MainActivity ) . returnError ( 444 )
2021-11-14 10:13:40 +02:00
requireActivity ( ) . finishAndRemoveTask ( )
}
2021-10-15 18:23:21 +03:00
} else {
findNavController ( ) . navigate ( R . id . action _canFragment _to _homeFragment )
}
2021-09-26 14:42:38 +03:00
}
2021-11-28 16:24:08 +02:00
/ * *
* Method that creates and shows a snackbar that tells the user that CAN has been saved
* /
private fun showSnackbar ( ) {
val snackbar = Snackbar . make ( requireView ( ) , R . string . can _status _saved , Snackbar . LENGTH _SHORT )
val snackbarText : TextView = snackbar . view . findViewById ( R . id . snackbar _text )
snackbarText . setTextSize ( TypedValue . COMPLEX _UNIT _SP , resources . getDimension ( R . dimen . small _text ) )
snackbar . show ( )
}
2021-11-24 20:44:40 +02:00
/ * *
* 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 ( ) {
2021-12-04 17:21:07 +02:00
val enteredCan = binding . canTextField . editText ?. text . toString ( )
2021-11-24 20:44:40 +02:00
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
2021-11-28 16:24:08 +02:00
showSnackbar ( )
2021-11-24 20:44:40 +02:00
if ( args . saving ) {
goToTheStart ( )
} else {
goToTheNextFragment ( )
}
}
}
2021-09-26 14:42:38 +03:00
override fun onDestroy ( ) {
super . onDestroy ( )
2021-12-04 17:21:07 +02:00
_binding = null
2021-09-26 14:42:38 +03:00
}
}