Refactor code structure for improved readability and maintainability
Test Suite / test (24.15.0) (push) Has been cancelled
Test Suite / test (24.15.0) (push) Has been cancelled
This commit is contained in:
@@ -646,6 +646,9 @@ class _IngredientPreviewRow extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final cs = theme.colorScheme;
|
||||
final label = ingredient.productName.trim().isEmpty
|
||||
? 'Okänd ingrediens'
|
||||
: ingredient.productName;
|
||||
|
||||
final (icon, color) = ingredient.fromPantry
|
||||
? (Icons.kitchen_outlined, cs.secondary)
|
||||
@@ -687,7 +690,7 @@ class _IngredientPreviewRow extends StatelessWidget {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
'${ingredient.productName}'
|
||||
'$label'
|
||||
'${ingredient.note != null ? ' (${ingredient.note})' : ''}'
|
||||
' – $requiredStr',
|
||||
style: theme.textTheme.bodyMedium,
|
||||
|
||||
@@ -17,14 +17,18 @@ import '../domain/recipe_ingredient.dart';
|
||||
|
||||
class _EditableIngredient {
|
||||
int? productId;
|
||||
String productName;
|
||||
String? productName;
|
||||
String rawName;
|
||||
String? rawLine;
|
||||
final TextEditingController quantityCtrl;
|
||||
String unit;
|
||||
final TextEditingController noteCtrl;
|
||||
|
||||
_EditableIngredient({
|
||||
required this.productId,
|
||||
required this.productName,
|
||||
this.productName,
|
||||
required this.rawName,
|
||||
this.rawLine,
|
||||
required String quantity,
|
||||
required this.unit,
|
||||
String note = '',
|
||||
@@ -35,6 +39,10 @@ class _EditableIngredient {
|
||||
return _EditableIngredient(
|
||||
productId: ingredient.productId,
|
||||
productName: ingredient.productName,
|
||||
rawName: ingredient.rawName.trim().isNotEmpty
|
||||
? ingredient.rawName
|
||||
: (ingredient.productName ?? ''),
|
||||
rawLine: ingredient.rawLine,
|
||||
quantity: formatQuantity(ingredient.quantity),
|
||||
unit: ingredient.unit,
|
||||
note: ingredient.note ?? '',
|
||||
@@ -135,6 +143,7 @@ class _RecipeEditScreenState extends ConsumerState<RecipeEditScreen> {
|
||||
_EditableIngredient(
|
||||
productId: null,
|
||||
productName: '',
|
||||
rawName: '',
|
||||
quantity: '',
|
||||
unit: 'st',
|
||||
),
|
||||
@@ -154,16 +163,17 @@ class _RecipeEditScreenState extends ConsumerState<RecipeEditScreen> {
|
||||
return context.l10n.recipeEditMinIngredients;
|
||||
}
|
||||
for (final ingredient in _ingredients) {
|
||||
if (ingredient.productId == null) {
|
||||
return context.l10n.recipeEditSelectProduct;
|
||||
if (ingredient.productId == null && ingredient.rawName.trim().isEmpty) {
|
||||
return 'Ange ingrediensnamn eller välj produkt';
|
||||
}
|
||||
final quantity = double.tryParse(
|
||||
ingredient.quantityCtrl.text.trim().replaceAll(',', '.'),
|
||||
);
|
||||
if (quantity == null || quantity < 0) {
|
||||
final qtyText = ingredient.quantityCtrl.text.trim();
|
||||
final quantity = qtyText.isEmpty
|
||||
? null
|
||||
: double.tryParse(qtyText.replaceAll(',', '.'));
|
||||
if (qtyText.isNotEmpty && (quantity == null || quantity < 0)) {
|
||||
return context.l10n.recipeEditValidQuantity;
|
||||
}
|
||||
if (ingredient.unit.trim().isEmpty) {
|
||||
if (qtyText.isNotEmpty && ingredient.unit.trim().isEmpty) {
|
||||
return context.l10n.recipeEditSelectUnit;
|
||||
}
|
||||
}
|
||||
@@ -189,14 +199,20 @@ class _RecipeEditScreenState extends ConsumerState<RecipeEditScreen> {
|
||||
final servings = int.tryParse(_servingsCtrl.text.trim());
|
||||
final ingredients = _ingredients
|
||||
.map(
|
||||
(ingredient) => {
|
||||
'productId': ingredient.productId,
|
||||
'quantity': double.parse(
|
||||
(ingredient) {
|
||||
final parsedQty = double.tryParse(
|
||||
ingredient.quantityCtrl.text.trim().replaceAll(',', '.'),
|
||||
),
|
||||
'unit': ingredient.unit,
|
||||
if (ingredient.noteCtrl.text.trim().isNotEmpty)
|
||||
'note': ingredient.noteCtrl.text.trim(),
|
||||
);
|
||||
return {
|
||||
'rawName': ingredient.rawName.trim(),
|
||||
if ((ingredient.rawLine ?? '').trim().isNotEmpty)
|
||||
'rawLine': ingredient.rawLine,
|
||||
if (ingredient.productId != null) 'productId': ingredient.productId,
|
||||
if (parsedQty != null) 'quantity': parsedQty,
|
||||
if (ingredient.unit.trim().isNotEmpty) 'unit': ingredient.unit,
|
||||
if (ingredient.noteCtrl.text.trim().isNotEmpty)
|
||||
'note': ingredient.noteCtrl.text.trim(),
|
||||
};
|
||||
},
|
||||
)
|
||||
.toList();
|
||||
@@ -401,7 +417,7 @@ class _RecipeEditScreenState extends ConsumerState<RecipeEditScreen> {
|
||||
initialValue: ingredient.productId,
|
||||
isExpanded: true,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Produkt *',
|
||||
labelText: 'Produkt (valfritt)',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
items: _allProducts
|
||||
@@ -433,6 +449,21 @@ class _RecipeEditScreenState extends ConsumerState<RecipeEditScreen> {
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextFormField(
|
||||
initialValue: ingredient.rawName,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Ingrediensnamn',
|
||||
border: OutlineInputBorder(),
|
||||
),
|
||||
onChanged: (value) => ingredient.rawName = value,
|
||||
validator: (value) {
|
||||
if (ingredient.productId == null && (value == null || value.trim().isEmpty)) {
|
||||
return 'Ange namn eller välj produkt';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
@@ -446,11 +477,8 @@ class _RecipeEditScreenState extends ConsumerState<RecipeEditScreen> {
|
||||
keyboardType:
|
||||
const TextInputType.numberWithOptions(decimal: true),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return context.l10n.quantityHint;
|
||||
}
|
||||
if (double.tryParse(value.trim().replaceAll(',', '.')) ==
|
||||
null) {
|
||||
if (value == null || value.trim().isEmpty) return null;
|
||||
if (double.tryParse(value.trim().replaceAll(',', '.')) == null) {
|
||||
return context.l10n.invalidNumber;
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user