Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: show loading indicator and don't hide button on subscription page #2023

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 7 additions & 25 deletions lib/pangea/subscription/controllers/subscription_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:collection/collection.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
Expand Down Expand Up @@ -195,30 +194,13 @@ class SubscriptionController extends BaseController {
);
return;
}
try {
GoogleAnalytics.beginPurchaseSubscription(
selectedSubscription,
context,
);
await Purchases.purchasePackage(selectedSubscription.package!);
GoogleAnalytics.updateUserSubscriptionStatus(true);
} catch (err) {
final errCode = PurchasesErrorHelper.getErrorCode(
err as PlatformException,
);
if (errCode == PurchasesErrorCode.purchaseCancelledError) {
debugPrint("User cancelled purchase");
return;
}
ErrorHandler.logError(
m: "Failed to purchase revenuecat package for user $_userID with error code $errCode",
s: StackTrace.current,
data: {
"selectedSubscription": selectedSubscription.toJson(),
},
);
return;
}

GoogleAnalytics.beginPurchaseSubscription(
selectedSubscription,
context,
);
await Purchases.purchasePackage(selectedSubscription.package!);
GoogleAnalytics.updateUserSubscriptionStatus(true);
}
}

Expand Down
37 changes: 20 additions & 17 deletions lib/pangea/subscription/pages/change_subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,27 @@ class ChangeSubscription extends StatelessWidget {
const Divider(height: 1),
SubscriptionButtons(controller: controller),
const SizedBox(height: 32),
if (controller.selectedSubscription != null)
IntrinsicWidth(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
OutlinedButton(
onPressed: () => controller.submitChange(),
child: Text(
controller.selectedSubscription!.isTrial
? L10n.of(context).activateTrial
: L10n.of(context).pay,
),
),
const SizedBox(height: 20),
],
),
IntrinsicWidth(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
OutlinedButton(
onPressed: controller.selectedSubscription != null
? () => controller.submitChange()
: null,
child: controller.loading
? const CircularProgressIndicator.adaptive()
: Text(
controller.selectedSubscription?.isTrial ?? false
? L10n.of(context).activateTrial
: L10n.of(context).pay,
),
),
const SizedBox(height: 20),
],
),
),
],
)
: const Center(
Expand Down
36 changes: 19 additions & 17 deletions lib/pangea/subscription/pages/settings_subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:async';

import 'package:flutter/material.dart';

import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:go_router/go_router.dart';
import 'package:url_launcher/url_launcher_string.dart';

Expand All @@ -12,7 +11,7 @@ import 'package:fluffychat/pangea/subscription/controllers/subscription_controll
import 'package:fluffychat/pangea/subscription/pages/settings_subscription_view.dart';
import 'package:fluffychat/pangea/subscription/utils/subscription_app_id.dart';
import 'package:fluffychat/pangea/subscription/widgets/subscription_snackbar.dart';
import 'package:fluffychat/widgets/adaptive_dialogs/show_ok_cancel_alert_dialog.dart';
import 'package:fluffychat/widgets/future_loading_dialog.dart';
import 'package:fluffychat/widgets/matrix.dart';

class SubscriptionManagement extends StatefulWidget {
Expand All @@ -26,9 +25,12 @@ class SubscriptionManagement extends StatefulWidget {
class SubscriptionManagementController extends State<SubscriptionManagement> {
final SubscriptionController subscriptionController =
MatrixState.pangeaController.subscriptionController;

SubscriptionDetails? selectedSubscription;
late StreamSubscription _settingsSubscription;
StreamSubscription? _subscriptionStatusStream;
bool loading = false;

late StreamSubscription _settingsSubscription;

@override
void initState() {
Expand Down Expand Up @@ -100,23 +102,23 @@ class SubscriptionManagementController extends State<SubscriptionManagement> {
.currentSubscriptionInfo!.currentPlatformMatchesPurchasePlatform;
}

void submitChange({bool isPromo = false}) {
try {
subscriptionController.submitSubscriptionChange(
Future<void> submitChange({bool isPromo = false}) async {
setState(() => loading = true);
await showFutureLoadingDialog(
context: context,
future: () async => subscriptionController.submitSubscriptionChange(
selectedSubscription,
context,
isPromo: isPromo,
);
setState(() {
selectedSubscription = null;
});
} catch (err) {
showOkAlertDialog(
context: context,
title: L10n.of(context).oopsSomethingWentWrong,
message: L10n.of(context).errorPleaseRefresh,
okLabel: L10n.of(context).close,
);
),
onError: (error, s) {
setState(() => loading = false);
return null;
},
);

if (mounted && loading) {
setState(() => loading = false);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/future_loading_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Future<Result<T>> showFutureLoadingDialog<T>({
bool delay = true,
ExceptionContext? exceptionContext,
// #Pangea
String Function(Object, StackTrace?)? onError,
String? Function(Object, StackTrace?)? onError,
VoidCallback? onDismiss,
// Pangea#
}) async {
Expand Down Expand Up @@ -79,7 +79,7 @@ class LoadingDialog<T> extends StatefulWidget {
final Future<T> future;
final ExceptionContext? exceptionContext;
// #Pangea
final String Function(Object, StackTrace?)? onError;
final String? Function(Object, StackTrace?)? onError;
final VoidCallback? onDismiss;
// Pangea#

Expand Down
Loading