A whole bunch of changes
This commit is contained in:
parent
901ec678a3
commit
956231048e
122 changed files with 5442 additions and 914 deletions
115
lib/widgets/receipt_capture.dart
Normal file
115
lib/widgets/receipt_capture.dart
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../screens/confirm_fuel_entry_screen.dart';
|
||||
import '../services/app_state.dart';
|
||||
import '../services/ocr_service.dart';
|
||||
import '../services/receipt_parser.dart';
|
||||
import 'image_source_sheet.dart';
|
||||
|
||||
/// Long edge and JPEG quality a receipt photo is downscaled to unless
|
||||
/// "keep max quality" is on — a receipt is a photo of small printed text,
|
||||
/// not something that benefits from a multi-megapixel original, and this
|
||||
/// keeps typical files in the low hundreds of KB instead of several MB.
|
||||
const receiptImageMaxDimension = 1600.0;
|
||||
const receiptImageQuality = 70;
|
||||
|
||||
/// Shared by [VehicleDetailScreen]'s "Log Fuel Receipt" button and the
|
||||
/// Receipts tab's "+" action: pick a photo, OCR it, and — unless the
|
||||
/// receipt looks like it's from outside Missouri, in which case the user
|
||||
/// gets a chance to back out first — hand off to [ConfirmFuelEntryScreen]
|
||||
/// for [vehicleId].
|
||||
Future<void> captureReceiptForVehicle(BuildContext context, String vehicleId) async {
|
||||
final source = await chooseImageSource(context, heading: 'Receipt From');
|
||||
if (source == null || !context.mounted) return;
|
||||
|
||||
final keepMaxQuality = context.read<AppState>().keepMaxQualityReceiptPhotos;
|
||||
final picker = ImagePicker();
|
||||
XFile? photo;
|
||||
try {
|
||||
photo = await picker.pickImage(
|
||||
source: source,
|
||||
imageQuality: keepMaxQuality ? null : receiptImageQuality,
|
||||
maxWidth: keepMaxQuality ? null : receiptImageMaxDimension,
|
||||
maxHeight: keepMaxQuality ? null : receiptImageMaxDimension,
|
||||
);
|
||||
} catch (e) {
|
||||
if (context.mounted) {
|
||||
final sourceLabel = source == ImageSource.camera ? 'camera' : 'photo library';
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Could not open $sourceLabel: $e')),
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (photo == null) return;
|
||||
|
||||
final imageFile = File(photo.path);
|
||||
|
||||
if (!context.mounted) return;
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => const Center(child: CircularProgressIndicator()),
|
||||
);
|
||||
|
||||
final ocrService = OcrService();
|
||||
String recognizedText = '';
|
||||
try {
|
||||
recognizedText = await ocrService.recognizeText(imageFile);
|
||||
} catch (_) {
|
||||
recognizedText = '';
|
||||
} finally {
|
||||
ocrService.dispose();
|
||||
}
|
||||
|
||||
final parsed = ReceiptParser.parse(recognizedText);
|
||||
|
||||
if (!context.mounted) return;
|
||||
Navigator.of(context).pop(); // close the OCR loading spinner
|
||||
|
||||
final state = parsed.state;
|
||||
if (state != null && state != 'MO') {
|
||||
final proceed = await _confirmNonMissouriPurchase(context, state);
|
||||
if (!proceed || !context.mounted) return;
|
||||
}
|
||||
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ConfirmFuelEntryScreen(
|
||||
vehicleId: vehicleId,
|
||||
imageFile: imageFile,
|
||||
parsed: parsed,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Missouri's fuel tax refund only applies to fuel bought in Missouri —
|
||||
/// warn if the receipt's address is somewhere else, and let the user
|
||||
/// decide whether to log it anyway (e.g. it might still be worth tracking
|
||||
/// for other reasons even if it won't qualify for a refund).
|
||||
Future<bool> _confirmNonMissouriPurchase(BuildContext context, String stateCode) async {
|
||||
final stateName = usStateNames[stateCode] ?? stateCode;
|
||||
final proceed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Non-Missouri Purchase'),
|
||||
content: Text(
|
||||
'This receipt looks like it\'s from a gas station in $stateName. '
|
||||
'The Missouri fuel tax refund only applies to fuel purchased in '
|
||||
'Missouri.\n\nDo you still want to add this entry?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.of(context).pop(false), child: const Text('No')),
|
||||
FilledButton(onPressed: () => Navigator.of(context).pop(true), child: const Text('Yes')),
|
||||
],
|
||||
),
|
||||
);
|
||||
return proceed ?? false;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue