MotorSignal-App/lib/services/entitlements/entitlement_providers.dart

47 lines
1.8 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../billing/billing_providers.dart';
import 'entitlement_coordinator.dart';
import 'entitlement_state.dart';
import 'entitlement_storage.dart';
import 'feature_access_service.dart';
/// Provider for the physical entitlement storage contract.
/// Can be overridden in tests with [InMemoryEntitlementStorage].
final entitlementStorageProvider = Provider<EntitlementStorage>((ref) {
return const SecureEntitlementStorage();
});
/// Provider for the [EntitlementCoordinator] which bridges store events to entitlement state.
final entitlementCoordinatorProvider =
ChangeNotifierProvider<EntitlementCoordinator>((ref) {
final billing = ref.watch(billingServiceProvider);
final storage = ref.watch(entitlementStorageProvider);
final verifier = ref.watch(purchaseVerificationServiceProvider);
final queue = ref.watch(verificationQueueProvider);
return EntitlementCoordinator(
billingService: billing,
storage: storage,
verificationService: verifier,
verificationQueue: queue,
);
});
/// Authoritative entitlement state provider.
/// Follows the state published by [EntitlementCoordinator].
final entitlementProvider = Provider<EntitlementState>((ref) {
final coordinator = ref.watch(entitlementCoordinatorProvider);
return coordinator.state;
});
/// Centralized provider for checking feature accessibility.
/// Consumers watch this provider and call [FeatureAccessService.canAccess].
final featureAccessProvider = Provider<FeatureAccessService>((ref) {
final state = ref.watch(entitlementProvider);
return FeatureAccessService(state);
});
/// Convenience provider returning whether user is currently a Pro subscriber.
final isProSubscriberProvider = Provider<bool>((ref) {
return ref.watch(entitlementProvider).isPro;
});