Check OS permission and reveal child toggles in notification settings
This commit is contained in:
1 parent
9ac915bdf2
commit
eafc4f1152
6 files changed
+1024
-184
No files matched your search
@@ -63,9 +63,9 @@ class AppSettingsScreen extends StatelessWidget {
|
||||
|
||||
// NOTIFICATIONS
|
||||
SectionHeader(context.l10n.appSettings_notifications),
|
||||
MeshCard(
|
||||
const MeshCard(
|
||||
padding: EdgeInsets.zero,
|
||||
child: _settingsCard(_buildNotificationsContent),
|
||||
child: NotificationsSettingsSection(),
|
||||
),
|
||||
|
||||
// MESSAGING
|
||||
@@ -266,156 +266,6 @@ class AppSettingsScreen extends StatelessWidget {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildNotificationsContent(
|
||||
BuildContext context,
|
||||
AppSettingsService settingsService,
|
||||
) {
|
||||
final notifEnabled = settingsService.settings.notificationsEnabled;
|
||||
return Column(
|
||||
children: [
|
||||
SwitchListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
secondary: const Icon(Icons.notifications_outlined, size: 24),
|
||||
title: Text(context.l10n.appSettings_enableNotifications),
|
||||
subtitle: Text(context.l10n.appSettings_enableNotificationsSubtitle),
|
||||
value: settingsService.settings.notificationsEnabled,
|
||||
onChanged: (value) async {
|
||||
if (value) {
|
||||
final granted = await NotificationService().requestPermissions();
|
||||
if (!granted) {
|
||||
if (context.mounted) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(
|
||||
context.l10n.appSettings_notificationPermissionDenied,
|
||||
),
|
||||
duration: const Duration(seconds: 2),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
await settingsService.setNotificationsEnabled(value);
|
||||
if (context.mounted) {
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(
|
||||
value
|
||||
? context.l10n.appSettings_notificationsEnabled
|
||||
: context.l10n.appSettings_notificationsDisabled,
|
||||
),
|
||||
duration: const Duration(seconds: 2),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
secondary: Icon(
|
||||
Icons.message_outlined,
|
||||
size: 24,
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
title: Text(
|
||||
context.l10n.appSettings_messageNotifications,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
context.l10n.appSettings_messageNotificationsSubtitle,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
value: settingsService.settings.notifyOnNewMessage,
|
||||
onChanged: notifEnabled
|
||||
? (value) => settingsService.setNotifyOnNewMessage(value)
|
||||
: null,
|
||||
),
|
||||
if (PlatformInfo.isAndroid) ...[
|
||||
const Divider(height: 1, indent: 16),
|
||||
ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
leading: const Icon(Icons.battery_saver_outlined, size: 24),
|
||||
title: Text(context.l10n.appSettings_batteryOptimization),
|
||||
subtitle: Text(
|
||||
context.l10n.appSettings_batteryOptimizationSubtitle,
|
||||
),
|
||||
trailing: const Icon(Icons.open_in_new, size: 24),
|
||||
onTap: BackgroundService.openBatteryOptimizationSettings,
|
||||
),
|
||||
],
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
secondary: Icon(
|
||||
Icons.forum_outlined,
|
||||
size: 24,
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
title: Text(
|
||||
context.l10n.appSettings_channelMessageNotifications,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
context.l10n.appSettings_channelMessageNotificationsSubtitle,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
value: settingsService.settings.notifyOnNewChannelMessage,
|
||||
onChanged: notifEnabled
|
||||
? (value) => settingsService.setNotifyOnNewChannelMessage(value)
|
||||
: null,
|
||||
),
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
secondary: Icon(
|
||||
Icons.cell_tower,
|
||||
size: 24,
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
title: Text(
|
||||
context.l10n.appSettings_advertisementNotifications,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
context.l10n.appSettings_advertisementNotificationsSubtitle,
|
||||
style: TextStyle(
|
||||
color: notifEnabled ? null : Theme.of(context).disabledColor,
|
||||
),
|
||||
),
|
||||
value: settingsService.settings.notifyOnNewAdvert,
|
||||
onChanged: notifEnabled
|
||||
? (value) => settingsService.setNotifyOnNewAdvert(value)
|
||||
: null,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMessagingContent(
|
||||
BuildContext context,
|
||||
AppSettingsService settingsService,
|
||||
@@ -1870,3 +1720,205 @@ class _ScrollIntoViewOnceState extends State<_ScrollIntoViewOnce> {
|
||||
@override
|
||||
Widget build(BuildContext context) => widget.child;
|
||||
}
|
||||
|
||||
class NotificationsSettingsSection extends StatefulWidget {
|
||||
final Future<bool> Function()? hasPermission;
|
||||
final Future<bool> Function()? requestPermission;
|
||||
|
||||
const NotificationsSettingsSection({
|
||||
super.key,
|
||||
this.hasPermission,
|
||||
this.requestPermission,
|
||||
});
|
||||
|
||||
@override
|
||||
State<NotificationsSettingsSection> createState() =>
|
||||
_NotificationsSettingsSectionState();
|
||||
}
|
||||
|
||||
class _NotificationsSettingsSectionState
|
||||
extends State<NotificationsSettingsSection>
|
||||
with WidgetsBindingObserver {
|
||||
bool _hasNotificationPermission = false;
|
||||
bool _permissionCheckComplete = false;
|
||||
int _permissionCheckGeneration = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_refreshNotificationPermission(inInit: true);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant NotificationsSettingsSection oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.hasPermission != oldWidget.hasPermission) {
|
||||
_refreshNotificationPermission();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
_refreshNotificationPermission();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> _checkPermission() {
|
||||
final checker = widget.hasPermission;
|
||||
if (checker != null) return checker();
|
||||
return NotificationService().hasNotificationPermission();
|
||||
}
|
||||
|
||||
Future<bool> _requestPermission() {
|
||||
final requester = widget.requestPermission;
|
||||
if (requester != null) return requester();
|
||||
return NotificationService().requestPermissions();
|
||||
}
|
||||
|
||||
void _refreshNotificationPermission({bool inInit = false}) {
|
||||
final generation = ++_permissionCheckGeneration;
|
||||
var completedSynchronously = false;
|
||||
var syncResult = false;
|
||||
var isSyncWindow = true;
|
||||
_checkPermission().then((granted) {
|
||||
if (generation != _permissionCheckGeneration) return;
|
||||
if (isSyncWindow && inInit) {
|
||||
completedSynchronously = true;
|
||||
syncResult = granted;
|
||||
return;
|
||||
}
|
||||
if (!mounted) return;
|
||||
if (_permissionCheckComplete && _hasNotificationPermission == granted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_hasNotificationPermission = granted;
|
||||
_permissionCheckComplete = true;
|
||||
});
|
||||
});
|
||||
isSyncWindow = false;
|
||||
if (completedSynchronously) {
|
||||
_hasNotificationPermission = syncResult;
|
||||
_permissionCheckComplete = true;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final settingsService = context.watch<AppSettingsService>();
|
||||
final settings = settingsService.settings;
|
||||
final l10n = context.l10n;
|
||||
final effectiveEnabled =
|
||||
settings.notificationsEnabled && _hasNotificationPermission;
|
||||
final permissionDenied =
|
||||
_permissionCheckComplete && !_hasNotificationPermission;
|
||||
const tilePadding = EdgeInsets.symmetric(horizontal: 16, vertical: 4);
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
SwitchListTile(
|
||||
contentPadding: tilePadding,
|
||||
secondary: const Icon(Icons.notifications_outlined, size: 24),
|
||||
title: Text(l10n.appSettings_enableNotifications),
|
||||
subtitle: Text(
|
||||
permissionDenied
|
||||
? l10n.appSettings_notificationPermissionDenied
|
||||
: l10n.appSettings_enableNotificationsSubtitle,
|
||||
),
|
||||
value: effectiveEnabled,
|
||||
onChanged: (value) async {
|
||||
_permissionCheckGeneration++;
|
||||
if (value) {
|
||||
final granted = await _requestPermission();
|
||||
if (!granted) {
|
||||
if (!context.mounted) return;
|
||||
setState(() {
|
||||
_hasNotificationPermission = false;
|
||||
_permissionCheckComplete = true;
|
||||
});
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(l10n.appSettings_notificationPermissionDenied),
|
||||
duration: const Duration(seconds: 2),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (context.mounted) {
|
||||
setState(() {
|
||||
_hasNotificationPermission = true;
|
||||
_permissionCheckComplete = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
await settingsService.setNotificationsEnabled(value);
|
||||
if (!context.mounted) return;
|
||||
showDismissibleSnackBar(
|
||||
context,
|
||||
content: Text(
|
||||
value
|
||||
? l10n.appSettings_notificationsEnabled
|
||||
: l10n.appSettings_notificationsDisabled,
|
||||
),
|
||||
duration: const Duration(seconds: 2),
|
||||
);
|
||||
},
|
||||
),
|
||||
SettingsReveal(
|
||||
visible: effectiveEnabled,
|
||||
children: [
|
||||
SwitchListTile(
|
||||
contentPadding: tilePadding,
|
||||
secondary: const Icon(Icons.message_outlined, size: 24),
|
||||
title: Text(l10n.appSettings_messageNotifications),
|
||||
subtitle: Text(l10n.appSettings_messageNotificationsSubtitle),
|
||||
value: settings.notifyOnNewMessage,
|
||||
onChanged: settingsService.setNotifyOnNewMessage,
|
||||
),
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: tilePadding,
|
||||
secondary: const Icon(Icons.forum_outlined, size: 24),
|
||||
title: Text(l10n.appSettings_channelMessageNotifications),
|
||||
subtitle: Text(
|
||||
l10n.appSettings_channelMessageNotificationsSubtitle,
|
||||
),
|
||||
value: settings.notifyOnNewChannelMessage,
|
||||
onChanged: settingsService.setNotifyOnNewChannelMessage,
|
||||
),
|
||||
const Divider(height: 1, indent: 16),
|
||||
SwitchListTile(
|
||||
contentPadding: tilePadding,
|
||||
secondary: const Icon(Icons.cell_tower, size: 24),
|
||||
title: Text(l10n.appSettings_advertisementNotifications),
|
||||
subtitle: Text(
|
||||
l10n.appSettings_advertisementNotificationsSubtitle,
|
||||
),
|
||||
value: settings.notifyOnNewAdvert,
|
||||
onChanged: settingsService.setNotifyOnNewAdvert,
|
||||
),
|
||||
if (PlatformInfo.isAndroid) ...[
|
||||
const Divider(height: 1, indent: 16),
|
||||
ListTile(
|
||||
contentPadding: tilePadding,
|
||||
leading: const Icon(Icons.battery_saver_outlined, size: 24),
|
||||
title: Text(l10n.appSettings_batteryOptimization),
|
||||
subtitle: Text(l10n.appSettings_batteryOptimizationSubtitle),
|
||||
trailing: const Icon(Icons.open_in_new, size: 24),
|
||||
onTap: BackgroundService.openBatteryOptimizationSettings,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user