Implement offline mode
This commit is contained in:
@@ -15,8 +15,8 @@ class AnnotationSyncService {
|
||||
AnnotationSyncService({
|
||||
required ApiClient apiClient,
|
||||
required StorageService storageService,
|
||||
}) : _apiClient = apiClient,
|
||||
_storageService = storageService;
|
||||
}) : _apiClient = apiClient,
|
||||
_storageService = storageService;
|
||||
|
||||
/// Downloads annotations from server and merges with local storage.
|
||||
///
|
||||
@@ -31,8 +31,8 @@ class AnnotationSyncService {
|
||||
final serverAnnotations = await _apiClient.fetchAnnotations(sheetUuid);
|
||||
|
||||
// Get all local annotations with metadata
|
||||
final localAnnotations = await _storageService
|
||||
.readAllAnnotationsWithMetadata(sheetUuid);
|
||||
final localAnnotations =
|
||||
await _storageService.readAllAnnotationsWithMetadata(sheetUuid);
|
||||
|
||||
int updatedCount = 0;
|
||||
|
||||
@@ -88,7 +88,7 @@ class AnnotationSyncService {
|
||||
/// Uploads a single page's annotation to the server.
|
||||
///
|
||||
/// Called when annotations are saved (e.g., exiting paint mode).
|
||||
/// Silently fails if upload fails (allows offline usage).
|
||||
/// If upload fails (e.g., offline), the annotation is queued for later sync.
|
||||
Future<bool> uploadAnnotation({
|
||||
required String sheetUuid,
|
||||
required int page,
|
||||
@@ -108,11 +108,42 @@ class AnnotationSyncService {
|
||||
_log.info('Upload successful');
|
||||
return true;
|
||||
} on ApiException catch (e) {
|
||||
_log.warning('Failed to upload annotation: $e');
|
||||
_log.warning('Failed to upload annotation, queuing for later: $e');
|
||||
await _queueForLaterUpload(
|
||||
sheetUuid: sheetUuid,
|
||||
page: page,
|
||||
annotationsJson: annotationsJson,
|
||||
lastModified: lastModified,
|
||||
);
|
||||
return false;
|
||||
} catch (e) {
|
||||
_log.warning('Unexpected error uploading annotation: $e');
|
||||
_log.warning(
|
||||
'Unexpected error uploading annotation, queuing for later: $e');
|
||||
await _queueForLaterUpload(
|
||||
sheetUuid: sheetUuid,
|
||||
page: page,
|
||||
annotationsJson: annotationsJson,
|
||||
lastModified: lastModified,
|
||||
);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// Queues an annotation for later upload when connection is restored.
|
||||
Future<void> _queueForLaterUpload({
|
||||
required String sheetUuid,
|
||||
required int page,
|
||||
required String annotationsJson,
|
||||
required DateTime lastModified,
|
||||
}) async {
|
||||
await _storageService.writePendingAnnotationUpload(
|
||||
PendingAnnotationUpload(
|
||||
sheetUuid: sheetUuid,
|
||||
page: page,
|
||||
annotationsJson: annotationsJson,
|
||||
lastModified: lastModified,
|
||||
),
|
||||
);
|
||||
_log.info('Annotation queued for later upload: $sheetUuid page $page');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user