use ingredient.license_object_url to link to human readable page

This commit is contained in:
Dieter Plaetinck committed 2024-07-12 14:26:09 +03:00
1 parent d99ca4160f
commit 1ae3f33edd
3 files changed
+13 -9

No files matched your search

+5 -1
View File
@@ -26,7 +26,7 @@ part 'ingredient.g.dart';
class Ingredient {
// fields returned by django api that we ignore here:
// uuid, last_updated, last_imported, weight_units, language
// all license fields
// most license fields
@JsonKey(required: true)
final int id;
@@ -42,6 +42,9 @@ class Ingredient {
@JsonKey(required: true, name: 'source_url')
final String? sourceUrl;
@JsonKey(required: true, name: 'license_object_url')
final String? licenseObjectURl;
/// Barcode of the product
@JsonKey(required: true)
final String? code;
@@ -91,6 +94,7 @@ class Ingredient {
required this.remoteId,
required this.sourceName,
required this.sourceUrl,
this.licenseObjectURl,
required this.id,
required this.code,
required this.name,
+3
View File
@@ -14,6 +14,7 @@ Ingredient _$IngredientFromJson(Map<String, dynamic> json) {
'remote_id',
'source_name',
'source_url',
'license_object_url',
'code',
'name',
'created',
@@ -31,6 +32,7 @@ Ingredient _$IngredientFromJson(Map<String, dynamic> json) {
remoteId: json['remote_id'] as String?,
sourceName: json['source_name'] as String?,
sourceUrl: json['source_url'] as String?,
licenseObjectURl: json['license_object_url'] as String?,
id: (json['id'] as num).toInt(),
code: json['code'] as String?,
name: json['name'] as String,
@@ -55,6 +57,7 @@ Map<String, dynamic> _$IngredientToJson(Ingredient instance) =>
'remote_id': instance.remoteId,
'source_name': instance.sourceName,
'source_url': instance.sourceUrl,
'license_object_url': instance.licenseObjectURl,
'code': instance.code,
'name': instance.name,
'created': instance.created.toIso8601String(),
+5 -8
View File
@@ -110,15 +110,11 @@ void showIngredientDetails(BuildContext context, int id, {String? image}) {
Ingredient? ingredient;
NutritionalGoals? goals;
String? source;
String? url;
if (snapshot.hasData) {
ingredient = snapshot.data;
goals = ingredient!.nutritionalValues.toGoals();
source = ingredient.sourceName ?? 'unknown';
url = ingredient.remoteId == null
? null
: 'https://world.openfoodfacts.org/product/${ingredient.remoteId}';
}
return AlertDialog(
title: (snapshot.hasData) ? Text(ingredient!.name) : null,
@@ -131,7 +127,7 @@ void showIngredientDetails(BuildContext context, int id, {String? image}) {
if (image != null) const SizedBox(height: 12),
if (snapshot.hasError)
Text(
'Ingredient lookup error: ${snapshot.error}',
'Ingredient lookup error: ${snapshot.error ?? 'unknown error'}',
style: const TextStyle(color: Colors.red),
),
if (!snapshot.hasData && !snapshot.hasError) const CircularProgressIndicator(),
@@ -144,13 +140,14 @@ void showIngredientDetails(BuildContext context, int id, {String? image}) {
showGperKg: false,
),
),
if (snapshot.hasData && url == null) Text('Source: ${source!}'),
if (snapshot.hasData && url != null)
if (snapshot.hasData && ingredient!.licenseObjectURl == null)
Text('Source: ${source!}'),
if (snapshot.hasData && ingredient!.licenseObjectURl != null)
Padding(
padding: const EdgeInsets.only(top: 12),
child: InkWell(
child: Text('Source: ${source!}'),
onTap: () => launchURL(url!, context),
onTap: () => launchURL(ingredient!.licenseObjectURl!, context),
),
),
],