A whole bunch of changes
This commit is contained in:
parent
901ec678a3
commit
956231048e
122 changed files with 5442 additions and 914 deletions
|
|
@ -1,174 +1,66 @@
|
|||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../models/fuel_entry.dart';
|
||||
import '../services/app_state.dart';
|
||||
import '../services/ocr_service.dart';
|
||||
import '../services/receipt_parser.dart';
|
||||
import '../widgets/image_source_sheet.dart';
|
||||
import '../widgets/hero_banner.dart';
|
||||
import '../widgets/receipt_capture.dart';
|
||||
import '../widgets/receipt_thumbnail.dart';
|
||||
import 'add_edit_vehicle_screen.dart';
|
||||
import 'confirm_fuel_entry_screen.dart';
|
||||
import 'receipt_image_screen.dart';
|
||||
import 'receipt_detail_screen.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;
|
||||
|
||||
class VehicleDetailScreen extends StatelessWidget {
|
||||
/// A swipeable [PageView] over every vehicle (in the same order as the
|
||||
/// Vehicles tab's list), starting on [vehicleId] — sliding left/right moves
|
||||
/// to the next/previous vehicle, the same "modern navigation" swipe
|
||||
/// [MainShell] uses for the bottom-nav tabs. Each page is a full,
|
||||
/// independent [_VehicleDetailPage] (own AppBar/FAB), so this widget only
|
||||
/// owns the [PageController].
|
||||
class VehicleDetailScreen extends StatefulWidget {
|
||||
final String vehicleId;
|
||||
|
||||
const VehicleDetailScreen({super.key, required this.vehicleId});
|
||||
|
||||
Future<void> _captureReceipt(BuildContext context) async {
|
||||
final source = await chooseImageSource(context);
|
||||
if (source == null || !context.mounted) return;
|
||||
@override
|
||||
State<VehicleDetailScreen> createState() => _VehicleDetailScreenState();
|
||||
}
|
||||
|
||||
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;
|
||||
class _VehicleDetailScreenState extends State<VehicleDetailScreen> {
|
||||
late final PageController _pageController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final vehicles = context.read<AppState>().vehicles;
|
||||
final initialIndex = vehicles.indexWhere((v) => v.id == widget.vehicleId);
|
||||
_pageController = PageController(initialPage: initialIndex < 0 ? 0 : initialIndex);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pageController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final vehicles = context.watch<AppState>().vehicles;
|
||||
|
||||
if (vehicles.isEmpty) {
|
||||
return const Scaffold(body: Center(child: Text('Vehicle not found')));
|
||||
}
|
||||
if (photo == null) return;
|
||||
|
||||
final imageFile = File(photo.path);
|
||||
|
||||
if (!context.mounted) return;
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: false,
|
||||
builder: (_) => const Center(child: CircularProgressIndicator()),
|
||||
return PageView.builder(
|
||||
controller: _pageController,
|
||||
itemCount: vehicles.length,
|
||||
itemBuilder: (context, index) => _VehicleDetailPage(vehicleId: vehicles[index].id),
|
||||
);
|
||||
|
||||
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;
|
||||
}
|
||||
class _VehicleDetailPage extends StatelessWidget {
|
||||
final String vehicleId;
|
||||
|
||||
Future<void> _deleteEntry(BuildContext context, FuelEntry entry) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Delete fuel entry?'),
|
||||
content: const Text('This will remove the entry and its receipt photo.'),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.of(context).pop(false), child: const Text('Cancel')),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(true),
|
||||
child: const Text('Delete', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed == true && context.mounted) {
|
||||
await context.read<AppState>().deleteFuelEntry(entry.id);
|
||||
}
|
||||
}
|
||||
|
||||
Widget _buildReceiptLeading(BuildContext context, FuelEntry entry) {
|
||||
if (entry.receiptImagePath != null) {
|
||||
return GestureDetector(
|
||||
onTap: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ReceiptImageScreen(localImagePath: entry.receiptImagePath),
|
||||
),
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: Image.file(
|
||||
File(entry.receiptImagePath!),
|
||||
width: 48,
|
||||
height: 48,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (entry.isReceiptUploadedToDrive) {
|
||||
return GestureDetector(
|
||||
onTap: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ReceiptImageScreen(driveFileId: entry.receiptDriveFileId),
|
||||
),
|
||||
),
|
||||
child: const CircleAvatar(child: Icon(Icons.cloud_outlined)),
|
||||
);
|
||||
}
|
||||
return const CircleAvatar(child: Icon(Icons.receipt_long));
|
||||
}
|
||||
const _VehicleDetailPage({required this.vehicleId});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
|
@ -181,8 +73,12 @@ class VehicleDetailScreen extends StatelessWidget {
|
|||
|
||||
final entries = appState.entriesForVehicle(vehicleId);
|
||||
final totalGallons = appState.totalGallonsForVehicle(vehicleId);
|
||||
final totalCost = entries.fold(0.0, (sum, e) => sum + e.totalCost);
|
||||
final dateFormat = DateFormat.yMMMd().add_jm();
|
||||
final currencyFormat = NumberFormat.simpleCurrency();
|
||||
// The hero stat is a big headline number, not a precise figure — round
|
||||
// to whole gallons there, matching the Receipts tab's hero banner.
|
||||
final heroGallonsFormat = NumberFormat('#,##0');
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
|
|
@ -199,42 +95,74 @@ class VehicleDetailScreen extends StatelessWidget {
|
|||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(16),
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Total gallons logged', style: Theme.of(context).textTheme.labelLarge),
|
||||
Text(
|
||||
'${totalGallons.toStringAsFixed(3)} gal',
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
],
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 12),
|
||||
child: HeroBanner(
|
||||
child: HeroStatsRow(
|
||||
gallonsValue: heroGallonsFormat.format(totalGallons),
|
||||
costValue: currencyFormat.format(totalCost),
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: entries.isEmpty
|
||||
? const Center(child: Text('No fuel entries yet. Tap the camera button to log a receipt.'))
|
||||
: ListView.separated(
|
||||
// Keyed by vehicle so each vehicle's scroll position is
|
||||
// remembered independently as the user swipes between
|
||||
// pages — PageView.builder can dispose an off-screen
|
||||
// page's element, which would otherwise reset it.
|
||||
key: PageStorageKey('vehicle-entries-$vehicleId'),
|
||||
padding: const EdgeInsets.all(12),
|
||||
itemCount: entries.length,
|
||||
separatorBuilder: (_, _) => const SizedBox(height: 8),
|
||||
itemBuilder: (context, index) {
|
||||
final entry = entries[index];
|
||||
void openDetail() => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ReceiptDetailScreen(entryId: entry.id),
|
||||
),
|
||||
);
|
||||
return Card(
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: ListTile(
|
||||
leading: _buildReceiptLeading(context, entry),
|
||||
title: Text('${entry.gallons.toStringAsFixed(3)} gal • ${currencyFormat.format(entry.totalCost)}'),
|
||||
subtitle: Text(
|
||||
'${currencyFormat.format(entry.pricePerGallon)}/gal\n${dateFormat.format(entry.date)}',
|
||||
),
|
||||
isThreeLine: true,
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
onPressed: () => _deleteEntry(context, entry),
|
||||
child: InkWell(
|
||||
onTap: openDetail,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
||||
// Row cross-axis defaults to center, so the
|
||||
// thumbnail sits vertically centered against the
|
||||
// title+subtitle block even though it's three
|
||||
// lines tall — ListTile's isThreeLine would
|
||||
// instead force leading/trailing to the top, per
|
||||
// Material spec (the same issue the vehicle list
|
||||
// cards had).
|
||||
child: Row(
|
||||
children: [
|
||||
ReceiptThumbnail(entry: entry, onTap: openDetail),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
'${entry.gallons.toStringAsFixed(3)} gal • '
|
||||
'${currencyFormat.format(entry.totalCost)}',
|
||||
style: Theme.of(context).textTheme.titleMedium,
|
||||
),
|
||||
Text(
|
||||
'${currencyFormat.format(entry.pricePerGallon)}/gal\n'
|
||||
'${dateFormat.format(entry.date)}',
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Icon(Icons.chevron_right),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
@ -244,7 +172,7 @@ class VehicleDetailScreen extends StatelessWidget {
|
|||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: () => _captureReceipt(context),
|
||||
onPressed: () => captureReceiptForVehicle(context, vehicleId),
|
||||
icon: const Icon(Icons.camera_alt_outlined),
|
||||
label: const Text('Log Fuel Receipt'),
|
||||
),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue