Skip to content

Conversation

fire-at-will
Copy link
Contributor

@fire-at-will fire-at-will commented Jul 11, 2025

Description

Android counterpart to RevenueCat/purchases-ios#5108.

This PR introduces virtual currencies to the Android SDK. It's a large PR, but all parts of it have been reviewed individually before here:

It includes the following:

New APIs for Fetching Virtual Currency Balances + Metadata

New Top-Level Functions

These functions allow you to fetch the virtual currencies for the current subscriber. The invalidateVirtualCurrenciesCache function allows developers to clear the cached virtual currencies, which will then force the refreshing of the virtual currencies from the backend the next time getVirtualCurrencies() is called.

class Purchases {
   fun getVirtualCurrencies(
        callback: GetVirtualCurrenciesCallback,
    )

   fun invalidateVirtualCurrenciesCache()

   val cachedVirtualCurrencies: VirtualCurrencies?
}

New Objects

class VirtualCurrencies internal constructor(
    val all: Map<String, VirtualCurrency>,
) : Parcelable {
    operator fun get(code: String): VirtualCurrency? = all[code]
}

class VirtualCurrency internal constructor(
    val balance: Int,
    val name: String,
    val code: String,
    val serverDescription: String? = null,
)

Example Usage

Kotlin:

Purchases.sharedInstance.invalidateVirtualCurrenciesCache()

Purchases.sharedInstance.getVirtualCurrencies(
    object: GetVirtualCurrenciesCallback {
        override fun onReceived(virtualCurrencies: VirtualCurrencies) {}
        override fun onError(error: PurchasesError) {}
    }
)

Purchases.sharedInstance.getVirtualCurrenciesWith(
        onError = { _: PurchasesError -> },
        onSuccess = { _: VirtualCurrencies -> },
)

val getVirtualCurrenciesResult: VirtualCurrencies = Purchases.sharedInstance.awaitGetVirtualCurrencies()

val cachedVirtualCurrencies = Purchases.sharedInstance.cachedVirtualCurrencies

This PR includes the following PRs with no other changes:
- #2466
- #2496
- #2505

It is the collection of all of the work required to update the VC APIs
and fetch the virtual currencies from the new v1 endpoint. To keep the
PR sizes manageable, the work was broken up into smaller PRs, which have
been individually approved and aggregated into this branch.

Forgoing a review on this PR since all changes have already been
reviewed, and we'll do a final review when we merge
`virtual-currency-dev` into `main`
@fire-at-will fire-at-will marked this pull request as ready for review July 11, 2025 16:21
Copy link

codecov bot commented Jul 11, 2025

Codecov Report

Attention: Patch coverage is 84.92462% with 30 lines in your changes missing coverage. Please review.

Project coverage is 78.23%. Comparing base (b9a831c) to head (7693129).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
...chases/virtualcurrencies/VirtualCurrencyManager.kt 88.13% 7 Missing ⚠️
...in/com/revenuecat/purchases/listenerConversions.kt 0.00% 6 Missing ⚠️
...n/com/revenuecat/purchases/coroutinesExtensions.kt 0.00% 5 Missing ⚠️
...revenuecat/purchases/common/caching/DeviceCache.kt 90.74% 3 Missing and 2 partials ⚠️
.../com/revenuecat/purchases/PurchasesOrchestrator.kt 25.00% 3 Missing ⚠️
.../kotlin/com/revenuecat/purchases/common/Backend.kt 97.67% 0 Missing and 1 partial ⚠️
...nuecat/purchases/strings/VirtualCurrencyStrings.kt 0.00% 1 Missing ⚠️
...t/purchases/virtualcurrencies/VirtualCurrencies.kt 75.00% 0 Missing and 1 partial ⚠️
...cat/purchases/virtualcurrencies/VirtualCurrency.kt 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2519      +/-   ##
==========================================
+ Coverage   78.10%   78.23%   +0.13%     
==========================================
  Files         286      291       +5     
  Lines       10573    10772     +199     
  Branches     1485     1504      +19     
==========================================
+ Hits         8258     8428     +170     
- Misses       1668     1692      +24     
- Partials      647      652       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

return Date(preferences.getLong(virtualCurrenciesLastUpdatedCacheKey(appUserID), 0))
}

private fun SharedPreferences.Editor.clearVirtualCurrenciesCacheTimestamp(

Choose a reason for hiding this comment

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

Nit: Since clearVirtualCurrenciesCache and clearVirtualCurrenciesCacheTimestamp are always called together, consider merging them into a single function like:

fun SharedPreferences.Editor.clearVirtualCurrenciesCacheAndTimestamp(appUserID: String): SharedPreferences.Editor

This would reduce call-site verbosity, clarify intent, and prevent potential future inconsistencies where one might be called without the other.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be good to keep as a separate private method to avoid bloating the clearVirtualCurrenciesCache method but I do agree we should probably only use clearVirtualCurrenciesCache generally.

There may be cases where we want to clear the timestamp and not the data, for example, if we want to force a stale cache. However, we're not doing that right now, so I do think this makes sense (we can always modify in the future if needed).

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.

Looking good! Just some small comments but nothing blocking!

return Date(preferences.getLong(virtualCurrenciesLastUpdatedCacheKey(appUserID), 0))
}

