From b991ea9f94cfd9738043b2f97715e20ddf18270d Mon Sep 17 00:00:00 2001 From: Taras Smakula <tarassmakula@gmail.com> Date: Tue, 12 Dec 2023 12:34:29 +0200 Subject: [PATCH] Auto submit data on subscription stage if is active --- .../SubscriptionStageViewModel.kt | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/auth/src/main/java/org/futo/circles/auth/feature/sign_up/subscription_stage/SubscriptionStageViewModel.kt b/auth/src/main/java/org/futo/circles/auth/feature/sign_up/subscription_stage/SubscriptionStageViewModel.kt index fbe82ca2d..65d58bc5e 100644 --- a/auth/src/main/java/org/futo/circles/auth/feature/sign_up/subscription_stage/SubscriptionStageViewModel.kt +++ b/auth/src/main/java/org/futo/circles/auth/feature/sign_up/subscription_stage/SubscriptionStageViewModel.kt @@ -22,15 +22,19 @@ class SubscriptionStageViewModel @Inject constructor( val purchaseLiveData = SingleEventLiveData<Response<Unit>>() val subscriptionsListLiveData = MutableLiveData<Response<List<SubscriptionListItem>>>() - fun validateSubscription(subscriptionReceiptData: SubscriptionReceiptData) { + fun initiateSubscriptionStage(subscriptionManager: SubscriptionManager) { launchBg { - subscribeLiveData.postValue(dataSource.validateSubscription(subscriptionReceiptData)) + when (val activeSubscriptionResult = + getLastActiveSubscriptionReceipt(subscriptionManager)) { + is Response.Success -> validateSubscription(activeSubscriptionResult.data) + is Response.Error -> loadSubscriptionsList(subscriptionManager) + } } } - fun loadSubscriptionsList(subscriptionManager: SubscriptionManager) { + fun validateSubscription(subscriptionReceiptData: SubscriptionReceiptData) { launchBg { - subscriptionsListLiveData.postValue(subscriptionManager.getDetails(dataSource.getProductIdsList())) + subscribeLiveData.postValue(dataSource.validateSubscription(subscriptionReceiptData)) } } @@ -40,4 +44,12 @@ class SubscriptionStageViewModel @Inject constructor( } } + private suspend fun loadSubscriptionsList(subscriptionManager: SubscriptionManager) { + val details = subscriptionManager.getDetails(dataSource.getProductIdsList()) + subscriptionsListLiveData.postValue(details) + } + + private suspend fun getLastActiveSubscriptionReceipt(subscriptionManager: SubscriptionManager) = + subscriptionManager.getActiveSubscriptionReceipt() + } \ No newline at end of file -- GitLab