Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

photos: fix subscription toggle state

laurenspriem3c6c858b159e
diff --git a/mobile/apps/photos/lib/ui/payment/store_subscription_page.dart b/mobile/apps/photos/lib/ui/payment/store_subscription_page.dart
index ca95197604..0870d81a21 100644
--- a/mobile/apps/photos/lib/ui/payment/store_subscription_page.dart
+++ b/mobile/apps/photos/lib/ui/payment/store_subscription_page.dart
@@ -322,6 +322,7 @@ class _StoreSubscriptionPageState extends State<StoreSubscriptionPage> {
if (hasYearlyPlans) {
widgets.add(
SubscriptionToggle(
+ isYearly: showYearlyPlan,
onToggle: (p0) {
showYearlyPlan = p0;
_filterStorePlansForUi();
diff --git a/mobile/apps/photos/lib/ui/payment/stripe_subscription_page.dart b/mobile/apps/photos/lib/ui/payment/stripe_subscription_page.dart
index 9086ed6eb4..e896da08b6 100644
--- a/mobile/apps/photos/lib/ui/payment/stripe_subscription_page.dart
+++ b/mobile/apps/photos/lib/ui/payment/stripe_subscription_page.dart
@@ -237,6 +237,7 @@ class _StripeSubscriptionPageState extends State<StripeSubscriptionPage> {
widgets.add(
SubscriptionToggle(
+ isYearly: _showYearlyPlan,
onToggle: (p0) {
_showYearlyPlan = p0;
_filterStripeForUI();
diff --git a/mobile/apps/photos/lib/ui/payment/subscription_common_widgets.dart b/mobile/apps/photos/lib/ui/payment/subscription_common_widgets.dart
index a18b1de21c..5dbaf1e59a 100644
--- a/mobile/apps/photos/lib/ui/payment/subscription_common_widgets.dart
+++ b/mobile/apps/photos/lib/ui/payment/subscription_common_widgets.dart
@@ -131,15 +131,35 @@ class SubFaqWidget extends StatelessWidget {
}
class SubscriptionToggle extends StatefulWidget {
+ final bool isYearly;
final Function(bool) onToggle;
- const SubscriptionToggle({required this.onToggle, super.key});
+ const SubscriptionToggle({
+ required this.isYearly,
+ required this.onToggle,
+ super.key,
+ });
@override
State<SubscriptionToggle> createState() => _SubscriptionToggleState();
}
class _SubscriptionToggleState extends State<SubscriptionToggle> {
- bool _isYearly = true;
+ late bool _isYearly;
+
+ @override
+ void initState() {
+ super.initState();
+ _isYearly = widget.isYearly;
+ }
+
+ @override
+ void didUpdateWidget(covariant SubscriptionToggle oldWidget) {
+ super.didUpdateWidget(oldWidget);
+ if (oldWidget.isYearly != widget.isYearly) {
+ _isYearly = widget.isYearly;
+ }
+ }
+
@override
Widget build(BuildContext context) {
const borderPadding = 2.5;
diff --git a/mobile/apps/photos/test/ui/payment/subscription_toggle_test.dart b/mobile/apps/photos/test/ui/payment/subscription_toggle_test.dart
new file mode 100644
index 0000000000..f891f3b517
--- /dev/null
+++ b/mobile/apps/photos/test/ui/payment/subscription_toggle_test.dart
@@ -0,0 +1,67 @@
+import "package:flutter/material.dart";
+import "package:flutter_test/flutter_test.dart";
+import "package:photos/ente_theme_data.dart";
+import "package:photos/generated/intl/app_localizations.dart";
+import "package:photos/ui/payment/subscription_common_widgets.dart";
+
+void main() {
+ group("SubscriptionToggle", () {
+ testWidgets("uses the provided initial billing period", (tester) async {
+ await tester.pumpWidget(
+ _buildTestApp(
+ const SubscriptionToggle(
+ isYearly: false,
+ onToggle: _noopToggle,
+ ),
+ ),
+ );
+
+ final togglePosition =
+ tester.widget<AnimatedPositioned>(find.byType(AnimatedPositioned));
+ expect(togglePosition.left, greaterThan(0));
+ });
+
+ testWidgets("updates when the selected billing period changes", (
+ tester,
+ ) async {
+ await tester.pumpWidget(
+ _buildTestApp(
+ const SubscriptionToggle(
+ isYearly: false,
+ onToggle: _noopToggle,
+ ),
+ ),
+ );
+
+ await tester.pumpWidget(
+ _buildTestApp(
+ const SubscriptionToggle(
+ isYearly: true,
+ onToggle: _noopToggle,
+ ),
+ ),
+ );
+ await tester.pumpAndSettle();
+
+ final togglePosition =
+ tester.widget<AnimatedPositioned>(find.byType(AnimatedPositioned));
+ expect(togglePosition.left, 0);
+ });
+ });
+}
+
+Widget _buildTestApp(Widget child) {
+ return MaterialApp(
+ theme: darkThemeData,
+ localizationsDelegates: AppLocalizations.localizationsDelegates,
+ supportedLocales: AppLocalizations.supportedLocales,
+ home: Scaffold(
+ body: SizedBox(
+ width: 320,
+ child: child,
+ ),
+ ),
+ );
+}
+
+void _noopToggle(bool _) {}