startSync method
Initialize the attachment queue by:
- Creating the attachments directory.
- Adding watches for uploads, downloads, and deletes.
- Adding a trigger to run uploads, downloads, and deletes when the device is online after being offline.
Implementation
Future<void> startSync() async {
await _mutex.lock(() async {
if (_closed) {
throw StateError('Attachment queue has been closed');
}
await _stopSyncingInternal();
await _localStorage.initialize();
await _attachmentsService.withContext((context) async {
await _verifyAttachments(context);
});
// Listen for connectivity changes and watched attachments
await _syncingService.startSync();
_watchedAttachmentsSubscription =
_watchAttachments().listen((items) async {
await _processWatchedAttachments(items);
});
var previouslyConnected = _db.currentStatus.connected;
_syncStatusSubscription = _db.statusStream.listen((status) {
if (!previouslyConnected && status.connected) {
_syncingService.triggerSync();
}
previouslyConnected = status.connected;
});
_watchAttachments().listen((items) async {
await _processWatchedAttachments(items);
});
_logger.info('AttachmentQueue started syncing.');
});
}