Skip to content

Commit 159b158

Browse files
Merge branch 'master' into testing-credentials
2 parents a007899 + ea8e924 commit 159b158

File tree

15 files changed

+99
-19
lines changed

15 files changed

+99
-19
lines changed

demos/supabase-simple-chat/lib/pages/login_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class LoginPageState extends State<LoginPage> {
3737
} catch (_) {
3838
context.showErrorSnackBar(message: unexpectedErrorMessage);
3939
}
40-
if (mounted) {
40+
if (context.mounted) {
4141
setState(() {
4242
_isLoading = true;
4343
});

demos/supabase-todolist/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
97C146E61CF9000F007C117D /* Project object */ = {
156156
isa = PBXProject;
157157
attributes = {
158-
LastUpgradeCheck = 1430;
158+
LastUpgradeCheck = 1510;
159159
ORGANIZATIONNAME = "";
160160
TargetAttributes = {
161161
97C146ED1CF9000F007C117D = {

demos/supabase-todolist/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "1430"
3+
LastUpgradeVersion = "1510"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

demos/supabase-todolist/lib/attachments/photo_widget.dart

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:io';
22

33
import 'package:flutter/material.dart';
4+
import 'package:powersync_attachments_helper/powersync_attachments_helper.dart';
45
import 'package:powersync_flutter_demo/attachments/camera_helpers.dart';
56
import 'package:powersync_flutter_demo/attachments/photo_capture_widget.dart';
67
import 'package:powersync_flutter_demo/attachments/queue.dart';
@@ -23,8 +24,10 @@ class PhotoWidget extends StatefulWidget {
2324
class _ResolvedPhotoState {
2425
String? photoPath;
2526
bool fileExists;
27+
Attachment? attachment;
2628

27-
_ResolvedPhotoState({required this.photoPath, required this.fileExists});
29+
_ResolvedPhotoState(
30+
{required this.photoPath, required this.fileExists, this.attachment});
2831
}
2932

3033
class _PhotoWidgetState extends State<PhotoWidget> {
@@ -38,7 +41,17 @@ class _PhotoWidgetState extends State<PhotoWidget> {
3841

3942
bool fileExists = await File(photoPath).exists();
4043

41-
return _ResolvedPhotoState(photoPath: photoPath, fileExists: fileExists);
44+
final row = await attachmentQueue.db
45+
.getOptional('SELECT * FROM attachments_queue WHERE id = ?', [photoId]);
46+
47+
if (row != null) {
48+
Attachment attachment = Attachment.fromRow(row);
49+
return _ResolvedPhotoState(
50+
photoPath: photoPath, fileExists: fileExists, attachment: attachment);
51+
}
52+
53+
return _ResolvedPhotoState(
54+
photoPath: photoPath, fileExists: fileExists, attachment: null);
4255
}
4356

4457
@override
@@ -84,6 +97,20 @@ class _PhotoWidgetState extends State<PhotoWidget> {
8497

8598
String? filePath = data.photoPath;
8699
bool fileIsDownloading = !data.fileExists;
100+
bool fileArchived =
101+
data.attachment?.state == AttachmentState.archived.index;
102+
103+
if (fileArchived) {
104+
return Column(
105+
crossAxisAlignment: CrossAxisAlignment.center,
106+
mainAxisAlignment: MainAxisAlignment.center,
107+
children: [
108+
const Text("Unavailable"),
109+
const SizedBox(height: 8),
110+
takePhotoButton
111+
],
112+
);
113+
}
87114

88115
if (fileIsDownloading) {
89116
return const Text("Downloading...");

demos/supabase-todolist/lib/attachments/queue.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,21 @@ import 'package:powersync_flutter_demo/models/schema.dart';
1111
late final PhotoAttachmentQueue attachmentQueue;
1212
final remoteStorage = SupabaseStorageAdapter();
1313

14+
/// Function to handle errors when downloading attachments
15+
/// Return false if you want to archive the attachment
16+
Future<bool> onDownloadError(Attachment attachment, Object exception) async {
17+
if (exception.toString().contains('Object not found')) {
18+
return false;
19+
}
20+
return true;
21+
}
22+
1423
class PhotoAttachmentQueue extends AbstractAttachmentQueue {
1524
PhotoAttachmentQueue(db, remoteStorage)
16-
: super(db: db, remoteStorage: remoteStorage);
25+
: super(
26+
db: db,
27+
remoteStorage: remoteStorage,
28+
onDownloadError: onDownloadError);
1729

1830
@override
1931
init() async {

demos/supabase-todolist/lib/widgets/lists_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class _ListsWidgetState extends State<ListsWidget> {
6161
super.initState();
6262
final stream = TodoList.watchListsWithStats();
6363
_subscription = stream.listen((data) {
64-
if (!mounted) {
64+
if (!context.mounted) {
6565
return;
6666
}
6767
setState(() {

demos/supabase-todolist/lib/widgets/query_widget.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class QueryWidgetState extends State<QueryWidget> {
4646
_subscription?.cancel();
4747
final stream = db.watch(_query);
4848
_subscription = stream.listen((data) {
49-
if (!mounted) {
49+
if (!context.mounted) {
5050
return;
5151
}
5252
setState(() {

demos/supabase-todolist/lib/widgets/todo_list_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class TodoListWidgetState extends State<TodoListWidget> {
6262
super.initState();
6363
final stream = widget.list.watchItems();
6464
_subscription = stream.listen((data) {
65-
if (!mounted) {
65+
if (!context.mounted) {
6666
return;
6767
}
6868
setState(() {

demos/supabase-todolist/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ packages:
429429
path: "../../packages/powersync_attachments_helper"
430430
relative: true
431431
source: path
432-
version: "0.2.0"
432+
version: "0.2.1"
433433
realtime_client:
434434
dependency: transitive
435435
description:

packages/powersync_attachments_helper/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.2.1
2+
3+
- Added `onUploadError` as an optional function that can be set when setting up the queue to handle upload errors
4+
- Added `onDownloadError` as an optional function that can be set when setting up the queue to handle upload errors
5+
16
## 0.2.0
27

38
- Potentially BREAKING CHANGE for users who rely on multiple attachment queues.

packages/powersync_attachments_helper/lib/src/attachments_queue.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,28 @@ abstract class AbstractAttachmentQueue {
2424
final LocalStorageAdapter localStorage = LocalStorageAdapter();
2525
String attachmentsQueueTableName;
2626

27+
/// Function to handle errors when downloading attachments
28+
/// Return true if you want to ignore attachment
29+
Future<bool> Function(Attachment attachment, Object exception)?
30+
onDownloadError;
31+
32+
/// Function to handle errors when uploading attachments
33+
/// Return true if you want to ignore attachment
34+
Future<bool> Function(Attachment attachment, Object exception)? onUploadError;
35+
2736
AbstractAttachmentQueue(
2837
{required this.db,
2938
required this.remoteStorage,
3039
this.attachmentDirectoryName = 'attachments',
3140
this.attachmentsQueueTableName = defaultAttachmentsQueueTableName,
41+
this.onDownloadError,
42+
this.onUploadError,
3243
performInitialSync = true}) {
3344
attachmentsService = AttachmentsService(
3445
db, localStorage, attachmentDirectoryName, attachmentsQueueTableName);
3546
syncingService = SyncingService(
36-
db, remoteStorage, localStorage, attachmentsService, getLocalUri);
47+
db, remoteStorage, localStorage, attachmentsService, getLocalUri,
48+
onDownloadError: onDownloadError, onUploadError: onUploadError);
3749
}
3850

3951
/// Create watcher to get list of ID's from a table to be used for syncing in the attachment queue.

packages/powersync_attachments_helper/lib/src/attachments_queue_table.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ class Attachment {
6464
/// 1. Attachment to be uploaded
6565
/// 2. Attachment to be downloaded
6666
/// 3. Attachment to be deleted
67-
enum AttachmentState {
68-
queuedUpload,
69-
queuedDownload,
70-
queuedDelete,
71-
}
67+
/// 4. Attachment to be archived
68+
enum AttachmentState { queuedUpload, queuedDownload, queuedDelete, archived }
7269

7370
class AttachmentsQueueTable extends Table {
7471
AttachmentsQueueTable(

packages/powersync_attachments_helper/lib/src/attachments_service.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ class AttachmentsService {
2323
Future<void> deleteAttachment(String id) async =>
2424
db.execute('DELETE FROM $table WHERE id = ?', [id]);
2525

26+
///Set the state of the attachment to ignore.
27+
Future<void> ignoreAttachment(String id) async => db.execute(
28+
'UPDATE $table SET state = ${AttachmentState.archived.index} WHERE id = ?',
29+
[id]);
30+
2631
/// Get the attachment from the attachment queue using an ID.
2732
Future<Attachment?> getAttachment(String id) async =>
2833
db.getOptional('SELECT * FROM $table WHERE id = ?', [id]).then((row) {

packages/powersync_attachments_helper/lib/src/syncing_service.dart

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ class SyncingService {
1515
final LocalStorageAdapter localStorage;
1616
final AttachmentsService attachmentsService;
1717
final Function getLocalUri;
18+
final Future<bool> Function(Attachment attachment, Object exception)?
19+
onDownloadError;
20+
final Future<bool> Function(Attachment attachment, Object exception)?
21+
onUploadError;
1822

1923
SyncingService(this.db, this.remoteStorage, this.localStorage,
20-
this.attachmentsService, this.getLocalUri);
24+
this.attachmentsService, this.getLocalUri,
25+
{this.onDownloadError, this.onUploadError});
2126

2227
/// Upload attachment from local storage and to remote storage
2328
/// then remove it from the queue.
24-
/// If duplicate of the file is found uploading is ignored and
29+
/// If duplicate of the file is found uploading is archived and
2530
/// the attachment is removed from the queue.
2631
Future<void> uploadAttachment(Attachment attachment) async {
2732
if (attachment.localUri == null) {
@@ -44,6 +49,14 @@ class SyncingService {
4449
}
4550

4651
log.severe('Upload attachment error for attachment $attachment', e);
52+
if (onUploadError != null) {
53+
bool shouldRetry = await onUploadError!(attachment, e);
54+
if (!shouldRetry) {
55+
log.info('Attachment with ID ${attachment.id} has been archived', e);
56+
await attachmentsService.ignoreAttachment(attachment.id);
57+
}
58+
}
59+
4760
return;
4861
}
4962
}
@@ -63,6 +76,15 @@ class SyncingService {
6376
await attachmentsService.deleteAttachment(attachment.id);
6477
return;
6578
} catch (e) {
79+
if (onDownloadError != null) {
80+
bool shouldRetry = await onDownloadError!(attachment, e);
81+
if (!shouldRetry) {
82+
log.info('Attachment with ID ${attachment.id} has been archived', e);
83+
await attachmentsService.ignoreAttachment(attachment.id);
84+
return;
85+
}
86+
}
87+
6688
log.severe('Download attachment error for attachment $attachment', e);
6789
return;
6890
}

packages/powersync_attachments_helper/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: powersync_attachments_helper
22
description: A helper library for handling attachments when using PowerSync.
3-
version: 0.2.0
3+
version: 0.2.1
44
repository: https://github.com/powersync-ja/powersync.dart
55
homepage: https://www.powersync.com/
66
environment:

0 commit comments

Comments
 (0)