diff --git a/lib/pages/chat_list/chat_list_item.dart b/lib/pages/chat_list/chat_list_item.dart index 680fe8dcf9..447b5f6c33 100644 --- a/lib/pages/chat_list/chat_list_item.dart +++ b/lib/pages/chat_list/chat_list_item.dart @@ -205,7 +205,7 @@ class ChatListItem extends StatelessWidget { // Future.value(L10n.of(context)!.emptyChat), future: room.lastEvent != null ? GetChatListItemSubtitle().getSubtitle( - context, + L10n.of(context)!, room.lastEvent, MatrixState.pangeaController, ) diff --git a/lib/pangea/controllers/subscription_controller.dart b/lib/pangea/controllers/subscription_controller.dart index 7dac081d7f..eed1cb3719 100644 --- a/lib/pangea/controllers/subscription_controller.dart +++ b/lib/pangea/controllers/subscription_controller.dart @@ -33,8 +33,6 @@ enum CanSendStatus { class SubscriptionController extends BaseController { late PangeaController _pangeaController; SubscriptionInfo? subscription; - - bool initialized = false; final StreamController subscriptionStream = StreamController.broadcast(); SubscriptionController(PangeaController pangeaController) : super() { @@ -46,7 +44,28 @@ class SubscriptionController extends BaseController { (subscription!.currentSubscriptionId != null || subscription!.currentSubscription != null); + bool _isInitializing = false; + Completer initialized = Completer(); + Future initialize() async { + if (initialized.isCompleted) return; + if (_isInitializing) { + await initialized.future; + return; + } + _isInitializing = true; + await _initialize(); + _isInitializing = false; + initialized.complete(); + } + + Future reinitialize() async { + initialized = Completer(); + _isInitializing = false; + await initialize(); + } + + Future _initialize() async { try { if (_pangeaController.matrixState.client.userID == null) { debugPrint( @@ -64,8 +83,6 @@ class SubscriptionController extends BaseController { setNewUserTrial(); } - initialized = true; - if (!kIsWeb) { Purchases.addCustomerInfoUpdateListener( (CustomerInfo info) async { @@ -198,6 +215,9 @@ class SubscriptionController extends BaseController { } Future updateCustomerInfo() async { + if (!initialized.isCompleted) { + await initialize(); + } if (subscription == null) { ErrorHandler.logError( m: "Null subscription info in subscription settings", @@ -234,7 +254,7 @@ class SubscriptionController extends BaseController { } bool get _shouldShowPaywall { - return initialized && + return initialized.isCompleted && !isSubscribed && (_lastDismissedPaywall == null || DateTime.now().difference(_lastDismissedPaywall!).inHours > @@ -265,7 +285,7 @@ class SubscriptionController extends BaseController { Future showPaywall(BuildContext context) async { try { - if (!initialized) { + if (!initialized.isCompleted) { await initialize(); } if (subscription?.availableSubscriptions.isEmpty ?? true) { diff --git a/lib/pangea/models/mobile_subscriptions.dart b/lib/pangea/models/mobile_subscriptions.dart index 4a57d6200d..00fc6e3fd3 100644 --- a/lib/pangea/models/mobile_subscriptions.dart +++ b/lib/pangea/models/mobile_subscriptions.dart @@ -1,15 +1,13 @@ import 'dart:io'; -import 'package:flutter/material.dart'; - import 'package:collection/collection.dart'; -import 'package:purchases_flutter/purchases_flutter.dart'; -import 'package:sentry_flutter/sentry_flutter.dart'; - import 'package:fluffychat/pangea/config/environment.dart'; import 'package:fluffychat/pangea/controllers/subscription_controller.dart'; import 'package:fluffychat/pangea/models/base_subscription_info.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; +import 'package:flutter/material.dart'; +import 'package:purchases_flutter/purchases_flutter.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; class MobileSubscriptionInfo extends SubscriptionInfo { MobileSubscriptionInfo({required super.pangeaController}) : super(); @@ -119,11 +117,11 @@ class MobileSubscriptionInfo extends SubscriptionInfo { Future setCustomerInfo() async { if (allProducts == null) { ErrorHandler.logError( - m: "Null appProducts in setCustomerInfo", + m: "Null allProducts in setCustomerInfo", s: StackTrace.current, ); debugPrint( - "Null appProducts in setCustomerInfo", + "Null allProducts in setCustomerInfo", ); return; } diff --git a/lib/pangea/models/pangea_message_event.dart b/lib/pangea/models/pangea_message_event.dart index 61bcceb4ee..8dab042abc 100644 --- a/lib/pangea/models/pangea_message_event.dart +++ b/lib/pangea/models/pangea_message_event.dart @@ -95,7 +95,6 @@ class PangeaMessageEvent { BuildContext context, ) async { final String text = (await representationByLanguageGlobal( - context: context, langCode: langCode, )) ?.text ?? @@ -399,7 +398,6 @@ class PangeaMessageEvent { } Future representationByLanguageGlobal({ - required BuildContext context, required String langCode, }) async { // try { diff --git a/lib/pangea/pages/settings_subscription/settings_subscription.dart b/lib/pangea/pages/settings_subscription/settings_subscription.dart index 86c43ff47c..374abedb30 100644 --- a/lib/pangea/pages/settings_subscription/settings_subscription.dart +++ b/lib/pangea/pages/settings_subscription/settings_subscription.dart @@ -30,7 +30,7 @@ class SubscriptionManagementController extends State { @override void initState() { - if (!subscriptionController.initialized) { + if (!subscriptionController.initialized.isCompleted) { subscriptionController.initialize().then((_) => setState(() {})); } diff --git a/lib/pangea/utils/get_chat_list_item_subtitle.dart b/lib/pangea/utils/get_chat_list_item_subtitle.dart index e5b1388a91..f9616cc7c5 100644 --- a/lib/pangea/utils/get_chat_list_item_subtitle.dart +++ b/lib/pangea/utils/get_chat_list_item_subtitle.dart @@ -4,7 +4,6 @@ import 'package:fluffychat/pangea/controllers/pangea_controller.dart'; import 'package:fluffychat/pangea/models/class_model.dart'; import 'package:fluffychat/pangea/models/pangea_message_event.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; -import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:matrix/matrix.dart'; @@ -12,16 +11,17 @@ import '../../utils/matrix_sdk_extensions/matrix_locals.dart'; class GetChatListItemSubtitle { Future getSubtitle( - BuildContext context, + L10n l10n, Event? event, PangeaController pangeaController, ) async { - if (event == null) return L10n.of(context)!.emptyChat; + if (event == null) return l10n.emptyChat; try { String? eventContextId = event.eventId; if (!event.eventId.isValidMatrixId || event.eventId.sigil != '\$') { eventContextId = null; } + final Timeline timeline = await event.room.getTimeline(eventContextId: eventContextId); @@ -47,7 +47,7 @@ class GetChatListItemSubtitle { !pangeaController.permissionsController .isToolEnabled(ToolSetting.immersionMode, event.room)) { return event.calcLocalizedBody( - MatrixLocals(L10n.of(context)!), + MatrixLocals(l10n), hideReply: true, hideEdit: true, plaintextBody: true, @@ -71,14 +71,13 @@ class GetChatListItemSubtitle { final String? text = (await pangeaMessageEvent.representationByLanguageGlobal( - context: context, langCode: l2Code, )) ?.text; - final i18n = MatrixLocals(L10n.of(context)!); + final i18n = MatrixLocals(l10n); - if (text == null) return L10n.of(context)!.emptyChat; + if (text == null) return l10n.emptyChat; if (!event.room.isDirectChat || event.room.directChatMatrixID != event.room.lastEvent?.senderId) { @@ -95,7 +94,7 @@ class GetChatListItemSubtitle { } catch (e, s) { // debugger(when: kDebugMode); ErrorHandler.logError(e: e, s: s); - return event?.body ?? L10n.of(context)!.emptyChat; + return event?.body ?? l10n.emptyChat; } } } diff --git a/lib/pangea/widgets/chat/message_translation_card.dart b/lib/pangea/widgets/chat/message_translation_card.dart index 5014d83f33..ae1a26c4c3 100644 --- a/lib/pangea/widgets/chat/message_translation_card.dart +++ b/lib/pangea/widgets/chat/message_translation_card.dart @@ -51,7 +51,6 @@ class MessageTranslationCardState extends State { if (repEvent == null && mounted) { repEvent = await widget.messageEvent.representationByLanguageGlobal( - context: context, langCode: langCode, ); } diff --git a/lib/pangea/widgets/igc/pangea_rich_text.dart b/lib/pangea/widgets/igc/pangea_rich_text.dart index 2aef25099d..3ae2597d42 100644 --- a/lib/pangea/widgets/igc/pangea_rich_text.dart +++ b/lib/pangea/widgets/igc/pangea_rich_text.dart @@ -86,7 +86,6 @@ class PangeaRichTextState extends State { widget.pangeaMessageEvent .representationByLanguageGlobal( - context: context, langCode: widget.pangeaMessageEvent.messageDisplayLangCode, ) .onError((error, stackTrace) => ErrorHandler.logError()) diff --git a/lib/widgets/matrix.dart b/lib/widgets/matrix.dart index b67699a972..445044a3c1 100644 --- a/lib/widgets/matrix.dart +++ b/lib/widgets/matrix.dart @@ -344,6 +344,7 @@ class MatrixState extends State with WidgetsBindingObserver { // #Pangea if (state == LoginState.loggedIn) { await (await pangeaController.userController.completer).future; + await pangeaController.subscriptionController.reinitialize(); } String routeDestination; if (state == LoginState.loggedIn) {