-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathgetting_started.dart
67 lines (57 loc) · 2.13 KB
/
getting_started.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
import 'dart:async';
import 'package:powersync_attachments_helper/powersync_attachments_helper.dart';
import 'package:powersync_core/powersync_core.dart';
const schema = Schema([
Table('users', [Column.text('name'), Column.text('photo_id')])
]);
// Assume PowerSync database is initialized elsewhere
late PowerSyncDatabase db;
// Assume remote storage is implemented elsewhere
late AbstractRemoteStorageAdapter remoteStorage;
late PhotoAttachmentQueue attachmentQueue;
class PhotoAttachmentQueue extends AbstractAttachmentQueue {
PhotoAttachmentQueue(
PowerSyncDatabase db, AbstractRemoteStorageAdapter remoteStorage)
: super(db: db, remoteStorage: remoteStorage);
@override
Future<Attachment> saveFile(String fileId, int size,
{String 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}) {
return db.watch('''
SELECT photo_id FROM users
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);
});
}
}
Future<void> initializeAttachmentQueue(PowerSyncDatabase db) async {
attachmentQueue = PhotoAttachmentQueue(db, remoteStorage);
await attachmentQueue.init();
}