Browse Source

Merge pull request #314 from Leptopoda/cleanup/globalPopups

neon: make GlobalPopups a singleton
pull/316/head
Kate 2 years ago committed by GitHub
parent
commit
2067de09a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      packages/neon/neon/lib/src/pages/home.dart
  2. 8
      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());
}

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

@ -8,7 +8,6 @@ class AppRouter extends RouterDelegate<Account> with ChangeNotifier, PopNavigato
});
final AccountsBloc accountsBloc;
final _globalPopups = const GlobalPopups();
@override
final GlobalKey<NavigatorState> navigatorKey;
@ -33,15 +32,10 @@ class AppRouter extends RouterDelegate<Account> with ChangeNotifier, PopNavigato
name: 'home',
child: Scaffold(
resizeToAvoidBottomInset: false,
body: Stack(
children: [
_globalPopups,
HomePage(
body: 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';
class GlobalPopups extends StatefulWidget {
const GlobalPopups({
super.key,
});
@override
State<GlobalPopups> createState() => _GlobalPopupsState();
}
class GlobalPopups {
factory GlobalPopups() => _instance ??= GlobalPopups._();
GlobalPopups._();
class _GlobalPopupsState extends State<GlobalPopups> {
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<GlobalOptions>(context, listen: false);
_firstLaunchBloc = Provider.of<FirstLaunchBloc>(context, listen: false);
_nextPushBloc = Provider.of<NextPushBloc>(context, listen: false);
final globalOptions = Provider.of<GlobalOptions>(context, listen: false);
final firstLaunchBloc = Provider.of<FirstLaunchBloc>(context, listen: false);
final nextPushBloc = Provider.of<NextPushBloc>(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<GlobalPopups> {
}
});
_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<GlobalPopups> {
),
);
});
}
@override
Widget build(final BuildContext context) => Container();
_registered = true;
}
}

Loading…
Cancel
Save