new plus button
This commit is contained in:
+243
-78
@@ -1,19 +1,22 @@
|
||||
/// Log of saved shopping trips.
|
||||
/// Log of saved shopping trips, with a monthly heatmap and product search.
|
||||
library;
|
||||
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'models/shop.dart';
|
||||
import 'models/supermarket.dart';
|
||||
import 'services/app_settings.dart';
|
||||
import 'services/product_repository.dart';
|
||||
import 'shop_detail_screen.dart';
|
||||
import 'utils/dates.dart';
|
||||
import 'utils/money.dart';
|
||||
import 'utils/shop_calendar.dart';
|
||||
import 'utils/shop_log_filter.dart';
|
||||
import 'widgets/confirm_dialog.dart';
|
||||
import 'widgets/settings_button.dart';
|
||||
import 'widgets/shop_heatmap.dart';
|
||||
import 'widgets/store_tag.dart';
|
||||
import 'widgets/tag_editor.dart';
|
||||
|
||||
class ShopLogScreen extends StatefulWidget {
|
||||
const ShopLogScreen({super.key, required this.repository});
|
||||
@@ -25,17 +28,37 @@ class ShopLogScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ShopLogScreenState extends State<ShopLogScreen> {
|
||||
late Future<List<Shop>> _future;
|
||||
late Future<({List<Shop> shops, Map<int, List<String>> productNames})>
|
||||
_future;
|
||||
final _search = TextEditingController();
|
||||
bool _editing = false;
|
||||
bool _searching = false;
|
||||
DateTime _month = monthStart(DateTime.now());
|
||||
DateTime? _selectedDay;
|
||||
String? _tagFilter;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_future = widget.repository.getShops();
|
||||
_future = _load();
|
||||
_search.addListener(() => setState(() {}));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_search.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<({List<Shop> shops, Map<int, List<String>> productNames})>
|
||||
_load() async {
|
||||
final shops = await widget.repository.getShops();
|
||||
final productNames = await widget.repository.getShopItemNamesByShop();
|
||||
return (shops: shops, productNames: productNames);
|
||||
}
|
||||
|
||||
void _reload() {
|
||||
setState(() => _future = widget.repository.getShops());
|
||||
setState(() => _future = _load());
|
||||
}
|
||||
|
||||
Future<void> _openShop(Shop shop) async {
|
||||
@@ -59,15 +82,52 @@ class _ShopLogScreenState extends State<ShopLogScreen> {
|
||||
if (mounted) _reload();
|
||||
}
|
||||
|
||||
void _selectDay(DateTime? day) {
|
||||
setState(() => _selectedDay = day);
|
||||
}
|
||||
|
||||
void _goMonth(int delta) {
|
||||
final next = addMonths(_month, delta);
|
||||
final current = monthStart(DateTime.now());
|
||||
if (delta > 0 && !next.isBefore(current) && next != current) return;
|
||||
setState(() {
|
||||
_month = next.isAfter(current) ? current : next;
|
||||
_selectedDay = null;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final stores = SettingsScope.maybeOf(context)?.supermarkets ?? const [];
|
||||
final nowMonth = monthStart(DateTime.now());
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Shop log'),
|
||||
title: _searching
|
||||
? TextField(
|
||||
controller: _search,
|
||||
autofocus: true,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'Search products in trips',
|
||||
border: InputBorder.none,
|
||||
enabledBorder: InputBorder.none,
|
||||
focusedBorder: InputBorder.none,
|
||||
filled: false,
|
||||
),
|
||||
)
|
||||
: const Text('Shop log'),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: _searching ? 'Close search' : 'Search products',
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_searching = !_searching;
|
||||
if (!_searching) _search.clear();
|
||||
});
|
||||
},
|
||||
icon: Icon(_searching ? Icons.close : Icons.search),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: _editing ? 'Done' : 'Edit',
|
||||
onPressed: () => setState(() => _editing = !_editing),
|
||||
@@ -76,7 +136,7 @@ class _ShopLogScreenState extends State<ShopLogScreen> {
|
||||
const SettingsButton(),
|
||||
],
|
||||
),
|
||||
body: FutureBuilder<List<Shop>>(
|
||||
body: FutureBuilder(
|
||||
future: _future,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
@@ -91,49 +151,104 @@ class _ShopLogScreenState extends State<ShopLogScreen> {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
final shops = snapshot.data!;
|
||||
if (shops.isEmpty) {
|
||||
return Center(
|
||||
child: Text(
|
||||
'Scan a receipt or enter a trip to log it.',
|
||||
textAlign: TextAlign.center,
|
||||
style: theme.textTheme.bodyLarge,
|
||||
),
|
||||
);
|
||||
}
|
||||
final shops = snapshot.data!.shops;
|
||||
final productNames = snapshot.data!.productNames;
|
||||
final counts = tripCountsByDay([
|
||||
for (final shop in shops)
|
||||
if (isSameMonth(shop.shoppedAt, _month)) shop.shoppedAt,
|
||||
]);
|
||||
final monthShops = [
|
||||
for (final shop in shops)
|
||||
if (isSameMonth(shop.shoppedAt, _month)) shop,
|
||||
];
|
||||
final monthTotal = monthShops.fold<double>(
|
||||
0,
|
||||
(sum, shop) => sum + shop.total,
|
||||
);
|
||||
final visible = filterShops(
|
||||
shops: shops,
|
||||
productNames: productNames,
|
||||
selectedDay: _selectedDay,
|
||||
tag: _tagFilter,
|
||||
query: _search.text,
|
||||
);
|
||||
|
||||
return ListView.separated(
|
||||
padding: const EdgeInsets.fromLTRB(8, 8, 8, 16),
|
||||
itemCount: shops.length,
|
||||
separatorBuilder: (_, _) => const SizedBox(height: 4),
|
||||
itemBuilder: (context, index) {
|
||||
final shop = shops[index];
|
||||
final count = shop.itemCount;
|
||||
return ListTile(
|
||||
leading: _ReceiptThumb(path: shop.receiptImagePath),
|
||||
title: Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: StoreTag(name: shop.supermarket, stores: stores),
|
||||
return ListView(
|
||||
padding: const EdgeInsets.only(bottom: 16),
|
||||
children: [
|
||||
ShopHeatmap(
|
||||
month: _month,
|
||||
counts: counts,
|
||||
monthTotal: monthTotal,
|
||||
monthTripCount: monthShops.length,
|
||||
canGoForward: _month.isBefore(nowMonth),
|
||||
selectedDay: _selectedDay,
|
||||
onPreviousMonth: () => _goMonth(-1),
|
||||
onNextMonth: () => _goMonth(1),
|
||||
onSelectDay: _selectDay,
|
||||
),
|
||||
if (_selectedDay != null || _tagFilter != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
if (_selectedDay != null)
|
||||
InputChip(
|
||||
label: Text(formatDate(_selectedDay!)),
|
||||
onDeleted: () => _selectDay(null),
|
||||
),
|
||||
if (_tagFilter != null)
|
||||
InputChip(
|
||||
label: Text(_tagFilter!),
|
||||
onDeleted: () => setState(() => _tagFilter = null),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
subtitle: Text(
|
||||
[
|
||||
formatDate(shop.shoppedAt),
|
||||
count == 1 ? '1 product' : '$count products',
|
||||
].join(' · '),
|
||||
),
|
||||
trailing: _editing
|
||||
? IconButton(
|
||||
tooltip: 'Remove',
|
||||
onPressed: () => _removeShop(shop),
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
)
|
||||
: Text(
|
||||
formatEuro(shop.total),
|
||||
style: theme.textTheme.titleMedium,
|
||||
),
|
||||
onTap: _editing ? null : () => _openShop(shop),
|
||||
);
|
||||
},
|
||||
if (shops.isEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(24, 24, 24, 8),
|
||||
child: Text(
|
||||
'Scan a receipt or enter a trip to log it.',
|
||||
textAlign: TextAlign.center,
|
||||
style: theme.textTheme.bodyLarge,
|
||||
),
|
||||
)
|
||||
else if (visible.isEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(24, 16, 24, 8),
|
||||
child: Text(
|
||||
_search.text.trim().isNotEmpty
|
||||
? 'No trips contain that product.'
|
||||
: 'No trips match this filter.',
|
||||
textAlign: TextAlign.center,
|
||||
style: theme.textTheme.bodyLarge,
|
||||
),
|
||||
)
|
||||
else
|
||||
for (final shop in visible)
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(8, 0, 8, 4),
|
||||
child: _ShopLogTile(
|
||||
shop: shop,
|
||||
stores: stores,
|
||||
editing: _editing,
|
||||
selectedTag: _tagFilter,
|
||||
onOpen: () => _openShop(shop),
|
||||
onRemove: () => _removeShop(shop),
|
||||
onTagTap: (tag) {
|
||||
setState(() {
|
||||
_tagFilter =
|
||||
_tagFilter?.toLowerCase() == tag.toLowerCase()
|
||||
? null
|
||||
: tag;
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
@@ -141,42 +256,92 @@ class _ShopLogScreenState extends State<ShopLogScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
class _ReceiptThumb extends StatelessWidget {
|
||||
const _ReceiptThumb({this.path});
|
||||
class _ShopLogTile extends StatelessWidget {
|
||||
const _ShopLogTile({
|
||||
required this.shop,
|
||||
required this.stores,
|
||||
required this.editing,
|
||||
required this.onOpen,
|
||||
required this.onRemove,
|
||||
required this.onTagTap,
|
||||
this.selectedTag,
|
||||
});
|
||||
|
||||
final String? path;
|
||||
final Shop shop;
|
||||
final List<Supermarket> stores;
|
||||
final bool editing;
|
||||
final VoidCallback onOpen;
|
||||
final VoidCallback onRemove;
|
||||
final ValueChanged<String> onTagTap;
|
||||
final String? selectedTag;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final file = path == null ? null : File(path!);
|
||||
final hasFile = file != null && file.existsSync();
|
||||
final scheme = Theme.of(context).colorScheme;
|
||||
final theme = Theme.of(context);
|
||||
final count = shop.itemCount;
|
||||
final hasName = shop.name != null && shop.name!.isNotEmpty;
|
||||
final meta = [
|
||||
formatDate(shop.shoppedAt),
|
||||
count == 1 ? '1 product' : '$count products',
|
||||
].join(' · ');
|
||||
|
||||
return ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: SizedBox(
|
||||
width: 48,
|
||||
height: 48,
|
||||
child: hasFile
|
||||
? Image.file(
|
||||
file,
|
||||
fit: BoxFit.cover,
|
||||
cacheWidth: 144,
|
||||
errorBuilder: (_, _, _) => ColoredBox(
|
||||
color: scheme.secondaryContainer,
|
||||
child: Icon(
|
||||
Icons.receipt_long_outlined,
|
||||
color: scheme.onSecondaryContainer,
|
||||
),
|
||||
),
|
||||
)
|
||||
: ColoredBox(
|
||||
color: scheme.secondaryContainer,
|
||||
child: Icon(
|
||||
Icons.receipt_long_outlined,
|
||||
color: scheme.onSecondaryContainer,
|
||||
return Material(
|
||||
color: Colors.transparent,
|
||||
child: InkWell(
|
||||
onTap: editing ? null : onOpen,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 10, 8, 10),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (hasName)
|
||||
Text(shop.name!, style: theme.textTheme.titleMedium)
|
||||
else
|
||||
StoreTag(name: shop.supermarket, stores: stores),
|
||||
const SizedBox(height: 4),
|
||||
if (hasName) ...[
|
||||
StoreTag(
|
||||
name: shop.supermarket,
|
||||
stores: stores,
|
||||
compact: true,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
],
|
||||
Text(meta, style: theme.textTheme.bodyMedium),
|
||||
if (shop.tags.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
TagChips(
|
||||
tags: shop.tags,
|
||||
compact: true,
|
||||
selected: selectedTag,
|
||||
onTap: onTagTap,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
editing
|
||||
? IconButton(
|
||||
tooltip: 'Remove',
|
||||
onPressed: onRemove,
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
)
|
||||
: Padding(
|
||||
padding: const EdgeInsets.only(top: 8, right: 8),
|
||||
child: Text(
|
||||
formatEuro(shop.total),
|
||||
style: theme.textTheme.titleMedium,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user