Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[mobile][photos] Centralize notification permission polling

Priyanshu Dangare6ae250e98350
diff --git a/mobile/apps/photos/lib/services/memories_cache_service.dart b/mobile/apps/photos/lib/services/memories_cache_service.dart
index 20d0685563..a2bd69a427 100644
--- a/mobile/apps/photos/lib/services/memories_cache_service.dart
+++ b/mobile/apps/photos/lib/services/memories_cache_service.dart
@@ -1431,22 +1431,24 @@ class MemoriesCacheService {
);
}
- Future<void> toggleOnThisDayNotifications() async {
- final oldValue = localSettings.isOnThisDayNotificationsEnabled;
- await localSettings.setOnThisDayNotificationsEnabled(!oldValue);
- _logger.info("Turning onThisDayNotifications ${oldValue ? "off" : "on"}");
- if (oldValue) {
+ Future<void> setOnThisDayNotifications(bool value) async {
+ if (localSettings.isOnThisDayNotificationsEnabled == value) return;
+
+ await localSettings.setOnThisDayNotificationsEnabled(value);
+ _logger.info("Turning onThisDayNotifications ${value ? "on" : "off"}");
+ if (!value) {
await _clearAllScheduledOnThisDayNotifications();
} else {
queueUpdateCache();
}
}
- Future<void> toggleBirthdayNotifications() async {
- final oldValue = localSettings.birthdayNotificationsEnabled;
- await localSettings.setBirthdayNotificationsEnabled(!oldValue);
- _logger.info("Turning birhtdayNotifications ${oldValue ? "off" : "on"}");
- if (oldValue) {
+ Future<void> setBirthdayNotifications(bool value) async {
+ if (localSettings.birthdayNotificationsEnabled == value) return;
+
+ await localSettings.setBirthdayNotificationsEnabled(value);
+ _logger.info("Turning birthdayNotifications ${value ? "on" : "off"}");
+ if (!value) {
await _clearAllScheduledBirthdayNotifications();
} else {
queueUpdateCache();
diff --git a/mobile/apps/photos/lib/services/notification_service.dart b/mobile/apps/photos/lib/services/notification_service.dart
index c0adcaf385..127ec2d7b9 100644
--- a/mobile/apps/photos/lib/services/notification_service.dart
+++ b/mobile/apps/photos/lib/services/notification_service.dart
@@ -1,3 +1,4 @@
+import "dart:async";
import 'dart:io';
import "package:android_intent_plus/android_intent.dart";
@@ -161,7 +162,26 @@ class NotificationService {
return 'UTC';
}
- Future<void> requestPermissions() async {
+ Future<bool> requestPermissions() async {
+ if (await _askPermissions()) {
+ return true;
+ }
+
+ const interval = Duration(milliseconds: 500);
+ const maxAttempts = 1000;
+
+ for (var attempt = 0; attempt < maxAttempts; attempt++) {
+ await Future.delayed(interval);
+
+ if (await hasGrantedPermissions()) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ Future<bool> _askPermissions() async {
await _ensurePluginInitialized();
final granted = Platform.isIOS
? await _notificationsPlugin
@@ -175,6 +195,7 @@ class NotificationService {
>()
?.requestNotificationsPermission();
if (granted == false) await _openNotificationSettings();
+ return granted ?? false;
}
Future<void> _openNotificationSettings() async {
diff --git a/mobile/apps/photos/lib/ui/home/memories/craft_memories.dart b/mobile/apps/photos/lib/ui/home/memories/craft_memories.dart
index 7a6b453d3f..d9aa78a48a 100644
--- a/mobile/apps/photos/lib/ui/home/memories/craft_memories.dart
+++ b/mobile/apps/photos/lib/ui/home/memories/craft_memories.dart
@@ -1,5 +1,3 @@
-import "dart:async";
-
import "package:ente_components/ente_components.dart";
import "package:flutter/material.dart";
import "package:photos/services/notification_service.dart";
@@ -25,8 +23,6 @@ class CraftMemories extends StatefulWidget {
class _CraftMemoriesState extends State<CraftMemories> {
late final rive.FileLoader _riveFileLoader;
bool _isButtonPressed = false;
- bool _isCheckingPermission = false;
- Timer? _permissionTimer;
@override
void initState() {
@@ -39,7 +35,6 @@ class _CraftMemoriesState extends State<CraftMemories> {
@override
void dispose() {
- _permissionTimer?.cancel();
_riveFileLoader.dispose();
super.dispose();
}
@@ -110,7 +105,12 @@ class _CraftMemoriesState extends State<CraftMemories> {
Widget _buildButton() {
return GestureDetector(
- onTap: _requestPermissions,
+ onTap: () async {
+ if (await NotificationService.instance.requestPermissions() &&
+ mounted) {
+ widget.onNotificationsPermissionGranted?.call();
+ }
+ },
onTapDown: (_) => setState(() => _isButtonPressed = true),
onTapUp: (_) => setState(() => _isButtonPressed = false),
onTapCancel: () => setState(() => _isButtonPressed = false),
@@ -140,34 +140,4 @@ class _CraftMemoriesState extends State<CraftMemories> {
),
);
}
-
- Future<void> _requestPermissions() async {
- const pollInterval = Duration(milliseconds: 500);
- const pollTimeout = Duration(minutes: 1);
- _permissionTimer ??= Timer.periodic(pollInterval, (timer) {
- _completePermissionRequest();
- if (timer.tick * pollInterval.inMilliseconds >=
- pollTimeout.inMilliseconds) {
- timer.cancel();
- _permissionTimer = null;
- }
- });
- await NotificationService.instance.requestPermissions();
- await _completePermissionRequest();
- }
-
- Future<void> _completePermissionRequest() async {
- if (_isCheckingPermission) return;
- _isCheckingPermission = true;
-
- try {
- if (!await NotificationService.instance.hasGrantedPermissions()) return;
- _permissionTimer?.cancel();
- _permissionTimer = null;
- if (!mounted) return;
- widget.onNotificationsPermissionGranted?.call();
- } finally {
- _isCheckingPermission = false;
- }
- }
}
diff --git a/mobile/apps/photos/lib/ui/settings/notification_settings_screen.dart b/mobile/apps/photos/lib/ui/settings/notification_settings_screen.dart
index b1b3a37a05..eea4b4d79b 100644
--- a/mobile/apps/photos/lib/ui/settings/notification_settings_screen.dart
+++ b/mobile/apps/photos/lib/ui/settings/notification_settings_screen.dart
@@ -1,5 +1,3 @@
-import "dart:async";
-
import "package:ente_components/ente_components.dart";
import "package:flutter/material.dart";
import "package:photos/generated/l10n.dart";
@@ -18,60 +16,33 @@ class NotificationSettingsScreen extends StatefulWidget {
class _NotificationSettingsScreenState
extends State<NotificationSettingsScreen> {
- bool _hasPermission = false, _isCompletingToggle = false;
- Future<void> Function(bool toggleCurrent)? _pendingToggle;
- Timer? _permissionTimer;
+ bool _hasPermission = false;
@override
void initState() {
super.initState();
- _refreshNotificationPermission();
- }
-
- @override
- void dispose() {
- _permissionTimer?.cancel();
- super.dispose();
- }
-
- Future<bool> _refreshNotificationPermission() async {
- final granted = await NotificationService.instance.hasGrantedPermissions();
- if (mounted && _hasPermission != granted) {
- setState(() => _hasPermission = granted);
- }
- return granted;
+ NotificationService.instance.hasGrantedPermissions().then((granted) {
+ if (mounted && _hasPermission != granted) {
+ setState(() => _hasPermission = granted);
+ }
+ });
}
- Future<void> _runWithPermission(
- Future<void> Function(bool toggleCurrent) toggle,
+ Future<void> _toggleWithPermission(
+ bool Function() getValue,
+ Future<void> Function(bool value) setValue,
) async {
- if (_hasPermission) return toggle(true);
+ final value = _hasPermission ? !getValue() : true;
- _pendingToggle = toggle;
- _permissionTimer ??= Timer.periodic(
- const Duration(milliseconds: 500),
- (_) => _completePendingToggle(),
- );
- await NotificationService.instance.requestPermissions();
- await _completePendingToggle();
- }
-
- Future<void> _completePendingToggle() async {
- if (_isCompletingToggle) return;
- _isCompletingToggle = true;
-
- try {
- final toggle = _pendingToggle;
- if (!(await _refreshNotificationPermission()) || toggle == null) return;
-
- _pendingToggle = null;
- _permissionTimer?.cancel();
- _permissionTimer = null;
- await toggle(false);
- if (mounted) setState(() {});
- } finally {
- _isCompletingToggle = false;
+ if (!_hasPermission &&
+ (!await NotificationService.instance.requestPermissions() ||
+ !mounted)) {
+ return;
}
+
+ _hasPermission = true;
+ await setValue(value);
+ if (mounted) setState(() {});
}
@override
@@ -93,19 +64,10 @@ class _NotificationSettingsScreenState
value: () =>
_hasPermission &&
service.shouldShowNotificationsForSharedPhotosAndAlbums(),
- onChanged: () => _runWithPermission((toggleCurrent) async {
- final prev = service
- .shouldShowNotificationsForSharedPhotosAndAlbums();
- if (toggleCurrent) {
- await service
- .setShouldShowNotificationsForSharedPhotosAndAlbums(
- !prev,
- );
- } else if (!prev) {
- await service
- .setShouldShowNotificationsForSharedPhotosAndAlbums(true);
- }
- }),
+ onChanged: () => _toggleWithPermission(
+ service.shouldShowNotificationsForSharedPhotosAndAlbums,
+ service.setShouldShowNotificationsForSharedPhotosAndAlbums,
+ ),
showStateIcon: false,
),
),
@@ -118,14 +80,10 @@ class _NotificationSettingsScreenState
trailing: ToggleSwitchComponent.async(
value: () =>
_hasPermission && service.shouldShowSocialNotifications(),
- onChanged: () => _runWithPermission((toggleCurrent) async {
- final prev = service.shouldShowSocialNotifications();
- if (toggleCurrent) {
- await service.setShouldShowSocialNotifications(!prev);
- } else if (!prev) {
- await service.setShouldShowSocialNotifications(true);
- }
- }),
+ onChanged: () => _toggleWithPermission(
+ service.shouldShowSocialNotifications,
+ service.setShouldShowSocialNotifications,
+ ),
showStateIcon: false,
),
),
@@ -139,12 +97,10 @@ class _NotificationSettingsScreenState
trailing: ToggleSwitchComponent.async(
value: () =>
_hasPermission && localSettings.isOnThisDayNotificationsEnabled,
- onChanged: () => _runWithPermission((toggleCurrent) async {
- final prev = localSettings.isOnThisDayNotificationsEnabled;
- if (toggleCurrent || !prev) {
- await memoriesCacheService.toggleOnThisDayNotifications();
- }
- }),
+ onChanged: () => _toggleWithPermission(
+ () => localSettings.isOnThisDayNotificationsEnabled,
+ memoriesCacheService.setOnThisDayNotifications,
+ ),
showStateIcon: false,
),
),
@@ -158,12 +114,10 @@ class _NotificationSettingsScreenState
trailing: ToggleSwitchComponent.async(
value: () =>
_hasPermission && localSettings.birthdayNotificationsEnabled,
- onChanged: () => _runWithPermission((toggleCurrent) async {
- final prev = localSettings.birthdayNotificationsEnabled;
- if (toggleCurrent || !prev) {
- await memoriesCacheService.toggleBirthdayNotifications();
- }
- }),
+ onChanged: () => _toggleWithPermission(
+ () => localSettings.birthdayNotificationsEnabled,
+ memoriesCacheService.setBirthdayNotifications,
+ ),
showStateIcon: false,
),
),