Skip to content

Commit

Permalink
Amend subs manager (#3571)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/488551667048375/1205538346631078/f

### Description
This PR changes the subs manager to avoid creating account when is not
needed

### Steps to test this PR
See task
  • Loading branch information
marcosholgado authored Sep 22, 2023
1 parent b2c7bbc commit 0ad9fec
Show file tree
Hide file tree
Showing 3 changed files with 229 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,26 @@ import logcat.logcat
import retrofit2.HttpException

interface SubscriptionsManager {

/**
* Executes the pre-purchase flow which tries to recover the external_id from the store,
* if it cannot, it creates a new account
*/
suspend fun prePurchaseFlow(): SubscriptionsDataResult

/**
* Recovers a subscription from the store
*/
suspend fun recoverSubscriptionFromStore(): SubscriptionsDataResult

/**
* Gets the subscription data for an authenticated user
*/
suspend fun getSubscriptionData(): SubscriptionsDataResult

/**
* Flow to know if a user is signed in or not
*/
val isSignedIn: Flow<Boolean>
}

Expand All @@ -59,7 +77,7 @@ class RealSubscriptionsManager @Inject constructor(

private fun isUserAuthenticated(): Boolean = !authDataStore.token.isNullOrBlank()

override suspend fun getSubscriptionData(): SubscriptionsDataResult {
override suspend fun recoverSubscriptionFromStore(): SubscriptionsDataResult {
try {
val externalId = if (isUserAuthenticated()) {
getSubscriptionDataFromToken()
Expand All @@ -68,16 +86,47 @@ class RealSubscriptionsManager @Inject constructor(
}
return if (externalId is Success) {
externalId
} else {
return Failure("Subscription data not found")
}
} catch (e: HttpException) {
val error = parseError(e)?.error ?: "An error happened"
return Failure(error)
} catch (e: Exception) {
return Failure(e.message ?: "An error happened")
}
}

override suspend fun getSubscriptionData(): SubscriptionsDataResult {
return try {
if (isUserAuthenticated()) {
getSubscriptionDataFromToken()
} else {
Failure("Subscription data not found")
}
} catch (e: HttpException) {
val error = parseError(e)?.error ?: "An error happened"
Failure(error)
} catch (e: Exception) {
Failure(e.message ?: "An error happened")
}
}

override suspend fun prePurchaseFlow(): SubscriptionsDataResult {
return try {
val externalId = recoverSubscriptionFromStore()
if (externalId is Success) {
externalId
} else {
val newAccount = createAccount()
logcat(LogPriority.DEBUG) { "Subs: account created ${newAccount.externalId}" }
Success(externalId = newAccount.externalId, pat = newAccount.authToken, entitlements = emptyList())
}
} catch (e: HttpException) {
val error = parseError(e)?.error ?: "An error happened"
return Failure(error)
Failure(error)
} catch (e: Exception) {
return Failure(e.message ?: "An error happened")
Failure(e.message ?: "An error happened")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SubscriptionsViewModel @Inject constructor(

fun buySubscription(activity: Activity, productDetails: ProductDetails, offerToken: String, isReset: Boolean = false) {
viewModelScope.launch(dispatcherProvider.io()) {
when (val response = subscriptionsManager.getSubscriptionData()) {
when (val response = subscriptionsManager.prePurchaseFlow()) {
is Success -> {
val billingParams = billingFlowParamsBuilder(
productDetails = productDetails,
Expand All @@ -106,7 +106,7 @@ class SubscriptionsViewModel @Inject constructor(

fun recoverSubscription() {
viewModelScope.launch(dispatcherProvider.io()) {
when (val response = subscriptionsManager.getSubscriptionData()) {
when (val response = subscriptionsManager.recoverSubscriptionFromStore()) {
is Success -> {
logcat(LogPriority.DEBUG) { "Subs: external id is ${response.externalId}" }
}
Expand Down
Loading

0 comments on commit 0ad9fec

Please sign in to comment.