Browse Source

neon: make GlobalPopups a singleton

Signed-off-by: Nikolas Rimikis <rimikis.nikolas@gmail.com>
pull/314/head
Nikolas Rimikis 2 years ago
parent
commit
dc9a8a71b0
No known key found for this signature in database
GPG Key ID: 85ED1DE9786A4FF2
  1. 2
      packages/neon/neon/lib/src/pages/home.dart
  2. 12
      packages/neon/neon/lib/src/router.dart
  3. 44
      packages/neon/neon/lib/src/utils/global_popups.dart

2
packages/neon/neon/lib/src/pages/home.dart

@ -93,6 +93,8 @@ class _HomePageState extends State<HomePage> {
} }
}); });
GlobalPopups().register(context);
unawaited(_checkMaintenanceMode()); unawaited(_checkMaintenanceMode());
} }

12
packages/neon/neon/lib/src/router.dart

@ -8,7 +8,6 @@ class AppRouter extends RouterDelegate<Account> with ChangeNotifier, PopNavigato
}); });
final AccountsBloc accountsBloc; final AccountsBloc accountsBloc;
final _globalPopups = const GlobalPopups();
@override @override
final GlobalKey<NavigatorState> navigatorKey; final GlobalKey<NavigatorState> navigatorKey;
@ -33,14 +32,9 @@ class AppRouter extends RouterDelegate<Account> with ChangeNotifier, PopNavigato
name: 'home', name: 'home',
child: Scaffold( child: Scaffold(
resizeToAvoidBottomInset: false, resizeToAvoidBottomInset: false,
body: Stack( body: HomePage(
children: [ key: Key(currentConfiguration!.id),
_globalPopups, account: currentConfiguration!,
HomePage(
key: Key(currentConfiguration!.id),
account: currentConfiguration!,
),
],
), ),
), ),
), ),

44
packages/neon/neon/lib/src/utils/global_popups.dart

@ -1,30 +1,25 @@
part of '../../neon.dart'; part of '../../neon.dart';
class GlobalPopups extends StatefulWidget { class GlobalPopups {
const GlobalPopups({ factory GlobalPopups() => _instance ??= GlobalPopups._();
super.key, GlobalPopups._();
});
@override static GlobalPopups? _instance;
State<GlobalPopups> createState() => _GlobalPopupsState(); bool _registered = false;
}
class _GlobalPopupsState extends State<GlobalPopups> {
late GlobalOptions _globalOptions;
late FirstLaunchBloc _firstLaunchBloc;
late NextPushBloc _nextPushBloc;
@override void register(final BuildContext context) {
void initState() { if (_registered) {
super.initState(); return;
}
_globalOptions = Provider.of<GlobalOptions>(context, listen: false); final globalOptions = Provider.of<GlobalOptions>(context, listen: false);
_firstLaunchBloc = Provider.of<FirstLaunchBloc>(context, listen: false); final firstLaunchBloc = Provider.of<FirstLaunchBloc>(context, listen: false);
_nextPushBloc = Provider.of<NextPushBloc>(context, listen: false); final nextPushBloc = Provider.of<NextPushBloc>(context, listen: false);
_firstLaunchBloc.onFirstLaunch.listen((final _) async { firstLaunchBloc.onFirstLaunch.listen((final _) async {
if (await _globalOptions.pushNotificationsEnabled.enabled.first) { if (await globalOptions.pushNotificationsEnabled.enabled.first) {
if (!mounted) { // ignore: use_build_context_synchronously
if (!context.mounted) {
return; return;
} }
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
@ -45,7 +40,7 @@ class _GlobalPopupsState extends State<GlobalPopups> {
} }
}); });
_nextPushBloc.onNextPushSupported.listen((final _) async { nextPushBloc.onNextPushSupported.listen((final _) async {
await showDialog( await showDialog(
context: context, context: context,
builder: (final context) => AlertDialog( builder: (final context) => AlertDialog(
@ -72,8 +67,7 @@ class _GlobalPopupsState extends State<GlobalPopups> {
), ),
); );
}); });
}
@override _registered = true;
Widget build(final BuildContext context) => Container(); }
} }

Loading…
Cancel
Save