Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

purchase coroutine #1142

Merged
merged 19 commits into from
Jul 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
used async purchase method for purchase tester
  • Loading branch information
aboedo authored and tonidero committed Jul 21, 2023
commit 142d94b9b077c980dc5d2fe1756eb9d6a82cc9bc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.transition.MaterialContainerTransform
import com.revenuecat.purchases.CustomerInfo
import com.revenuecat.purchases.ExperimentalPreviewRevenueCatPurchasesAPI
import com.revenuecat.purchases.Offerings
import com.revenuecat.purchases.Package
import com.revenuecat.purchases.PurchaseParams
import com.revenuecat.purchases.Purchases
import com.revenuecat.purchases.PurchasesError
import com.revenuecat.purchases.PurchasesTransactionException
import com.revenuecat.purchases.awaitPurchase
import com.revenuecat.purchases.getCustomerInfoWith
import com.revenuecat.purchases.getOfferingsWith
import com.revenuecat.purchases.models.GoogleProrationMode
Expand All @@ -30,36 +33,26 @@ import com.revenuecat.purchases.models.SubscriptionOption
import com.revenuecat.purchases.purchaseWith
import com.revenuecat.purchases_sample.R
import com.revenuecat.purchases_sample.databinding.FragmentOfferingBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

@SuppressWarnings("TooManyFunctions")
@OptIn(ExperimentalPreviewRevenueCatPurchasesAPI::class)
class OfferingFragment : Fragment(), PackageCardAdapter.PackageCardAdapterListener {

lateinit var binding: FragmentOfferingBinding

private val args: OfferingFragmentArgs by navArgs()
private val offeringId: String by lazy { args.offeringId }
private var activeSubscriptions: Set<String> = setOf()
private val scope = CoroutineScope(Dispatchers.Main)

private lateinit var dataStoreUtils: DataStoreUtils
private var isPlayStore: Boolean = true

private val purchaseErrorCallback: (error: PurchasesError, userCancelled: Boolean) -> Unit =
{ error, userCancelled ->
toggleLoadingIndicator(false)
if (!userCancelled) {
showUserError(requireActivity(), error)
}
}

private val successfulPurchaseCallback: (purchase: StoreTransaction?, customerInfo: CustomerInfo) -> Unit =
{ storeTransaction, _ ->
toggleLoadingIndicator(false)
handleSuccessfulPurchase(storeTransaction?.orderId)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedElementEnterTransition = MaterialContainerTransform().apply {
Expand All @@ -70,10 +63,6 @@ class OfferingFragment : Fragment(), PackageCardAdapter.PackageCardAdapterListen
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
Purchases.sharedInstance.getCustomerInfoWith {
activeSubscriptions = it.activeSubscriptions
}

dataStoreUtils = DataStoreUtils(requireActivity().applicationContext.configurationDataStore)
binding = FragmentOfferingBinding.inflate(inflater)

Expand Down Expand Up @@ -164,23 +153,30 @@ class OfferingFragment : Fragment(), PackageCardAdapter.PackageCardAdapterListen
purchaseParamsBuilder.isPersonalizedPrice(isPersonalizedPrice)
}

Purchases.sharedInstance.purchaseWith(
purchaseParamsBuilder.build(),
purchaseErrorCallback,
successfulPurchaseCallback,
)
purchase(purchaseParamsBuilder.build())
}
}
} else {
if (isPersonalizedPrice) {
purchaseParamsBuilder.isPersonalizedPrice(isPersonalizedPrice)
}
purchase(purchaseParamsBuilder.build())
}
}

Purchases.sharedInstance.purchaseWith(
purchaseParamsBuilder.build(),
purchaseErrorCallback,
successfulPurchaseCallback,
)
private fun purchase(params: PurchaseParams) {
scope.launch {
try {
val (storeTransaction, _) = Purchases.sharedInstance.awaitPurchase(params)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩

toggleLoadingIndicator(false)
handleSuccessfulPurchase(storeTransaction.orderId)
} catch (exception: PurchasesTransactionException) {
toggleLoadingIndicator(false)
showUserError(
requireActivity(),
PurchasesError(underlyingErrorMessage = exception.underlyingErrorMessage, code = exception.code)
)
}
}
}

Expand All @@ -194,6 +190,7 @@ class OfferingFragment : Fragment(), PackageCardAdapter.PackageCardAdapterListen
false,
)
}

is GooglePurchasingData.InAppProduct -> {
ObserverModeBillingClient.purchase(
requireActivity(),
Expand Down