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

purchase coroutine #1142

merged 19 commits into from
Jul 21, 2023

Conversation

aboedo
Copy link
Member

@aboedo aboedo commented Jul 19, 2023

Adds a coroutine alternative to purchase()

@aboedo aboedo requested a review from vegaro July 19, 2023 10:26
@aboedo aboedo self-assigned this Jul 19, 2023

class PurchasesTransactionException(
purchasesError: PurchasesError,
val userCancelled: Boolean
Copy link
Member Author

Choose a reason for hiding this comment

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

needed a new type so we can pass in the value for userCancelled as well

@ExperimentalPreviewRevenueCatPurchasesAPI
@Throws(PurchasesTransactionException::class)
suspend fun Purchases.awaitPurchase(purchaseParams: PurchaseParams):
Pair<StoreTransaction, CustomerInfo> {
Copy link
Member Author

Choose a reason for hiding this comment

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

similar to others, the Pair return type and the new exception are the only notable differences

@aboedo aboedo added the pr:feat A new feature label Jul 19, 2023
@aboedo aboedo marked this pull request as ready for review July 19, 2023 12:23
@aboedo
Copy link
Member Author

aboedo commented Jul 19, 2023

tested on device and it's working correctly

@aboedo aboedo requested a review from a team July 19, 2023 13:20
Copy link
Contributor

@NachoSoto NachoSoto left a comment

Choose a reason for hiding this comment

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

Yessss

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.

🤩

Comment on lines +3 to +6
class PurchasesTransactionException(
purchasesError: PurchasesError,
val userCancelled: Boolean,
) : PurchasesException(purchasesError)
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes sense

@codecov
Copy link

codecov bot commented Jul 19, 2023

Codecov Report

Merging #1142 (4ed3a74) into main (a81f73a) will decrease coverage by 0.24%.
The diff coverage is 92.30%.

❗ Current head 4ed3a74 differs from pull request most recent head 95af972. Consider uploading reports for the commit 95af972 to get more accurate results

@@            Coverage Diff             @@
##             main    #1142      +/-   ##
==========================================
- Coverage   85.65%   85.42%   -0.24%     
==========================================
  Files         178      178              
  Lines        6163     6290     +127     
  Branches      917      923       +6     
==========================================
+ Hits         5279     5373      +94     
- Misses        543      571      +28     
- Partials      341      346       +5     
Impacted Files Coverage Δ
...n/com/revenuecat/purchases/CoroutinesExtensions.kt 86.36% <90.00%> (ø)
...lin/com/revenuecat/purchases/PurchasesException.kt 100.00% <100.00%> (ø)
...enuecat/purchases/PurchasesTransactionException.kt 100.00% <100.00%> (ø)

... and 7 files with indirect coverage changes

Copy link
Contributor

@tonidero tonidero left a comment

Choose a reason for hiding this comment

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

Left some comments but looking great!

) {
val customerInfo: CustomerInfo = purchases.awaitCustomerInfo()
val customerInfoFetchPolicy: CustomerInfo =
purchases.awaitCustomerInfo(fetchPolicy = CacheFetchPolicy.FETCH_CURRENT)

val offerings: Offerings = purchases.awaitOfferings()

val purchasePackageBuilder: PurchaseParams.Builder = PurchaseParams.Builder(activity, packageToPurchase)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not directly related to coroutines. I would just pass a PurchaseParams as a parameter to the function, and pass that to the awaitPurchase function

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

@JvmSynthetic
@ExperimentalPreviewRevenueCatPurchasesAPI
@Throws(PurchasesTransactionException::class)
suspend fun Purchases.awaitPurchase(purchaseParams: PurchaseParams): Pair<StoreTransaction, CustomerInfo> {
Copy link
Contributor

@tonidero tonidero Jul 20, 2023

Choose a reason for hiding this comment

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

I wonder if we should return a new class instead of a Pair... Thinking about, if we ever want to add a third or more return values in the future, if it's a pair, it would be a breaking change (we could potentially deprecated it). But if we create a new class and just add a parameter, it should be fine to just add the parameter.

Copy link
Member Author

Choose a reason for hiding this comment

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

great idea! I'll do that

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

@tonidero tonidero left a comment

Choose a reason for hiding this comment

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

Just that comment about the unnecessary catch in the api tests. Other than that, looks great!

@@ -60,3 +61,45 @@ suspend fun Purchases.awaitOfferings(): Offerings {
)
}
}

data class PurchaseResult(
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally we would use this as well for the non-coroutine version... But we would need to deprecate the existing ones I guess. I think it's ok to keep this to the coroutines version for now and move to this in a future PR.

@tonidero
Copy link
Contributor

Looks like the linter is failing

Copy link
Contributor

@vegaro vegaro left a comment

Choose a reason for hiding this comment

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

Looks great

@aboedo
Copy link
Member Author

aboedo commented Jul 21, 2023

I had the lint fixes locally but never pushed 🤦 fixed now

@tonidero tonidero enabled auto-merge (squash) July 21, 2023 15:29
@tonidero tonidero disabled auto-merge July 21, 2023 15:31
Comment on lines +134 to +135
val (transaction, newCustomerInfo) = purchases.awaitPurchase(purchasePackageBuilder.build())
val purchaseResult: PurchaseResult = purchases.awaitPurchase(purchasePackageBuilder.build())
Copy link
Member Author

Choose a reason for hiding this comment

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

you can unpack both ways? kotlin is great

@tonidero tonidero enabled auto-merge (squash) July 21, 2023 15:53
@tonidero tonidero merged commit e2cab2a into main Jul 21, 2023
@tonidero tonidero deleted the andy/purchase_coroutine branch July 21, 2023 15:57
tonidero added a commit that referenced this pull request Jul 21, 2023
### Description
Fix tests after merges of #1142 and #1156 caused issue in main
This was referenced Jul 21, 2023
aboedo added a commit that referenced this pull request Jul 24, 2023
**This is an automatic release.**

### New Features

Introduced Custom Entitlements Computation mode. 

This is new library intended for apps that will do their own entitlement
computation separate from RevenueCat. It's distributed as a separate
artifact in Maven.

Apps using this mode rely on webhooks to signal their backends to
refresh entitlements with RevenueCat.

See the [demo app for an example and usage
instructions](https://github.com/RevenueCat/purchases-android/tree/main/examples/CustomEntitlementComputationSample).

* Custom entitlements: add README and other improvements (#1167) via
Andy Boedo (@aboedo)
* Update Custom Entitlements Sample app (#1166) via Andy Boedo (@aboedo)
* purchase coroutine (#1142) via Andy Boedo (@aboedo)
* Add switchUser (#1156) via Cesar de la Vega (@vegaro)
* CustomEntitlementsComputation: disable first listener callback when
set (#1152) via Andy Boedo (@aboedo)
* CustomEntitlementsComputation: Prevent posting subscriber attributes
in post receipt (#1151) via Andy Boedo (@aboedo)
* Fix `customEntitlementComputation` library deployment (#1169) via Toni
Rico (@tonidero)
* CustomEntitlementComputation: Configure method for
customEntitlementComputation mode (#1168) via Toni Rico (@tonidero)
* Add publish system for customEntitlementComputation package (#1149)
via Cesar de la Vega (@vegaro)
* Use purchase coroutine in CustomEntitlementComputationSample (#1162)
via Cesar de la Vega (@vegaro)
* Adds CustomEntitlementComputationSample (#1160) via Cesar de la Vega
(@vegaro)
* Fix tests in customEntitlementComputation after merges (#1161) via
Toni Rico (@tonidero)
* CustomEntitlementComputation: Remove custom entitlement computation
flavor for amazon module (#1158) via Toni Rico (@tonidero)
* CustomEntitlementComputation: Generate dokka docs only for defaults
flavor (#1159) via Toni Rico (@tonidero)
* CustomEntitlementComputation: Create different PurchasesConfiguration
that requires an appUserId parameter (#1154) via Toni Rico (@tonidero)
* CustomEntitlementComputation: New Purchases class (#1153) via Toni
Rico (@tonidero)
* CustomEntitlementComputation: Disable automatic cache refresh (#1157)
via Toni Rico (@tonidero)
* Add `customEntitlementComputation` flavor (#1147) via Toni Rico
(@tonidero)
* Make `customEntitlementComputation` singular (#1148) via Toni Rico
(@tonidero)
* Disable offline entitlements in custom entitlements computation mode
(#1146) via Toni Rico (@tonidero)
* Remove integration test flavor (#1143) via Toni Rico (@tonidero)
* Add header to requests when in custom entitlement computation mode
(#1145) via Toni Rico (@tonidero)
* Add internal customEntitlementsComputation mode to app config (#1141)
via Toni Rico (@tonidero)

### New Coroutines
* `awaitPurchase` is available as a coroutine-friendly alternative to
`purchase()`. (#1142) via Andy Boedo (@aboedo)

### Dependency Updates
* Bump fastlane from 2.213.0 to 2.214.0 (#1140) via dependabot[bot]
(@dependabot[bot])

### Other changes
* CI: make all Codecov jobs informational (#1155) via Cesar de la Vega
(@vegaro)
* Creates PurchasesOrchestrator (#1144) via Cesar de la Vega (@vegaro)

---------

Co-authored-by: revenuecat-ops <ops@revenuecat.com>
Co-authored-by: Andy Boedo <andresboedo@gmail.com>
tonidero pushed a commit that referenced this pull request Jul 25, 2023
### New Features

Introduced Custom Entitlements Computation mode. 

This is new library intended for apps that will do their own entitlement
computation separate from RevenueCat. It's distributed as a separate
artifact in Maven.

Apps using this mode rely on webhooks to signal their backends to
refresh entitlements with RevenueCat.

See the [demo app for an example and usage
instructions](https://github.com/RevenueCat/purchases-android/tree/main/examples/CustomEntitlementComputationSample).

* Custom entitlements: add README and other improvements (#1167) via
Andy Boedo (@aboedo)
* Update Custom Entitlements Sample app (#1166) via Andy Boedo (@aboedo)
* purchase coroutine (#1142) via Andy Boedo (@aboedo)
* Add switchUser (#1156) via Cesar de la Vega (@vegaro)
* CustomEntitlementsComputation: disable first listener callback when
set (#1152) via Andy Boedo (@aboedo)
* CustomEntitlementsComputation: Prevent posting subscriber attributes
in post receipt (#1151) via Andy Boedo (@aboedo)
* Fix `customEntitlementComputation` library deployment (#1169) via Toni
Rico (@tonidero)
* CustomEntitlementComputation: Configure method for
customEntitlementComputation mode (#1168) via Toni Rico (@tonidero)
* Add publish system for customEntitlementComputation package (#1149)
via Cesar de la Vega (@vegaro)
* Use purchase coroutine in CustomEntitlementComputationSample (#1162)
via Cesar de la Vega (@vegaro)
* Adds CustomEntitlementComputationSample (#1160) via Cesar de la Vega
(@vegaro)
* Fix tests in customEntitlementComputation after merges (#1161) via
Toni Rico (@tonidero)
* CustomEntitlementComputation: Remove custom entitlement computation
flavor for amazon module (#1158) via Toni Rico (@tonidero)
* CustomEntitlementComputation: Generate dokka docs only for defaults
flavor (#1159) via Toni Rico (@tonidero)
* CustomEntitlementComputation: Create different PurchasesConfiguration
that requires an appUserId parameter (#1154) via Toni Rico (@tonidero)
* CustomEntitlementComputation: New Purchases class (#1153) via Toni
Rico (@tonidero)
* CustomEntitlementComputation: Disable automatic cache refresh (#1157)
via Toni Rico (@tonidero)
* Add `customEntitlementComputation` flavor (#1147) via Toni Rico
(@tonidero)
* Make `customEntitlementComputation` singular (#1148) via Toni Rico
(@tonidero)
* Disable offline entitlements in custom entitlements computation mode
(#1146) via Toni Rico (@tonidero)
* Remove integration test flavor (#1143) via Toni Rico (@tonidero)
* Add header to requests when in custom entitlement computation mode
(#1145) via Toni Rico (@tonidero)
* Add internal customEntitlementsComputation mode to app config (#1141)
via Toni Rico (@tonidero)

### New Coroutines
* `awaitPurchase` is available as a coroutine-friendly alternative to
`purchase()`. (#1142) via Andy Boedo (@aboedo)

### Dependency Updates
* Bump fastlane from 2.213.0 to 2.214.0 (#1140) via dependabot[bot]
(@dependabot[bot])

### Other changes
* CI: make all Codecov jobs informational (#1155) via Cesar de la Vega
(@vegaro)
* Creates PurchasesOrchestrator (#1144) via Cesar de la Vega (@vegaro)

---------

Co-authored-by: revenuecat-ops <ops@revenuecat.com>
Co-authored-by: Andy Boedo <andresboedo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:feat A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants