mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-06 15:54:27 +01:00
Count promotional Pro grants as Pro
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
53aa0b2b25
commit
5dcdeed5fb
6 files changed
+111
-10
No files matched your search
@@ -13,13 +13,28 @@ class Entitlement {
|
||||
|
||||
bool get isActive => isActiveAt(DateTime.now());
|
||||
|
||||
/// RevenueCat stores a promotional grant under `rc_promo_<entitlement>_<duration>`.
|
||||
static const String _promoPrefix = 'rc_promo_';
|
||||
|
||||
/// Whether a row with [productKey] unlocks Pro: a monthly or yearly
|
||||
/// subscription, or a promotional Pro grant from RevenueCat
|
||||
/// (`rc_promo_pro_weekly`, `rc_promo_pro_lifetime`, ...).
|
||||
static bool isProProductKey(String productKey) =>
|
||||
productKey == 'premium_monthly' ||
|
||||
productKey == 'premium_yearly' ||
|
||||
productKey.startsWith('${_promoPrefix}pro_');
|
||||
|
||||
bool get isPro => isProProductKey(productKey);
|
||||
|
||||
/// Store and Stripe rows are kept current by webhooks, and their
|
||||
/// `activeUntil` can lag a renewal, so only their status counts. Nothing
|
||||
/// flips a manual grant's status, so it ends at its `activeUntil`.
|
||||
/// flips a manual grant's status, so it ends at its `activeUntil`; a
|
||||
/// promotional grant never renews, so it ends there too.
|
||||
bool isActiveAt(DateTime now) {
|
||||
if (status != 'active') return false; // || status == 'grace';
|
||||
final until = activeUntil;
|
||||
if (source == 'manual' && until != null && !until.isAfter(now)) return false;
|
||||
final endsAtActiveUntil = source == 'manual' || productKey.startsWith(_promoPrefix);
|
||||
if (endsAtActiveUntil && until != null && !until.isAfter(now)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,24 @@ class EntitlementsService extends ChangeNotifier {
|
||||
});
|
||||
}
|
||||
|
||||
/// Any active Pro entitlement, see [Entitlement.isPro].
|
||||
bool get hasActivePro {
|
||||
final now = _now();
|
||||
return _entitlements.any((entitlement) => entitlement.isPro && entitlement.isActiveAt(now));
|
||||
}
|
||||
|
||||
/// The latest end date among the active Pro entitlements.
|
||||
DateTime? get proActiveUntil {
|
||||
final now = _now();
|
||||
DateTime? latest;
|
||||
for (final entitlement in _entitlements) {
|
||||
if (!entitlement.isPro || !entitlement.isActiveAt(now)) continue;
|
||||
final value = entitlement.activeUntil;
|
||||
if (value != null && (latest == null || value.isAfter(latest))) latest = value;
|
||||
}
|
||||
return latest;
|
||||
}
|
||||
|
||||
DateTime? activeUntil(String productKey) {
|
||||
DateTime? latest;
|
||||
for (final entitlement in _entitlements) {
|
||||
|
||||
@@ -157,9 +157,7 @@ class IAPManager {
|
||||
bool get isBetaTester => (isLoggedIn && entitlements.hasActive(betaAccessProductKey) || kDebugMode);
|
||||
|
||||
bool get hasActiveSubscription =>
|
||||
(isLoggedIn && (entitlements.hasActive(premiumMonthlyProductKey)) ||
|
||||
entitlements.hasActive(premiumYearlyProductKey)) ||
|
||||
(!isLoggedIn && isLocalPro.value);
|
||||
(isLoggedIn && entitlements.hasActivePro) || (!isLoggedIn && isLocalPro.value);
|
||||
|
||||
bool get isProEnabled => hasActiveSubscription && (isLoggedIn || (!isLoggedIn && isLocalPro.value));
|
||||
|
||||
@@ -211,7 +209,7 @@ class IAPManager {
|
||||
if (fromStore != null) return fromStore;
|
||||
Entitlement? latest;
|
||||
for (final e in entitlements.current) {
|
||||
if (!e.isActive || (e.productKey != premiumMonthlyProductKey && e.productKey != premiumYearlyProductKey)) {
|
||||
if (!e.isActive || !e.isPro) {
|
||||
continue;
|
||||
}
|
||||
if (latest == null || (e.activeUntil?.isAfter(latest.activeUntil ?? DateTime(0)) ?? false)) latest = e;
|
||||
@@ -243,8 +241,7 @@ class IAPManager {
|
||||
@visibleForTesting
|
||||
set purchaseChannelForTesting(PurchaseChannel? channel) => _purchaseChannelForTesting = channel;
|
||||
|
||||
DateTime? get premiumActiveUntil =>
|
||||
entitlements.activeUntil(premiumMonthlyProductKey) ?? entitlements.activeUntil(premiumYearlyProductKey);
|
||||
DateTime? get premiumActiveUntil => entitlements.proActiveUntil;
|
||||
|
||||
/// Initialize the IAP manager.
|
||||
Future<void> initialize() async {
|
||||
|
||||
@@ -492,8 +492,7 @@ class RevenueCatService {
|
||||
static Map<String, dynamic>? syncSubscriptionsBody({required bool expectActive}) =>
|
||||
expectActive ? const {'expectActive': true} : null;
|
||||
|
||||
bool get _backendPro =>
|
||||
entitlementsService.hasActive(premiumProductKeyMonthly) || entitlementsService.hasActive(premiumProductKeyYearly);
|
||||
bool get _backendPro => entitlementsService.hasActivePro;
|
||||
|
||||
/// Whether the backend must be told to re-read the store: signed into an
|
||||
/// account, RevenueCat says Pro, but the account's entitlements don't.
|
||||
|
||||
@@ -64,4 +64,54 @@ void main() {
|
||||
expect(service.hasActive('beta_access'), isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('Pro entitlements', () {
|
||||
Entitlement row(String key, {String source = 'revenuecat', DateTime? activeUntil, String status = 'active'}) =>
|
||||
Entitlement(productKey: key, status: status, activeUntil: activeUntil, source: source);
|
||||
|
||||
test('subscriptions and RevenueCat promotional Pro grants are Pro', () {
|
||||
for (final key in ['premium_monthly', 'premium_yearly', 'rc_promo_pro_weekly', 'rc_promo_pro_lifetime']) {
|
||||
expect(row(key).isPro, isTrue, reason: key);
|
||||
}
|
||||
});
|
||||
|
||||
test('Base, beta and promotional Base grants are not Pro', () {
|
||||
for (final key in ['full_version', 'full_access_unlock', 'beta_access', 'rc_promo_Full Version_lifetime']) {
|
||||
expect(row(key).isPro, isFalse, reason: key);
|
||||
}
|
||||
});
|
||||
|
||||
test('a promotional grant ends at its end date even before RevenueCat expires the row', () {
|
||||
expect(row('rc_promo_pro_weekly', activeUntil: _past).isActiveAt(_now), isFalse);
|
||||
expect(row('rc_promo_pro_weekly', activeUntil: _future).isActiveAt(_now), isTrue);
|
||||
expect(row('rc_promo_pro_lifetime').isActiveAt(_now), isTrue);
|
||||
});
|
||||
|
||||
Future<EntitlementsService> serviceWith(List<Entitlement> rows) async {
|
||||
SharedPreferences.setMockInitialValues({
|
||||
'entitlements_cache_items': jsonEncode([for (final r in rows) r.toJson()]),
|
||||
});
|
||||
final client = SupabaseClient('https://example.test', 'test-anon-key');
|
||||
addTearDown(client.dispose);
|
||||
final service = EntitlementsService(client, deviceIdentityService: DeviceIdentityService(), now: () => _now);
|
||||
await service.initialize();
|
||||
return service;
|
||||
}
|
||||
|
||||
test('hasActivePro counts a promotional Pro week', () async {
|
||||
final service = await serviceWith([row('rc_promo_pro_weekly', activeUntil: _future)]);
|
||||
|
||||
expect(service.hasActivePro, isTrue);
|
||||
expect(service.proActiveUntil, _future);
|
||||
});
|
||||
|
||||
test('hasActivePro is false with only Base or an expired promo', () async {
|
||||
final service = await serviceWith([
|
||||
row('full_version'),
|
||||
row('rc_promo_pro_weekly', status: 'expired', activeUntil: _past),
|
||||
]);
|
||||
|
||||
expect(service.hasActivePro, isFalse);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -75,6 +75,28 @@ Future<void> main() async {
|
||||
});
|
||||
});
|
||||
|
||||
group('signed-in account with a promotional Pro grant', () {
|
||||
test('has Pro (rc_promo_pro_weekly counts like a subscription)', () async {
|
||||
SharedPreferences.setMockInitialValues({
|
||||
'entitlements_cache_items': jsonEncode([
|
||||
{
|
||||
'product_key': 'rc_promo_pro_weekly',
|
||||
'status': 'active',
|
||||
'active_until': DateTime.now().add(const Duration(days: 4)).toUtc().toIso8601String(),
|
||||
'source': 'revenuecat',
|
||||
},
|
||||
]),
|
||||
});
|
||||
await iap.entitlements.initialize();
|
||||
await auth.recoverSession(jsonEncode(_sessionJson(anonymous: false, email: 'rider@example.com')));
|
||||
|
||||
expect(iap.isLoggedIn, isTrue);
|
||||
expect(iap.hasActiveSubscription, isTrue);
|
||||
expect(iap.isProEnabled, isTrue);
|
||||
expect(iap.premiumActiveUntil, isNotNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('revenueCatLogin', () {
|
||||
test('never logs RevenueCat in as an anonymous user', () {
|
||||
for (final event in AuthChangeEvent.values) {
|
||||
|
||||
Reference in new issue
Block a user