Browse Source

refactor(neon): Cleanup global popups

pull/509/head
jld3103 1 year ago
parent
commit
df35c6ee80
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 1
      packages/neon/neon/lib/src/pages/home.dart
  2. 38
      packages/neon/neon/lib/src/utils/global_popups.dart

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

@ -74,6 +74,7 @@ class _HomePageState extends State<HomePage> {
@override
void dispose() {
unawaited(_versionCheckSubscription.cancel());
GlobalPopups().dispose();
super.dispose();
}

38
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 = <StreamSubscription>[];
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,31 +47,34 @@ class GlobalPopups {
final nextPushBloc = Provider.of<NextPushBloc>(context, listen: false);
final platform = Provider.of<NeonPlatform>(context, listen: false);
_subscriptions.addAll([
if (platform.canUsePushNotifications) ...[
firstLaunchBloc.onFirstLaunch.listen((final _) {
if (!platform.canUsePushNotifications || !globalOptions.pushNotificationsEnabled.enabled) {
assert(context.mounted, 'Context should be mounted');
if (!globalOptions.pushNotificationsEnabled.enabled) {
return;
}
ScaffoldMessenger.of(context).showSnackBar(
ScaffoldMessenger.of(_context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context).firstLaunchGoToSettingsToEnablePushNotifications),
content: Text(AppLocalizations.of(_context).firstLaunchGoToSettingsToEnablePushNotifications),
action: SnackBarAction(
label: AppLocalizations.of(context).settings,
label: AppLocalizations.of(_context).settings,
onPressed: () {
const SettingsRoute(initialCategory: SettingsCageories.pushNotifications).go(context);
const SettingsRoute(initialCategory: SettingsCageories.pushNotifications).go(_context);
},
),
),
);
});
}),
nextPushBloc.onNextPushSupported.listen((final _) async {
if (!platform.canUsePushNotifications || !globalOptions.pushNotificationsEnabled.enabled) {
assert(context.mounted, 'Context should be mounted');
if (!globalOptions.pushNotificationsEnabled.enabled) {
return;
}
await showDialog(
context: context,
context: _context,
builder: (final context) => AlertDialog(
title: Text(AppLocalizations.of(context).nextPushSupported),
content: Text(AppLocalizations.of(context).nextPushSupportedText),
@ -82,7 +98,9 @@ class GlobalPopups {
],
),
);
});
}),
],
]);
_registered = true;
}

Loading…
Cancel
Save