Merge pull request #1019 from pangeachat/update-trial

check for trial update when getting isSubscribed
pull/1490/head
ggurdin 1 year ago committed by GitHub
commit cdce25813a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -45,12 +45,21 @@ class SubscriptionController extends BaseController {
_pangeaController = pangeaController; _pangeaController = pangeaController;
} }
UserController get userController => _pangeaController.userController; UserController get _userController => _pangeaController.userController;
String? get userID => _pangeaController.matrixState.client.userID; String? get _userID => _pangeaController.matrixState.client.userID;
bool get isSubscribed => bool get isSubscribed {
final bool hasSubscription =
currentSubscriptionInfo?.currentSubscriptionId != null; currentSubscriptionInfo?.currentSubscriptionId != null;
if (_activatedNewUserTrial && !hasSubscription) {
_setNewUserTrial();
return true;
}
return hasSubscription;
}
bool _isInitializing = false; bool _isInitializing = false;
Completer<void> initialized = Completer<void>(); Completer<void> initialized = Completer<void>();
@ -74,7 +83,7 @@ class SubscriptionController extends BaseController {
Future<void> _initialize() async { Future<void> _initialize() async {
try { try {
if (userID == null) { if (_userID == null) {
debugPrint( debugPrint(
"Attempted to initalize subscription information with null userId", "Attempted to initalize subscription information with null userId",
); );
@ -86,18 +95,18 @@ class SubscriptionController extends BaseController {
currentSubscriptionInfo = kIsWeb currentSubscriptionInfo = kIsWeb
? WebSubscriptionInfo( ? WebSubscriptionInfo(
userID: userID!, userID: _userID!,
availableSubscriptionInfo: availableSubscriptionInfo!, availableSubscriptionInfo: availableSubscriptionInfo!,
) )
: MobileSubscriptionInfo( : MobileSubscriptionInfo(
userID: userID!, userID: _userID!,
availableSubscriptionInfo: availableSubscriptionInfo!, availableSubscriptionInfo: availableSubscriptionInfo!,
); );
await currentSubscriptionInfo!.configure(); await currentSubscriptionInfo!.configure();
await currentSubscriptionInfo!.setCurrentSubscription(); await currentSubscriptionInfo!.setCurrentSubscription();
if (_activatedNewUserTrial) { if (_activatedNewUserTrial) {
setNewUserTrial(); _setNewUserTrial();
} }
if (!kIsWeb) { if (!kIsWeb) {
@ -187,7 +196,7 @@ class SubscriptionController extends BaseController {
return; return;
} }
ErrorHandler.logError( ErrorHandler.logError(
m: "Failed to purchase revenuecat package for user $userID with error code $errCode", m: "Failed to purchase revenuecat package for user $_userID with error code $errCode",
s: StackTrace.current, s: StackTrace.current,
); );
return; return;
@ -195,30 +204,30 @@ class SubscriptionController extends BaseController {
} }
} }
int get currentTrialDays => userController.inTrialWindow(trialDays: 1) int get _currentTrialDays => _userController.inTrialWindow(trialDays: 1)
? 1 ? 1
: userController.inTrialWindow(trialDays: 7) : _userController.inTrialWindow(trialDays: 7)
? 7 ? 7
: 0; : 0;
bool get _activatedNewUserTrial => bool get _activatedNewUserTrial =>
userController.inTrialWindow(trialDays: 1) || _userController.inTrialWindow(trialDays: 1) ||
(userController.inTrialWindow() && (_userController.inTrialWindow() &&
userController.profile.userSettings.activatedFreeTrial); _userController.profile.userSettings.activatedFreeTrial);
void activateNewUserTrial() { void activateNewUserTrial() {
userController.updateProfile( _userController.updateProfile(
(profile) { (profile) {
profile.userSettings.activatedFreeTrial = true; profile.userSettings.activatedFreeTrial = true;
return profile; return profile;
}, },
); );
setNewUserTrial(); _setNewUserTrial();
trialActivationStream.add(true); trialActivationStream.add(true);
} }
void setNewUserTrial() { void _setNewUserTrial() {
final DateTime? createdAt = userController.profile.userSettings.createdAt; final DateTime? createdAt = _userController.profile.userSettings.createdAt;
if (createdAt == null) { if (createdAt == null) {
ErrorHandler.logError( ErrorHandler.logError(
m: "Null user profile createAt in subscription settings", m: "Null user profile createAt in subscription settings",
@ -228,7 +237,7 @@ class SubscriptionController extends BaseController {
} }
final DateTime expirationDate = createdAt.add( final DateTime expirationDate = createdAt.add(
Duration(days: currentTrialDays), Duration(days: _currentTrialDays),
); );
currentSubscriptionInfo?.setTrial(expirationDate); currentSubscriptionInfo?.setTrial(expirationDate);
} }
@ -333,13 +342,13 @@ class SubscriptionController extends BaseController {
accessToken: _pangeaController.userController.accessToken, accessToken: _pangeaController.userController.accessToken,
); );
final String reqUrl = Uri.encodeFull( final String reqUrl = Uri.encodeFull(
"${PApiUrls.paymentLink}?pangea_user_id=$userID&duration=${duration.value}&redeem=$isPromo", "${PApiUrls.paymentLink}?pangea_user_id=$_userID&duration=${duration.value}&redeem=$isPromo",
); );
final Response res = await req.get(url: reqUrl); final Response res = await req.get(url: reqUrl);
final json = jsonDecode(res.body); final json = jsonDecode(res.body);
String paymentLink = json["link"]["url"]; String paymentLink = json["link"]["url"];
final String? email = await userController.userEmail; final String? email = await _userController.userEmail;
if (email != null) { if (email != null) {
paymentLink += "?prefilled_email=${Uri.encodeComponent(email)}"; paymentLink += "?prefilled_email=${Uri.encodeComponent(email)}";
} }

Loading…
Cancel
Save