2021-09-25 16:28:37 +03:00
|
|
|
package com.tarkvaraprojekt.mobileauthapp
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.fragment.app.Fragment
|
2021-09-26 14:42:38 +03:00
|
|
|
import androidx.fragment.app.activityViewModels
|
|
|
|
import androidx.navigation.fragment.findNavController
|
2021-09-25 16:28:37 +03:00
|
|
|
import com.tarkvaraprojekt.mobileauthapp.databinding.FragmentHomeBinding
|
2021-09-26 14:42:38 +03:00
|
|
|
import com.tarkvaraprojekt.mobileauthapp.model.SmartCardViewModel
|
2021-09-25 16:28:37 +03:00
|
|
|
|
2021-10-16 13:20:55 +03:00
|
|
|
/**
|
|
|
|
* HomeFragment is only shown to the user when then the user launches the application. When the application
|
|
|
|
* is launched by another application or a website then this Fragment will be skipped.
|
|
|
|
* This fragment uses the fields from the MainActivity by casting the activity to MainActivity.
|
|
|
|
* This might not be the best practice, but the application uses a single activity design so it should
|
|
|
|
* always work.
|
|
|
|
*/
|
2021-09-25 16:28:37 +03:00
|
|
|
class HomeFragment : Fragment() {
|
|
|
|
|
2021-09-26 14:42:38 +03:00
|
|
|
private val viewModel: SmartCardViewModel by activityViewModels()
|
|
|
|
|
2021-09-25 16:28:37 +03:00
|
|
|
private var binding: FragmentHomeBinding? = null
|
|
|
|
|
|
|
|
override fun onCreateView(
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
container: ViewGroup?,
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
): View? {
|
|
|
|
binding = FragmentHomeBinding.inflate(inflater, container, false)
|
2021-10-16 13:20:55 +03:00
|
|
|
// Making settings menu active again
|
|
|
|
(activity as MainActivity).menuAvailable = true
|
2021-09-25 16:28:37 +03:00
|
|
|
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
|
|
|
initialChecks()
|
2021-10-21 21:22:11 +03:00
|
|
|
// TODO: If app launched with intent then go to the CanFragment immediately.
|
|
|
|
if (true){ // Currently true for testing purposes
|
|
|
|
goToTheNextFragment(true)
|
|
|
|
}
|
2021-10-16 13:20:55 +03:00
|
|
|
binding!!.beginButton.setOnClickListener { goToTheNextFragment() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method where all the initial checks that should be done before any user input is accepted should be added.
|
|
|
|
*/
|
|
|
|
private fun initialChecks() {
|
2021-10-15 22:12:06 +03:00
|
|
|
viewModel.checkCan(requireContext())
|
2021-10-16 11:02:48 +03:00
|
|
|
viewModel.checkPin(requireContext())
|
2021-10-16 13:20:55 +03:00
|
|
|
displayStates()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts the process of interacting with the ID card by sending user to the CAN fragment.
|
|
|
|
*/
|
2021-10-21 21:22:11 +03:00
|
|
|
private fun goToTheNextFragment(auth: Boolean = false) {
|
2021-10-16 13:20:55 +03:00
|
|
|
// Making settings menu inactive
|
|
|
|
(activity as MainActivity).menuAvailable = false
|
|
|
|
// Currently saving is true because the application is not yet integrated with
|
|
|
|
// other applications or websites.
|
2021-10-21 21:22:11 +03:00
|
|
|
// TODO: Check the navigation action default values. Not everything has to be declared implicitly.
|
|
|
|
if (auth) {
|
|
|
|
val action = HomeFragmentDirections.actionHomeFragmentToCanFragment(reading = false, auth = true)
|
|
|
|
findNavController().navigate(action)
|
|
|
|
} else {
|
|
|
|
val action = HomeFragmentDirections.actionHomeFragmentToCanFragment(reading = true, auth = false)
|
|
|
|
findNavController().navigate(action)
|
|
|
|
}
|
2021-10-16 13:20:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays texts that inform the user whether the CAN and PIN 1 are saved on the device or not.
|
|
|
|
* This might help the user to save some time as checking menu is not necessary unless the user
|
|
|
|
* wishes to make changes to the saved CAN or PIN 1.
|
|
|
|
*/
|
|
|
|
private fun displayStates() {
|
|
|
|
canState()
|
|
|
|
pinState()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks the state of the CAN, saved or not saved. Updates the text and logo.
|
|
|
|
*/
|
|
|
|
private fun canState() {
|
|
|
|
if (viewModel.userCan.length == 6) {
|
|
|
|
binding!!.canStatusText.text = getString(R.string.can_status_saved)
|
|
|
|
binding!!.canStatusLogo.setImageResource(R.drawable.ic_check_logo)
|
|
|
|
} else {
|
|
|
|
binding!!.canStatusText.text = getString(R.string.can_status_negative)
|
|
|
|
binding!!.canStatusLogo.setImageResource(R.drawable.ic_info_logo)
|
|
|
|
}
|
2021-09-26 14:42:38 +03:00
|
|
|
}
|
|
|
|
|
2021-10-16 13:20:55 +03:00
|
|
|
/**
|
|
|
|
* Checks the state of the PIN 1, saved or not saved. Updates the text and logo.
|
|
|
|
*/
|
|
|
|
private fun pinState() {
|
|
|
|
if (viewModel.userPin.length in 4..12) {
|
|
|
|
binding!!.pinStatusText.text = getString(R.string.pin_status_saved)
|
|
|
|
binding!!.pinStatusLogo.setImageResource(R.drawable.ic_check_logo)
|
|
|
|
} else {
|
|
|
|
binding!!.pinStatusText.text = getString(R.string.pin_status_negative)
|
|
|
|
binding!!.pinStatusLogo.setImageResource(R.drawable.ic_info_logo)
|
|
|
|
}
|
2021-09-26 14:42:38 +03:00
|
|
|
}
|
|
|
|
|
2021-09-25 16:28:37 +03:00
|
|
|
override fun onDestroyView() {
|
|
|
|
super.onDestroyView()
|
|
|
|
binding = null
|
|
|
|
}
|
|
|
|
}
|