@@ -75,6 +75,8 @@ class InAppBillingViewModel : ViewModel() {
7575 )
7676 )
7777 private lateinit var lastBuyFlowResult: BuyFlowResult
78+ private val screenHistory = BillingScreenHistory ()
79+ private var currentScreenId: String? = null
7880
7981 private fun finishWithResult (result : Bundle ) {
8082 viewModelScope.launch {
@@ -268,14 +270,36 @@ class InAppBillingViewModel : ViewModel() {
268270 }
269271 }
270272
273+ UIType .BILLING_PROFILE_SCREEN_ABANDON -> {
274+ if (showPreviousScreen()) return
275+ finishWithResult(billingUiViewState.result)
276+ }
277+
271278 else -> finishWithResult(billingUiViewState.result)
272279 }
273280 }
274281 }
275282 }
276283
277- private fun showScreen (screenId : String ): Boolean {
284+ private fun showPreviousScreen (): Boolean {
285+ val previousScreenId = screenHistory.popPrevious(
286+ currentScreenId,
287+ billingUiViewState.screenMap::containsKey
288+ )
289+ return previousScreenId != null && showScreen(
290+ previousScreenId,
291+ recordHistory = false
292+ )
293+ }
294+
295+ private fun showScreen (
296+ screenId : String ,
297+ recordHistory : Boolean = true
298+ ): Boolean {
299+ val previousScreenId = currentScreenId
278300 val showScreen = billingUiViewState.screenMap[screenId] ? : return false
301+ if (recordHistory) screenHistory.recordTransition(previousScreenId, screenId)
302+ currentScreenId = screenId
279303 billingUiViewState = billingUiViewState.copy(
280304 showScreen = showScreen,
281305 visible = true
@@ -300,11 +324,15 @@ class InAppBillingViewModel : ViewModel() {
300324 }
301325 val action = buyFlowResult.acquireResult?.action ? : return failAction()
302326 val screenMap = buyFlowResult.acquireResult.screenMap
303- val showScreen = screenMap[action.screenId] ? : return failAction()
327+ val targetScreenId = action.screenId ? : return failAction()
328+ val showScreen = screenMap[targetScreenId] ? : return failAction()
304329 if (Log .isLoggable(TAG , Log .DEBUG )) Log .d(TAG , " handleAcquireResult, showScreen:$showScreen result:${buyFlowResult.acquireResult} " )
305330 if (action.type != ActionType .SHOW ) return failAction()
306331 lastBuyFlowResult = buyFlowResult
332+ val previousScreenId = currentScreenId
307333 billingUiViewState.screenMap.putAll(screenMap)
334+ screenHistory.recordTransition(previousScreenId, targetScreenId)
335+ currentScreenId = targetScreenId
308336 billingUiViewState = billingUiViewState.copy(
309337 showScreen = showScreen,
310338 result = buyFlowResult.result,
@@ -400,4 +428,4 @@ class InAppBillingViewModel : ViewModel() {
400428 resultBundle(BillingClient .BillingResponseCode .USER_CANCELED , " " )
401429 finishWithResult(result)
402430 }
403- }
431+ }
0 commit comments