Skip to content

Commit ea8e924

Browse files
DominicGBauerDominicGBauerstevensJourney
authored
feat(attachments): add error handlers (#65)
* feat(attachments): add error handlers * fix: lint issue * chore: version bump * chore: pr reverts * Update packages/powersync_attachments_helper/lib/src/attachments_queue_table.dart Co-authored-by: stevensJourney <51082125+stevensJourney@users.noreply.github.com> * Update demos/supabase-todolist/lib/attachments/queue.dart Co-authored-by: stevensJourney <51082125+stevensJourney@users.noreply.github.com> * chore: improve demo * fix: image issue --------- Co-authored-by: DominicGBauer <dominic@nomanini.com> Co-authored-by: stevensJourney <51082125+stevensJourney@users.noreply.github.com>
1 parent 92033b2 commit ea8e924

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.

0 commit comments

Comments
 (0)