cebeaf889a
Porta la funzionalità QR code dalla versione Vue a Flutter.
Condividi: pulsante in fondo alla tab Pasti (attivo solo con piano
non vuoto) → bottom sheet con QR code generato da qr_flutter.
Payload: { "v": 1, "meals": { ... } }, limite 2953 byte.
Ricevi: pulsante sempre visibile → QrScanPage full-screen con
fotocamera via mobile_scanner, cornice di mira, validazione
struttura JSON e importazione via MealPlannerProvider.importPlan().
Nuovi package: qr_flutter ^4.1.0, mobile_scanner ^5.0.0
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
69 lines
1.8 KiB
Dart
69 lines
1.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../models/meal_plan.dart';
|
|
import '../../../shared/services/storage_service.dart';
|
|
import '../../../core/constants/app_constants.dart';
|
|
|
|
class MealPlannerProvider extends ChangeNotifier {
|
|
MealPlan _plan = MealPlan.empty(kDayIds);
|
|
|
|
MealPlan get plan => _plan;
|
|
|
|
Future<void> load() async {
|
|
final json = await StorageService.load(kStorageKeyMeals);
|
|
if (json != null) {
|
|
try {
|
|
_plan = MealPlan.fromJsonString(json);
|
|
} catch (_) {
|
|
_plan = MealPlan.empty(kDayIds);
|
|
}
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> _save() =>
|
|
StorageService.save(kStorageKeyMeals, _plan.toJsonString());
|
|
|
|
void addItem(String dayId, String slot, String text) {
|
|
final t = text.trim();
|
|
if (t.isEmpty) return;
|
|
final day = _plan.days[dayId]!;
|
|
final updated = switch (slot) {
|
|
'colazione' => day.copyWith(colazione: [...day.colazione, t]),
|
|
'pranzo' => day.copyWith(pranzo: [...day.pranzo, t]),
|
|
'cena' => day.copyWith(cena: [...day.cena, t]),
|
|
_ => day,
|
|
};
|
|
_plan = _plan.withUpdatedDay(dayId, updated);
|
|
notifyListeners();
|
|
_save();
|
|
}
|
|
|
|
void removeItem(String dayId, String slot, int index) {
|
|
final day = _plan.days[dayId]!;
|
|
final updated = switch (slot) {
|
|
'colazione' =>
|
|
day.copyWith(colazione: List.of(day.colazione)..removeAt(index)),
|
|
'pranzo' =>
|
|
day.copyWith(pranzo: List.of(day.pranzo)..removeAt(index)),
|
|
'cena' =>
|
|
day.copyWith(cena: List.of(day.cena)..removeAt(index)),
|
|
_ => day,
|
|
};
|
|
_plan = _plan.withUpdatedDay(dayId, updated);
|
|
notifyListeners();
|
|
_save();
|
|
}
|
|
|
|
void clearAll() {
|
|
_plan = MealPlan.empty(kDayIds);
|
|
notifyListeners();
|
|
_save();
|
|
}
|
|
|
|
void importPlan(MealPlan plan) {
|
|
_plan = plan;
|
|
notifyListeners();
|
|
_save();
|
|
}
|
|
}
|