refactor: wait for user controller to init before initing subscription controller, if error happens when fetching subscription, give user a subscription (#2030)
This commit is contained in:
parent
b7ae77ebd5
commit
50c5fac8dc
5 changed files with 29 additions and 20 deletions
|
|
@ -165,7 +165,6 @@ abstract class AppConfig {
|
|||
"https://buy.stripe.com/test_bIY6ssd8z5Uz8ec8ww";
|
||||
static String iosPromoCode =
|
||||
"https://apps.apple.com/redeem?ctx=offercodes&id=1445118630&code=";
|
||||
static String trialSubscriptionId = "pangea_new_user_trial";
|
||||
static String androidUpdateURL =
|
||||
"https://play.google.com/store/apps/details?id=com.talktolearn.chat";
|
||||
static String iosUpdateURL = "itms-apps://itunes.apple.com/app/id1445118630";
|
||||
|
|
@ -176,6 +175,9 @@ abstract class AppConfig {
|
|||
"https://support.google.com/accessibility/android/answer/6006983?hl=en";
|
||||
static String assetsBaseURL =
|
||||
"https://pangea-chat-client-assets.s3.us-east-1.amazonaws.com";
|
||||
|
||||
static String trialSubscriptionId = "pangea_new_user_trial";
|
||||
static String errorSubscriptionId = "pangea_subscription_error";
|
||||
// Pangea#
|
||||
|
||||
static void loadFromJson(Map<String, dynamic> json) {
|
||||
|
|
|
|||
|
|
@ -93,6 +93,8 @@ class SubscriptionController extends BaseController {
|
|||
return;
|
||||
}
|
||||
|
||||
await _pangeaController.userController.initCompleter.future;
|
||||
|
||||
availableSubscriptionInfo = AvailableSubscriptionsInfo();
|
||||
await availableSubscriptionInfo!.setAvailableSubscriptions();
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
|
|||
import 'package:purchases_flutter/purchases_flutter.dart';
|
||||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||||
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/pangea/common/config/environment.dart';
|
||||
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
|
||||
import 'package:fluffychat/pangea/subscription/controllers/subscription_controller.dart';
|
||||
|
|
@ -72,6 +73,7 @@ class MobileSubscriptionInfo extends CurrentSubscriptionInfo {
|
|||
s: StackTrace.current,
|
||||
data: {},
|
||||
);
|
||||
currentSubscriptionId = AppConfig.errorSubscriptionId;
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||||
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/pangea/subscription/models/base_subscription_info.dart';
|
||||
import 'package:fluffychat/pangea/subscription/repo/subscription_repo.dart';
|
||||
|
||||
|
|
@ -12,13 +13,17 @@ class WebSubscriptionInfo extends CurrentSubscriptionInfo {
|
|||
@override
|
||||
Future<void> setCurrentSubscription() async {
|
||||
if (currentSubscriptionId != null) return;
|
||||
final rcResponse = await SubscriptionRepo.getCurrentSubscriptionInfo(
|
||||
userID,
|
||||
availableSubscriptionInfo.allProducts,
|
||||
);
|
||||
try {
|
||||
final rcResponse = await SubscriptionRepo.getCurrentSubscriptionInfo(
|
||||
userID,
|
||||
availableSubscriptionInfo.allProducts,
|
||||
);
|
||||
|
||||
currentSubscriptionId = rcResponse.currentSubscriptionId;
|
||||
expirationDate = rcResponse.expirationDate;
|
||||
currentSubscriptionId = rcResponse.currentSubscriptionId;
|
||||
expirationDate = rcResponse.expirationDate;
|
||||
} catch (err) {
|
||||
currentSubscriptionId = AppConfig.errorSubscriptionId;
|
||||
}
|
||||
|
||||
if (currentSubscriptionId != null && currentSubscription == null) {
|
||||
Sentry.addBreadcrumb(
|
||||
|
|
|
|||
|
|
@ -92,22 +92,18 @@ class UserController extends BaseController {
|
|||
}
|
||||
|
||||
/// A completer for the profile model of a user.
|
||||
Completer<void>? _profileCompleter;
|
||||
Completer<void> initCompleter = Completer<void>();
|
||||
bool _initializing = false;
|
||||
|
||||
/// Initializes the user's profile. Runs a function to wait for account data to load,
|
||||
/// read account data into profile, and migrate any missing info from the pangea profile.
|
||||
/// Finally, it adds a listen to update the profile data when new account data comes in.
|
||||
Future<void> initialize() async {
|
||||
if (_profileCompleter?.isCompleted ?? false) {
|
||||
return _profileCompleter!.future;
|
||||
if (_initializing || initCompleter.isCompleted) {
|
||||
return initCompleter.future;
|
||||
}
|
||||
|
||||
if (_profileCompleter != null) {
|
||||
await _profileCompleter!.future;
|
||||
return _profileCompleter!.future;
|
||||
}
|
||||
|
||||
_profileCompleter = Completer<void>();
|
||||
_initializing = true;
|
||||
|
||||
try {
|
||||
await _initialize();
|
||||
|
|
@ -126,12 +122,13 @@ class UserController extends BaseController {
|
|||
data: {},
|
||||
);
|
||||
} finally {
|
||||
if (!_profileCompleter!.isCompleted) {
|
||||
_profileCompleter!.complete();
|
||||
if (!initCompleter.isCompleted) {
|
||||
initCompleter.complete();
|
||||
}
|
||||
_initializing = false;
|
||||
}
|
||||
|
||||
return _profileCompleter!.future;
|
||||
return initCompleter.future;
|
||||
}
|
||||
|
||||
/// Initializes the user's profile by waiting for account data to load, reading in account
|
||||
|
|
@ -168,7 +165,8 @@ class UserController extends BaseController {
|
|||
}
|
||||
|
||||
void clear() {
|
||||
_profileCompleter = null;
|
||||
_initializing = false;
|
||||
initCompleter = Completer<void>();
|
||||
_cachedProfile = null;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue