veilidchat/lib/notifications/views/notifications_preferences.dart
2025-05-27 16:43:38 -04:00

254 lines
10 KiB
Dart

import 'package:awesome_extensions/awesome_extensions.dart';
import 'package:flutter/material.dart';
import 'package:flutter_translate/flutter_translate.dart';
import '../../settings/settings.dart';
import '../../theme/theme.dart';
import '../notifications.dart';
Widget buildSettingsPageNotificationPreferences(
{required BuildContext context}) {
final theme = Theme.of(context);
final scale = theme.extension<ScaleScheme>()!;
final scaleConfig = theme.extension<ScaleConfig>()!;
final textTheme = theme.textTheme;
final preferencesRepository = PreferencesRepository.instance;
final notificationsPreference =
preferencesRepository.value.notificationsPreference;
Future<void> updatePreferences(
NotificationsPreference newNotificationsPreference) async {
final newPrefs = preferencesRepository.value
.copyWith(notificationsPreference: newNotificationsPreference);
await preferencesRepository.set(newPrefs);
}
List<DropdownMenuItem<NotificationMode>> notificationModeItems() {
final out = <DropdownMenuItem<NotificationMode>>[];
final items = [
(NotificationMode.none, true, translate('settings_page.none')),
(NotificationMode.inApp, true, translate('settings_page.in_app')),
(NotificationMode.push, false, translate('settings_page.push')),
(
NotificationMode.inAppOrPush,
true,
translate('settings_page.in_app_or_push')
),
];
for (final x in items) {
out.add(DropdownMenuItem(
value: x.$1,
enabled: x.$2,
child: Text(
x.$3,
softWrap: false,
style: textTheme.labelMedium,
textAlign: TextAlign.center,
).fit(fit: BoxFit.scaleDown)));
}
return out;
}
List<DropdownMenuItem<SoundEffect>> soundEffectItems() {
final out = <DropdownMenuItem<SoundEffect>>[];
final items = [
(SoundEffect.none, true, translate('settings_page.none')),
(SoundEffect.bonk, true, translate('settings_page.bonk')),
(SoundEffect.boop, true, translate('settings_page.boop')),
(SoundEffect.baDeep, true, translate('settings_page.badeep')),
(SoundEffect.beepBaDeep, true, translate('settings_page.beep_badeep')),
(SoundEffect.custom, false, translate('settings_page.custom')),
];
for (final x in items) {
out.add(DropdownMenuItem(
value: x.$1,
enabled: x.$2,
child: Text(
x.$3,
softWrap: false,
style: textTheme.labelMedium,
textAlign: TextAlign.center,
).fit(fit: BoxFit.scaleDown)));
}
return out;
}
List<DropdownMenuItem<MessageNotificationContent>>
messageNotificationContentItems() {
final out = <DropdownMenuItem<MessageNotificationContent>>[];
final items = [
(
MessageNotificationContent.nameAndContent,
true,
translate('settings_page.name_and_content')
),
(
MessageNotificationContent.nameOnly,
true,
translate('settings_page.name_only')
),
(
MessageNotificationContent.nothing,
true,
translate('settings_page.nothing')
),
];
for (final x in items) {
out.add(DropdownMenuItem(
value: x.$1,
enabled: x.$2,
child: Text(
x.$3,
softWrap: false,
style: textTheme.labelMedium,
textAlign: TextAlign.center,
)));
}
return out;
}
// Invitation accepted
Widget notificationSettingsItem(
{required String title,
required bool notificationsEnabled,
NotificationMode? deliveryValue,
SoundEffect? soundValue,
Future<void> Function(NotificationMode)? onNotificationModeChanged,
Future<void> Function(SoundEffect)? onSoundChanged}) =>
Column(
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 8.scaled(context),
children: [
Text('$title:', style: textTheme.titleMedium),
Wrap(
spacing: 8.scaled(context), // gap between adjacent chips
runSpacing: 8.scaled(context), // gap between lines
children: [
if (deliveryValue != null)
IntrinsicWidth(
child: StyledDropdown<NotificationMode>(
decoratorLabel: translate('settings_page.delivery'),
items: notificationModeItems(),
value: deliveryValue,
onChanged: !notificationsEnabled
? null
: onNotificationModeChanged,
)),
if (soundValue != null)
IntrinsicWidth(
child: StyledDropdown<SoundEffect>(
decoratorLabel: translate('settings_page.sound'),
items: soundEffectItems(),
value: soundValue,
onChanged: !notificationsEnabled ? null : onSoundChanged,
))
])
]).paddingAll(4.scaled(context));
return InputDecorator(
decoration: InputDecoration(
labelText: translate('settings_page.notifications'),
border: OutlineInputBorder(
borderRadius:
BorderRadius.circular(8 * scaleConfig.borderRadiusScale),
borderSide: BorderSide(width: 2, color: scale.primaryScale.border),
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
spacing: 8.scaled(context),
children: [
// Display Beta Warning
StyledCheckbox(
label: translate('settings_page.display_beta_warning'),
value: notificationsPreference.displayBetaWarning,
onChanged: (value) async {
final newNotificationsPreference = notificationsPreference
.copyWith(displayBetaWarning: value);
await updatePreferences(newNotificationsPreference);
}),
// Enable Badge
StyledCheckbox(
label: translate('settings_page.enable_badge'),
value: notificationsPreference.enableBadge,
onChanged: (value) async {
final newNotificationsPreference =
notificationsPreference.copyWith(enableBadge: value);
await updatePreferences(newNotificationsPreference);
}),
// Enable Notifications
StyledCheckbox(
label: translate('settings_page.enable_notifications'),
value: notificationsPreference.enableNotifications,
onChanged: (value) async {
final newNotificationsPreference = notificationsPreference
.copyWith(enableNotifications: value);
await updatePreferences(newNotificationsPreference);
}),
StyledDropdown<MessageNotificationContent>(
items: messageNotificationContentItems(),
value: notificationsPreference.messageNotificationContent,
decoratorLabel:
translate('settings_page.message_notification_content'),
onChanged: !notificationsPreference.enableNotifications
? null
: (value) async {
final newNotificationsPreference = notificationsPreference
.copyWith(messageNotificationContent: value);
await updatePreferences(newNotificationsPreference);
},
).paddingAll(4.scaled(context)),
// Notifications
// Invitation accepted
notificationSettingsItem(
title: translate('settings_page.invitation_accepted'),
notificationsEnabled:
notificationsPreference.enableNotifications,
deliveryValue: notificationsPreference.onInvitationAcceptedMode,
soundValue: notificationsPreference.onInvitationAcceptedSound,
onNotificationModeChanged: (value) async {
final newNotificationsPreference = notificationsPreference
.copyWith(onInvitationAcceptedMode: value);
await updatePreferences(newNotificationsPreference);
},
onSoundChanged: (value) async {
final newNotificationsPreference = notificationsPreference
.copyWith(onInvitationAcceptedSound: value);
await updatePreferences(newNotificationsPreference);
}),
// Message received
notificationSettingsItem(
title: translate('settings_page.message_received'),
notificationsEnabled:
notificationsPreference.enableNotifications,
deliveryValue: notificationsPreference.onMessageReceivedMode,
soundValue: notificationsPreference.onMessageReceivedSound,
onNotificationModeChanged: (value) async {
final newNotificationsPreference = notificationsPreference
.copyWith(onMessageReceivedMode: value);
await updatePreferences(newNotificationsPreference);
},
onSoundChanged: (value) async {
final newNotificationsPreference = notificationsPreference
.copyWith(onMessageReceivedSound: value);
await updatePreferences(newNotificationsPreference);
}),
// Message sent
notificationSettingsItem(
title: translate('settings_page.message_sent'),
notificationsEnabled:
notificationsPreference.enableNotifications,
soundValue: notificationsPreference.onMessageSentSound,
onSoundChanged: (value) async {
final newNotificationsPreference = notificationsPreference
.copyWith(onMessageSentSound: value);
await updatePreferences(newNotificationsPreference);
}),
]).paddingAll(4.scaled(context)));
}