Skip to content

Commit

Permalink
Merge pull request #423 from tangem/merge_5.18_dev
Browse files Browse the repository at this point in the history
Merge 5.18 dev
  • Loading branch information
kozarezvlad authored Nov 17, 2024
2 parents 3d234c9 + 811ac99 commit b933220
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class SdkTaskSpinnerFragment : BaseFragment() {
override fun onTagLost(productType: ProductType) {}
override fun onTagConnected() {}
override fun onWrongCard(wrongValueType: WrongValueType) {}
override fun onSessionStopped(message: Message?) {}
override fun onSessionStopped(message: Message?, onDialogHidden: () -> Unit) {}
override fun onError(error: TangemError) {}
override fun requestUserCode(
type: UserCodeType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class SecurityDelay : BaseDelegateAction() {
}
}

withMainContext { delegate.onSessionStopped(null) }
withMainContext { delegate.onSessionStopped(null) {} }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,45 @@ class DefaultSessionViewDelegate(
}
}

override fun onSessionStopped(message: Message?) {
override fun onSessionStopped(message: Message?, onDialogHidden: () -> Unit) {
Log.view { "session stopped" }
stoppedBySession = true
readingDialog?.show(SessionViewDelegateState.Success(message))
readingDialog?.show(SessionViewDelegateState.Success(message)) {
onDialogShown()
onDialogHidden()
}
}

override fun onSecurityDelay(ms: Int, totalDurationSeconds: Int, productType: ProductType) {
Log.view { "showing security delay: $ms, $totalDurationSeconds" }
readingDialog?.show(SessionViewDelegateState.SecurityDelay(ms, totalDurationSeconds, productType))
readingDialog?.show(
SessionViewDelegateState.SecurityDelay(ms, totalDurationSeconds, productType),
::onDialogShown,
)
}

override fun onDelay(total: Int, current: Int, step: Int, productType: ProductType) {
Log.view { "showing delay" }
readingDialog?.show(SessionViewDelegateState.Delay(total, current, step, productType))
readingDialog?.show(SessionViewDelegateState.Delay(total, current, step, productType), ::onDialogShown)
}

override fun onTagLost(productType: ProductType) {
Log.view { "tag lost" }
readingDialog?.show(SessionViewDelegateState.TagLost(productType))
readingDialog?.show(SessionViewDelegateState.TagLost(productType), ::onDialogShown)
}

override fun onTagConnected() {
Log.view { "tag connected" }
readingDialog?.show(SessionViewDelegateState.TagConnected)
readingDialog?.show(SessionViewDelegateState.TagConnected, ::onDialogShown)
}

override fun onWrongCard(wrongValueType: WrongValueType) {
Log.view { "wrong card detected" }
readingDialog?.show(SessionViewDelegateState.WrongCard(wrongValueType))
readingDialog?.show(SessionViewDelegateState.WrongCard(wrongValueType), ::onDialogShown)
}

override fun onError(error: TangemError) {
readingDialog?.show(SessionViewDelegateState.Error(error))
readingDialog?.show(SessionViewDelegateState.Error(error), ::onDialogShown)
}

override fun requestUserCode(
Expand All @@ -129,6 +135,7 @@ class DefaultSessionViewDelegate(
cardId = cardId,
callback = callback,
),
::onDialogShown,
)
}
}
Expand All @@ -137,7 +144,7 @@ class DefaultSessionViewDelegate(
Log.view { "showing pin change request with type: $type" }
val dialog = readingDialog ?: createReadingDialog(activity)

dialog.show(SessionViewDelegateState.PinChangeRequested(type, cardId, callback))
dialog.show(SessionViewDelegateState.PinChangeRequested(type, cardId, callback), ::onDialogShown)
}

