Skip to content

Commit

Permalink
AND-4730 [Welcome / Home] Improved handling of intents when the app i…
Browse files Browse the repository at this point in the history
…s launched
  • Loading branch information
iiiburnyiii committed Oct 4, 2023
1 parent 90748af commit 7376b8a
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 123 deletions.
24 changes: 10 additions & 14 deletions app/src/main/java/com/tangem/tap/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -365,25 +365,21 @@ class MainActivity : AppCompatActivity(), SnackbarHandler, ActivityResultCallbac

private fun navigateToInitialScreen(intentWhichStartedActivity: Intent?) {
if (store.state.globalState.userWalletsListManager?.hasUserWallets == true) {
store.dispatchOnMain(NavigationAction.NavigateTo(AppScreen.Welcome))
store.dispatchOnMain(WelcomeAction.SetInitialIntent(intentWhichStartedActivity))
lifecycleScope.launch {
val handler = BackgroundScanIntentHandler(
hasSavedUserWalletsProvider = { true },
lifecycleCoroutineScope = lifecycleScope,
)
val isBackgroundScanHandled = handler.handleIntent(intentWhichStartedActivity)
val hasNotIncompletedBackup = !backupService.hasIncompletedBackup
if (!isBackgroundScanHandled && hasNotIncompletedBackup) {
store.dispatchOnMain(WelcomeAction.ProceedWithBiometrics)
}
}
store.dispatch(
NavigationAction.NavigateTo(
screen = AppScreen.Welcome,
bundle = intentWhichStartedActivity?.let {
bundleOf(WelcomeFragment.INITIAL_INTENT_KEY to it)
},
),
)
} else {
store.dispatchOnMain(NavigationAction.NavigateTo(AppScreen.Home))
store.dispatch(NavigationAction.NavigateTo(AppScreen.Home))
lifecycleScope.launch {
intentProcessor.handleIntent(intentWhichStartedActivity)
}
}

store.dispatch(BackupAction.CheckForUnfinishedBackup)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class HomeFragment : Fragment(), StoreSubscriber<HomeState> {
Analytics.send(IntroductionProcess.ButtonScanCard())
lifecycleScope.launch {
store.dispatch(
HomeAction.ReadCard(lifecycleCoroutineScope = lifecycleScope),
HomeAction.ReadCard(scope = this),
)
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.tangem.tap.features.home.redux

import androidx.lifecycle.LifecycleCoroutineScope
import com.tangem.core.analytics.models.AnalyticsEvent
import com.tangem.tap.common.analytics.events.AnalyticsParam
import com.tangem.tap.common.analytics.events.Basic
import com.tangem.tap.common.entities.IndeterminateProgressButton
import kotlinx.coroutines.CoroutineScope
import org.rekotlin.Action

sealed class HomeAction : Action {
Expand All @@ -23,7 +23,7 @@ sealed class HomeAction : Action {
*/
data class ReadCard(
val analyticsEvent: AnalyticsEvent? = Basic.CardWasScanned(AnalyticsParam.ScannedFrom.Introduction),
val lifecycleCoroutineScope: LifecycleCoroutineScope,
val scope: CoroutineScope,
) : HomeAction()

data class ScanInProgress(val scanInProgress: Boolean) : HomeAction()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.tangem.tap.features.home.redux

import androidx.lifecycle.LifecycleCoroutineScope
import com.tangem.common.doOnFailure
import com.tangem.common.doOnResult
import com.tangem.common.doOnSuccess
Expand Down Expand Up @@ -63,7 +62,9 @@ private fun handleHomeAction(action: Action) {
store.dispatch(GlobalAction.FetchUserCountry)
}
is HomeAction.ReadCard -> {
readCard(action.analyticsEvent, action.lifecycleCoroutineScope)
action.scope.launch {
readCard(action.analyticsEvent)
}
}
is HomeAction.GoToShop -> {
Analytics.send(Shop.ScreenOpened())
Expand All @@ -78,34 +79,31 @@ private fun handleHomeAction(action: Action) {
}
}

private fun readCard(analyticsEvent: AnalyticsEvent?, lifecycleCoroutineScope: LifecycleCoroutineScope) {
lifecycleCoroutineScope.launch {
delay(timeMillis = 200)
store.state.daggerGraphState.get(DaggerGraphState::cardSdkConfigRepository).setAccessCodeRequestPolicy(
isBiometricsRequestPolicy = preferencesStorage.shouldSaveAccessCodes,
)
private suspend fun readCard(analyticsEvent: AnalyticsEvent?) {
store.state.daggerGraphState.get(DaggerGraphState::cardSdkConfigRepository).setAccessCodeRequestPolicy(
isBiometricsRequestPolicy = preferencesStorage.shouldSaveAccessCodes,
)

store.state.daggerGraphState.get(DaggerGraphState::scanCardProcessor).scan(
analyticsEvent = analyticsEvent,
onProgressStateChange = { showProgress ->
if (showProgress) {
changeButtonState(ButtonState.PROGRESS)
} else {
changeButtonState(ButtonState.ENABLED)
}
},
onScanStateChange = { scanInProgress ->
store.dispatch(HomeAction.ScanInProgress(scanInProgress))
},
onFailure = {
Timber.e(it, "Unable to scan card")
store.state.daggerGraphState.get(DaggerGraphState::scanCardProcessor).scan(
analyticsEvent = analyticsEvent,
onProgressStateChange = { showProgress ->
if (showProgress) {
changeButtonState(ButtonState.PROGRESS)
} else {
changeButtonState(ButtonState.ENABLED)
},
onSuccess = { scanResponse ->
proceedWithScanResponse(scanResponse)
},
)
}
}
},
onScanStateChange = { scanInProgress ->
store.dispatch(HomeAction.ScanInProgress(scanInProgress))
},
onFailure = {
Timber.e(it, "Unable to scan card")
changeButtonState(ButtonState.ENABLED)
},
onSuccess = { scanResponse ->
proceedWithScanResponse(scanResponse)
},
)
}

private fun proceedWithScanResponse(scanResponse: ScanResponse) = scope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ import android.content.Intent
* @author Anton Zhilenkov on 04.06.2023.
*/
interface IntentHandler {
suspend fun handleIntent(intent: Intent?): Boolean

fun handleIntent(intent: Intent?): Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import android.content.Intent
import android.nfc.NfcAdapter
import android.nfc.Tag
import android.os.Build
import androidx.lifecycle.LifecycleCoroutineScope
import com.tangem.tap.common.extensions.dispatchWithMain
import com.tangem.tap.common.extensions.dispatchOnMain
import com.tangem.tap.features.home.redux.HomeAction
import com.tangem.tap.features.intentHandler.IntentHandler
import com.tangem.tap.features.welcome.redux.WelcomeAction
import com.tangem.tap.store
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.CoroutineScope

/**
* @author Anton Zhilenkov on 04.06.2023.
*/
class BackgroundScanIntentHandler(
private val hasSavedUserWalletsProvider: () -> Boolean,
private val lifecycleCoroutineScope: LifecycleCoroutineScope,
private val scope: CoroutineScope,
) : IntentHandler {

private val nfcActions = arrayOf(
Expand All @@ -27,7 +25,7 @@ class BackgroundScanIntentHandler(
NfcAdapter.ACTION_TAG_DISCOVERED,
)

override suspend fun handleIntent(intent: Intent?): Boolean {
override fun handleIntent(intent: Intent?): Boolean {
if (intent == null || intent.action !in nfcActions) return false

val tag: Tag? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Expand All @@ -40,13 +38,9 @@ class BackgroundScanIntentHandler(

intent.action = null
if (hasSavedUserWalletsProvider.invoke()) {
// TODO: Remove delay after https://tangem.atlassian.net/browse/AND-2840
lifecycleCoroutineScope.launch {
delay(timeMillis = 200)
store.dispatchWithMain(WelcomeAction.ProceedWithCard(lifecycleCoroutineScope))
}
store.dispatchOnMain(WelcomeAction.ProceedWithCard)
} else {
store.dispatchWithMain(HomeAction.ReadCard(lifecycleCoroutineScope = lifecycleCoroutineScope))
store.dispatchOnMain(HomeAction.ReadCard(scope = scope))
}

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.tangem.tap.store
*/
class BuyCurrencyIntentHandler : IntentHandler {

override suspend fun handleIntent(intent: Intent?): Boolean {
override fun handleIntent(intent: Intent?): Boolean {
val data = intent?.data ?: return false
val currency = store.state.walletState.selectedCurrency ?: return false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.tangem.tap.features.intentHandler.handlers

import android.content.Intent
import com.tangem.domain.tokens.legacy.TradeCryptoAction
import com.tangem.tap.common.extensions.dispatchWithMain
import com.tangem.tap.common.extensions.dispatchOnMain
import com.tangem.tap.features.intentHandler.IntentHandler
import com.tangem.tap.store
import timber.log.Timber
Expand All @@ -12,7 +12,7 @@ import timber.log.Timber
*/
class SellCurrencyIntentHandler : IntentHandler {

override suspend fun handleIntent(intent: Intent?): Boolean {
override fun handleIntent(intent: Intent?): Boolean {
return try {
val intentData = intent?.data ?: return false
val transactionID = intentData.getQueryParameter(TRANSACTION_ID_PARAM) ?: return false
Expand All @@ -21,7 +21,7 @@ class SellCurrencyIntentHandler : IntentHandler {
val destinationAddress = intentData.getQueryParameter(DEPOSIT_WALLET_ADDRESS_PARAM) ?: return false

Timber.d("MoonPay Sell: $amount $currency to $destinationAddress")
store.dispatchWithMain(
store.dispatchOnMain(
TradeCryptoAction.SendCrypto(
currencyId = currency,
amount = amount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.tangem.tap.features.intentHandler.handlers

import android.content.Intent
import com.tangem.tap.common.extensions.dispatchWithMain
import com.tangem.tap.common.extensions.dispatchOnMain
import com.tangem.tap.common.extensions.removePrefixOrNull
import com.tangem.tap.domain.walletconnect.WalletConnectManager
import com.tangem.tap.features.details.redux.walletconnect.WalletConnectAction
Expand All @@ -15,7 +15,7 @@ import java.net.URLDecoder
*/
class WalletConnectLinkIntentHandler : IntentHandler {

override suspend fun handleIntent(intent: Intent?): Boolean {
override fun handleIntent(intent: Intent?): Boolean {
val intentData = intent?.data ?: return false
val scheme = intent.scheme ?: return false

Expand All @@ -34,7 +34,7 @@ class WalletConnectLinkIntentHandler : IntentHandler {
Timber.e(e)
return false
}
store.dispatchWithMain(WalletConnectAction.HandleDeepLink(decodedWcUri))
store.dispatchOnMain(WalletConnectAction.HandleDeepLink(decodedWcUri))
true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private fun handleWalletAction(action: Action) {

if (scanResponse == null) {
store.dispatch(NavigationAction.PopBackTo())
store.dispatch(HomeAction.ReadCard(lifecycleCoroutineScope = action.lifecycleCoroutineScope))
store.dispatch(HomeAction.ReadCard(scope = action.lifecycleCoroutineScope))
} else {
val backupState = store.state.onboardingWalletState.backupState
val updatedScanResponse = updateScanResponseAfterBackup(scanResponse, backupState)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.tangem.tap.features.welcome.redux

import android.content.Intent
import androidx.lifecycle.LifecycleCoroutineScope
import com.tangem.common.core.TangemError
import kotlinx.coroutines.CoroutineScope
import org.rekotlin.Action

internal sealed interface WelcomeAction : Action {
object ProceedWithBiometrics : WelcomeAction {

data class SetCoroutineScope(val scope: CoroutineScope) : WelcomeAction

object ClearCoroutineScope : WelcomeAction

data class ProceedWithBiometrics(val afterUnlockIntent: Intent? = null) : WelcomeAction {
object Success : WelcomeAction
data class Error(val error: TangemError) : WelcomeAction
}

data class ProceedWithCard(val lifecycleCoroutineScope: LifecycleCoroutineScope) : WelcomeAction {
object ProceedWithCard : WelcomeAction {
object Success : WelcomeAction
data class Error(val error: TangemError) : WelcomeAction
}

data class SetInitialIntent(val intent: Intent?) : WelcomeAction
data class ProceedWithIntent(val intent: Intent) : WelcomeAction

object CloseError : WelcomeAction

Expand Down
Loading

0 comments on commit 7376b8a

Please sign in to comment.