-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathqueue.dart
90 lines (75 loc) · 2.7 KB
/
queue.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import 'dart:async';
import 'package:powersync/powersync.dart';
import 'package:powersync_attachments_helper/powersync_attachments_helper.dart';
import 'package:supabase_todolist_drift/app_config.dart';
import 'package:supabase_todolist_drift/attachments/remote_storage_adapter.dart';
import 'package:supabase_todolist_drift/models/schema.dart';
/// Global reference to the queue
late final PhotoAttachmentQueue attachmentQueue;
final remoteStorage = SupabaseStorageAdapter();
/// Function to handle errors when downloading attachments
/// Return false if you want to archive the attachment
Future<bool> onDownloadError(Attachment attachment, Object exception) async {
if (exception.toString().contains('Object not found')) {
return false;
}
return true;
}
class PhotoAttachmentQueue extends AbstractAttachmentQueue {
PhotoAttachmentQueue(db, remoteStorage)
: super(
db: db,
remoteStorage: remoteStorage,
onDownloadError: onDownloadError);
@override
init() async {
if (AppConfig.supabaseStorageBucket.isEmpty) {
log.info(
'No Supabase bucket configured, skip setting up PhotoAttachmentQueue watches');
return;
}
await super.init();
}
@override
Future<Attachment> saveFile(String fileId, int size,
{mediaType = 'image/jpeg'}) async {
String filename = '$fileId.jpg';
Attachment photoAttachment = Attachment(
id: fileId,
filename: filename,
state: AttachmentState.queuedUpload.index,
mediaType: mediaType,
localUri: getLocalFilePathSuffix(filename),
size: size,
);
return attachmentsService.saveAttachment(photoAttachment);
}
@override
Future<Attachment> deleteFile(String fileId) async {
String filename = '$fileId.jpg';
Attachment photoAttachment = Attachment(
id: fileId,
filename: filename,
state: AttachmentState.queuedDelete.index);
return attachmentsService.saveAttachment(photoAttachment);
}
@override
StreamSubscription<void> watchIds({String? fileExtension}) {
log.info('Watching photos in $todosTable...');
return db.watch('''
SELECT photo_id FROM $todosTable
WHERE photo_id IS NOT NULL
''').map((results) {
return results.map((row) => row['photo_id'] as String).toList();
}).listen((ids) async {
List<String> idsInQueue = await attachmentsService.getAttachmentIds();
List<String> relevantIds =
ids.where((element) => !idsInQueue.contains(element)).toList();
syncingService.processIds(relevantIds, fileExtension);
});
}
}
initializeAttachmentQueue(PowerSyncDatabase db) async {
attachmentQueue = PhotoAttachmentQueue(db, remoteStorage);
await attachmentQueue.init();
}