Skip to content

Commit

Permalink
use ingredient.license_object_url to link to human readable page
Browse files Browse the repository at this point in the history
  • Loading branch information
Dieterbe committed Jul 12, 2024
1 parent d99ca41 commit 1ae3f33
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/models/nutrition/ingredient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions lib/models/nutrition/ingredient.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 5 additions & 8 deletions lib/widgets/nutrition/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand All @@ -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),
),
),
],
Expand Down

0 comments on commit 1ae3f33

Please sign in to comment.