mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-24 08:46:24 +01:00
food_bank_screen.dart (712) carried a near-clone of the filter sheet batch 2
had just factored out of history: the same name-search field, the same slider
captions, and four more copies of the same min/max macro range block. Splitting
it in isolation would have baked that duplication in permanently, so the shared
widgets moved to a neutral `lib/widgets/filters/` first and both screens now
import them.
food_bank_screen.dart 712 -> 238
lib/widgets/food_bank/
fb_filter.dart 156 FbFilter, FbSortField, applyFbFilter
fb_filter_sheet.dart 244 the sheet itself
fb_add_entry_dialog.dart 103 the manual-entry dialog
lib/widgets/history/history_filter_fields.dart
-> lib/widgets/filters/filter_fields.dart
FilterRangeRow gained `showEndpointLabels`, defaulting to true. History showed
endpoint captions and a selected-range readout; the food-bank sheet never did,
and switching them on there would have been a UI change smuggled in under a
refactor. The food-bank call sites pass false, so both screens render exactly
what they rendered before.
FbSortField and HistorySortField stay separate on purpose -- they are not the
same enum (`name` vs `date`/`description`), so the sort dropdown is not shared.
The test file mirrors the source split, three files under test/screens/
food_bank/ plus fb_test_support.dart for the setUp/tearDown/settle prologue,
same shape as batch 2's history_test_support.dart.
Verified: 555 flutter tests (unchanged count -- no test lost, and food bank's
own RangeSlider/sort-toggle tests still pass against the shared widget),
flutter analyze clean over lib/ and test/, live ~/.local/share/diet_guard
byte-identical. Over-cap files: 40 -> 38.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
239 lines
7.1 KiB
Dart
239 lines
7.1 KiB
Dart
/// Food bank browser: lists every entry across the log-derived and manual
|
||
/// banks with filtering, sorting, and the ability to add new manual entries.
|
||
///
|
||
/// The filter model, the filter sheet and the add-entry dialog live under
|
||
/// `lib/widgets/food_bank/` (split for the repo's 250-line cap); this file is
|
||
/// the screen that wires them together.
|
||
library;
|
||
|
||
import 'dart:async';
|
||
|
||
import 'package:diet_guard_app/models/food_bank_record.dart';
|
||
import 'package:diet_guard_app/services/foodbank_service.dart';
|
||
import 'package:diet_guard_app/widgets/food_bank/fb_add_entry_dialog.dart';
|
||
import 'package:diet_guard_app/widgets/food_bank/fb_filter.dart';
|
||
import 'package:diet_guard_app/widgets/food_bank/fb_filter_sheet.dart';
|
||
import 'package:flutter/material.dart';
|
||
|
||
/// Browses every banked food -- log-derived and manually curated -- with
|
||
/// filtering, sorting, and an add-entry dialog.
|
||
class FoodBankScreen extends StatefulWidget {
|
||
/// Creates a [FoodBankScreen].
|
||
const FoodBankScreen({super.key});
|
||
|
||
@override
|
||
State<FoodBankScreen> createState() => _FoodBankScreenState();
|
||
}
|
||
|
||
class _FoodBankScreenState extends State<FoodBankScreen> {
|
||
List<FoodBankRecord>? _allEntries;
|
||
List<FoodBankRecord> _displayed = const [];
|
||
FbFilter _filter = FbFilter();
|
||
FbSortField _sortField = FbSortField.count;
|
||
bool _sortAscending = false;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
unawaited(_load());
|
||
}
|
||
|
||
Future<void> _load() async {
|
||
final entries = await FoodBankService.instance.mergedEntries();
|
||
if (!mounted) return;
|
||
setState(() {
|
||
_allEntries = entries;
|
||
_displayed = applyFbFilter(
|
||
entries,
|
||
_filter,
|
||
_sortField,
|
||
ascending: _sortAscending,
|
||
);
|
||
});
|
||
}
|
||
|
||
void _applyFilterSort() {
|
||
setState(() {
|
||
_displayed = applyFbFilter(
|
||
_allEntries!,
|
||
_filter,
|
||
_sortField,
|
||
ascending: _sortAscending,
|
||
);
|
||
});
|
||
}
|
||
|
||
Future<void> _openFilterSheet() async {
|
||
final all = _allEntries!;
|
||
|
||
double maxVal(double Function(FoodBankRecord) f, double fallback) =>
|
||
all.isEmpty ? fallback : all.map(f).reduce((a, b) => a > b ? a : b);
|
||
|
||
final maxKcal = maxVal((e) => e.kcal, 2000);
|
||
final maxProtein = maxVal((e) => e.proteinG, 200);
|
||
final maxCarbs = maxVal((e) => e.carbsG, 200);
|
||
final maxFat = maxVal((e) => e.fatG, 100);
|
||
|
||
var draft = FbFilter(
|
||
nameQuery: _filter.nameQuery,
|
||
minKcal: _filter.minKcal,
|
||
maxKcal: _filter.maxKcal,
|
||
minProtein: _filter.minProtein,
|
||
maxProtein: _filter.maxProtein,
|
||
minCarbs: _filter.minCarbs,
|
||
maxCarbs: _filter.maxCarbs,
|
||
minFat: _filter.minFat,
|
||
maxFat: _filter.maxFat,
|
||
);
|
||
var draftSort = _sortField;
|
||
var draftAsc = _sortAscending;
|
||
|
||
await showModalBottomSheet<void>(
|
||
context: context,
|
||
isScrollControlled: true,
|
||
useSafeArea: true,
|
||
builder: (ctx) => StatefulBuilder(
|
||
builder: (ctx, setSheet) => FbFilterSheet(
|
||
filter: draft,
|
||
sortField: draftSort,
|
||
ascending: draftAsc,
|
||
maxKcal: maxKcal,
|
||
maxProtein: maxProtein,
|
||
maxCarbs: maxCarbs,
|
||
maxFat: maxFat,
|
||
onFilterChanged: (f) => setSheet(() => draft = f),
|
||
onSortChanged: ({required field, required asc}) {
|
||
setSheet(() {
|
||
draftSort = field;
|
||
draftAsc = asc;
|
||
});
|
||
},
|
||
onApply: () {
|
||
setState(() {
|
||
_filter = draft;
|
||
_sortField = draftSort;
|
||
_sortAscending = draftAsc;
|
||
});
|
||
_applyFilterSort();
|
||
Navigator.of(ctx).pop();
|
||
},
|
||
onClear: () {
|
||
setSheet(() {
|
||
draft = FbFilter();
|
||
draftSort = FbSortField.count;
|
||
draftAsc = false;
|
||
});
|
||
},
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
Future<void> _openAddDialog() async {
|
||
final result = await showDialog<FoodBankRecord>(
|
||
context: context,
|
||
builder: (_) => const AddEntryDialog(),
|
||
);
|
||
if (result == null) return;
|
||
await FoodBankService.instance.addManualEntry(result);
|
||
await _load();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final all = _allEntries;
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
title: const Text('Food Bank'),
|
||
actions: [
|
||
if (all != null)
|
||
Stack(
|
||
alignment: Alignment.center,
|
||
children: [
|
||
IconButton(
|
||
icon: const Icon(Icons.filter_list),
|
||
tooltip: 'Filter & sort',
|
||
onPressed: _openFilterSheet,
|
||
),
|
||
if (_filter.isActive)
|
||
Positioned(
|
||
top: 8,
|
||
right: 8,
|
||
child: Container(
|
||
width: 8,
|
||
height: 8,
|
||
decoration: BoxDecoration(
|
||
color: Theme.of(context).colorScheme.error,
|
||
shape: BoxShape.circle,
|
||
),
|
||
),
|
||
),
|
||
],
|
||
),
|
||
],
|
||
),
|
||
body: all == null
|
||
? const Center(child: CircularProgressIndicator())
|
||
: _displayed.isEmpty
|
||
? Center(
|
||
child: Text(
|
||
all.isEmpty
|
||
? 'Food bank is empty.\n'
|
||
'Log meals to populate it, or add entries manually.'
|
||
: 'No entries match the current filter.',
|
||
textAlign: TextAlign.center,
|
||
),
|
||
)
|
||
: ListView.builder(
|
||
itemCount: _displayed.length,
|
||
itemBuilder: (context, i) => RecordTile(_displayed[i]),
|
||
),
|
||
floatingActionButton: FloatingActionButton(
|
||
onPressed: _openAddDialog,
|
||
tooltip: 'Add manual entry',
|
||
child: const Icon(Icons.add),
|
||
),
|
||
);
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Record tile
|
||
// ---------------------------------------------------------------------------
|
||
|
||
/// One banked food: its name, macros, and how often it has been logged.
|
||
class RecordTile extends StatelessWidget {
|
||
/// Creates a [RecordTile] for [record].
|
||
const RecordTile(this.record, {super.key});
|
||
|
||
/// The banked record this tile renders.
|
||
final FoodBankRecord record;
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
final macros =
|
||
'P ${record.proteinG.toStringAsFixed(0)} g '
|
||
'C ${record.carbsG.toStringAsFixed(0)} g '
|
||
'F ${record.fatG.toStringAsFixed(0)} g';
|
||
final per = record.grams > 0
|
||
? ' per ${record.grams.toStringAsFixed(0)} g'
|
||
: '';
|
||
return ListTile(
|
||
title: Text(record.desc),
|
||
subtitle: Text(
|
||
'${record.kcal.toStringAsFixed(0)} kcal$per · $macros',
|
||
),
|
||
trailing: record.count > 0
|
||
? Text(
|
||
'×${record.count.toStringAsFixed(0)}',
|
||
style: Theme.of(context).textTheme.bodySmall,
|
||
)
|
||
: null,
|
||
);
|
||
}
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Filter sheet
|
||
// ---------------------------------------------------------------------------
|