feat: rename ingredients→products, products→user_products; add barcode/OFF import

- Rename catalog: ingredient/* → product/* (canonical_name, barcode, nutrition per 100g)
- Rename pantry: product/* → userproduct/* (user-owned items with expiry)
- Squash migrations into single 001_initial_schema.sql (clean-db baseline)
- product_categories: add English canonical name column; fix COALESCE in queries
- Remove product_translations: product names are stored in their original language
- Add default_unit_name to product API responses via unit_translations JOIN
- Add cmd/importoff: bulk import from OpenFoodFacts JSONL dump (COPY + ON CONFLICT)
- Diary: support product_id entries alongside dish_id (CHECK num_nonnulls = 1)
- Home: getLoggedCalories joins both recipes and catalog products
- Flutter: rename models/providers/services to match backend rename
- Flutter: add barcode scan flow for diary (mobile_scanner, product_portion_sheet)
- Flutter: localise 6 new keys across 12 languages (barcode scan, portion weight)
- Routes: GET /products/search, GET /products/barcode/{barcode}, /user-products

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-21 12:45:48 +02:00
parent 6861e5e754
commit 205edbdade
72 changed files with 2588 additions and 1444 deletions

View File

@@ -0,0 +1,107 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import '../../core/auth/auth_provider.dart';
import '../../l10n/app_localizations.dart';
import '../../shared/models/product.dart';
import 'food_product_service.dart';
import 'product_portion_sheet.dart';
/// Screen that activates the device camera to scan a barcode.
/// On successful scan it looks up the catalog product and shows
/// [ProductPortionSheet] to confirm the portion before adding to diary.
class BarcodeScanScreen extends ConsumerStatefulWidget {
const BarcodeScanScreen({
super.key,
required this.mealType,
required this.date,
required this.onAdded,
});
final String mealType;
final String date;
final VoidCallback onAdded;
@override
ConsumerState<BarcodeScanScreen> createState() => _BarcodeScanScreenState();
}
class _BarcodeScanScreenState extends ConsumerState<BarcodeScanScreen> {
bool _scanning = true;
void _onBarcodeDetected(BarcodeCapture capture) async {
if (!_scanning) return;
final rawValue = capture.barcodes.firstOrNull?.rawValue;
if (rawValue == null) return;
setState(() => _scanning = false);
final l10n = AppLocalizations.of(context)!;
final service = FoodProductService(ref.read(apiClientProvider));
final catalogProduct = await service.getByBarcode(rawValue);
if (!mounted) return;
if (catalogProduct == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(l10n.productNotFound)),
);
setState(() => _scanning = true);
return;
}
_showPortionSheet(catalogProduct);
}
void _showPortionSheet(CatalogProduct catalogProduct) {
showModalBottomSheet(
context: context,
isScrollControlled: true,
builder: (_) => ProductPortionSheet(
catalogProduct: catalogProduct,
onConfirm: (portionGrams) => _addToDiary(catalogProduct, portionGrams),
),
).then((_) {
if (mounted) setState(() => _scanning = true);
});
}
Future<void> _addToDiary(
CatalogProduct catalogProduct, double portionGrams) async {
final l10n = AppLocalizations.of(context)!;
try {
await ref.read(apiClientProvider).post('/diary', data: {
'product_id': catalogProduct.id,
'portion_g': portionGrams,
'meal_type': widget.mealType,
'date': widget.date,
'source': 'barcode',
});
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(catalogProduct.displayName)),
);
widget.onAdded();
Navigator.pop(context);
}
} catch (_) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(l10n.addFailed)),
);
}
}
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
return Scaffold(
appBar: AppBar(title: Text(l10n.scanBarcode)),
body: MobileScanner(
onDetect: _onBarcodeDetected,
),
);
}
}

View File

@@ -0,0 +1,32 @@
import '../../core/api/api_client.dart';
import '../../shared/models/product.dart';
/// Service for looking up catalog products by barcode or search query.
/// Used in the diary flow to log pre-packaged foods.
class FoodProductService {
const FoodProductService(this._client);
final ApiClient _client;
/// Fetches catalog product by barcode. Returns null when not found.
Future<CatalogProduct?> getByBarcode(String barcode) async {
try {
final data = await _client.get('/products/barcode/$barcode');
return CatalogProduct.fromJson(data);
} catch (_) {
return null;
}
}
/// Searches catalog products by name query.
Future<List<CatalogProduct>> searchProducts(String query) async {
if (query.isEmpty) return [];
final list = await _client.getList(
'/products/search',
params: {'q': query, 'limit': '10'},
);
return list
.map((e) => CatalogProduct.fromJson(e as Map<String, dynamic>))
.toList();
}
}

View File

@@ -0,0 +1,128 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../l10n/app_localizations.dart';
import '../../shared/models/product.dart';
/// Bottom sheet that shows catalog product details and lets the user
/// specify a portion weight before adding to diary.
class ProductPortionSheet extends ConsumerStatefulWidget {
const ProductPortionSheet({
super.key,
required this.catalogProduct,
required this.onConfirm,
});
final CatalogProduct catalogProduct;
final void Function(double portionGrams) onConfirm;
@override
ConsumerState<ProductPortionSheet> createState() =>
_ProductPortionSheetState();
}
class _ProductPortionSheetState extends ConsumerState<ProductPortionSheet> {
late final _weightController = TextEditingController(text: '100');
@override
void dispose() {
_weightController.dispose();
super.dispose();
}
void _confirm() {
final grams = double.tryParse(_weightController.text);
if (grams == null || grams <= 0) return;
widget.onConfirm(grams);
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final theme = Theme.of(context);
final insets = MediaQuery.viewInsetsOf(context);
final catalogProduct = widget.catalogProduct;
return Padding(
padding: EdgeInsets.fromLTRB(16, 16, 16, 16 + insets.bottom),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
catalogProduct.displayName,
style: theme.textTheme.titleMedium,
),
if (catalogProduct.categoryName != null)
Text(
catalogProduct.categoryName!,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 12),
if (catalogProduct.caloriesPer100g != null)
_NutritionRow(
label: l10n.perHundredG,
calories: catalogProduct.caloriesPer100g!,
protein: catalogProduct.proteinPer100g,
fat: catalogProduct.fatPer100g,
carbs: catalogProduct.carbsPer100g,
),
const SizedBox(height: 16),
TextField(
controller: _weightController,
keyboardType: const TextInputType.numberWithOptions(decimal: true),
decoration: InputDecoration(
labelText: l10n.portionWeightG,
border: const OutlineInputBorder(),
),
autofocus: true,
),
const SizedBox(height: 16),
FilledButton(
onPressed: _confirm,
child: Text(l10n.addToJournal),
),
],
),
);
}
}
class _NutritionRow extends StatelessWidget {
const _NutritionRow({
required this.label,
required this.calories,
this.protein,
this.fat,
this.carbs,
});
final String label;
final double calories;
final double? protein;
final double? fat;
final double? carbs;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(label, style: theme.textTheme.bodySmall),
Text(
'${calories.toInt()} kcal'
'${protein != null ? ' · P ${protein!.toStringAsFixed(1)}g' : ''}'
'${fat != null ? ' · F ${fat!.toStringAsFixed(1)}g' : ''}'
'${carbs != null ? ' · C ${carbs!.toStringAsFixed(1)}g' : ''}',
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
],
);
}
}