forked from openfoodfacts/smooth-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
product_image_viewer.dart
411 lines (387 loc) · 14 KB
/
product_image_viewer.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:photo_view/photo_view.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task_unselect.dart';
import 'package:smooth_app/data_models/product_image_data.dart';
import 'package:smooth_app/database/dao_int.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/database/transient_file.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/generic_lib/loading_dialog.dart';
import 'package:smooth_app/generic_lib/widgets/picture_not_found.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/image/uploaded_image_gallery.dart';
import 'package:smooth_app/pages/image_crop_page.dart';
import 'package:smooth_app/pages/product/common/product_refresher.dart';
import 'package:smooth_app/pages/product/edit_image_button.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/tmp_crop_image/new_crop_page.dart';
import 'package:smooth_app/tmp_crop_image/rotation.dart';
import 'package:smooth_app/widgets/smooth_scaffold.dart';
/// Displays a full-screen image with an "edit" floating button.
class ProductImageViewer extends StatefulWidget {
const ProductImageViewer({
required this.product,
required this.imageField,
});
final Product product;
final ImageField imageField;
@override
State<ProductImageViewer> createState() => _ProductImageViewerState();
}
class _ProductImageViewerState extends State<ProductImageViewer> {
late Product _product;
late final Product _initialProduct;
late final LocalDatabase _localDatabase;
late ProductImageData _imageData;
String get _barcode => _initialProduct.barcode!;
@override
void initState() {
super.initState();
_initialProduct = widget.product;
_localDatabase = context.read<LocalDatabase>();
_localDatabase.upToDate.showInterest(_barcode);
}
@override
void dispose() {
_localDatabase.upToDate.loseInterest(_barcode);
super.dispose();
}
@override
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
context.watch<LocalDatabase>();
_product = _localDatabase.upToDate.getLocalUpToDate(_initialProduct);
_imageData = getProductImageData(_product, widget.imageField);
final ImageProvider? imageProvider = TransientFile.getImageProvider(
_imageData,
_barcode,
);
return SmoothScaffold(
extendBodyBehindAppBar: true,
backgroundColor: Colors.black,
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(MINIMUM_TOUCH_SIZE / 2),
child: imageProvider == null
? const SizedBox.expand(child: PictureNotFound())
: PhotoView(
minScale: 0.2,
imageProvider: imageProvider,
heroAttributes: PhotoViewHeroAttributes(
tag: imageProvider,
),
backgroundDecoration: const BoxDecoration(
color: Colors.black,
),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: SMALL_SPACE),
child: _getGalleryButton(appLocalizations),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: SMALL_SPACE),
child: _getCameraImageButton(
appLocalizations,
imageProvider == null,
),
),
),
],
),
if (imageProvider != null)
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: SMALL_SPACE),
child: (imageProvider == null)
? Container()
: EditImageButton(
iconData: Icons.do_disturb_on,
label: appLocalizations
.edit_photo_unselect_button_label,
onPressed: () async {
final bool? confirmed = await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return SmoothAlertDialog(
title:
appLocalizations.confirm_button_label,
body: Text(
appLocalizations.are_you_sure,
),
close: true,
positiveAction: SmoothActionButton(
text: appLocalizations.yes,
onPressed: () =>
Navigator.of(context).pop(true)),
negativeAction: SmoothActionButton(
text: appLocalizations.no,
onPressed: () => Navigator.of(context)
.pop(false)));
},
);
if (confirmed == true) {
await BackgroundTaskUnselect.addTask(
_barcode,
imageField: widget.imageField,
widget: this,
);
}
}),
),
),
Expanded(
child: Padding(
padding:
const EdgeInsets.symmetric(horizontal: SMALL_SPACE),
child: _getEditImageButton(appLocalizations),
),
),
],
),
],
),
);
}
Widget _getEditImageButton(final AppLocalizations appLocalizations) =>
EditImageButton(
iconData: Icons.edit,
label: appLocalizations.edit_photo_button_label,
onPressed: _actionEditImage,
);
Widget _getCameraImageButton(
final AppLocalizations appLocalizations,
final bool firstPhoto,
) =>
EditImageButton(
iconData: firstPhoto ? Icons.add : Icons.add_a_photo,
label: firstPhoto ? appLocalizations.add : appLocalizations.capture,
onPressed: _actionNewImage,
);
Widget _getUnselectImageButton(final AppLocalizations appLocalizations) =>
EditImageButton(
iconData: Icons.do_disturb_on,
label: appLocalizations.edit_photo_unselect_button_label,
onPressed: _actionUnselect,
);
Widget _getGalleryButton(final AppLocalizations appLocalizations) =>
EditImageButton(
iconData: Icons.image_search,
label: appLocalizations.edit_photo_select_existing_button_label,
onPressed: _actionGallery,
);
// TODO(monsieurtanuki): we should also suggest the existing image gallery
Future<File?> _actionNewImage() async {
// ignore: use_build_context_synchronously
if (!await ProductRefresher().checkIfLoggedIn(context)) {
return null;
}
return confirmAndUploadNewPicture(
this,
imageField: _imageData.imageField,
barcode: _barcode,
);
}
Future<void> _actionGallery() async {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
// ignore: use_build_context_synchronously
if (!await ProductRefresher().checkIfLoggedIn(context)) {
return;
}
// ignore: use_build_context_synchronously
final List<int>? result = await LoadingDialog.run<List<int>>(
future: OpenFoodAPIClient.getProductImageIds(
_barcode,
user: ProductQuery.getUser(),
),
context: context,
title: appLocalizations.edit_photo_select_existing_download_label,
);
if (result == null) {
return;
}
if (!mounted) {
return;
}
if (result.isEmpty) {
await showDialog<void>(
context: context,
builder: (BuildContext context) => SmoothAlertDialog(
body:
Text(appLocalizations.edit_photo_select_existing_downloaded_none),
actionsAxis: Axis.vertical,
positiveAction: SmoothActionButton(
text: appLocalizations.okay,
onPressed: () => Navigator.of(context).pop(),
),
),
);
return;
}
await Navigator.push<void>(
context,
MaterialPageRoute<void>(
builder: (BuildContext context) => UploadedImageGallery(
barcode: _barcode,
imageIds: result,
imageField: widget.imageField,
),
),
);
}
Future<File?> _actionEditImage() async {
final NavigatorState navigatorState = Navigator.of(context);
// ignore: use_build_context_synchronously
if (!await ProductRefresher().checkIfLoggedIn(context)) {
return null;
}
// best possibility: with the crop parameters
// TODO(monsieurtanuki): maybe we should keep the big image locally, in order to avoid the server call?
final ProductImage? productImage = _getBestProductImage();
if (productImage != null) {
final int? imageId = int.tryParse(productImage.imgid!);
if (imageId != null) {
return _openEditCroppedImage(imageId, productImage);
}
}
// alternate option: use the transient file.
File? imageFile = TransientFile.getImage(
_imageData.imageField,
_barcode,
);
if (imageFile != null) {
return _openCropPage(navigatorState, imageFile);
}
// but if not possible, get the best picture from the server.
final String? imageUrl = _imageData.getImageUrl(ImageSize.ORIGINAL);
// ignore: use_build_context_synchronously
imageFile = await downloadImageUrl(
context,
imageUrl,
DaoInt(_localDatabase),
);
if (imageFile != null) {
return _openCropPage(navigatorState, imageFile);
}
return null;
}
Future<void> _actionUnselect() async {
final NavigatorState navigatorState = Navigator.of(context);
// ignore: use_build_context_synchronously
if (!await ProductRefresher().checkIfLoggedIn(context)) {
return;
}
await BackgroundTaskUnselect.addTask(
_barcode,
imageField: widget.imageField,
widget: this,
);
_localDatabase.notifyListeners();
navigatorState.pop();
}
Future<File?> _openCropPage(
final NavigatorState navigatorState,
final File imageFile, {
final int? imageId,
final Rect? initialCropRect,
final Rotation? initialRotation,
}) async =>
navigatorState.push<File>(
MaterialPageRoute<File>(
builder: (BuildContext context) => CropPage(
barcode: _product.barcode!,
imageField: _imageData.imageField,
inputFile: imageFile,
imageId: imageId,
initiallyDifferent: false,
initialCropRect: initialCropRect,
initialRotation: initialRotation,
),
fullscreenDialog: true,
),
);
Future<File?> _openEditCroppedImage(
final int imageId,
final ProductImage productImage,
) async {
final NavigatorState navigatorState = Navigator.of(context);
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final File? imageFile = await downloadImageUrl(
context,
ImageHelper.getUploadedImageUrl(
_product.barcode!,
imageId,
ImageSize.ORIGINAL,
),
DaoInt(localDatabase),
);
if (imageFile == null) {
return null;
}
return _openCropPage(
navigatorState,
imageFile,
imageId: imageId,
initialCropRect: _getCropRect(productImage),
initialRotation: RotationExtension.fromDegrees(
productImage.angle?.degree ?? 0,
),
);
}
ProductImage? _getBestProductImage() {
if (_product.images == null) {
return null;
}
for (final ProductImage productImage in _product.images!) {
if (productImage.field != _imageData.imageField) {
continue;
}
if (productImage.language != ProductQuery.getLanguage()) {
continue;
}
if (productImage.size == ImageSize.ORIGINAL) {
if (productImage.imgid != null) {
return productImage;
}
}
}
return null;
}
/// Returns a crop rect, to be compared with the full image dimensions.
Rect? _getCropRect(final ProductImage productImage) =>
productImage.x1 == null ||
productImage.y1 == null ||
productImage.x2 == null ||
productImage.y2 == null
? null
: Rect.fromLTRB(
productImage.x1!.toDouble(),
productImage.y1!.toDouble(),
productImage.x2!.toDouble(),
productImage.y2!.toDouble(),
);
}