Compare commits
2
Commits
98574af903
..
v1.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8b040e83c0 | ||
|
|
ae42ba8193 |
@@ -22,16 +22,39 @@ class OcrElement {
|
|||||||
double get width => (right - left).abs();
|
double get width => (right - left).abs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// One ML Kit text line with its word-level elements.
|
||||||
|
class OcrLine {
|
||||||
|
const OcrLine({
|
||||||
|
required this.text,
|
||||||
|
required this.elements,
|
||||||
|
required this.left,
|
||||||
|
required this.top,
|
||||||
|
required this.right,
|
||||||
|
required this.bottom,
|
||||||
|
});
|
||||||
|
|
||||||
|
final String text;
|
||||||
|
final List<OcrElement> elements;
|
||||||
|
final double left;
|
||||||
|
final double top;
|
||||||
|
final double right;
|
||||||
|
final double bottom;
|
||||||
|
|
||||||
|
double get centerY => (top + bottom) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
/// All positioned elements from one ML Kit recognition pass.
|
/// All positioned elements from one ML Kit recognition pass.
|
||||||
class OcrDocument {
|
class OcrDocument {
|
||||||
const OcrDocument({
|
const OcrDocument({
|
||||||
required this.elements,
|
required this.elements,
|
||||||
|
required this.lines,
|
||||||
required this.imageWidth,
|
required this.imageWidth,
|
||||||
required this.imageHeight,
|
required this.imageHeight,
|
||||||
required this.flattenedText,
|
required this.flattenedText,
|
||||||
});
|
});
|
||||||
|
|
||||||
final List<OcrElement> elements;
|
final List<OcrElement> elements;
|
||||||
|
final List<OcrLine> lines;
|
||||||
final double imageWidth;
|
final double imageWidth;
|
||||||
final double imageHeight;
|
final double imageHeight;
|
||||||
final String flattenedText;
|
final String flattenedText;
|
||||||
|
|||||||
+28
-23
@@ -263,29 +263,34 @@ class _ProductsScreenState extends State<ProductsScreen> {
|
|||||||
children: [
|
children: [
|
||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.fromLTRB(16, 4, 16, 4),
|
padding: const EdgeInsets.fromLTRB(16, 4, 16, 4),
|
||||||
child: SegmentedButton<ProductSort>(
|
child: SizedBox(
|
||||||
segments: const [
|
width: double.infinity,
|
||||||
ButtonSegment(
|
child: SegmentedButton<ProductSort>(
|
||||||
value: ProductSort.name,
|
expandedInsets: EdgeInsets.zero,
|
||||||
label: Text('A–Z'),
|
showSelectedIcon: false,
|
||||||
),
|
segments: const [
|
||||||
ButtonSegment(
|
ButtonSegment(
|
||||||
value: ProductSort.preferred,
|
value: ProductSort.name,
|
||||||
label: Text('Frequent'),
|
label: Text('A–Z'),
|
||||||
),
|
),
|
||||||
ButtonSegment(
|
ButtonSegment(
|
||||||
value: ProductSort.favourite,
|
value: ProductSort.preferred,
|
||||||
icon: Icon(Icons.star),
|
label: Text('Frequent'),
|
||||||
label: Text('Star'),
|
),
|
||||||
),
|
ButtonSegment(
|
||||||
],
|
value: ProductSort.favourite,
|
||||||
selected: {_sort},
|
icon: Icon(Icons.star),
|
||||||
onSelectionChanged: (selected) {
|
label: Text('Star'),
|
||||||
setState(() {
|
),
|
||||||
_sort = selected.first;
|
],
|
||||||
});
|
selected: {_sort},
|
||||||
_load();
|
onSelectionChanged: (selected) {
|
||||||
},
|
setState(() {
|
||||||
|
_sort = selected.first;
|
||||||
|
});
|
||||||
|
_load();
|
||||||
|
},
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (_nameIssues.isNotEmpty && !_editing)
|
if (_nameIssues.isNotEmpty && !_editing)
|
||||||
|
|||||||
+278
-99
@@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'models/line_item.dart';
|
import 'models/line_item.dart';
|
||||||
import 'services/app_settings.dart';
|
import 'services/app_settings.dart';
|
||||||
import 'services/product_repository.dart';
|
import 'services/product_repository.dart';
|
||||||
|
import 'services/receipt_capture.dart';
|
||||||
import 'utils/dates.dart';
|
import 'utils/dates.dart';
|
||||||
import 'utils/money.dart';
|
import 'utils/money.dart';
|
||||||
import 'utils/supermarkets.dart';
|
import 'utils/supermarkets.dart';
|
||||||
@@ -55,12 +56,20 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
List<String> _tags = [];
|
List<String> _tags = [];
|
||||||
List<String> _tagSuggestions = [];
|
List<String> _tagSuggestions = [];
|
||||||
String? _highlightedItemId;
|
String? _highlightedItemId;
|
||||||
|
bool _showTripExtras = false;
|
||||||
|
late String _rawText;
|
||||||
|
String? _imagePath;
|
||||||
|
late bool _needsReview;
|
||||||
|
bool _addingReceipt = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_items = List<LineItem>.from(widget.items);
|
_items = List<LineItem>.from(widget.items);
|
||||||
_shoppedAt = DateTime.now();
|
_shoppedAt = DateTime.now();
|
||||||
|
_rawText = widget.rawText;
|
||||||
|
_imagePath = widget.imagePath;
|
||||||
|
_needsReview = widget.needsReview;
|
||||||
final detected = detectSupermarket(
|
final detected = detectSupermarket(
|
||||||
widget.rawText,
|
widget.rawText,
|
||||||
names: widget.supermarketNames,
|
names: widget.supermarketNames,
|
||||||
@@ -95,10 +104,56 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool get _hasReceipt {
|
bool get _hasReceipt {
|
||||||
final path = widget.imagePath;
|
final path = _imagePath;
|
||||||
return path != null && path.isNotEmpty && File(path).existsSync();
|
return path != null && path.isNotEmpty && File(path).existsSync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _addReceipt() async {
|
||||||
|
if (_saving || _addingReceipt) return;
|
||||||
|
setState(() => _addingReceipt = true);
|
||||||
|
try {
|
||||||
|
final captured = await captureAndParseReceipt(context);
|
||||||
|
if (captured == null || !mounted) return;
|
||||||
|
|
||||||
|
final keepExistingPhoto = _hasReceipt;
|
||||||
|
final merged = [
|
||||||
|
for (final item in captured.items)
|
||||||
|
keepExistingPhoto
|
||||||
|
? item.copyWith(clearSourceBounds: true)
|
||||||
|
: item,
|
||||||
|
];
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_items.addAll(merged);
|
||||||
|
_needsReview = _needsReview || captured.needsReview;
|
||||||
|
if (_rawText.isEmpty) {
|
||||||
|
_rawText = captured.rawText;
|
||||||
|
} else if (captured.rawText.isNotEmpty) {
|
||||||
|
_rawText = '$_rawText\n\n---\n\n${captured.rawText}';
|
||||||
|
}
|
||||||
|
if (!keepExistingPhoto && captured.imagePath != null) {
|
||||||
|
_imagePath = captured.imagePath;
|
||||||
|
}
|
||||||
|
if (_supermarket == null && captured.detectedStore != null) {
|
||||||
|
_supermarket = captured.detectedStore;
|
||||||
|
}
|
||||||
|
_highlightedItemId = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
merged.length == 1
|
||||||
|
? 'Added 1 item from receipt.'
|
||||||
|
: 'Added ${merged.length} items from receipt.',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
if (mounted) setState(() => _addingReceipt = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _edit({LineItem? existing}) async {
|
Future<void> _edit({LineItem? existing}) async {
|
||||||
final result = await showItemEditSheet(context, item: existing);
|
final result = await showItemEditSheet(context, item: existing);
|
||||||
if (result == null || !mounted) return;
|
if (result == null || !mounted) return;
|
||||||
@@ -137,18 +192,91 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
setState(() => _shoppedAt = picked);
|
setState(() => _shoppedAt = picked);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _openReceipt() async {
|
Future<void> _openReceiptPanel() async {
|
||||||
final path = widget.imagePath;
|
final path = _imagePath;
|
||||||
if (path == null || path.isEmpty) return;
|
if (path == null || path.isEmpty) return;
|
||||||
final file = File(path);
|
final file = File(path);
|
||||||
if (!file.existsSync()) return;
|
if (!file.existsSync()) return;
|
||||||
await showDialog<void>(
|
|
||||||
|
final highlighted = _highlightedItemId == null
|
||||||
|
? null
|
||||||
|
: _items.cast<LineItem?>().firstWhere(
|
||||||
|
(item) => item?.id == _highlightedItemId,
|
||||||
|
orElse: () => null,
|
||||||
|
);
|
||||||
|
|
||||||
|
await showModalBottomSheet<void>(
|
||||||
context: context,
|
context: context,
|
||||||
|
isScrollControlled: true,
|
||||||
|
showDragHandle: true,
|
||||||
builder: (context) {
|
builder: (context) {
|
||||||
return Dialog(
|
return SafeArea(
|
||||||
insetPadding: const EdgeInsets.all(16),
|
child: Padding(
|
||||||
child: InteractiveViewer(
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
|
||||||
child: Image.file(file, fit: BoxFit.contain),
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
Text('Receipt', style: Theme.of(context).textTheme.titleMedium),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
if (highlighted?.sourceBounds != null)
|
||||||
|
ReceiptHighlightOverlay(
|
||||||
|
imagePath: path,
|
||||||
|
bounds: highlighted!.sourceBounds!,
|
||||||
|
maxHeight: 360,
|
||||||
|
)
|
||||||
|
else
|
||||||
|
GestureDetector(
|
||||||
|
onTap: () async {
|
||||||
|
await showDialog<void>(
|
||||||
|
context: context,
|
||||||
|
builder: (ctx) => Dialog(
|
||||||
|
insetPadding: const EdgeInsets.all(16),
|
||||||
|
child: InteractiveViewer(
|
||||||
|
child: Image.file(file, fit: BoxFit.contain),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: ClipRRect(
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
child: Image.file(file, fit: BoxFit.contain, height: 360),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _openOcrPanel() async {
|
||||||
|
if (_rawText.isEmpty) return;
|
||||||
|
await showModalBottomSheet<void>(
|
||||||
|
context: context,
|
||||||
|
isScrollControlled: true,
|
||||||
|
showDragHandle: true,
|
||||||
|
builder: (context) {
|
||||||
|
return SafeArea(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
Text('Raw OCR text', style: Theme.of(context).textTheme.titleMedium),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
ConstrainedBox(
|
||||||
|
constraints: BoxConstraints(
|
||||||
|
maxHeight: MediaQuery.sizeOf(context).height * 0.55,
|
||||||
|
),
|
||||||
|
child: SingleChildScrollView(
|
||||||
|
child: SelectableText(_rawText),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -175,7 +303,7 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
supermarket: supermarket,
|
supermarket: supermarket,
|
||||||
shoppedAt: _shoppedAt,
|
shoppedAt: _shoppedAt,
|
||||||
items: _items,
|
items: _items,
|
||||||
receiptImagePath: widget.imagePath,
|
receiptImagePath: _imagePath,
|
||||||
name: _tripName.text,
|
name: _tripName.text,
|
||||||
tags: _tags,
|
tags: _tags,
|
||||||
);
|
);
|
||||||
@@ -202,18 +330,19 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
if (_supermarket != null && !names.contains(_supermarket)) {
|
if (_supermarket != null && !names.contains(_supermarket)) {
|
||||||
names.add(_supermarket!);
|
names.add(_supermarket!);
|
||||||
}
|
}
|
||||||
final receiptPath = widget.imagePath;
|
final flaggedCount =
|
||||||
final highlighted = _highlightedItemId == null
|
_items.where((item) => item.needsManualReview).length;
|
||||||
? null
|
final reviewMessage = flaggedCount > 0
|
||||||
: _items.cast<LineItem?>().firstWhere(
|
? 'Some lines could not be parsed confidently — '
|
||||||
(item) => item?.id == _highlightedItemId,
|
'${flaggedCount == 1 ? '1 item is' : '$flaggedCount items are'} '
|
||||||
orElse: () => null,
|
'marked below. Check them against the receipt before saving.'
|
||||||
);
|
: 'Item count or totals do not match the receipt. '
|
||||||
|
'Check the items below before saving.';
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text(
|
title: Text(
|
||||||
widget.rawText.isEmpty && widget.imagePath == null
|
_rawText.isEmpty && _imagePath == null
|
||||||
? 'New trip'
|
? 'New trip'
|
||||||
: 'Review items',
|
: 'Review items',
|
||||||
),
|
),
|
||||||
@@ -225,14 +354,14 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
children: [
|
children: [
|
||||||
if (widget.needsReview) ...[
|
if (_needsReview) ...[
|
||||||
Material(
|
Material(
|
||||||
color: theme.colorScheme.errorContainer,
|
color: theme.colorScheme.errorContainer,
|
||||||
borderRadius: BorderRadius.circular(12),
|
borderRadius: BorderRadius.circular(12),
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(12),
|
padding: const EdgeInsets.all(12),
|
||||||
child: Text(
|
child: Text(
|
||||||
'Item count or totals do not match the receipt, or some lines need manual review. Check highlighted items before saving.',
|
reviewMessage,
|
||||||
style: theme.textTheme.bodyMedium?.copyWith(
|
style: theme.textTheme.bodyMedium?.copyWith(
|
||||||
color: theme.colorScheme.onErrorContainer,
|
color: theme.colorScheme.onErrorContainer,
|
||||||
),
|
),
|
||||||
@@ -273,43 +402,57 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
ListTile(
|
ListTile(
|
||||||
contentPadding: EdgeInsets.zero,
|
contentPadding: EdgeInsets.zero,
|
||||||
leading: const Icon(Icons.calendar_today_outlined),
|
leading: Row(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
IconButton(
|
||||||
|
icon: Icon(
|
||||||
|
_showTripExtras
|
||||||
|
? Icons.expand_less
|
||||||
|
: Icons.edit_note_outlined,
|
||||||
|
),
|
||||||
|
tooltip: _showTripExtras
|
||||||
|
? 'Hide trip name & tags'
|
||||||
|
: 'Trip name & tags',
|
||||||
|
onPressed: _saving
|
||||||
|
? null
|
||||||
|
: () => setState(
|
||||||
|
() => _showTripExtras = !_showTripExtras,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const Icon(Icons.calendar_today_outlined),
|
||||||
|
],
|
||||||
|
),
|
||||||
title: Text(formatDate(_shoppedAt)),
|
title: Text(formatDate(_shoppedAt)),
|
||||||
subtitle: const Text('Shopping date'),
|
subtitle: const Text('Shopping date'),
|
||||||
onTap: _saving ? null : _pickDate,
|
onTap: _saving ? null : _pickDate,
|
||||||
),
|
),
|
||||||
|
if (_showTripExtras) ...[
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
TextField(
|
||||||
|
controller: _tripName,
|
||||||
|
enabled: !_saving,
|
||||||
|
textCapitalization: TextCapitalization.sentences,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'Trip name',
|
||||||
|
hintText: 'Optional, e.g. Weekly shop',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
TagEditor(
|
||||||
|
tags: _tags,
|
||||||
|
suggestions: _tagSuggestions,
|
||||||
|
enabled: !_saving,
|
||||||
|
onChanged: (tags) => setState(() => _tags = tags),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 8),
|
||||||
|
],
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: CustomScrollView(
|
child: CustomScrollView(
|
||||||
slivers: [
|
slivers: [
|
||||||
SliverPadding(
|
|
||||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
|
||||||
sliver: SliverToBoxAdapter(
|
|
||||||
child: Column(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
||||||
children: [
|
|
||||||
TextField(
|
|
||||||
controller: _tripName,
|
|
||||||
enabled: !_saving,
|
|
||||||
textCapitalization: TextCapitalization.sentences,
|
|
||||||
decoration: const InputDecoration(
|
|
||||||
labelText: 'Trip name',
|
|
||||||
hintText: 'Optional, e.g. Weekly shop',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 12),
|
|
||||||
TagEditor(
|
|
||||||
tags: _tags,
|
|
||||||
suggestions: _tagSuggestions,
|
|
||||||
enabled: !_saving,
|
|
||||||
onChanged: (tags) => setState(() => _tags = tags),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
if (_items.isEmpty)
|
if (_items.isEmpty)
|
||||||
SliverFillRemaining(
|
SliverFillRemaining(
|
||||||
hasScrollBody: false,
|
hasScrollBody: false,
|
||||||
@@ -329,6 +472,8 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
separatorBuilder: (_, _) => const SizedBox(height: 4),
|
separatorBuilder: (_, _) => const SizedBox(height: 4),
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final item = _items[index];
|
final item = _items[index];
|
||||||
|
final flagged = item.needsManualReview;
|
||||||
|
final onReceipt = _highlightedItemId == item.id;
|
||||||
return Dismissible(
|
return Dismissible(
|
||||||
key: ValueKey(item.id),
|
key: ValueKey(item.id),
|
||||||
direction: DismissDirection.endToStart,
|
direction: DismissDirection.endToStart,
|
||||||
@@ -350,13 +495,26 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
child: ListTile(
|
child: ListTile(
|
||||||
selected: _highlightedItemId == item.id,
|
shape: RoundedRectangleBorder(
|
||||||
selectedTileColor: theme.colorScheme.primaryContainer
|
borderRadius: BorderRadius.circular(12),
|
||||||
.withValues(alpha: 0.35),
|
side: flagged
|
||||||
|
? BorderSide(
|
||||||
|
color: theme.colorScheme.error
|
||||||
|
.withValues(alpha: 0.55),
|
||||||
|
)
|
||||||
|
: BorderSide.none,
|
||||||
|
),
|
||||||
|
tileColor: flagged
|
||||||
|
? theme.colorScheme.errorContainer
|
||||||
|
.withValues(alpha: 0.35)
|
||||||
|
: onReceipt
|
||||||
|
? theme.colorScheme.primaryContainer
|
||||||
|
.withValues(alpha: 0.35)
|
||||||
|
: null,
|
||||||
leading: item.sourceBounds != null && _hasReceipt
|
leading: item.sourceBounds != null && _hasReceipt
|
||||||
? IconButton(
|
? IconButton(
|
||||||
icon: Icon(
|
icon: Icon(
|
||||||
_highlightedItemId == item.id
|
onReceipt
|
||||||
? Icons.visibility
|
? Icons.visibility
|
||||||
: Icons.visibility_outlined,
|
: Icons.visibility_outlined,
|
||||||
size: 20,
|
size: 20,
|
||||||
@@ -366,7 +524,7 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
? null
|
? null
|
||||||
: () => _toggleHighlight(item),
|
: () => _toggleHighlight(item),
|
||||||
)
|
)
|
||||||
: item.needsManualReview
|
: flagged
|
||||||
? Icon(
|
? Icon(
|
||||||
Icons.flag_outlined,
|
Icons.flag_outlined,
|
||||||
color: theme.colorScheme.error,
|
color: theme.colorScheme.error,
|
||||||
@@ -374,7 +532,7 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
)
|
)
|
||||||
: null,
|
: null,
|
||||||
title: Text(item.name),
|
title: Text(item.name),
|
||||||
subtitle: _itemSubtitle(item),
|
subtitle: _itemSubtitle(item, flagged: flagged),
|
||||||
trailing: DiscountedPrice(item: item),
|
trailing: DiscountedPrice(item: item),
|
||||||
onTap: _saving ? null : () => _edit(existing: item),
|
onTap: _saving ? null : () => _edit(existing: item),
|
||||||
),
|
),
|
||||||
@@ -385,45 +543,35 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (_hasReceipt)
|
if (_hasReceipt || _rawText.isNotEmpty)
|
||||||
ExpansionTile(
|
Padding(
|
||||||
initiallyExpanded: highlighted?.sourceBounds != null,
|
padding: const EdgeInsets.fromLTRB(16, 0, 16, 4),
|
||||||
title: const Text('See receipt'),
|
child: Row(
|
||||||
children: [
|
children: [
|
||||||
Padding(
|
if (_hasReceipt)
|
||||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
|
Expanded(
|
||||||
child: highlighted?.sourceBounds != null
|
child: OutlinedButton.icon(
|
||||||
? ReceiptHighlightOverlay(
|
onPressed: _saving || _addingReceipt
|
||||||
imagePath: receiptPath!,
|
? null
|
||||||
bounds: highlighted!.sourceBounds!,
|
: _openReceiptPanel,
|
||||||
)
|
icon: const Icon(Icons.receipt_long_outlined, size: 18),
|
||||||
: GestureDetector(
|
label: const Text('Receipt'),
|
||||||
onTap: _openReceipt,
|
),
|
||||||
child: ClipRRect(
|
),
|
||||||
borderRadius: BorderRadius.circular(12),
|
if (_hasReceipt && _rawText.isNotEmpty)
|
||||||
child: Image.file(
|
const SizedBox(width: 8),
|
||||||
File(receiptPath!),
|
if (_rawText.isNotEmpty)
|
||||||
fit: BoxFit.contain,
|
Expanded(
|
||||||
height: 220,
|
child: OutlinedButton.icon(
|
||||||
width: double.infinity,
|
onPressed: _saving || _addingReceipt
|
||||||
),
|
? null
|
||||||
),
|
: _openOcrPanel,
|
||||||
),
|
icon: const Icon(Icons.text_snippet_outlined, size: 18),
|
||||||
),
|
label: const Text('OCR text'),
|
||||||
],
|
),
|
||||||
),
|
),
|
||||||
if (widget.rawText.isNotEmpty)
|
],
|
||||||
ExpansionTile(
|
),
|
||||||
title: const Text('Raw OCR text'),
|
|
||||||
children: [
|
|
||||||
ConstrainedBox(
|
|
||||||
constraints: const BoxConstraints(maxHeight: 180),
|
|
||||||
child: SingleChildScrollView(
|
|
||||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 16),
|
|
||||||
child: SelectableText(widget.rawText),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
SafeArea(
|
SafeArea(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
@@ -431,14 +579,40 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
children: [
|
children: [
|
||||||
OutlinedButton.icon(
|
Row(
|
||||||
onPressed: _saving ? null : () => _edit(),
|
children: [
|
||||||
icon: const Icon(Icons.add),
|
Expanded(
|
||||||
label: const Text('Add item'),
|
child: OutlinedButton.icon(
|
||||||
|
onPressed: _saving || _addingReceipt
|
||||||
|
? null
|
||||||
|
: () => _edit(),
|
||||||
|
icon: const Icon(Icons.add),
|
||||||
|
label: const Text('Add item'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
Expanded(
|
||||||
|
child: OutlinedButton.icon(
|
||||||
|
onPressed: _saving || _addingReceipt
|
||||||
|
? null
|
||||||
|
: _addReceipt,
|
||||||
|
icon: _addingReceipt
|
||||||
|
? const SizedBox(
|
||||||
|
width: 16,
|
||||||
|
height: 16,
|
||||||
|
child: CircularProgressIndicator(
|
||||||
|
strokeWidth: 2,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: const Icon(Icons.receipt_long_outlined),
|
||||||
|
label: const Text('Add receipt'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
FilledButton(
|
FilledButton(
|
||||||
onPressed: _saving ? null : _confirm,
|
onPressed: _saving || _addingReceipt ? null : _confirm,
|
||||||
child: Text(
|
child: Text(
|
||||||
_saving ? 'Saving…' : 'Save trip · ${formatEuro(total)}',
|
_saving ? 'Saving…' : 'Save trip · ${formatEuro(total)}',
|
||||||
),
|
),
|
||||||
@@ -452,8 +626,13 @@ class _ReviewScreenState extends State<ReviewScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget? _itemSubtitle(LineItem item) {
|
Widget? _itemSubtitle(LineItem item, {bool flagged = false}) {
|
||||||
if (item.quantity == null) return null;
|
final parts = <String>[];
|
||||||
return Text('${item.quantity} × ${formatEuro(item.unitPrice ?? 0)}');
|
if (flagged) parts.add('Needs review');
|
||||||
|
if (item.quantity != null) {
|
||||||
|
parts.add('${item.quantity} × ${formatEuro(item.unitPrice ?? 0)}');
|
||||||
|
}
|
||||||
|
if (parts.isEmpty) return null;
|
||||||
|
return Text(parts.join(' · '));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,188 @@
|
|||||||
|
/// Shared pick → crop → OCR → parse flow for adding a receipt to a trip.
|
||||||
|
library;
|
||||||
|
|
||||||
|
import 'dart:io';
|
||||||
|
import 'dart:ui' as ui;
|
||||||
|
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
import 'package:google_mlkit_text_recognition/google_mlkit_text_recognition.dart';
|
||||||
|
import 'package:image_picker/image_picker.dart';
|
||||||
|
|
||||||
|
import '../crop_receipt_screen.dart';
|
||||||
|
import '../models/line_item.dart';
|
||||||
|
import 'receipt_parser.dart';
|
||||||
|
|
||||||
|
class CapturedReceiptParse {
|
||||||
|
const CapturedReceiptParse({
|
||||||
|
required this.items,
|
||||||
|
required this.rawText,
|
||||||
|
this.imagePath,
|
||||||
|
this.needsReview = false,
|
||||||
|
this.detectedStore,
|
||||||
|
});
|
||||||
|
|
||||||
|
final List<LineItem> items;
|
||||||
|
final String rawText;
|
||||||
|
final String? imagePath;
|
||||||
|
final bool needsReview;
|
||||||
|
final String? detectedStore;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Asks camera vs gallery, crops, runs OCR, and returns parsed items.
|
||||||
|
///
|
||||||
|
/// Returns null if the user cancels at any step.
|
||||||
|
Future<CapturedReceiptParse?> captureAndParseReceipt(
|
||||||
|
BuildContext context, {
|
||||||
|
ImagePicker? picker,
|
||||||
|
}) async {
|
||||||
|
final source = await showModalBottomSheet<ImageSource>(
|
||||||
|
context: context,
|
||||||
|
showDragHandle: true,
|
||||||
|
builder: (context) {
|
||||||
|
return SafeArea(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.photo_camera_outlined),
|
||||||
|
title: const Text('Take photo'),
|
||||||
|
onTap: () => Navigator.pop(context, ImageSource.camera),
|
||||||
|
),
|
||||||
|
ListTile(
|
||||||
|
leading: const Icon(Icons.photo_library_outlined),
|
||||||
|
title: const Text('Pick from gallery'),
|
||||||
|
onTap: () => Navigator.pop(context, ImageSource.gallery),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (source == null || !context.mounted) return null;
|
||||||
|
|
||||||
|
final imagePicker = picker ?? ImagePicker();
|
||||||
|
final file = await imagePicker.pickImage(source: source, imageQuality: 95);
|
||||||
|
if (file == null || !context.mounted) return null;
|
||||||
|
|
||||||
|
final croppedPath = await Navigator.of(context).push<String>(
|
||||||
|
MaterialPageRoute(
|
||||||
|
fullscreenDialog: true,
|
||||||
|
builder: (_) => CropReceiptScreen(imagePath: file.path),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (croppedPath == null || !context.mounted) return null;
|
||||||
|
|
||||||
|
return _runOcrAndParse(context, croppedPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<CapturedReceiptParse?> _runOcrAndParse(
|
||||||
|
BuildContext context,
|
||||||
|
String imagePath,
|
||||||
|
) async {
|
||||||
|
showDialog<void>(
|
||||||
|
context: context,
|
||||||
|
barrierDismissible: false,
|
||||||
|
builder: (context) {
|
||||||
|
return const PopScope(
|
||||||
|
canPop: false,
|
||||||
|
child: Center(
|
||||||
|
child: Card(
|
||||||
|
child: Padding(
|
||||||
|
padding: EdgeInsets.all(24),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
CircularProgressIndicator(),
|
||||||
|
SizedBox(height: 16),
|
||||||
|
Text('Reading receipt…'),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
final recognizer = TextRecognizer(script: TextRecognitionScript.latin);
|
||||||
|
try {
|
||||||
|
final bytes = await File(imagePath).readAsBytes();
|
||||||
|
final codec = await ui.instantiateImageCodec(bytes);
|
||||||
|
final frame = await codec.getNextFrame();
|
||||||
|
final imageWidth = frame.image.width.toDouble();
|
||||||
|
final imageHeight = frame.image.height.toDouble();
|
||||||
|
frame.image.dispose();
|
||||||
|
|
||||||
|
final recognized = await recognizer.processImage(
|
||||||
|
InputImage.fromFilePath(imagePath),
|
||||||
|
);
|
||||||
|
final rawText = recognized.text.trim();
|
||||||
|
if (rawText.isEmpty) {
|
||||||
|
if (context.mounted) {
|
||||||
|
Navigator.of(context, rootNavigator: true).pop();
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('No text found in this image.')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final parsed = ReceiptParser.parseRecognizedText(
|
||||||
|
recognized,
|
||||||
|
imageWidth: imageWidth,
|
||||||
|
imageHeight: imageHeight,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (context.mounted) {
|
||||||
|
Navigator.of(context, rootNavigator: true).pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsed.items.isEmpty) {
|
||||||
|
if (context.mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('No products found on this receipt.')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fresh ids so merged trips never collide with existing item keys.
|
||||||
|
final stamp = DateTime.now().microsecondsSinceEpoch;
|
||||||
|
final items = [
|
||||||
|
for (var i = 0; i < parsed.items.length; i++)
|
||||||
|
parsed.items[i].copyWith(id: 'merged-$stamp-$i'),
|
||||||
|
];
|
||||||
|
|
||||||
|
return CapturedReceiptParse(
|
||||||
|
items: items,
|
||||||
|
rawText: rawText,
|
||||||
|
imagePath: imagePath,
|
||||||
|
needsReview: !parsed.validationPassed,
|
||||||
|
detectedStore: parsed.store,
|
||||||
|
);
|
||||||
|
} on PlatformException catch (error) {
|
||||||
|
if (context.mounted) {
|
||||||
|
Navigator.of(context, rootNavigator: true).pop();
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
'Could not read text from this image.'
|
||||||
|
'${error.message == null || error.message!.isEmpty ? '' : '\n${error.message}'}',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (error) {
|
||||||
|
if (context.mounted) {
|
||||||
|
Navigator.of(context, rootNavigator: true).pop();
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('Could not read text from this image.\n$error')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
await recognizer.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,15 +16,17 @@ class ReceiptOcr {
|
|||||||
required double imageHeight,
|
required double imageHeight,
|
||||||
}) {
|
}) {
|
||||||
final elements = <OcrElement>[];
|
final elements = <OcrElement>[];
|
||||||
|
final lines = <OcrLine>[];
|
||||||
var maxRight = 0.0;
|
var maxRight = 0.0;
|
||||||
var maxBottom = 0.0;
|
var maxBottom = 0.0;
|
||||||
|
|
||||||
for (final block in recognized.blocks) {
|
for (final block in recognized.blocks) {
|
||||||
for (final line in block.lines) {
|
for (final line in block.lines) {
|
||||||
|
final lineElements = <OcrElement>[];
|
||||||
if (line.elements.isNotEmpty) {
|
if (line.elements.isNotEmpty) {
|
||||||
for (final element in line.elements) {
|
for (final element in line.elements) {
|
||||||
final box = element.boundingBox;
|
final box = element.boundingBox;
|
||||||
elements.add(
|
lineElements.add(
|
||||||
OcrElement(
|
OcrElement(
|
||||||
text: element.text,
|
text: element.text,
|
||||||
left: box.left,
|
left: box.left,
|
||||||
@@ -38,7 +40,7 @@ class ReceiptOcr {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final box = line.boundingBox;
|
final box = line.boundingBox;
|
||||||
elements.add(
|
lineElements.add(
|
||||||
OcrElement(
|
OcrElement(
|
||||||
text: line.text,
|
text: line.text,
|
||||||
left: box.left,
|
left: box.left,
|
||||||
@@ -50,6 +52,18 @@ class ReceiptOcr {
|
|||||||
if (box.right > maxRight) maxRight = box.right;
|
if (box.right > maxRight) maxRight = box.right;
|
||||||
if (box.bottom > maxBottom) maxBottom = box.bottom;
|
if (box.bottom > maxBottom) maxBottom = box.bottom;
|
||||||
}
|
}
|
||||||
|
elements.addAll(lineElements);
|
||||||
|
final box = line.boundingBox;
|
||||||
|
lines.add(
|
||||||
|
OcrLine(
|
||||||
|
text: line.text,
|
||||||
|
elements: lineElements,
|
||||||
|
left: box.left,
|
||||||
|
top: box.top,
|
||||||
|
right: box.right,
|
||||||
|
bottom: box.bottom,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +72,7 @@ class ReceiptOcr {
|
|||||||
|
|
||||||
return OcrDocument(
|
return OcrDocument(
|
||||||
elements: elements,
|
elements: elements,
|
||||||
|
lines: lines,
|
||||||
imageWidth: width,
|
imageWidth: width,
|
||||||
imageHeight: height,
|
imageHeight: height,
|
||||||
flattenedText: recognized.text.trim(),
|
flattenedText: recognized.text.trim(),
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class ReceiptParseConfig {
|
|||||||
required this.trailingCategoryLetter,
|
required this.trailingCategoryLetter,
|
||||||
required this.noiseKeywords,
|
required this.noiseKeywords,
|
||||||
required this.itemCountKeywords,
|
required this.itemCountKeywords,
|
||||||
this.rowClusterHeightFactor = 1.2,
|
this.rowClusterHeightFactor = 0.55,
|
||||||
this.priceColumnWidthFraction = 0.25,
|
this.priceColumnWidthFraction = 0.25,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ class ReceiptParseConfig {
|
|||||||
rowClusterHeightFactor: _spatialDouble(
|
rowClusterHeightFactor: _spatialDouble(
|
||||||
json,
|
json,
|
||||||
'row_cluster_height_factor',
|
'row_cluster_height_factor',
|
||||||
fallback: 1.2,
|
fallback: 0.55,
|
||||||
),
|
),
|
||||||
priceColumnWidthFraction: _spatialDouble(
|
priceColumnWidthFraction: _spatialDouble(
|
||||||
json,
|
json,
|
||||||
@@ -132,20 +132,19 @@ class ReceiptParseConfig {
|
|||||||
|
|
||||||
/// True if [line] matches a config keyword.
|
/// True if [line] matches a config keyword.
|
||||||
///
|
///
|
||||||
/// Short tokens (3 characters or fewer) use a word boundary so `PIN` does
|
/// Always uses word boundaries so `CHIP` does not match `CHIPS` and `PIN`
|
||||||
/// not match `SPINAZIE`. Longer tokens use a case-insensitive substring,
|
/// does not match `SPINAZIE`. A letter-only compact form is a fallback for
|
||||||
/// and a letter-only compact form so OCR splits like `Betaal d:` still
|
/// OCR splits like `Betaal d:` → `BETAALD` (only for keywords of length ≥ 5,
|
||||||
/// match `BETAALD`.
|
/// so short tokens like `CHIP` cannot match inside longer product words).
|
||||||
bool matchesKeyword(String line, String keyword) {
|
bool matchesKeyword(String line, String keyword) {
|
||||||
final upper = line.toUpperCase();
|
final upper = line.toUpperCase();
|
||||||
final key = keyword.toUpperCase();
|
final key = keyword.toUpperCase();
|
||||||
if (key.length <= 3) {
|
if (RegExp('\\b${RegExp.escape(key)}\\b').hasMatch(upper)) {
|
||||||
return RegExp('\\b${RegExp.escape(key)}\\b').hasMatch(upper);
|
return true;
|
||||||
}
|
}
|
||||||
if (upper.contains(key)) return true;
|
|
||||||
final compactLine = upper.replaceAll(RegExp(r'[^A-Z0-9]'), '');
|
final compactLine = upper.replaceAll(RegExp(r'[^A-Z0-9]'), '');
|
||||||
final compactKey = key.replaceAll(RegExp(r'[^A-Z0-9]'), '');
|
final compactKey = key.replaceAll(RegExp(r'[^A-Z0-9]'), '');
|
||||||
return compactKey.length >= 4 && compactLine.contains(compactKey);
|
return compactKey.length >= 5 && compactLine.contains(compactKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isStandaloneKeywordLine(String line, List<String> keywords) {
|
bool isStandaloneKeywordLine(String line, List<String> keywords) {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class ReceiptParser {
|
|||||||
|
|
||||||
/// Primary path: positioned ML Kit elements → row clustering → lookahead.
|
/// Primary path: positioned ML Kit elements → row clustering → lookahead.
|
||||||
static ReceiptParseResult parseReceiptFromOcr(OcrDocument document) =>
|
static ReceiptParseResult parseReceiptFromOcr(OcrDocument document) =>
|
||||||
ReceiptSpatialParser.parse(document);
|
_parseDocument(document);
|
||||||
|
|
||||||
static ReceiptParseResult parseRecognizedText(
|
static ReceiptParseResult parseRecognizedText(
|
||||||
RecognizedText recognized, {
|
RecognizedText recognized, {
|
||||||
@@ -37,6 +37,50 @@ class ReceiptParser {
|
|||||||
imageWidth: imageWidth,
|
imageWidth: imageWidth,
|
||||||
imageHeight: imageHeight,
|
imageHeight: imageHeight,
|
||||||
);
|
);
|
||||||
return parseReceiptFromOcr(document);
|
return _parseDocument(document);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ReceiptParseResult _parseDocument(OcrDocument document) {
|
||||||
|
if (ReceiptTextParser.looksLikeColumnOcr(document.flattenedText)) {
|
||||||
|
return _withImageDimensions(
|
||||||
|
ReceiptTextParser.parseReceipt(document.flattenedText),
|
||||||
|
document,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final spatial = ReceiptSpatialParser.parse(document);
|
||||||
|
if (_shouldFallbackToText(spatial)) {
|
||||||
|
return _withImageDimensions(
|
||||||
|
ReceiptTextParser.parseReceipt(document.flattenedText),
|
||||||
|
document,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return spatial;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool _shouldFallbackToText(ReceiptParseResult spatial) {
|
||||||
|
if (spatial.items.isEmpty) return true;
|
||||||
|
final flagged =
|
||||||
|
spatial.items.where((item) => item.needsManualReview).length;
|
||||||
|
if (flagged > 0) return true;
|
||||||
|
if (spatial.items.any((item) => item.price <= 0)) return true;
|
||||||
|
if (spatial.receiptTotal != null && !spatial.validationPassed) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ReceiptParseResult _withImageDimensions(
|
||||||
|
ReceiptParseResult result,
|
||||||
|
OcrDocument document,
|
||||||
|
) {
|
||||||
|
return ReceiptParseResult(
|
||||||
|
items: result.items,
|
||||||
|
store: result.store,
|
||||||
|
receiptTotal: result.receiptTotal,
|
||||||
|
totalDiscount: result.totalDiscount,
|
||||||
|
validationPassed: result.validationPassed,
|
||||||
|
expectedItemCount: result.expectedItemCount,
|
||||||
|
ocrImageWidth: document.imageWidth,
|
||||||
|
ocrImageHeight: document.imageHeight,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,13 +28,28 @@ class ReceiptRowBuilder {
|
|||||||
List<ReceiptRow> buildRows(OcrDocument document) {
|
List<ReceiptRow> buildRows(OcrDocument document) {
|
||||||
if (document.elements.isEmpty) return const [];
|
if (document.elements.isEmpty) return const [];
|
||||||
|
|
||||||
final rowClusterThreshold = _rowClusterThreshold(document.elements);
|
|
||||||
final clustered = _clusterByY(document.elements, rowClusterThreshold);
|
|
||||||
final contentWidth = _contentWidth(document.elements);
|
final contentWidth = _contentWidth(document.elements);
|
||||||
final contentLeft = _contentLeft(document.elements);
|
final contentLeft = _contentLeft(document.elements);
|
||||||
final priceColumnStart =
|
final priceColumnStart =
|
||||||
contentLeft + contentWidth * (1 - config.priceColumnWidthFraction);
|
contentLeft + contentWidth * (1 - config.priceColumnWidthFraction);
|
||||||
|
|
||||||
|
// Prefer ML Kit line boundaries — element Y-clustering merges adjacent lines.
|
||||||
|
if (document.lines.isNotEmpty) {
|
||||||
|
return [
|
||||||
|
for (final line in document.lines)
|
||||||
|
_classifyRow(
|
||||||
|
line.elements,
|
||||||
|
priceColumnStart: priceColumnStart,
|
||||||
|
imageWidth: document.imageWidth,
|
||||||
|
imageHeight: document.imageHeight,
|
||||||
|
rowText: line.text,
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
final rowClusterThreshold = _rowClusterThreshold(document.elements);
|
||||||
|
final clustered = _clusterByY(document.elements, rowClusterThreshold);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
for (final rowElements in clustered)
|
for (final rowElements in clustered)
|
||||||
_classifyRow(
|
_classifyRow(
|
||||||
@@ -116,8 +131,9 @@ class ReceiptRowBuilder {
|
|||||||
required double priceColumnStart,
|
required double priceColumnStart,
|
||||||
required double imageWidth,
|
required double imageWidth,
|
||||||
required double imageHeight,
|
required double imageHeight,
|
||||||
|
String? rowText,
|
||||||
}) {
|
}) {
|
||||||
final text = rowElements.map((e) => e.text).join(' ').trim();
|
final text = rowText ?? rowElements.map((e) => e.text).join(' ').trim();
|
||||||
final normalized = _preprocess(text);
|
final normalized = _preprocess(text);
|
||||||
final bounds = _rowBounds(rowElements, imageWidth, imageHeight);
|
final bounds = _rowBounds(rowElements, imageWidth, imageHeight);
|
||||||
|
|
||||||
|
|||||||
@@ -116,6 +116,17 @@ class ReceiptTextParser {
|
|||||||
return _parseSequential(lines, config, rawText);
|
return _parseSequential(lines, config, rawText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// True when OCR dumped an entire name column before a price column (Jumbo-style).
|
||||||
|
static bool looksLikeColumnOcr(String rawText) {
|
||||||
|
final config = receiptParseConfig;
|
||||||
|
final lines = rawText
|
||||||
|
.split('\n')
|
||||||
|
.map(_preprocess)
|
||||||
|
.where((line) => line.isNotEmpty)
|
||||||
|
.toList();
|
||||||
|
return _looksLikeColumnOcr(lines, config);
|
||||||
|
}
|
||||||
|
|
||||||
static String _preprocess(String line) =>
|
static String _preprocess(String line) =>
|
||||||
line.trim().replaceAll(RegExp(r'\s+'), ' ');
|
line.trim().replaceAll(RegExp(r'\s+'), ' ');
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import 'models/line_item.dart';
|
|||||||
import 'models/shop.dart';
|
import 'models/shop.dart';
|
||||||
import 'product_detail_screen.dart';
|
import 'product_detail_screen.dart';
|
||||||
import 'services/product_repository.dart';
|
import 'services/product_repository.dart';
|
||||||
|
import 'services/receipt_capture.dart';
|
||||||
import 'utils/dates.dart';
|
import 'utils/dates.dart';
|
||||||
import 'utils/money.dart';
|
import 'utils/money.dart';
|
||||||
import 'widgets/confirm_dialog.dart';
|
import 'widgets/confirm_dialog.dart';
|
||||||
@@ -37,6 +38,7 @@ class _ShopDetailScreenState extends State<ShopDetailScreen> {
|
|||||||
bool _editing = false;
|
bool _editing = false;
|
||||||
List<String> _tagSuggestions = [];
|
List<String> _tagSuggestions = [];
|
||||||
int _loadToken = 0;
|
int _loadToken = 0;
|
||||||
|
bool _addingReceipt = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@@ -150,6 +152,42 @@ class _ShopDetailScreenState extends State<ShopDetailScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _addFromReceipt() async {
|
||||||
|
final shop = _shop;
|
||||||
|
if (shop == null || _addingReceipt) return;
|
||||||
|
setState(() => _addingReceipt = true);
|
||||||
|
try {
|
||||||
|
final captured = await captureAndParseReceipt(context);
|
||||||
|
if (captured == null || !mounted) return;
|
||||||
|
|
||||||
|
var added = 0;
|
||||||
|
for (final item in captured.items) {
|
||||||
|
await widget.repository.addShopItem(shopId: shop.id, item: item);
|
||||||
|
added++;
|
||||||
|
}
|
||||||
|
if (!mounted) return;
|
||||||
|
await _load();
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(
|
||||||
|
added == 1
|
||||||
|
? 'Added 1 item from receipt.'
|
||||||
|
: 'Added $added items from receipt.',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch (_) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(content: Text('Could not add items from this receipt.')),
|
||||||
|
);
|
||||||
|
_load();
|
||||||
|
} finally {
|
||||||
|
if (mounted) setState(() => _addingReceipt = false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _editItem(ShopItem item) async {
|
Future<void> _editItem(ShopItem item) async {
|
||||||
final result = await showItemEditSheet(
|
final result = await showItemEditSheet(
|
||||||
context,
|
context,
|
||||||
@@ -299,11 +337,41 @@ class _ShopDetailScreenState extends State<ShopDetailScreen> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
|
||||||
floatingActionButton: _editing
|
floatingActionButton: _editing
|
||||||
? FloatingActionButton.extended(
|
? Padding(
|
||||||
onPressed: _addItem,
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||||
icon: const Icon(Icons.add),
|
child: Row(
|
||||||
label: const Text('Add product'),
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: FilledButton.tonalIcon(
|
||||||
|
onPressed: _addingReceipt ? null : _addFromReceipt,
|
||||||
|
icon: _addingReceipt
|
||||||
|
? const SizedBox(
|
||||||
|
width: 18,
|
||||||
|
height: 18,
|
||||||
|
child: CircularProgressIndicator(strokeWidth: 2),
|
||||||
|
)
|
||||||
|
: const Icon(Icons.receipt_long_outlined),
|
||||||
|
label: const Text('Add receipt'),
|
||||||
|
style: FilledButton.styleFrom(
|
||||||
|
minimumSize: const Size.fromHeight(52),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: 12),
|
||||||
|
Expanded(
|
||||||
|
child: FilledButton.icon(
|
||||||
|
onPressed: _addingReceipt ? null : _addItem,
|
||||||
|
icon: const Icon(Icons.add),
|
||||||
|
label: const Text('Add product'),
|
||||||
|
style: FilledButton.styleFrom(
|
||||||
|
minimumSize: const Size.fromHeight(52),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
)
|
)
|
||||||
: null,
|
: null,
|
||||||
body: Builder(
|
body: Builder(
|
||||||
|
|||||||
@@ -185,8 +185,8 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
"spatial_parsing": {
|
"spatial_parsing": {
|
||||||
"_comment": "Bounding-box pipeline settings. row_cluster_height_factor multiplies median element height when grouping rows. price_column_width_fraction is the right-hand slice of receipt content width treated as the price column.",
|
"_comment": "Bounding-box pipeline settings. row_cluster_height_factor multiplies median element height when grouping elements into rows (only used when ML Kit lines are unavailable — keep below 0.6 so adjacent receipt lines stay separate). price_column_width_fraction is the right-hand slice of receipt content width treated as the price column.",
|
||||||
"row_cluster_height_factor": 1.2,
|
"row_cluster_height_factor": 0.55,
|
||||||
"price_column_width_fraction": 0.25
|
"price_column_width_fraction": 0.25
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:groci/models/ocr_element.dart';
|
||||||
import 'package:groci/services/receipt_parser.dart';
|
import 'package:groci/services/receipt_parser.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@@ -152,6 +153,72 @@ Bedr.Incl 2,09
|
|||||||
expect(items.last.price, 6.20);
|
expect(items.last.price, 6.20);
|
||||||
expect(items, hasLength(26));
|
expect(items, hasLength(26));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('does not treat CHIPS as payment keyword CHIP', () {
|
||||||
|
final items = ReceiptParser.parse('''
|
||||||
|
AH Protein
|
||||||
|
AH Chips
|
||||||
|
1,49
|
||||||
|
0.69
|
||||||
|
''');
|
||||||
|
|
||||||
|
expect(items, hasLength(2));
|
||||||
|
expect(items.map((item) => item.name), ['AH Protein', 'AH Chips']);
|
||||||
|
expect(items.map((item) => item.price), [1.49, 0.69]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('scan path uses column parser for Jumbo name-then-price OCR dump', () {
|
||||||
|
const ocr = '''
|
||||||
|
BIO VASTKOKEND AARD
|
||||||
|
2 X 1,99
|
||||||
|
AARDBEIEN
|
||||||
|
2 X 3,99
|
||||||
|
ACTIE AARDBEIEN
|
||||||
|
BROCCOLII
|
||||||
|
PREI
|
||||||
|
CITROENEN
|
||||||
|
KOMKOMMER
|
||||||
|
2 X 0,95
|
||||||
|
ACTIE KOMKOMMER
|
||||||
|
BOL KNOFLOOK
|
||||||
|
BROODSTICKS OLIJF
|
||||||
|
4 X 1.25
|
||||||
|
3,98
|
||||||
|
7,98
|
||||||
|
-2,00
|
||||||
|
1,24
|
||||||
|
0,69
|
||||||
|
2,11
|
||||||
|
1,90
|
||||||
|
-0,21
|
||||||
|
1,79
|
||||||
|
5,00
|
||||||
|
''';
|
||||||
|
|
||||||
|
final result = ReceiptParser.parseReceiptFromOcr(
|
||||||
|
OcrDocument(
|
||||||
|
elements: const [],
|
||||||
|
lines: const [],
|
||||||
|
imageWidth: 800,
|
||||||
|
imageHeight: 1200,
|
||||||
|
flattenedText: ocr,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.items, hasLength(8));
|
||||||
|
expect(result.items[0].name, 'BIO VASTKOKEND AARD');
|
||||||
|
expect(result.items[0].quantity, 2);
|
||||||
|
expect(result.items[0].price, 3.98);
|
||||||
|
expect(result.items[1].name, 'AARDBEIEN');
|
||||||
|
expect(result.items[1].price, 5.98);
|
||||||
|
expect(result.items[1].discount, -2.00);
|
||||||
|
expect(result.items[5].name, 'KOMKOMMER');
|
||||||
|
expect(result.items[5].price, 1.69);
|
||||||
|
expect(result.items.last.name, 'BROODSTICKS OLIJF');
|
||||||
|
expect(result.items.last.quantity, 4);
|
||||||
|
expect(result.items.last.price, 5.00);
|
||||||
|
expect(result.items.every((item) => !item.needsManualReview), isTrue);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const _jumboColumnOcr = '''
|
const _jumboColumnOcr = '''
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ import 'package:groci/services/receipt_row_builder.dart';
|
|||||||
import 'package:groci/services/receipt_spatial_parser.dart';
|
import 'package:groci/services/receipt_spatial_parser.dart';
|
||||||
|
|
||||||
/// Synthetic two-column receipt layout (800×1200 image).
|
/// Synthetic two-column receipt layout (800×1200 image).
|
||||||
///
|
|
||||||
/// Y increases downward; name column ~left 40–420, price column ~right 620–760.
|
|
||||||
const _imageWidth = 800.0;
|
const _imageWidth = 800.0;
|
||||||
const _imageHeight = 1200.0;
|
const _imageHeight = 1200.0;
|
||||||
const _nameLeft = 40.0;
|
const _nameLeft = 40.0;
|
||||||
@@ -28,12 +26,39 @@ OcrElement _word(String text, double row, {bool price = false}) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
OcrDocument _document(List<OcrElement> elements, {String? flattened}) {
|
OcrLine _line(List<OcrElement> elements) {
|
||||||
|
var left = double.infinity;
|
||||||
|
var top = double.infinity;
|
||||||
|
var right = 0.0;
|
||||||
|
var bottom = 0.0;
|
||||||
|
for (final e in elements) {
|
||||||
|
if (e.left < left) left = e.left;
|
||||||
|
if (e.top < top) top = e.top;
|
||||||
|
if (e.right > right) right = e.right;
|
||||||
|
if (e.bottom > bottom) bottom = e.bottom;
|
||||||
|
}
|
||||||
|
return OcrLine(
|
||||||
|
text: elements.map((e) => e.text).join(' '),
|
||||||
|
elements: elements,
|
||||||
|
left: left,
|
||||||
|
top: top,
|
||||||
|
right: right,
|
||||||
|
bottom: bottom,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
OcrDocument _document(
|
||||||
|
List<List<OcrElement>> rows, {
|
||||||
|
String? flattened,
|
||||||
|
}) {
|
||||||
|
final elements = [for (final row in rows) ...row];
|
||||||
|
final lines = [for (final row in rows) _line(row)];
|
||||||
return OcrDocument(
|
return OcrDocument(
|
||||||
elements: elements,
|
elements: elements,
|
||||||
|
lines: lines,
|
||||||
imageWidth: _imageWidth,
|
imageWidth: _imageWidth,
|
||||||
imageHeight: _imageHeight,
|
imageHeight: _imageHeight,
|
||||||
flattenedText: flattened ?? elements.map((e) => e.text).join('\n'),
|
flattenedText: flattened ?? lines.map((l) => l.text).join('\n'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,8 +71,7 @@ void main() {
|
|||||||
test('clusters name and price on the same row by Y position', () {
|
test('clusters name and price on the same row by Y position', () {
|
||||||
final rows = ReceiptRowBuilder(receiptParseConfig).buildRows(
|
final rows = ReceiptRowBuilder(receiptParseConfig).buildRows(
|
||||||
_document([
|
_document([
|
||||||
_word('MILK', 0),
|
[_word('MILK', 0), _word('1,50', 0, price: true)],
|
||||||
_word('1,50', 0, price: true),
|
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -60,9 +84,7 @@ void main() {
|
|||||||
test('classifies pure quantity row before item logic', () {
|
test('classifies pure quantity row before item logic', () {
|
||||||
final rows = ReceiptRowBuilder(receiptParseConfig).buildRows(
|
final rows = ReceiptRowBuilder(receiptParseConfig).buildRows(
|
||||||
_document([
|
_document([
|
||||||
_word('2', 0),
|
[_word('2', 1), _word('X', 1), _word('2,99', 1, price: true)],
|
||||||
_word('X', 0),
|
|
||||||
_word('2,99', 0, price: true),
|
|
||||||
], flattened: '2 X 2,99'),
|
], flattened: '2 X 2,99'),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -74,24 +96,36 @@ void main() {
|
|||||||
|
|
||||||
test('treats row without price-column token as name-only', () {
|
test('treats row without price-column token as name-only', () {
|
||||||
final rows = ReceiptRowBuilder(receiptParseConfig).buildRows(
|
final rows = ReceiptRowBuilder(receiptParseConfig).buildRows(
|
||||||
_document([_word('BROCCOLI', 0)]),
|
_document([
|
||||||
|
[_word('BROCCOLI', 0)],
|
||||||
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(rows.single.kind.name, 'nameOnly');
|
expect(rows.single.kind.name, 'nameOnly');
|
||||||
expect(rows.single.nameText, 'BROCCOLI');
|
expect(rows.single.nameText, 'BROCCOLI');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('does not merge adjacent ML Kit lines into one row', () {
|
||||||
|
final rows = ReceiptRowBuilder(receiptParseConfig).buildRows(
|
||||||
|
_document([
|
||||||
|
[_word('BIO', 0), _word('VASTKOKEND', 0), _word('AARD', 0)],
|
||||||
|
[_word('2', 1), _word('X', 1), _word('1,99', 1, price: true)],
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(rows, hasLength(2));
|
||||||
|
expect(rows[0].nameText, contains('BIO'));
|
||||||
|
expect(rows[1].kind.name, 'quantityModifier');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('ReceiptSpatialParser lookahead', () {
|
group('ReceiptSpatialParser lookahead', () {
|
||||||
test('merges Jumbo-style name row with following quantity modifier', () {
|
test('merges Jumbo-style name row with following quantity modifier', () {
|
||||||
final result = ReceiptSpatialParser.parse(
|
final result = ReceiptSpatialParser.parse(
|
||||||
_document([
|
_document([
|
||||||
_word('AARDBEIEN', 0),
|
[_word('AARDBEIEN', 0)],
|
||||||
_word('2', 1),
|
[_word('2', 1), _word('X', 1), _word('3,99', 1, price: true)],
|
||||||
_word('X', 1),
|
[_word('Totaal', 2), _word('7,98', 2, price: true)],
|
||||||
_word('3,99', 1, price: true),
|
|
||||||
_word('Totaal', 2),
|
|
||||||
_word('7,98', 2, price: true),
|
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -106,12 +140,8 @@ void main() {
|
|||||||
test('overrides inline price when quantity modifier follows (Kruidvat)', () {
|
test('overrides inline price when quantity modifier follows (Kruidvat)', () {
|
||||||
final result = ReceiptSpatialParser.parse(
|
final result = ReceiptSpatialParser.parse(
|
||||||
_document([
|
_document([
|
||||||
_word('ZEEPTABLET', 0),
|
[_word('ZEEPTABLET', 0), _word('NEUTRAL', 0), _word('7,98', 0, price: true)],
|
||||||
_word('NEUTRAL', 0),
|
[_word('2', 1), _word('X', 1), _word('3,99', 1, price: true)],
|
||||||
_word('7,98', 0, price: true),
|
|
||||||
_word('2', 1),
|
|
||||||
_word('X', 1),
|
|
||||||
_word('3,99', 1, price: true),
|
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -124,9 +154,8 @@ void main() {
|
|||||||
test('flags name-only row without following modifier for manual review', () {
|
test('flags name-only row without following modifier for manual review', () {
|
||||||
final result = ReceiptSpatialParser.parse(
|
final result = ReceiptSpatialParser.parse(
|
||||||
_document([
|
_document([
|
||||||
_word('MILK', 0),
|
[_word('MILK', 0)],
|
||||||
_word('BREAD', 1),
|
[_word('BREAD', 1), _word('2,00', 1, price: true)],
|
||||||
_word('2,00', 1, price: true),
|
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -140,12 +169,9 @@ void main() {
|
|||||||
test('parses two side-by-side items without cross-assigning prices', () {
|
test('parses two side-by-side items without cross-assigning prices', () {
|
||||||
final result = ReceiptSpatialParser.parse(
|
final result = ReceiptSpatialParser.parse(
|
||||||
_document([
|
_document([
|
||||||
_word('MILK', 0),
|
[_word('MILK', 0), _word('1,50', 0, price: true)],
|
||||||
_word('1,50', 0, price: true),
|
[_word('BREAD', 1), _word('2,00', 1, price: true)],
|
||||||
_word('BREAD', 1),
|
[_word('Totaal', 2), _word('3,50', 2, price: true)],
|
||||||
_word('2,00', 1, price: true),
|
|
||||||
_word('Totaal', 2),
|
|
||||||
_word('3,50', 2, price: true),
|
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -157,11 +183,8 @@ void main() {
|
|||||||
test('fails validation when printed item count does not match', () {
|
test('fails validation when printed item count does not match', () {
|
||||||
final result = ReceiptSpatialParser.parse(
|
final result = ReceiptSpatialParser.parse(
|
||||||
_document([
|
_document([
|
||||||
_word('MILK', 0),
|
[_word('MILK', 0), _word('1,50', 0, price: true)],
|
||||||
_word('1,50', 0, price: true),
|
[_word('Aantal Artikel: 3', 1)],
|
||||||
_word('Aantal', 1),
|
|
||||||
_word('Artikel:', 1),
|
|
||||||
_word('3', 1, price: true),
|
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user