From dc9a8a71b033b784bf744032acc81a994fe8ddad Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Fri, 12 May 2023 17:25:44 +0200 Subject: [PATCH] neon: make GlobalPopups a singleton Signed-off-by: Nikolas Rimikis --- packages/neon/neon/lib/src/pages/home.dart | 2 + packages/neon/neon/lib/src/router.dart | 12 ++--- .../neon/lib/src/utils/global_popups.dart | 44 ++++++++----------- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index d807df3a..54dc64eb 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -93,6 +93,8 @@ class _HomePageState extends State { } }); + GlobalPopups().register(context); + unawaited(_checkMaintenanceMode()); } diff --git a/packages/neon/neon/lib/src/router.dart b/packages/neon/neon/lib/src/router.dart index e482c58f..cb76d6ee 100644 --- a/packages/neon/neon/lib/src/router.dart +++ b/packages/neon/neon/lib/src/router.dart @@ -8,7 +8,6 @@ class AppRouter extends RouterDelegate with ChangeNotifier, PopNavigato }); final AccountsBloc accountsBloc; - final _globalPopups = const GlobalPopups(); @override final GlobalKey navigatorKey; @@ -33,14 +32,9 @@ class AppRouter extends RouterDelegate with ChangeNotifier, PopNavigato name: 'home', child: Scaffold( resizeToAvoidBottomInset: false, - body: Stack( - children: [ - _globalPopups, - HomePage( - key: Key(currentConfiguration!.id), - account: currentConfiguration!, - ), - ], + body: HomePage( + key: Key(currentConfiguration!.id), + account: currentConfiguration!, ), ), ), diff --git a/packages/neon/neon/lib/src/utils/global_popups.dart b/packages/neon/neon/lib/src/utils/global_popups.dart index bf0a210f..c9412d5a 100644 --- a/packages/neon/neon/lib/src/utils/global_popups.dart +++ b/packages/neon/neon/lib/src/utils/global_popups.dart @@ -1,30 +1,25 @@ part of '../../neon.dart'; -class GlobalPopups extends StatefulWidget { - const GlobalPopups({ - super.key, - }); +class GlobalPopups { + factory GlobalPopups() => _instance ??= GlobalPopups._(); + GlobalPopups._(); - @override - State createState() => _GlobalPopupsState(); -} - -class _GlobalPopupsState extends State { - late GlobalOptions _globalOptions; - late FirstLaunchBloc _firstLaunchBloc; - late NextPushBloc _nextPushBloc; + static GlobalPopups? _instance; + bool _registered = false; - @override - void initState() { - super.initState(); + void register(final BuildContext context) { + if (_registered) { + return; + } - _globalOptions = Provider.of(context, listen: false); - _firstLaunchBloc = Provider.of(context, listen: false); - _nextPushBloc = Provider.of(context, listen: false); + final globalOptions = Provider.of(context, listen: false); + final firstLaunchBloc = Provider.of(context, listen: false); + final nextPushBloc = Provider.of(context, listen: false); - _firstLaunchBloc.onFirstLaunch.listen((final _) async { - if (await _globalOptions.pushNotificationsEnabled.enabled.first) { - if (!mounted) { + firstLaunchBloc.onFirstLaunch.listen((final _) async { + if (await globalOptions.pushNotificationsEnabled.enabled.first) { + // ignore: use_build_context_synchronously + if (!context.mounted) { return; } ScaffoldMessenger.of(context).showSnackBar( @@ -45,7 +40,7 @@ class _GlobalPopupsState extends State { } }); - _nextPushBloc.onNextPushSupported.listen((final _) async { + nextPushBloc.onNextPushSupported.listen((final _) async { await showDialog( context: context, builder: (final context) => AlertDialog( @@ -72,8 +67,7 @@ class _GlobalPopupsState extends State { ), ); }); - } - @override - Widget build(final BuildContext context) => Container(); + _registered = true; + } }