2022-08-21 18:29:24 +02:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2023-11-09 18:19:53 +02:00
|
|
|
import 'package:immich_mobile/extensions/build_context_extensions.dart';
|
2022-08-21 18:29:24 +02:00
|
|
|
import 'package:immich_mobile/modules/settings/providers/app_settings.provider.dart';
|
2023-02-28 18:22:18 +02:00
|
|
|
import 'package:immich_mobile/modules/settings/providers/notification_permission.provider.dart';
|
2022-08-21 18:29:24 +02:00
|
|
|
import 'package:immich_mobile/modules/settings/services/app_settings.service.dart';
|
2023-02-21 14:28:52 +02:00
|
|
|
import 'package:immich_mobile/modules/settings/ui/settings_switch_list_tile.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
2022-08-21 18:29:24 +02:00
|
|
|
|
|
|
|
class NotificationSetting extends HookConsumerWidget {
|
|
|
|
const NotificationSetting({
|
2024-01-27 18:14:32 +02:00
|
|
|
super.key,
|
|
|
|
});
|
2022-08-21 18:29:24 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
final appSettingService = ref.watch(appSettingsServiceProvider);
|
2023-02-21 14:28:52 +02:00
|
|
|
final permissionService = ref.watch(notificationPermissionProvider);
|
2022-08-21 18:29:24 +02:00
|
|
|
|
|
|
|
final sliderValue = useState(0.0);
|
2022-10-05 16:59:35 +02:00
|
|
|
final totalProgressValue =
|
|
|
|
useState(AppSettingsEnum.backgroundBackupTotalProgress.defaultValue);
|
|
|
|
final singleProgressValue =
|
|
|
|
useState(AppSettingsEnum.backgroundBackupSingleProgress.defaultValue);
|
2023-02-21 14:28:52 +02:00
|
|
|
final hasPermission = permissionService == PermissionStatus.granted;
|
2022-08-21 18:29:24 +02:00
|
|
|
|
|
|
|
useEffect(
|
|
|
|
() {
|
|
|
|
sliderValue.value = appSettingService
|
|
|
|
.getSetting<int>(AppSettingsEnum.uploadErrorNotificationGracePeriod)
|
|
|
|
.toDouble();
|
2022-10-05 16:59:35 +02:00
|
|
|
totalProgressValue.value = appSettingService
|
|
|
|
.getSetting<bool>(AppSettingsEnum.backgroundBackupTotalProgress);
|
|
|
|
singleProgressValue.value = appSettingService
|
|
|
|
.getSetting<bool>(AppSettingsEnum.backgroundBackupSingleProgress);
|
2022-08-21 18:29:24 +02:00
|
|
|
return null;
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2023-02-21 14:28:52 +02:00
|
|
|
// When permissions are permanently denied, you need to go to settings to
|
|
|
|
// allow them
|
|
|
|
showPermissionsDialog() {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
content: const Text('notification_permission_dialog_content').tr(),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: const Text('notification_permission_dialog_cancel').tr(),
|
2023-11-09 18:19:53 +02:00
|
|
|
onPressed: () => context.pop(),
|
2023-02-21 14:28:52 +02:00
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
child: const Text('notification_permission_dialog_settings').tr(),
|
|
|
|
onPressed: () {
|
2023-11-09 18:19:53 +02:00
|
|
|
context.pop();
|
2023-02-21 14:28:52 +02:00
|
|
|
openAppSettings();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-21 18:29:24 +02:00
|
|
|
final String formattedValue = _formatSliderValue(sliderValue.value);
|
|
|
|
return ExpansionTile(
|
2023-11-09 18:19:53 +02:00
|
|
|
textColor: context.primaryColor,
|
2023-11-20 16:58:03 +02:00
|
|
|
title: Text(
|
2022-08-21 18:29:24 +02:00
|
|
|
'setting_notifications_title',
|
2023-11-20 16:58:03 +02:00
|
|
|
style: context.textTheme.titleMedium,
|
2022-08-21 18:29:24 +02:00
|
|
|
).tr(),
|
|
|
|
subtitle: const Text(
|
|
|
|
'setting_notifications_subtitle',
|
|
|
|
).tr(),
|
|
|
|
children: [
|
2023-02-21 14:28:52 +02:00
|
|
|
if (!hasPermission)
|
|
|
|
ListTile(
|
|
|
|
leading: const Icon(Icons.notifications_outlined),
|
2023-10-25 16:53:16 +02:00
|
|
|
title: Text(
|
|
|
|
'notification_permission_list_tile_title',
|
2023-11-09 18:19:53 +02:00
|
|
|
style: context.textTheme.labelLarge
|
2023-10-25 16:53:16 +02:00
|
|
|
?.copyWith(fontWeight: FontWeight.bold),
|
|
|
|
).tr(),
|
2023-02-21 14:28:52 +02:00
|
|
|
subtitle: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
2023-10-25 16:53:16 +02:00
|
|
|
Text(
|
|
|
|
'notification_permission_list_tile_content',
|
2023-11-09 18:19:53 +02:00
|
|
|
style: context.textTheme.labelMedium,
|
2023-10-25 16:53:16 +02:00
|
|
|
).tr(),
|
2023-02-21 14:28:52 +02:00
|
|
|
const SizedBox(height: 8),
|
|
|
|
ElevatedButton(
|
2023-10-25 16:53:16 +02:00
|
|
|
onPressed: () => ref
|
|
|
|
.watch(notificationPermissionProvider.notifier)
|
|
|
|
.requestNotificationPermission()
|
|
|
|
.then((permission) {
|
|
|
|
if (permission == PermissionStatus.permanentlyDenied) {
|
|
|
|
showPermissionsDialog();
|
|
|
|
}
|
2023-02-21 14:28:52 +02:00
|
|
|
}),
|
2023-10-25 16:53:16 +02:00
|
|
|
child: const Text(
|
|
|
|
'notification_permission_list_tile_enable_button',
|
|
|
|
).tr(),
|
2023-02-21 14:28:52 +02:00
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
isThreeLine: true,
|
|
|
|
),
|
|
|
|
SettingsSwitchListTile(
|
|
|
|
enabled: hasPermission,
|
|
|
|
appSettingService: appSettingService,
|
|
|
|
valueNotifier: totalProgressValue,
|
|
|
|
settingsEnum: AppSettingsEnum.backgroundBackupTotalProgress,
|
2022-10-05 16:59:35 +02:00
|
|
|
title: 'setting_notifications_total_progress_title'.tr(),
|
|
|
|
subtitle: 'setting_notifications_total_progress_subtitle'.tr(),
|
|
|
|
),
|
2023-02-21 14:28:52 +02:00
|
|
|
SettingsSwitchListTile(
|
|
|
|
enabled: hasPermission,
|
|
|
|
appSettingService: appSettingService,
|
|
|
|
valueNotifier: singleProgressValue,
|
|
|
|
settingsEnum: AppSettingsEnum.backgroundBackupSingleProgress,
|
2022-10-05 16:59:35 +02:00
|
|
|
title: 'setting_notifications_single_progress_title'.tr(),
|
|
|
|
subtitle: 'setting_notifications_single_progress_subtitle'.tr(),
|
|
|
|
),
|
2022-08-21 18:29:24 +02:00
|
|
|
ListTile(
|
2023-02-21 14:28:52 +02:00
|
|
|
enabled: hasPermission,
|
2022-08-21 18:29:24 +02:00
|
|
|
isThreeLine: false,
|
|
|
|
dense: true,
|
|
|
|
title: const Text(
|
|
|
|
'setting_notifications_notify_failures_grace_period',
|
|
|
|
style: TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
).tr(args: [formattedValue]),
|
|
|
|
subtitle: Slider(
|
|
|
|
value: sliderValue.value,
|
2023-10-25 16:53:16 +02:00
|
|
|
onChanged:
|
|
|
|
!hasPermission ? null : (double v) => sliderValue.value = v,
|
2022-08-21 18:29:24 +02:00
|
|
|
onChangeEnd: (double v) => appSettingService.setSetting(
|
2022-10-05 16:59:35 +02:00
|
|
|
AppSettingsEnum.uploadErrorNotificationGracePeriod,
|
|
|
|
v.toInt(),
|
|
|
|
),
|
2022-08-21 18:29:24 +02:00
|
|
|
max: 5.0,
|
|
|
|
divisions: 5,
|
|
|
|
label: formattedValue,
|
2023-11-09 18:19:53 +02:00
|
|
|
activeColor: context.primaryColor,
|
2022-08-21 18:29:24 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String _formatSliderValue(double v) {
|
|
|
|
if (v == 0.0) {
|
|
|
|
return 'setting_notifications_notify_immediately'.tr();
|
|
|
|
} else if (v == 1.0) {
|
|
|
|
return 'setting_notifications_notify_minutes'.tr(args: const ['30']);
|
|
|
|
} else if (v == 2.0) {
|
|
|
|
return 'setting_notifications_notify_hours'.tr(args: const ['2']);
|
|
|
|
} else if (v == 3.0) {
|
|
|
|
return 'setting_notifications_notify_hours'.tr(args: const ['8']);
|
|
|
|
} else if (v == 4.0) {
|
|
|
|
return 'setting_notifications_notify_hours'.tr(args: const ['24']);
|
|
|
|
} else {
|
|
|
|
return 'setting_notifications_notify_never'.tr();
|
|
|
|
}
|
|
|
|
}
|