Added report functionality and Dropbox connection
This commit is contained in:
parent
483fbeafb6
commit
c7494eda92
8 changed files with 614 additions and 2 deletions
|
|
@ -4,6 +4,7 @@ import 'package:provider/provider.dart';
|
|||
import '../models/vehicle.dart';
|
||||
import '../services/app_state.dart';
|
||||
import 'add_edit_vehicle_screen.dart';
|
||||
import 'report_screen.dart';
|
||||
import 'settings_screen.dart';
|
||||
import 'vehicle_detail_screen.dart';
|
||||
|
||||
|
|
@ -19,6 +20,13 @@ class HomeScreen extends StatelessWidget {
|
|||
appBar: AppBar(
|
||||
title: const Text('My Vehicles'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.summarize_outlined),
|
||||
tooltip: 'Fuel Report',
|
||||
onPressed: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const ReportScreen()),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.settings_outlined),
|
||||
tooltip: 'Settings',
|
||||
|
|
|
|||
172
lib/screens/report_screen.dart
Normal file
172
lib/screens/report_screen.dart
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:printing/printing.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../services/app_state.dart';
|
||||
import '../services/fuel_report.dart';
|
||||
import '../services/fuel_report_pdf.dart';
|
||||
|
||||
class ReportScreen extends StatefulWidget {
|
||||
const ReportScreen({super.key});
|
||||
|
||||
@override
|
||||
State<ReportScreen> createState() => _ReportScreenState();
|
||||
}
|
||||
|
||||
class _ReportScreenState extends State<ReportScreen> {
|
||||
static final _dateFormat = DateFormat.yMMMd();
|
||||
|
||||
DateTime? _startDate;
|
||||
DateTime? _endDate;
|
||||
bool _busy = false;
|
||||
|
||||
Future<void> _pickDate({required bool isStart}) async {
|
||||
final now = DateTime.now();
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: (isStart ? _startDate : _endDate) ?? now,
|
||||
firstDate: DateTime(2000),
|
||||
lastDate: now,
|
||||
);
|
||||
if (picked == null) return;
|
||||
setState(() {
|
||||
if (isStart) {
|
||||
_startDate = picked;
|
||||
} else {
|
||||
_endDate = picked;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _share(FuelReport report) async {
|
||||
setState(() => _busy = true);
|
||||
try {
|
||||
final bytes = await buildFuelReportPdf(report);
|
||||
await Printing.sharePdf(bytes: bytes, filename: fuelReportFileName(report));
|
||||
} finally {
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _print(FuelReport report) async {
|
||||
setState(() => _busy = true);
|
||||
try {
|
||||
final bytes = await buildFuelReportPdf(report);
|
||||
await Printing.layoutPdf(onLayout: (_) async => bytes, name: fuelReportFileName(report));
|
||||
} finally {
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final appState = context.watch<AppState>();
|
||||
|
||||
String? rangeError;
|
||||
FuelReport? report;
|
||||
if (_startDate != null && _endDate != null) {
|
||||
if (_endDate!.isBefore(_startDate!)) {
|
||||
rangeError = 'End date must be on or after the start date.';
|
||||
} else {
|
||||
report = buildFuelReport(
|
||||
vehicles: appState.vehicles,
|
||||
fuelEntries: appState.fuelEntries,
|
||||
startDate: _startDate!,
|
||||
endDate: _endDate!,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Fuel Report')),
|
||||
body: ListView(
|
||||
padding: const EdgeInsets.all(16),
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: OutlinedButton(
|
||||
onPressed: () => _pickDate(isStart: true),
|
||||
child: Text(_startDate == null ? 'Start date' : _dateFormat.format(_startDate!)),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: OutlinedButton(
|
||||
onPressed: () => _pickDate(isStart: false),
|
||||
child: Text(_endDate == null ? 'End date' : _dateFormat.format(_endDate!)),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (rangeError != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(rangeError, style: TextStyle(color: Theme.of(context).colorScheme.error)),
|
||||
],
|
||||
const SizedBox(height: 16),
|
||||
if (report == null)
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 24),
|
||||
child: Text('Choose a start and end date to generate a report.'),
|
||||
)
|
||||
else if (report.rows.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 24),
|
||||
child: Text('No vehicles had a receipted fuel entry in that date range.'),
|
||||
)
|
||||
else ...[
|
||||
for (final row in report.rows)
|
||||
Card(
|
||||
child: ListTile(
|
||||
title: Text(row.vehicle.displayLabel),
|
||||
subtitle: Text(
|
||||
'VIN: ${row.vehicle.vin}\n${row.entryCount} receipted '
|
||||
'entr${row.entryCount == 1 ? 'y' : 'ies'}',
|
||||
),
|
||||
isThreeLine: true,
|
||||
trailing: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text('${row.totalGallons.toStringAsFixed(3)} gal'),
|
||||
Text(NumberFormat.simpleCurrency().format(row.totalCost)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(height: 32),
|
||||
ListTile(
|
||||
title: const Text('Total', style: TextStyle(fontWeight: FontWeight.bold)),
|
||||
trailing: Text(
|
||||
'${report.totalGallons.toStringAsFixed(3)} gal · '
|
||||
'${NumberFormat.simpleCurrency().format(report.totalCost)}',
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: FilledButton.icon(
|
||||
onPressed: _busy ? null : () => _share(report!),
|
||||
icon: const Icon(Icons.share_outlined),
|
||||
label: const Text('Share'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: OutlinedButton.icon(
|
||||
onPressed: _busy ? null : () => _print(report!),
|
||||
icon: const Icon(Icons.print_outlined),
|
||||
label: const Text('Print'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -24,14 +24,14 @@ class CloudOAuthConfig {
|
|||
// From a "Full Dropbox" access app at dropbox.com/developers/apps.
|
||||
|
||||
/// The app's key (client ID for OAuth2 PKCE — no secret needed).
|
||||
static const String? dropboxAppKey = null; // TODO: fill in
|
||||
static const String? dropboxAppKey = 'id43qmk09hilpgk';
|
||||
|
||||
/// Custom URL scheme redirect registered in the Dropbox app console.
|
||||
/// The scheme "mofueltaxback-dropbox" is already wired up in
|
||||
/// AndroidManifest.xml / Info.plist, so unless you have a reason to pick
|
||||
/// a different scheme, use exactly:
|
||||
/// "mofueltaxback-dropbox://oauth2redirect"
|
||||
static const String? dropboxRedirectUri = null; // TODO: fill in
|
||||
static const String? dropboxRedirectUri = 'mofueltaxback-dropbox://oauth2redirect';
|
||||
|
||||
// --- OneDrive (Microsoft Graph) ---
|
||||
//
|
||||
|
|
|
|||
81
lib/services/fuel_report.dart
Normal file
81
lib/services/fuel_report.dart
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import '../models/fuel_entry.dart';
|
||||
import '../models/vehicle.dart';
|
||||
|
||||
/// One vehicle's totals within a [FuelReport]'s date range.
|
||||
class VehicleReportRow {
|
||||
final Vehicle vehicle;
|
||||
final int entryCount;
|
||||
final double totalGallons;
|
||||
final double totalCost;
|
||||
|
||||
VehicleReportRow({
|
||||
required this.vehicle,
|
||||
required this.entryCount,
|
||||
required this.totalGallons,
|
||||
required this.totalCost,
|
||||
});
|
||||
}
|
||||
|
||||
/// Per-vehicle fuel totals for entries with a receipt, dated within
|
||||
/// [startDate]–[endDate] inclusive. See [buildFuelReport].
|
||||
class FuelReport {
|
||||
final DateTime startDate;
|
||||
final DateTime endDate;
|
||||
final List<VehicleReportRow> rows;
|
||||
|
||||
FuelReport({required this.startDate, required this.endDate, required this.rows});
|
||||
|
||||
double get totalGallons => rows.fold(0.0, (sum, r) => sum + r.totalGallons);
|
||||
double get totalCost => rows.fold(0.0, (sum, r) => sum + r.totalCost);
|
||||
}
|
||||
|
||||
/// Builds a per-vehicle fuel summary for [startDate]–[endDate] (inclusive,
|
||||
/// judged by each entry's purchase date, in local time), restricted to
|
||||
/// entries that actually have a receipt attached — a report meant to
|
||||
/// substantiate a fuel tax refund claim is only as good as the
|
||||
/// documentation behind each entry, so an entry logged without a photo
|
||||
/// isn't something the report should be claiming on your behalf. "Has a
|
||||
/// receipt" means a local copy or a cloud copy — [FuelEntry.receiptImagePath]
|
||||
/// or [FuelEntry.receiptDriveFileId] — not specifically an already-synced
|
||||
/// one, since sync status is a technical detail unrelated to whether the
|
||||
/// purchase is documented.
|
||||
///
|
||||
/// Vehicles with no qualifying entries in range are omitted rather than
|
||||
/// shown with an all-zero row. Rows are sorted by [Vehicle.displayLabel].
|
||||
FuelReport buildFuelReport({
|
||||
required List<Vehicle> vehicles,
|
||||
required List<FuelEntry> fuelEntries,
|
||||
required DateTime startDate,
|
||||
required DateTime endDate,
|
||||
}) {
|
||||
final rangeStart = DateTime(startDate.year, startDate.month, startDate.day);
|
||||
final rangeEndExclusive =
|
||||
DateTime(endDate.year, endDate.month, endDate.day).add(const Duration(days: 1));
|
||||
|
||||
final vehiclesById = {for (final v in vehicles) v.id: v};
|
||||
final entriesByVehicle = <String, List<FuelEntry>>{};
|
||||
|
||||
for (final entry in fuelEntries) {
|
||||
final hasReceipt = entry.receiptImagePath != null || entry.receiptDriveFileId != null;
|
||||
if (!hasReceipt) continue;
|
||||
if (entry.date.isBefore(rangeStart) || !entry.date.isBefore(rangeEndExclusive)) continue;
|
||||
(entriesByVehicle[entry.vehicleId] ??= []).add(entry);
|
||||
}
|
||||
|
||||
final rows = <VehicleReportRow>[];
|
||||
for (final entry in entriesByVehicle.entries) {
|
||||
final vehicle = vehiclesById[entry.key];
|
||||
// No matching (active) vehicle — e.g. deleted since — nothing sane to
|
||||
// attribute these entries to.
|
||||
if (vehicle == null) continue;
|
||||
rows.add(VehicleReportRow(
|
||||
vehicle: vehicle,
|
||||
entryCount: entry.value.length,
|
||||
totalGallons: entry.value.fold(0.0, (sum, e) => sum + e.gallons),
|
||||
totalCost: entry.value.fold(0.0, (sum, e) => sum + e.totalCost),
|
||||
));
|
||||
}
|
||||
rows.sort((a, b) => a.vehicle.displayLabel.compareTo(b.vehicle.displayLabel));
|
||||
|
||||
return FuelReport(startDate: rangeStart, endDate: endDate, rows: rows);
|
||||
}
|
||||
85
lib/services/fuel_report_pdf.dart
Normal file
85
lib/services/fuel_report_pdf.dart
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:pdf/pdf.dart';
|
||||
import 'package:pdf/widgets.dart' as pw;
|
||||
|
||||
import 'fuel_report.dart';
|
||||
|
||||
final _dateFormat = DateFormat.yMMMd();
|
||||
final _currencyFormat = NumberFormat.simpleCurrency();
|
||||
|
||||
/// Renders [report] as a single-page-friendly PDF: a per-vehicle table
|
||||
/// (VIN, nickname, entry count, gallons, total cost) plus a grand-total
|
||||
/// row, suitable for attaching to a fuel tax refund submission or handing
|
||||
/// to a printer.
|
||||
Future<Uint8List> buildFuelReportPdf(FuelReport report) async {
|
||||
final doc = pw.Document();
|
||||
|
||||
doc.addPage(
|
||||
pw.MultiPage(
|
||||
pageFormat: PdfPageFormat.letter,
|
||||
header: (context) => pw.Column(
|
||||
crossAxisAlignment: pw.CrossAxisAlignment.start,
|
||||
children: [
|
||||
pw.Text('Fuel Report', style: pw.TextStyle(fontSize: 20, fontWeight: pw.FontWeight.bold)),
|
||||
pw.Text(
|
||||
'${_dateFormat.format(report.startDate)} – ${_dateFormat.format(report.endDate)}',
|
||||
style: const pw.TextStyle(fontSize: 12, color: PdfColors.grey700),
|
||||
),
|
||||
pw.SizedBox(height: 16),
|
||||
],
|
||||
),
|
||||
build: (context) => [
|
||||
if (report.rows.isEmpty)
|
||||
pw.Text('No vehicles had a receipted fuel entry in this date range.')
|
||||
else
|
||||
pw.TableHelper.fromTextArray(
|
||||
headers: ['Vehicle', 'VIN', 'Entries', 'Gallons', 'Total Cost'],
|
||||
data: [
|
||||
for (final row in report.rows)
|
||||
[
|
||||
row.vehicle.nickname?.trim().isNotEmpty == true ? row.vehicle.nickname! : '—',
|
||||
row.vehicle.vin,
|
||||
row.entryCount.toString(),
|
||||
row.totalGallons.toStringAsFixed(3),
|
||||
_currencyFormat.format(row.totalCost),
|
||||
],
|
||||
],
|
||||
cellAlignments: const {
|
||||
2: pw.Alignment.centerRight,
|
||||
3: pw.Alignment.centerRight,
|
||||
4: pw.Alignment.centerRight,
|
||||
},
|
||||
headerAlignments: const {
|
||||
2: pw.Alignment.centerRight,
|
||||
3: pw.Alignment.centerRight,
|
||||
4: pw.Alignment.centerRight,
|
||||
},
|
||||
headerStyle: pw.TextStyle(fontWeight: pw.FontWeight.bold),
|
||||
cellPadding: const pw.EdgeInsets.symmetric(horizontal: 8, vertical: 6),
|
||||
),
|
||||
pw.SizedBox(height: 12),
|
||||
pw.Divider(),
|
||||
pw.Row(
|
||||
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
pw.Text('Total', style: pw.TextStyle(fontWeight: pw.FontWeight.bold)),
|
||||
pw.Text(
|
||||
'${report.totalGallons.toStringAsFixed(3)} gal · ${_currencyFormat.format(report.totalCost)}',
|
||||
style: pw.TextStyle(fontWeight: pw.FontWeight.bold),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
return doc.save();
|
||||
}
|
||||
|
||||
/// `fuel-report-<start>-<end>.pdf`, e.g. `fuel-report-20260101-20260331.pdf`.
|
||||
String fuelReportFileName(FuelReport report) {
|
||||
final fmt = DateFormat('yyyyMMdd');
|
||||
return 'fuel-report-${fmt.format(report.startDate)}-${fmt.format(report.endDate)}.pdf';
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue