Skip to content

Commit

Permalink
Merge pull request #802 from minvws/2395-scan-instructions-deeplink
Browse files Browse the repository at this point in the history
refactor deeplink handling introduction or scan instructions not fini…
  • Loading branch information
aiden-shi authored Oct 28, 2021
2 parents de4f510 + f95c1db commit 577d0b5
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import nl.rijksoverheid.ctr.shared.MobileCoreWrapper
import nl.rijksoverheid.ctr.shared.livedata.EventObserver
import nl.rijksoverheid.ctr.shared.utils.IntentUtil
import nl.rijksoverheid.ctr.verifier.databinding.ActivityMainBinding
import nl.rijksoverheid.ctr.verifier.ui.scanqr.ScanQrViewModel
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel

Expand All @@ -32,13 +31,13 @@ class VerifierMainActivity : AppCompatActivity() {

private val introductionViewModel: IntroductionViewModel by viewModel()
private val appConfigViewModel: AppConfigViewModel by viewModel()
private val scanQrViewModel: ScanQrViewModel by viewModel()
private val mobileCoreWrapper: MobileCoreWrapper by inject()
private val dialogUtil: DialogUtil by inject()
private val intentUtil: IntentUtil by inject()

private var isFreshStart: Boolean = true // track if this is a fresh start of the app
private var returnUri: String? = null // return uri to external app given as argument from deeplink

var returnUri: String? = null // return uri to external app given as argument from deeplink
private var hasHandledDeeplink: Boolean = false

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -80,11 +79,11 @@ class VerifierMainActivity : AppCompatActivity() {
})

