|
1 |
| -import 'dart:io'; |
| 1 | +import 'dart:typed_data'; |
2 | 2 |
|
3 |
| -import 'package:path_provider/path_provider.dart'; |
4 |
| -import 'package:path/path.dart' as p; |
5 | 3 | import 'package:flutter/material.dart';
|
6 | 4 | import 'package:powersync_core/attachments/attachments.dart';
|
7 | 5 | import 'package:powersync_flutter_demo/attachments/camera_helpers.dart';
|
8 | 6 | import 'package:powersync_flutter_demo/attachments/photo_capture_widget.dart';
|
9 | 7 |
|
10 | 8 | import '../models/todo_item.dart';
|
11 | 9 | import '../powersync.dart';
|
| 10 | +import 'queue.dart'; |
12 | 11 |
|
13 |
| -class PhotoWidget extends StatefulWidget { |
| 12 | +class PhotoWidget extends StatelessWidget { |
14 | 13 | final TodoItem todo;
|
15 | 14 |
|
16 | 15 | PhotoWidget({
|
17 | 16 | required this.todo,
|
18 | 17 | }) : super(key: ObjectKey(todo.id));
|
19 | 18 |
|
20 | 19 | @override
|
21 |
| - State<StatefulWidget> createState() { |
22 |
| - return _PhotoWidgetState(); |
| 20 | + Widget build(BuildContext context) { |
| 21 | + return StreamBuilder( |
| 22 | + stream: _attachmentState(todo.photoId), |
| 23 | + builder: (context, snapshot) { |
| 24 | + if (snapshot.data == null) { |
| 25 | + return Container(); |
| 26 | + } |
| 27 | + final data = snapshot.data!; |
| 28 | + final attachment = data.attachment; |
| 29 | + if (todo.photoId == null || attachment == null) { |
| 30 | + return TakePhotoButton(todoId: todo.id); |
| 31 | + } |
| 32 | + |
| 33 | + var fileArchived = data.attachment?.state == AttachmentState.archived; |
| 34 | + |
| 35 | + if (fileArchived) { |
| 36 | + return Column( |
| 37 | + crossAxisAlignment: CrossAxisAlignment.center, |
| 38 | + mainAxisAlignment: MainAxisAlignment.center, |
| 39 | + children: [ |
| 40 | + const Text("Unavailable"), |
| 41 | + const SizedBox(height: 8), |
| 42 | + TakePhotoButton(todoId: todo.id), |
| 43 | + ], |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + return AttachmentImage(attachment: attachment); |
| 48 | + }, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + static Stream<_AttachmentState> _attachmentState(String? id) { |
| 53 | + return db.watch('SELECT * FROM attachments_queue WHERE id = ?', |
| 54 | + parameters: [id]).map((rows) { |
| 55 | + if (rows.isEmpty) { |
| 56 | + return const _AttachmentState(null); |
| 57 | + } |
| 58 | + |
| 59 | + return _AttachmentState(Attachment.fromRow(rows.single)); |
| 60 | + }); |
23 | 61 | }
|
24 | 62 | }
|
25 | 63 |
|
26 |
| -class _ResolvedPhotoState { |
27 |
| - String? photoPath; |
28 |
| - bool fileExists; |
29 |
| - Attachment? attachment; |
| 64 | +class TakePhotoButton extends StatelessWidget { |
| 65 | + final String todoId; |
| 66 | + |
| 67 | + const TakePhotoButton({super.key, required this.todoId}); |
30 | 68 |
|
31 |
| - _ResolvedPhotoState( |
32 |
| - {required this.photoPath, required this.fileExists, this.attachment}); |
| 69 | + @override |
| 70 | + Widget build(BuildContext context) { |
| 71 | + return ElevatedButton( |
| 72 | + onPressed: () async { |
| 73 | + final camera = await setupCamera(); |
| 74 | + if (!context.mounted) return; |
| 75 | + |
| 76 | + if (camera == null) { |
| 77 | + const snackBar = SnackBar( |
| 78 | + content: Text('No camera available'), |
| 79 | + backgroundColor: Colors.red, // Optional: to highlight it's an error |
| 80 | + ); |
| 81 | + |
| 82 | + ScaffoldMessenger.of(context).showSnackBar(snackBar); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + Navigator.push( |
| 87 | + context, |
| 88 | + MaterialPageRoute( |
| 89 | + builder: (context) => |
| 90 | + TakePhotoWidget(todoId: todoId, camera: camera), |
| 91 | + ), |
| 92 | + ); |
| 93 | + }, |
| 94 | + child: const Text('Take Photo'), |
| 95 | + ); |
| 96 | + } |
33 | 97 | }
|
34 | 98 |
|
35 |
| -class _PhotoWidgetState extends State<PhotoWidget> { |
36 |
| - late String photoPath; |
| 99 | +final class _AttachmentState { |
| 100 | + final Attachment? attachment; |
37 | 101 |
|
38 |
| - Future<_ResolvedPhotoState> _getPhotoState(photoId) async { |
39 |
| - if (photoId == null) { |
40 |
| - return _ResolvedPhotoState(photoPath: null, fileExists: false); |
41 |
| - } |
42 |
| - final appDocDir = await getApplicationDocumentsDirectory(); |
43 |
| - photoPath = p.join(appDocDir.path, '$photoId.jpg'); |
| 102 | + const _AttachmentState(this.attachment); |
| 103 | +} |
44 | 104 |
|
45 |
| - bool fileExists = await File(photoPath).exists(); |
| 105 | +/// A widget showing an [Attachment] as an image. |
| 106 | +/// |
| 107 | +/// For better web support, always loads the attachment into memory first. If |
| 108 | +/// you're only targeting native platforms, a more efficient mechanism would be |
| 109 | +/// to use `IOLocalStorage.pathFor` with an [Image.file] image. |
| 110 | +class AttachmentImage extends StatefulWidget { |
| 111 | + final Attachment attachment; |
46 | 112 |
|
47 |
| - final row = await db |
48 |
| - .getOptional('SELECT * FROM attachments_queue WHERE id = ?', [photoId]); |
| 113 | + const AttachmentImage({super.key, required this.attachment}); |
49 | 114 |
|
50 |
| - if (row != null) { |
51 |
| - Attachment attachment = Attachment.fromRow(row); |
52 |
| - return _ResolvedPhotoState( |
53 |
| - photoPath: photoPath, fileExists: fileExists, attachment: attachment); |
54 |
| - } |
| 115 | + @override |
| 116 | + State<AttachmentImage> createState() => _AttachmentImageState(); |
| 117 | +} |
55 | 118 |
|
56 |
| - return _ResolvedPhotoState( |
57 |
| - photoPath: photoPath, fileExists: fileExists, attachment: null); |
| 119 | +class _AttachmentImageState extends State<AttachmentImage> { |
| 120 | + Future<Uint8List?>? _imageBytes; |
| 121 | + |
| 122 | + void _loadBytes() { |
| 123 | + setState(() { |
| 124 | + _imageBytes = Future(() async { |
| 125 | + final buffer = BytesBuilder(); |
| 126 | + if (!await localStorage.fileExists(widget.attachment.filename)) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + await localStorage |
| 131 | + .readFile(widget.attachment.filename) |
| 132 | + .forEach(buffer.add); |
| 133 | + return buffer.takeBytes(); |
| 134 | + }); |
| 135 | + }); |
58 | 136 | }
|
59 | 137 |
|
60 | 138 | @override
|
61 |
| - Widget build(BuildContext context) { |
62 |
| - return FutureBuilder( |
63 |
| - future: _getPhotoState(widget.todo.photoId), |
64 |
| - builder: (BuildContext context, |
65 |
| - AsyncSnapshot<_ResolvedPhotoState> snapshot) { |
66 |
| - if (snapshot.data == null) { |
67 |
| - return Container(); |
68 |
| - } |
69 |
| - final data = snapshot.data!; |
70 |
| - Widget takePhotoButton = ElevatedButton( |
71 |
| - onPressed: () async { |
72 |
| - final camera = await setupCamera(); |
73 |
| - if (!context.mounted) return; |
74 |
| - |
75 |
| - if (camera == null) { |
76 |
| - const snackBar = SnackBar( |
77 |
| - content: Text('No camera available'), |
78 |
| - backgroundColor: |
79 |
| - Colors.red, // Optional: to highlight it's an error |
80 |
| - ); |
81 |
| - |
82 |
| - ScaffoldMessenger.of(context).showSnackBar(snackBar); |
83 |
| - return; |
84 |
| - } |
85 |
| - |
86 |
| - Navigator.push( |
87 |
| - context, |
88 |
| - MaterialPageRoute( |
89 |
| - builder: (context) => |
90 |
| - TakePhotoWidget(todoId: widget.todo.id, camera: camera), |
91 |
| - ), |
92 |
| - ); |
93 |
| - }, |
94 |
| - child: const Text('Take Photo'), |
95 |
| - ); |
| 139 | + void initState() { |
| 140 | + super.initState(); |
| 141 | + _loadBytes(); |
| 142 | + } |
96 | 143 |
|
97 |
| - if (widget.todo.photoId == null) { |
98 |
| - return takePhotoButton; |
99 |
| - } |
| 144 | + @override |
| 145 | + void didUpdateWidget(covariant AttachmentImage oldWidget) { |
| 146 | + super.didUpdateWidget(oldWidget); |
| 147 | + if (oldWidget.attachment != widget.attachment) { |
| 148 | + _loadBytes(); |
| 149 | + } |
| 150 | + } |
100 | 151 |
|
101 |
| - String? filePath = data.photoPath; |
102 |
| - bool fileIsDownloading = !data.fileExists; |
103 |
| - bool fileArchived = |
104 |
| - data.attachment?.state == AttachmentState.archived; |
105 |
| - |
106 |
| - if (fileArchived) { |
107 |
| - return Column( |
108 |
| - crossAxisAlignment: CrossAxisAlignment.center, |
109 |
| - mainAxisAlignment: MainAxisAlignment.center, |
110 |
| - children: [ |
111 |
| - const Text("Unavailable"), |
112 |
| - const SizedBox(height: 8), |
113 |
| - takePhotoButton |
114 |
| - ], |
| 152 | + @override |
| 153 | + Widget build(BuildContext context) { |
| 154 | + return FutureBuilder( |
| 155 | + future: _imageBytes, |
| 156 | + builder: (context, snapshot) { |
| 157 | + if (snapshot.connectionState == ConnectionState.done) { |
| 158 | + if (snapshot.data case final bytes?) { |
| 159 | + return Image.memory( |
| 160 | + bytes, |
| 161 | + width: 50, |
| 162 | + height: 50, |
115 | 163 | );
|
| 164 | + } else { |
| 165 | + return const Text('Downloading...'); |
116 | 166 | }
|
117 |
| - |
118 |
| - if (fileIsDownloading) { |
119 |
| - return const Text("Downloading..."); |
120 |
| - } |
121 |
| - |
122 |
| - File imageFile = File(filePath!); |
123 |
| - int lastModified = imageFile.existsSync() |
124 |
| - ? imageFile.lastModifiedSync().millisecondsSinceEpoch |
125 |
| - : 0; |
126 |
| - Key key = ObjectKey('$filePath:$lastModified'); |
127 |
| - |
128 |
| - return Image.file( |
129 |
| - key: key, |
130 |
| - imageFile, |
131 |
| - width: 50, |
132 |
| - height: 50, |
133 |
| - ); |
134 |
| - }); |
| 167 | + } else { |
| 168 | + return Container(); |
| 169 | + } |
| 170 | + }, |
| 171 | + ); |
135 | 172 | }
|
136 | 173 | }
|
0 commit comments