Skip to content

Commit

Permalink
Feat: double scan
Browse files Browse the repository at this point in the history
Co-authored-by: Kaizen <[email protected]>
  • Loading branch information
nicola-95 and Kaizen-7 authored Mar 2, 2022
1 parent 626178a commit 9ff0b8b
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ class FirstActivity : AppCompatActivity(), View.OnClickListener,
if (Utility.versionCompare(
it,
BuildConfig.VERSION_NAME
) > 0 || viewModel.isSDKVersionObsoleted()
) > 0 || viewModel.isSDKVersionObsolete()
) {
createForceUpdateDialog()
}
Expand Down Expand Up @@ -578,7 +578,7 @@ class FirstActivity : AppCompatActivity(), View.OnClickListener,
}

override fun onBackPressed() {
moveTaskToBack(false)
moveTaskToBack(true)
}

override fun onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.activity.addCallback
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
Expand All @@ -46,6 +47,7 @@ import it.ministerodellasalute.verificaC19.R
import it.ministerodellasalute.verificaC19.databinding.FragmentCodeReaderBinding
import it.ministerodellasalute.verificaC19sdk.model.ScanMode
import it.ministerodellasalute.verificaC19sdk.model.VerificationViewModel
import java.util.*

@AndroidEntryPoint
class CodeReaderFragment : Fragment(), NavController.OnDestinationChangedListener,
Expand Down Expand Up @@ -99,7 +101,7 @@ class CodeReaderFragment : Fragment(), NavController.OnDestinationChangedListene
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val hintsMap: MutableMap<DecodeHintType, Any> = HashMap()
val hintsMap: MutableMap<DecodeHintType, Any> = EnumMap(com.google.zxing.DecodeHintType::class.java)
val formats: Collection<BarcodeFormat> = listOf(BarcodeFormat.QR_CODE, BarcodeFormat.AZTEC)
hintsMap[DecodeHintType.TRY_HARDER] = false
binding.barcodeScanner.barcodeView.decoderFactory = DefaultDecoderFactory(formats, hintsMap, null, 0)
Expand All @@ -112,6 +114,7 @@ class CodeReaderFragment : Fragment(), NavController.OnDestinationChangedListene
ScanMode.SCHOOL -> getString(R.string.scan_mode_school_header)
ScanMode.WORK -> getString(R.string.scan_mode_work_header)
ScanMode.ENTRY_ITALY -> getString(R.string.scan_mode_entry_italy_header)
ScanMode.DOUBLE_SCAN -> getString(R.string.scan_mode_booster_header)
}
binding.scanModeText.text = chosenScanMode
binding.closeButton.setOnClickListener(this)
Expand Down Expand Up @@ -144,6 +147,12 @@ class CodeReaderFragment : Fragment(), NavController.OnDestinationChangedListene
beepManager = BeepManager(requireActivity())

binding.flipCamera.setOnClickListener(this)

activity?.onBackPressedDispatcher?.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
callOnBack()
}
})
}

override fun onDestroyView() {
Expand Down Expand Up @@ -185,8 +194,6 @@ class CodeReaderFragment : Fragment(), NavController.OnDestinationChangedListene

override fun onClick(v: View?) {
when (v?.id) {
R.id.back_image -> requireActivity().finish()
R.id.back_text -> requireActivity().finish()
R.id.flip_camera -> {
binding.barcodeScanner.pause()
binding.barcodeScanner.cameraSettings.requestedCameraId *= -1
Expand All @@ -211,10 +218,18 @@ class CodeReaderFragment : Fragment(), NavController.OnDestinationChangedListene
binding.barcodeScanner.setTorchOn()
}
}
R.id.close_button -> activity?.onBackPressed()
R.id.close_button -> callOnBack()
}
}

private fun callOnBack() {
if (viewModel.getDoubleScanFlag()) {
viewModel.setDoubleScanFlag(false)
findNavController().popBackStack()
} else {
findNavController().navigate(R.id.action_codeReaderFragment_to_firstActivity)
}
}