private fun SharedPreferences.Editor.clearVirtualCurrenciesCacheTimestamp(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it might be good to keep as a separate private method to avoid bloating the clearVirtualCurrenciesCache method but I do agree we should probably only use clearVirtualCurrenciesCache generally.

There may be cases where we want to clear the timestamp and not the data, for example, if we want to force a stale cache. However, we're not doing that right now, so I do think this makes sense (we can always modify in the future if needed).

@fire-at-will fire-at-will requested a review from tonidero July 14, 2025 20:06
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.

🚢

Copy link

emerge-tools bot commented Jul 15, 2025

📸 Snapshot Test

648 unchanged

Name Added Removed Modified Renamed Unchanged Errored Approval
TestPurchasesUIAndroidCompatibility Paparazzi
com.revenuecat.testpurchasesuiandroidcompatibility.paparazzi
0 0 0 0 251 0 N/A
TestPurchasesUIAndroidCompatibility
com.revenuecat.testpurchasesuiandroidcompatibility
0 0 0 0 397 0 N/A

🛸 Powered by Emerge Tools

@fire-at-will fire-at-will added this pull request to the merge queue Jul 16, 2025
Merged via the queue into main with commit 4fc17af Jul 16, 2025
14 checks passed
@fire-at-will fire-at-will deleted the virtual-currency-dev branch July 16, 2025 13:56
github-merge-queue bot pushed a commit that referenced this pull request Jul 17, 2025
**This is an automatic release.**

## RevenueCat SDK
### Virtual Currency
#### ✨ New Features
* Virtual Currency Support (#2519) via Will Taylor (@fire-at-will)

## RevenueCatUI SDK
### Paywallv2
#### ✨ New Features
* PaywallActivityLauncher: Add `edgeToEdge` parameter to display paywall
in full screen (#2530) via Toni Rico (@tonidero)
#### 🐞 Bugfixes
* Remove logic to avoid repurchasing already subscribed products (#2492)
via Toni Rico (@tonidero)

### 🔄 Other Changes
* Dont run VC tests on load shedder integration tests (#2538) via Will
Taylor (@fire-at-will)
* Introduces `CompatComposeView` to handle scenarios where the view tree
is not set up (#2527) via JayShortway (@JayShortway)

Co-authored-by: revenuecat-ops <ops@revenuecat.com>
tonidero pushed a commit that referenced this pull request Aug 25, 2025
Android counterpart to
RevenueCat/purchases-ios#5108.

This PR introduces virtual currencies to the Android SDK. It's a large
PR, but all parts of it have been reviewed individually before here:
- #2466
- #2496
- #2505

It includes the following:

These functions allow you to fetch the virtual currencies for the
current subscriber. The `invalidateVirtualCurrenciesCache` function
allows developers to clear the cached virtual currencies, which will
then force the refreshing of the virtual currencies from the backend the
next time `getVirtualCurrencies()` is called.

```kotlin
class Purchases {
   fun getVirtualCurrencies(
        callback: GetVirtualCurrenciesCallback,
    )

   fun invalidateVirtualCurrenciesCache()

   val cachedVirtualCurrencies: VirtualCurrencies?
}
```

```kotlin
class VirtualCurrencies internal constructor(
    val all: Map<String, VirtualCurrency>,
) : Parcelable {
    operator fun get(code: String): VirtualCurrency? = all[code]
}

class VirtualCurrency internal constructor(
    val balance: Int,
    val name: String,
    val code: String,
    val serverDescription: String? = null,
)
```

Kotlin:
```swift
Purchases.sharedInstance.invalidateVirtualCurrenciesCache()

Purchases.sharedInstance.getVirtualCurrencies(
    object: GetVirtualCurrenciesCallback {
        override fun onReceived(virtualCurrencies: VirtualCurrencies) {}
        override fun onError(error: PurchasesError) {}
    }
)

Purchases.sharedInstance.getVirtualCurrenciesWith(
        onError = { _: PurchasesError -> },
        onSuccess = { _: VirtualCurrencies -> },
)

val getVirtualCurrenciesResult: VirtualCurrencies = Purchases.sharedInstance.awaitGetVirtualCurrencies()

val cachedVirtualCurrencies = Purchases.sharedInstance.cachedVirtualCurrencies
```
@tonidero tonidero mentioned this pull request Aug 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants