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:
107
client/lib/features/diary/barcode_scan_screen.dart
Normal file
107
client/lib/features/diary/barcode_scan_screen.dart
Normal 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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user