navController.addOnDestinationChangedListener { _, destination, arguments ->
// Persist deeplink return uri in case it's not used immediately because of onboarding
if (destination.id == R.id.nav_main) {
// Persist deeplink return uri in case it's not used immediately because of onboarding
arguments?.getString("returnUri")?.let { returnUri = it }
navigateDeeplink(navController)
}
navigateDeeplink(navController)

// verifier can stay active for a long time, so it is not sufficient
// to try to refresh the config only every time the app resumes.
Expand All @@ -99,16 +98,9 @@ class VerifierMainActivity : AppCompatActivity() {

private fun navigateDeeplink(navController: NavController) {
if (returnUri != null && !hasHandledDeeplink && isIntroductionFinished()) {
hasHandledDeeplink = true
navController.navigate(
// Show scan instructions before scanner when it's not shown before
if (scanQrViewModel.hasSeenScanInstructions()) {
RootNavDirections.actionScanner(returnUri)
} else {
RootNavDirections.actionScanInstructions(returnUri)
}
)
navController.navigate(RootNavDirections.actionScanner())
}
hasHandledDeeplink = true
}

private fun isIntroductionFinished() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class ScanInstructionsFragment : Fragment(R.layout.fragment_scan_instructions) {
private var _binding: FragmentScanInstructionsBinding? = null
private val binding get() = _binding!!

private val args: ScanInstructionsFragmentArgs by navArgs()

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

Expand Down Expand Up @@ -77,7 +75,7 @@ class ScanInstructionsFragment : Fragment(R.layout.fragment_scan_instructions) {
// Instructions have been opened, set as seen
scanQrViewModel.setScanInstructionsSeen()
findNavController().popBackStack(R.id.nav_scan_qr, false)
scannerUtil.launchScanner(requireActivity(), args.returnUri)
scannerUtil.launchScanner(requireActivity())
} else {
binding.viewPager.currentItem = currentItem + 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ class ScanResultInvalidFragment : Fragment(R.layout.fragment_scan_result_invalid
private fun navigateToScanner() {
navigateSafety(
R.id.nav_scan_result_invalid,
ScanResultInvalidFragmentDirections.actionNavQrScanner(
returnUri = (args.invalidData as? ScanResultInvalidData.Error)?.returnUri
)
ScanResultInvalidFragmentDirections.actionNavQrScanner()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import nl.rijksoverheid.ctr.qrscanner.QrCodeScannerFragment
import nl.rijksoverheid.ctr.shared.livedata.EventObserver
import nl.rijksoverheid.ctr.verifier.BuildConfig
import nl.rijksoverheid.ctr.verifier.R
import nl.rijksoverheid.ctr.verifier.VerifierMainActivity
import nl.rijksoverheid.ctr.verifier.ui.scanner.models.ScanResultInvalidData
import nl.rijksoverheid.ctr.verifier.ui.scanner.models.ScanResultValidData
import nl.rijksoverheid.ctr.verifier.ui.scanner.models.VerifiedQrResultState
Expand All @@ -28,12 +29,14 @@ class VerifierQrScannerFragment : QrCodeScannerFragment() {

private val scannerViewModel: ScannerViewModel by viewModel()
private val dialogUtil: DialogUtil by inject()
private val args: VerifierQrScannerFragmentArgs by navArgs()

private val returnUri: String?
get() = (activity as? VerifierMainActivity)?.returnUri

override fun onQrScanned(content: String) {
scannerViewModel.validate(
qrContent = content,
returnUri = args.returnUri
returnUri = returnUri
)
}

Expand Down Expand Up @@ -97,8 +100,7 @@ class VerifierQrScannerFragment : QrCodeScannerFragment() {
findNavController().navigate(
VerifierQrScannerFragmentDirections.actionScanResultInvalid(
invalidData = ScanResultInvalidData.Error(
error = qrResultState.error,
returnUri = args.returnUri
error = qrResultState.error
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ sealed class ScanResultInvalidData : Parcelable {
data class Invalid(val verifiedQr: VerificationResult) : ScanResultInvalidData(), Parcelable

@Parcelize
data class Error(val error: String, val returnUri: String?) :
data class Error(val error: String) :
ScanResultInvalidData(), Parcelable
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import nl.rijksoverheid.ctr.verifier.RootNavDirections
*
*/
interface ScannerUtil {
fun launchScanner(activity: Activity, returnUri: String? = null)
fun launchScanner(activity: Activity)
}

class ScannerUtilImpl : ScannerUtil {

override fun launchScanner(activity: Activity, returnUri: String?) {
override fun launchScanner(activity: Activity) {
Navigation.findNavController(activity, R.id.main_nav_host_fragment)
.navigate(RootNavDirections.actionScanner(returnUri))
.navigate(RootNavDirections.actionScanner())
}
}
9 changes: 7 additions & 2 deletions verifier/src/main/res/navigation/verifier_nav_graph_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<action
android:id="@+id/action_scan_instructions"
app:destination="@id/scan_instructions_nav"
app:destination="@id/nav_scan_instructions"
app:enterAnim="@anim/fragment_open_enter"
app:exitAnim="@anim/fragment_open_exit"
app:popEnterAnim="@anim/fragment_close_enter"
Expand All @@ -40,6 +40,11 @@

<include app:graph="@navigation/verifier_nav_graph_scanner" />

<include app:graph="@navigation/verifier_nav_graph_scan_instructions" />
<fragment
android:id="@+id/nav_scan_instructions"
android:name="nl.rijksoverheid.ctr.verifier.ui.instructions.ScanInstructionsFragment"
android:label="@string/scan_instructions"
tools:layout="@layout/fragment_scan_instructions">
</fragment>

</navigation>
27 changes: 1 addition & 26 deletions verifier/src/main/res/navigation/verifier_nav_graph_root.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,8 @@
app:exitAnim="@anim/fragment_open_exit"
app:popEnterAnim="@anim/fragment_close_enter"
app:popExitAnim="@anim/fragment_close_exit"
app:popUpTo="@id/nav_scanner">

<argument
android:name="returnUri"
android:defaultValue="@null"
app:argType="string"
app:nullable="true" />
</action>

<action
android:id="@+id/action_scan_instructions"
app:destination="@id/scan_instructions_nav"
app:enterAnim="@anim/fragment_open_enter"
app:exitAnim="@anim/fragment_open_exit"
app:popEnterAnim="@anim/fragment_close_enter"
app:popExitAnim="@anim/fragment_close_exit"
app:popUpTo="@id/root_nav"
app:popUpToInclusive="true">

<argument
android:name="returnUri"
android:defaultValue="@null"
app:argType="string"
app:nullable="true" />
</action>
app:popUpTo="@id/nav_scanner" />

<include app:graph="@navigation/verifier_nav_graph_scanner" />
<include app:graph="@navigation/verifier_nav_graph_scan_instructions" />

</navigation>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
android:name="nl.rijksoverheid.ctr.verifier.ui.scanner.VerifierQrScannerFragment"
tools:layout="@layout/fragment_scanner">

<argument
android:name="returnUri"
android:defaultValue="@null"
app:argType="string"
app:nullable="true" />

<action
android:id="@+id/action_scan_result_invalid"
app:destination="@id/nav_scan_result_invalid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ScanResultInvalidFragmentTest : AutoCloseKoinTest() {

@Test
fun `Screen shows correct content`() {
launchScanResultInvalidFragment(data = ScanResultInvalidData.Error("invalid QR code", null))
launchScanResultInvalidFragment(data = ScanResultInvalidData.Error("invalid QR code"))
assertHasBackground(R.id.root, R.color.red)
scrollTo(R.id.title)
assertDisplayed(R.id.title, R.string.scan_result_invalid_title)
Expand All @@ -53,7 +53,7 @@ class ScanResultInvalidFragmentTest : AutoCloseKoinTest() {

@Test
fun `Invalid result on explanation button click opens explanation dialog`() {
launchScanResultInvalidFragment(data = ScanResultInvalidData.Error("invalid QR code", null))
launchScanResultInvalidFragment(data = ScanResultInvalidData.Error("invalid QR code"))
performActionOnView(withId(R.id.secondaryButton), click())
assertContains(R.string.scan_result_invalid_reason_title)
}
Expand Down

0 comments on commit 577d0b5

Please sign in to comment.