override fun dismiss() {
Expand Down Expand Up @@ -176,26 +183,23 @@ class DefaultSessionViewDelegate(
AttestationFailedDialog.completedWithWarnings(activity, positive)
}

private fun onDialogShown() {
readingDialog = null
}

private fun createAndShowState(
state: SessionViewDelegateState,
enableHowTo: Boolean,
message: ViewDelegateMessage? = null,
iconScanRes: Int? = null,
) {
Log.view { "createAndShowState" }
// Under the hood dialog dismiss could work async and new dialog could be created
// before old one is dismissed, that leads to 2 dialogs on screen.
// Create new one in onDismiss callback
postUI {
if (readingDialog != null) {
readingDialog?.setOnDismissListener {
Log.view { "old readingDialog onDismiss callback" }
createAndConfigureDialog(state, enableHowTo, message, iconScanRes)
}
readingDialog?.dismissInternal()
} else {
if (readingDialog == null) {
Log.view { "createAndConfigDialog" }
createAndConfigureDialog(state, enableHowTo, message, iconScanRes)
} else {
readingDialog?.show(state, ::onDialogShown)
}
}
}
Expand All @@ -211,7 +215,7 @@ class DefaultSessionViewDelegate(
showHowTo(enableHowTo)
setInitialMessage(message)
setScanImage(sdkConfig.scanTagImage)
show(state)
show(state, ::onDialogShown)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class NfcSessionDialog(
stateWidgets.add(messageWidget)
stateWidgets.add(howToTapWidget)

headerWidget.onHowTo = { show(SessionViewDelegateState.HowToTap) }
headerWidget.onHowTo = { show(SessionViewDelegateState.HowToTap) {} }

behavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
Expand All @@ -112,7 +112,7 @@ class NfcSessionDialog(
}

@Suppress("LongMethod", "ComplexMethod")
fun show(state: SessionViewDelegateState) {
fun show(state: SessionViewDelegateState, onDialogShowed: () -> Unit) {
postUI {
if (ownerActivity?.isFinishing == true) return@postUI
if (!this.isShowing) {
Expand All @@ -121,7 +121,7 @@ class NfcSessionDialog(

when (state) {
is SessionViewDelegateState.Ready -> onReady(state)
is SessionViewDelegateState.Success -> onSuccess(state)
is SessionViewDelegateState.Success -> onSuccess(state, onDialogShowed)
is SessionViewDelegateState.Error -> onError(state)
is SessionViewDelegateState.SecurityDelay -> onSecurityDelay(state)
is SessionViewDelegateState.Delay -> onDelay(state)
Expand All @@ -144,10 +144,13 @@ class NfcSessionDialog(
setStateAndShow(state, headerWidget, touchCardWidget, messageWidget)
}

private fun onSuccess(state: SessionViewDelegateState.Success) {
private fun onSuccess(state: SessionViewDelegateState.Success, onDialogShowed: () -> Unit) {
setStateAndShow(state, headerWidget, progressStateWidget, messageWidget)
performHapticFeedback()
postUI(msTime = 1000) { dismissInternal() }
postUI(msTime = 100) {
dismissInternal()
onDialogShowed()
}
}

private fun onError(state: SessionViewDelegateState.Error) {
Expand Down Expand Up @@ -274,7 +277,7 @@ class NfcSessionDialog(

howToTapWidget.previousState?.let {
howToTapWidget.setState(it)
show(it)
show(it) {}
}
howToTapWidget.previousState = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ interface SessionViewDelegate {
/**
* It is called when NFC session was completed and a user can take the card away from the Android device.
*/
fun onSessionStopped(message: Message? = null)
fun onSessionStopped(message: Message? = null, onDialogHidden: () -> Unit)

/**
* It is called when some error occur during NFC session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,9 @@ class CardSession(
* @param message If null, the default message will be shown.
*/
private fun stop(message: Message? = null) {
stopSessionIfActive()
viewDelegate.onSessionStopped(message)
viewDelegate.onSessionStopped(message) {
stopSessionIfActive()
}
}

/**
Expand Down

0 comments on commit b933220

Please sign in to comment.