From df35c6ee8095c173ff800860cf2e446b2dbc16dd Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 1 Aug 2023 22:25:27 +0200 Subject: [PATCH] refactor(neon): Cleanup global popups --- packages/neon/neon/lib/src/pages/home.dart | 1 + .../neon/lib/src/utils/global_popups.dart | 110 ++++++++++-------- 2 files changed, 65 insertions(+), 46 deletions(-) diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index b0e31211..f0792c17 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -74,6 +74,7 @@ class _HomePageState extends State { @override void dispose() { unawaited(_versionCheckSubscription.cancel()); + GlobalPopups().dispose(); super.dispose(); } diff --git a/packages/neon/neon/lib/src/utils/global_popups.dart b/packages/neon/neon/lib/src/utils/global_popups.dart index 58b12787..10748f2b 100644 --- a/packages/neon/neon/lib/src/utils/global_popups.dart +++ b/packages/neon/neon/lib/src/utils/global_popups.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; import 'package:meta/meta.dart'; import 'package:neon/l10n/localizations.dart'; @@ -23,8 +25,19 @@ class GlobalPopups { static GlobalPopups? instance; bool _registered = false; + late BuildContext _context; + final _subscriptions = []; + + void dispose() { + for (final subscription in _subscriptions) { + unawaited(subscription.cancel()); + } + _subscriptions.clear(); + _registered = false; + } void register(final BuildContext context) { + _context = context; if (_registered) { return; } @@ -34,55 +47,60 @@ class GlobalPopups { final nextPushBloc = Provider.of(context, listen: false); final platform = Provider.of(context, listen: false); - firstLaunchBloc.onFirstLaunch.listen((final _) { - if (!platform.canUsePushNotifications || !globalOptions.pushNotificationsEnabled.enabled) { - return; - } + _subscriptions.addAll([ + if (platform.canUsePushNotifications) ...[ + firstLaunchBloc.onFirstLaunch.listen((final _) { + assert(context.mounted, 'Context should be mounted'); + if (!globalOptions.pushNotificationsEnabled.enabled) { + return; + } - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text(AppLocalizations.of(context).firstLaunchGoToSettingsToEnablePushNotifications), - action: SnackBarAction( - label: AppLocalizations.of(context).settings, - onPressed: () { - const SettingsRoute(initialCategory: SettingsCageories.pushNotifications).go(context); - }, - ), - ), - ); - }); - - nextPushBloc.onNextPushSupported.listen((final _) async { - if (!platform.canUsePushNotifications || !globalOptions.pushNotificationsEnabled.enabled) { - return; - } - - await showDialog( - context: context, - builder: (final context) => AlertDialog( - title: Text(AppLocalizations.of(context).nextPushSupported), - content: Text(AppLocalizations.of(context).nextPushSupportedText), - actions: [ - OutlinedButton( - onPressed: () { - Navigator.of(context).pop(); - }, - child: Text(AppLocalizations.of(context).actionNo), + ScaffoldMessenger.of(_context).showSnackBar( + SnackBar( + content: Text(AppLocalizations.of(_context).firstLaunchGoToSettingsToEnablePushNotifications), + action: SnackBarAction( + label: AppLocalizations.of(_context).settings, + onPressed: () { + const SettingsRoute(initialCategory: SettingsCageories.pushNotifications).go(_context); + }, + ), ), - ElevatedButton( - onPressed: () { - Navigator.of(context).pop(); - launchUrlString( - 'https://f-droid.org/packages/$unifiedPushNextPushID', - mode: LaunchMode.externalApplication, - ); - }, - child: Text(AppLocalizations.of(context).nextPushSupportedInstall), + ); + }), + nextPushBloc.onNextPushSupported.listen((final _) async { + assert(context.mounted, 'Context should be mounted'); + if (!globalOptions.pushNotificationsEnabled.enabled) { + return; + } + + await showDialog( + context: _context, + builder: (final context) => AlertDialog( + title: Text(AppLocalizations.of(context).nextPushSupported), + content: Text(AppLocalizations.of(context).nextPushSupportedText), + actions: [ + OutlinedButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: Text(AppLocalizations.of(context).actionNo), + ), + ElevatedButton( + onPressed: () { + Navigator.of(context).pop(); + launchUrlString( + 'https://f-droid.org/packages/$unifiedPushNextPushID', + mode: LaunchMode.externalApplication, + ); + }, + child: Text(AppLocalizations.of(context).nextPushSupportedInstall), + ), + ], ), - ], - ), - ); - }); + ); + }), + ], + ]); _registered = true; }