Skip to content

armgit / ente

Public Imported from GitHub
0

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

Implemented first PR feedbacks: removed PhotoManagerSafe not needded with LocalSyncService lock

Simon Dubrullee9a8449a648d
diff --git a/mobile/lib/services/local_sync_service.dart b/mobile/lib/services/local_sync_service.dart
index ceb070011b..d8237dd92a 100644
--- a/mobile/lib/services/local_sync_service.dart
+++ b/mobile/lib/services/local_sync_service.dart
@@ -129,7 +129,7 @@ class LocalSyncService {
final duration = Duration(microseconds: endTime - startTime);
_logger.info("Load took " + duration.inMilliseconds.toString() + "ms");
});
-
+
_existingSync?.complete();
_existingSync = null;
}
@@ -374,7 +374,7 @@ class LocalSyncService {
unawaited(checkAndSync());
});
});
- PhotoManagerSafe.startChangeNotify(null);
+ PhotoManager.startChangeNotify();
}
Future<void> checkAndSync() async {
diff --git a/mobile/lib/ui/tools/editor/image_editor_page.dart b/mobile/lib/ui/tools/editor/image_editor_page.dart
index a24aa8bec7..2ef93601f2 100644
--- a/mobile/lib/ui/tools/editor/image_editor_page.dart
+++ b/mobile/lib/ui/tools/editor/image_editor_page.dart
@@ -27,7 +27,6 @@ import 'package:photos/ui/tools/editor/filtered_image.dart';
import 'package:photos/ui/viewer/file/detail_page.dart';
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/navigation_util.dart';
-import "package:photos/utils/photo_manager_util.dart";
import 'package:photos/utils/toast_util.dart';
import 'package:syncfusion_flutter_core/theme.dart';
import 'package:syncfusion_flutter_sliders/sliders.dart';
@@ -359,7 +358,7 @@ class _ImageEditorPageState extends State<ImageEditorPage> {
".JPEG";
//Disabling notifications for assets changing to insert the file into
//files db before triggering a sync.
- await PhotoManagerSafe.stopChangeNotify(widget.originalFile.title!);
+ await PhotoManager.stopChangeNotify();
final AssetEntity? newAsset =
await (PhotoManager.editor.saveImage(result, filename: fileName));
final newFile = await ente.EnteFile.fromAsset(
@@ -411,7 +410,7 @@ class _ImageEditorPageState extends State<ImageEditorPage> {
showToast(context, S.of(context).oopsCouldNotSaveEdits);
_logger.severe(e, s);
} finally {
- await PhotoManagerSafe.startChangeNotify(widget.originalFile.title!);
+ await PhotoManager.startChangeNotify();
}
}
diff --git a/mobile/lib/ui/tools/editor/video_editor_page.dart b/mobile/lib/ui/tools/editor/video_editor_page.dart
index 34f0e9ba97..d96f0008a7 100644
--- a/mobile/lib/ui/tools/editor/video_editor_page.dart
+++ b/mobile/lib/ui/tools/editor/video_editor_page.dart
@@ -24,7 +24,6 @@ import "package:photos/ui/tools/editor/video_trim_page.dart";
import "package:photos/ui/viewer/file/detail_page.dart";
import "package:photos/utils/dialog_util.dart";
import "package:photos/utils/navigation_util.dart";
-import "package:photos/utils/photo_manager_util.dart";
import "package:photos/utils/toast_util.dart";
import "package:video_editor/video_editor.dart";
@@ -239,7 +238,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
".mp4";
//Disabling notifications for assets changing to insert the file into
//files db before triggering a sync.
- await PhotoManagerSafe.stopChangeNotify(widget.file.title!);
+ await PhotoManager.stopChangeNotify();
try {
final AssetEntity? newAsset =
@@ -301,7 +300,7 @@ class _VideoEditorPageState extends State<VideoEditorPage> {
} catch (_) {
await dialog.hide();
} finally {
- await PhotoManagerSafe.startChangeNotify(widget.file.title!);
+ await PhotoManager.startChangeNotify();
}
}
}
diff --git a/mobile/lib/utils/file_download_util.dart b/mobile/lib/utils/file_download_util.dart
index 1841fcfca5..d91b62efef 100644
--- a/mobile/lib/utils/file_download_util.dart
+++ b/mobile/lib/utils/file_download_util.dart
@@ -21,7 +21,6 @@ import "package:photos/utils/data_util.dart";
import "package:photos/utils/fake_progress.dart";
import "package:photos/utils/file_key.dart";
import "package:photos/utils/file_util.dart";
-import "package:photos/utils/photo_manager_util.dart";
final _logger = Logger("file_download_util");
@@ -197,7 +196,7 @@ Future<void> downloadToGallery(EnteFile file) async {
final File? fileToSave = await getFile(file);
//Disabling notifications for assets changing to insert the file into
//files db before triggering a sync.
- await PhotoManagerSafe.stopChangeNotify(file.generatedID.toString());
+ await PhotoManager.stopChangeNotify();
if (type == FileType.image) {
savedAsset = await PhotoManager.editor
.saveImageWithPath(fileToSave!.path, title: file.title!);
@@ -220,6 +219,7 @@ Future<void> downloadToGallery(EnteFile file) async {
);
}
}
+
if (savedAsset != null) {
file.localID = savedAsset!.id;
await FilesDB.instance.insert(file);
@@ -237,7 +237,7 @@ Future<void> downloadToGallery(EnteFile file) async {
_logger.severe("Failed to save file", e);
rethrow;
} finally {
- await PhotoManagerSafe.startChangeNotify(file.generatedID.toString());
+ await PhotoManager.startChangeNotify();
LocalSyncService.instance.checkAndSync().ignore();
}
}
diff --git a/mobile/lib/utils/photo_manager_util.dart b/mobile/lib/utils/photo_manager_util.dart
index 948d1cf863..273d0b362e 100644
--- a/mobile/lib/utils/photo_manager_util.dart
+++ b/mobile/lib/utils/photo_manager_util.dart
@@ -1,7 +1,4 @@
-import 'dart:async';
-import "package:logging/logging.dart";
import "package:photo_manager/photo_manager.dart";
-import 'package:synchronized/synchronized.dart';
Future<PermissionState> requestPhotoMangerPermissions() {
return PhotoManager.requestPermissionExtend(
@@ -13,128 +10,3 @@ Future<PermissionState> requestPhotoMangerPermissions() {
),
);
}
-
-final _logger = Logger("PhotoManagerUtil");
-// This is a wrapper for safe handling of PhotoManager.startChangeNotify() and
-// PhotoManager.stopChangeNotify(). Since PhotoManager is globally shared, we want
-// to make sure no notification is sent while it should not. The logic is that it will
-// only start if no other asset (or task) requested to stop changes, or if the expiration
-// time for the asset (task) expired. '_processingAssets' should be seen as a queue of
-// open requests.
-class PhotoManagerSafe {
- // Tracks processing assets with their expiration times
- static final Map<String?, DateTime> _processingAssets = {};
-
- // Timer for monitoring asset processing
- static Timer? _expirationTimer;
-
- // Synchronization lock
- static final _lock = Lock();
-
- // Estimate processing duration based on file size
- static Duration _estimateProcessingDuration(int fileSize) {
- final estimatedSeconds = (fileSize / (1024 * 1024)).ceil() * 2;
- return Duration(
- seconds: estimatedSeconds.clamp(5, 120),
- );
- }
-
- // Manage asset processing state. Lock ensures no start/stop is performed
- // at the same time.
- static Future<void> manageAssetProcessing({
- required String? assetId,
- required bool isStarting,
- int? fileSize,
- }) async {
- await _lock.synchronized(() async {
- try {
- if (isStarting) {
- // Remove the asset from processing only if assetId is not null
- if (assetId != null) {
- _processingAssets.remove(assetId);
- }
-
- // Restart change notify only if no assets are processing and no stop was requested
- if (_processingAssets.isEmpty) {
- await PhotoManager.startChangeNotify();
- }
-
- _stopExpirationMonitoringIfEmpty();
- } else {
- // Stopping the asset
- final duration = _estimateProcessingDuration(
- fileSize ?? 10 * 1024 * 1024, // 10MB default
- );
-
- // First asset to request stop
- if (_processingAssets.isEmpty) {
- await PhotoManager.stopChangeNotify();
- }
-
- // Track the processing asset with expiration
- _processingAssets[assetId] = DateTime.now().add(duration);
-
- _startOrContinueExpirationMonitoring();
- }
- } catch (e, stackTrace) {
- _logger.severe(
- "${isStarting ? 'Start' : 'Stop'}ChangeNotify error for ID $assetId",
- e,
- stackTrace,
- );
- rethrow;
- }
- });
- }
-
- // Start or continue the expiration monitoring timer
- static void _startOrContinueExpirationMonitoring() {
- if (_expirationTimer != null && _expirationTimer!.isActive) return;
-
- _expirationTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
- final now = DateTime.now();
-
- // Remove expired assets
- _processingAssets.removeWhere((assetId, expiresAt) {
- final bool isExpired = expiresAt.isBefore(now);
- if (isExpired) {
- }
- return isExpired;
- });
-
- // Handle asset processing completion
- if (_processingAssets.isEmpty) {
-
- // Start ChangeNotify
- try {
- PhotoManager.startChangeNotify();
- } catch (e, stackTrace) {
- _logger.severe("Error restarting change notify", e, stackTrace);
- }
-
- _stopExpirationMonitoringIfEmpty();
- }
- });
- }
-
- // Stop the expiration monitoring timer if no assets are being processed
- static void _stopExpirationMonitoringIfEmpty() {
- if (_processingAssets.isEmpty) {
- _expirationTimer?.cancel();
- _expirationTimer = null;
- }
- }
-
- static Future<void> stopChangeNotify(String? assetId, {int? fileSize}) =>
- manageAssetProcessing(
- assetId: assetId,
- isStarting: false,
- fileSize: fileSize,
- );
-
- static Future<void> startChangeNotify(String? assetId) =>
- manageAssetProcessing(
- assetId: assetId,
- isStarting: true,
- );
-}
\ No newline at end of file