private fun hasFlash(): Boolean {
return requireActivity().packageManager
Expand All @@ -228,4 +243,5 @@ class CodeReaderFragment : Fragment(), NavController.OnDestinationChangedListene
override fun onTorchOff() {
torchOn = false
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,26 @@ package it.ministerodellasalute.verificaC19.ui.main.verification

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AlertDialog
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import dagger.hilt.android.AndroidEntryPoint
import it.ministerodellasalute.verificaC19.R
import it.ministerodellasalute.verificaC19.databinding.DoubleScanResultBinding
import it.ministerodellasalute.verificaC19.databinding.FragmentVerificationBinding
import it.ministerodellasalute.verificaC19.ui.base.isDebug
import it.ministerodellasalute.verificaC19.ui.compounds.QuestionCompound
Expand All @@ -59,6 +64,9 @@ class VerificationFragment : Fragment(), View.OnClickListener {
private val binding get() = _binding!!
private lateinit var certificateModel: CertificateViewBean

private var userName: String = ""
private var callback: OnBackPressedCallback? = null

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand All @@ -70,22 +78,29 @@ class VerificationFragment : Fragment(), View.OnClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.closeButton.setOnClickListener(this)
binding.noTestAvailableButton.setOnClickListener(this)
viewModel.certificate.observe(viewLifecycleOwner) { certificate ->
certificate?.let {
certificateModel = it

callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
findNavController().popBackStack()
}
}

setOnBackPressed(callback)
if (
viewModel.getTotemMode() &&
(certificate.certificateStatus == CertificateStatus.VALID) &&
!viewModel.getDoubleScanFlag()
) setOnBackTimer()
setPersonData(it.person, it.dateOfBirth)
binding.closeButton.visibility = View.VISIBLE
setupCertStatusView(it)
setupTimeStamp(it)
binding.closeButton.visibility = View.VISIBLE

binding.validationDate.visibility = View.VISIBLE
if (
viewModel.getTotemMode() &&
(certificate.certificateStatus == CertificateStatus.VALID)
) {
Handler().postDelayed({
activity?.onBackPressed()
}, 5000)
}
}
}
viewModel.inProgress.observe(viewLifecycleOwner) {
Expand All @@ -107,14 +122,89 @@ class VerificationFragment : Fragment(), View.OnClickListener {

private fun setupCertStatusView(cert: CertificateViewBean) {
cert.certificateStatus?.let {
setBackgroundColor(it)
setPersonDetailsVisibility(it)
setValidationIcon(it)
setValidationMainText(it)
setValidationSubTextVisibility(it)
setValidationSubText(it)
setLinkViews(it)
setDoubleScanButtons(it)
setScanModeText()

if (viewModel.getDoubleScanFlag() && it != CertificateStatus.NOT_EU_DCC) {
binding.doubleScanResultsContainer.visibility = View.VISIBLE

addDoubleScanResult(R.drawable.ic_valid_cert, R.string.certificateValid)

if (it.isANonValidCertificate()) {
addDoubleScanResult(R.drawable.ic_invalid, R.string.certificateTestNotValid)
setValidationLayout(CertificateStatus.NOT_VALID)
viewModel.setDoubleScanFlag(false)
} else if (it == CertificateStatus.VALID) {
addDoubleScanResult(R.drawable.ic_valid_cert, R.string.certificateTestValid)
viewModel.setDoubleScanFlag(false)
if (viewModel.getUserName() != userName) {
addDoubleScanResult(R.drawable.ic_invalid, R.string.userDataDoesNotMatch)
setValidationLayout(CertificateStatus.NOT_VALID)
} else {
setValidationLayout(CertificateStatus.VALID)
if (viewModel.getTotemMode()) setOnBackTimer()
}
}
viewModel.setUserName("")
} else {
setValidationLayout(it)
}
}
}

private fun setOnBackTimer() {
Handler(Looper.getMainLooper()).postDelayed({
findNavController().navigate(R.id.action_verificationFragment_to_codeReaderFragment)
}, 5000)
}

private fun addDoubleScanResult(icon: Int, text: Int) {
val container = binding.doubleScanResultsContainer
val binding = DoubleScanResultBinding.inflate(layoutInflater, container, false)
binding.validationText.text = getString(text)
binding.validationIcon.setImageResource(icon)
container.addView(binding.root)
}

private fun setValidationLayout(it: CertificateStatus) {
setBackgroundColor(it)
setValidationIcon(it)
setValidationMainText(it)
setValidationSubTextVisibility(it)
setValidationSubText(it)
setLinkViews(it)
}

private fun setDoubleScanButtons(status: CertificateStatus) {
if (status == CertificateStatus.TEST_NEEDED && !viewModel.getDoubleScanFlag()) {
binding.scanTestButton.visibility = View.VISIBLE
binding.noTestAvailableButton.visibility = View.VISIBLE
binding.closeButton.visibility = View.GONE

binding.subtitleText.updateLayoutParams<ConstraintLayout.LayoutParams> {
topToBottom = binding.noTestAvailableButton.id
bottomMargin = 64
}

binding.scanTestButton.setOnClickListener {
viewModel.setDoubleScanFlag(true)
it.findNavController().navigate(R.id.action_verificationFragment_to_codeReaderFragment)
}

val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {}
}
setOnBackPressed(callback)

} else if (viewModel.getDoubleScanFlag()) {
binding.scanTestButton.visibility = View.GONE
binding.noTestAvailableButton.visibility = View.GONE

binding.questionContainer.updateLayoutParams<ConstraintLayout.LayoutParams> {
topToBottom = binding.doubleScanResultsContainer.id
topMargin = 32
}
}
}

Expand All @@ -126,6 +216,7 @@ class VerificationFragment : Fragment(), View.OnClickListener {
ScanMode.SCHOOL -> getString(R.string.scan_mode_school_header)
ScanMode.WORK -> getString(R.string.scan_mode_work_header)
ScanMode.ENTRY_ITALY -> getString(R.string.scan_mode_entry_italy_header)
ScanMode.DOUBLE_SCAN -> getString(R.string.scan_mode_booster_header)
}
binding.scanModeText.text = chosenScanMode
}
Expand Down Expand Up @@ -164,7 +255,7 @@ class VerificationFragment : Fragment(), View.OnClickListener {
CertificateStatus.NOT_EU_DCC -> {
binding.questionContainer.updateLayoutParams<ConstraintLayout.LayoutParams> {
bottomToTop = binding.validationDate.id
bottomMargin = 64
bottomMargin = 32
}
View.GONE
}
Expand All @@ -175,8 +266,8 @@ class VerificationFragment : Fragment(), View.OnClickListener {
private fun setValidationSubText(certStatus: CertificateStatus) {
binding.subtitleText.text =
when (certStatus) {
CertificateStatus.VALID, CertificateStatus.TEST_NEEDED -> getString(R.string.subtitle_text)
CertificateStatus.NOT_VALID, CertificateStatus.EXPIRED, CertificateStatus.NOT_VALID_YET -> getString(R.string.subtitle_text_notvalid)
CertificateStatus.VALID -> getString(R.string.subtitle_text)
CertificateStatus.TEST_NEEDED, CertificateStatus.NOT_VALID, CertificateStatus.EXPIRED, CertificateStatus.NOT_VALID_YET -> getString(R.string.subtitle_text_notvalid)
else -> getString(R.string.subtitle_text_technicalError)
}
}
Expand Down Expand Up @@ -236,11 +327,39 @@ class VerificationFragment : Fragment(), View.OnClickListener {
binding.nameStandardisedText.text = person?.familyName.plus(" ").plus(person?.givenName)
}
binding.birthdateText.text = dateOfBirth?.formatDateOfBirth().orEmpty()

userName = binding.nameStandardisedText.text.toString()

if (certificateModel.certificateStatus == CertificateStatus.TEST_NEEDED && !viewModel.getDoubleScanFlag()) {
viewModel.setUserName(userName)
}
}

override fun onClick(v: View?) {
when (v?.id) {
R.id.close_button -> findNavController().popBackStack()
R.id.no_test_available_button -> {
viewModel.setDoubleScanFlag(false)

setValidationLayout(CertificateStatus.NOT_VALID)

binding.closeButton.visibility = View.VISIBLE
binding.scanTestButton.visibility = View.GONE
binding.noTestAvailableButton.visibility = View.GONE

val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
findNavController().popBackStack()
}
}
setOnBackPressed(callback)
}
}
}

private fun setOnBackPressed(callback: OnBackPressedCallback?) {
callback?.let {
activity?.onBackPressedDispatcher?.addCallback(viewLifecycleOwner, callback)
}
}

Expand All @@ -262,4 +381,8 @@ class VerificationFragment : Fragment(), View.OnClickListener {
dialog.show()
}

override fun onDestroy() {
viewModel.setDoubleScanFlag(false)
super.onDestroy()
}
}
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_first.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
android:textAlignment="textStart"
android:textColor="@color/blue_dark"
android:textSize="15sp"

app:layout_constraintBottom_toTopOf="@id/qrButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
Loading

0 comments on commit 9ff0b8b

Please sign in to comment.