2025-04-17 20:55:27 +05:30
|
|
|
// ignore_for_file: avoid-passing-async-when-sync-expected
|
|
|
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:immich_mobile/providers/infrastructure/sync_stream.provider.dart';
|
|
|
|
|
import 'package:immich_mobile/utils/isolate.dart';
|
|
|
|
|
import 'package:worker_manager/worker_manager.dart';
|
|
|
|
|
|
|
|
|
|
class BackgroundSyncManager {
|
2025-04-18 14:01:16 -05:00
|
|
|
Cancelable<void>? _syncTask;
|
2025-04-17 20:55:27 +05:30
|
|
|
|
|
|
|
|
BackgroundSyncManager();
|
|
|
|
|
|
|
|
|
|
Future<void> cancel() {
|
|
|
|
|
final futures = <Future>[];
|
2025-04-18 14:01:16 -05:00
|
|
|
|
|
|
|
|
if (_syncTask != null) {
|
|
|
|
|
futures.add(_syncTask!.future);
|
2025-04-17 20:55:27 +05:30
|
|
|
}
|
2025-04-18 14:01:16 -05:00
|
|
|
_syncTask?.cancel();
|
|
|
|
|
_syncTask = null;
|
|
|
|
|
|
2025-04-17 20:55:27 +05:30
|
|
|
return Future.wait(futures);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 14:01:16 -05:00
|
|
|
Future<void> sync() {
|
|
|
|
|
if (_syncTask != null) {
|
|
|
|
|
return _syncTask!.future;
|
2025-04-17 20:55:27 +05:30
|
|
|
}
|
|
|
|
|
|
2025-04-18 14:01:16 -05:00
|
|
|
_syncTask = runInIsolateGentle(
|
|
|
|
|
computation: (ref) => ref.read(syncStreamServiceProvider).sync(),
|
2025-04-17 20:55:27 +05:30
|
|
|
);
|
2025-04-18 14:01:16 -05:00
|
|
|
_syncTask!.whenComplete(() {
|
|
|
|
|
_syncTask = null;
|
2025-04-17 20:55:27 +05:30
|
|
|
});
|
2025-04-18 14:01:16 -05:00
|
|
|
return _syncTask!.future;
|
2025-04-17 20:55:27 +05:30
|
|
|
}
|
|
|
|
|
}
|