Added Google Drive storage location
This commit is contained in:
parent
e630f2a3ec
commit
73a7c93e64
22 changed files with 1690 additions and 205 deletions
|
|
@ -136,7 +136,7 @@ class _ConfirmFuelEntryScreenState extends State<ConfirmFuelEntryScreen> {
|
|||
GestureDetector(
|
||||
onTap: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ReceiptImageScreen(imagePath: widget.imageFile.path),
|
||||
builder: (_) => ReceiptImageScreen(localImagePath: widget.imageFile.path),
|
||||
),
|
||||
),
|
||||
child: ClipRRect(
|
||||
|
|
|
|||
219
lib/screens/drive_folder_browser_screen.dart
Normal file
219
lib/screens/drive_folder_browser_screen.dart
Normal file
|
|
@ -0,0 +1,219 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../services/app_state.dart';
|
||||
import '../services/drive_service.dart';
|
||||
|
||||
enum _BrowseRoot { myDrive, sharedWithMe }
|
||||
|
||||
/// Lets the user navigate their Google Drive — either "My Drive" or
|
||||
/// folders others have shared with them — and pick a parent location.
|
||||
/// Confirming looks for (or creates) the `MO-Fuel-Tax-Back` folder under
|
||||
/// that location, so two people pointing at the same shared parent
|
||||
/// converge on the same app folder.
|
||||
class DriveFolderBrowserScreen extends StatefulWidget {
|
||||
const DriveFolderBrowserScreen({super.key});
|
||||
|
||||
@override
|
||||
State<DriveFolderBrowserScreen> createState() => _DriveFolderBrowserScreenState();
|
||||
}
|
||||
|
||||
class _DriveFolderBrowserScreenState extends State<DriveFolderBrowserScreen> {
|
||||
late final http.Client _client;
|
||||
late final DriveService _drive;
|
||||
|
||||
_BrowseRoot _root = _BrowseRoot.myDrive;
|
||||
final List<DriveFolder> _pathStack = [];
|
||||
|
||||
List<DriveFolder>? _folders;
|
||||
bool _loading = true;
|
||||
String? _error;
|
||||
bool _confirming = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_client = context.read<AppState>().driveAuth.authenticatedHttpClient();
|
||||
_drive = DriveService(_client);
|
||||
_load();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_client.close();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String get _rootLabel => _root == _BrowseRoot.myDrive ? 'My Drive' : 'Shared with me';
|
||||
|
||||
/// The folder ID that "Use This Folder" would act on, or null if the
|
||||
/// current view is a virtual listing (top-level "Shared with me") rather
|
||||
/// than an actual folder.
|
||||
String? get _currentFolderId {
|
||||
if (_pathStack.isNotEmpty) return _pathStack.last.id;
|
||||
if (_root == _BrowseRoot.myDrive) return 'root';
|
||||
return null;
|
||||
}
|
||||
|
||||
String get _breadcrumbPath =>
|
||||
([_rootLabel] + _pathStack.map((f) => f.name).toList()).join(' / ');
|
||||
|
||||
Future<void> _load() async {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
});
|
||||
|
||||
try {
|
||||
List<DriveFolder> folders;
|
||||
if (_pathStack.isNotEmpty) {
|
||||
folders = await _drive.listFolders(parentId: _pathStack.last.id);
|
||||
} else if (_root == _BrowseRoot.myDrive) {
|
||||
folders = await _drive.listFolders(parentId: 'root');
|
||||
} else {
|
||||
folders = await _drive.listFolders(sharedWithMe: true);
|
||||
}
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_folders = folders;
|
||||
_loading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_error = 'Could not load folders: $e';
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _switchRoot(_BrowseRoot root) {
|
||||
if (root == _root) return;
|
||||
setState(() {
|
||||
_root = root;
|
||||
_pathStack.clear();
|
||||
});
|
||||
_load();
|
||||
}
|
||||
|
||||
void _openFolder(DriveFolder folder) {
|
||||
setState(() => _pathStack.add(folder));
|
||||
_load();
|
||||
}
|
||||
|
||||
void _goToBreadcrumb(int index) {
|
||||
// index == -1 means the root label itself.
|
||||
setState(() {
|
||||
if (index < 0) {
|
||||
_pathStack.clear();
|
||||
} else {
|
||||
_pathStack.removeRange(index + 1, _pathStack.length);
|
||||
}
|
||||
});
|
||||
_load();
|
||||
}
|
||||
|
||||
Future<void> _useThisFolder() async {
|
||||
final parentId = _currentFolderId;
|
||||
if (parentId == null) return;
|
||||
|
||||
setState(() => _confirming = true);
|
||||
try {
|
||||
await context.read<AppState>().chooseDriveFolder(
|
||||
parentId: parentId,
|
||||
breadcrumbPath: _breadcrumbPath,
|
||||
);
|
||||
if (mounted) Navigator.of(context).pop();
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Could not use this folder: $e')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) setState(() => _confirming = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final canUseCurrentFolder = _currentFolderId != null;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Choose Drive Folder')),
|
||||
body: Column(
|
||||
children: [
|
||||
SegmentedButton<_BrowseRoot>(
|
||||
segments: const [
|
||||
ButtonSegment(value: _BrowseRoot.myDrive, label: Text('My Drive')),
|
||||
ButtonSegment(value: _BrowseRoot.sharedWithMe, label: Text('Shared with me')),
|
||||
],
|
||||
selected: {_root},
|
||||
onSelectionChanged: (selection) => _switchRoot(selection.first),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||
child: SingleChildScrollView(
|
||||
scrollDirection: Axis.horizontal,
|
||||
child: Row(
|
||||
children: [
|
||||
TextButton(
|
||||
onPressed: () => _goToBreadcrumb(-1),
|
||||
child: Text(_rootLabel),
|
||||
),
|
||||
for (var i = 0; i < _pathStack.length; i++) ...[
|
||||
const Icon(Icons.chevron_right, size: 18),
|
||||
TextButton(
|
||||
onPressed: () => _goToBreadcrumb(i),
|
||||
child: Text(_pathStack[i].name),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const Divider(height: 1),
|
||||
Expanded(child: _buildBody()),
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: FilledButton.icon(
|
||||
onPressed: (!canUseCurrentFolder || _confirming) ? null : _useThisFolder,
|
||||
icon: _confirming
|
||||
? const SizedBox(height: 16, width: 16, child: CircularProgressIndicator(strokeWidth: 2))
|
||||
: const Icon(Icons.check),
|
||||
label: Text('Use "$_breadcrumbPath"'),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody() {
|
||||
if (_loading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (_error != null) {
|
||||
return Center(child: Padding(padding: const EdgeInsets.all(24), child: Text(_error!)));
|
||||
}
|
||||
final folders = _folders ?? [];
|
||||
if (folders.isEmpty) {
|
||||
return const Center(child: Text('No folders here.'));
|
||||
}
|
||||
return ListView.builder(
|
||||
itemCount: folders.length,
|
||||
itemBuilder: (context, index) {
|
||||
final folder = folders[index];
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.folder_outlined),
|
||||
title: Text(folder.name),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () => _openFolder(folder),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,12 +1,67 @@
|
|||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
/// Full-screen, pinch-zoomable view of a saved receipt photo.
|
||||
class ReceiptImageScreen extends StatelessWidget {
|
||||
final String imagePath;
|
||||
import '../services/app_state.dart';
|
||||
import '../services/drive_service.dart';
|
||||
|
||||
const ReceiptImageScreen({super.key, required this.imagePath});
|
||||
/// Full-screen, pinch-zoomable view of a receipt photo. Pass
|
||||
/// [localImagePath] for a receipt still held locally, or [driveFileId] for
|
||||
/// one already uploaded to Drive and removed locally — in which case it's
|
||||
/// downloaded on demand (no local caching afterward).
|
||||
class ReceiptImageScreen extends StatefulWidget {
|
||||
final String? localImagePath;
|
||||
final String? driveFileId;
|
||||
|
||||
const ReceiptImageScreen({super.key, this.localImagePath, this.driveFileId})
|
||||
: assert(localImagePath != null || driveFileId != null,
|
||||
'Must provide either a local path or a Drive file ID');
|
||||
|
||||
@override
|
||||
State<ReceiptImageScreen> createState() => _ReceiptImageScreenState();
|
||||
}
|
||||
|
||||
class _ReceiptImageScreenState extends State<ReceiptImageScreen> {
|
||||
Uint8List? _downloadedBytes;
|
||||
bool _loading = false;
|
||||
String? _error;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.driveFileId != null) {
|
||||
_download();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _download() async {
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
});
|
||||
|
||||
final authService = context.read<AppState>().driveAuth;
|
||||
final client = authService.authenticatedHttpClient();
|
||||
try {
|
||||
final drive = DriveService(client);
|
||||
final bytes = await drive.downloadFileBytes(widget.driveFileId!);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_downloadedBytes = Uint8List.fromList(bytes);
|
||||
_loading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_error = 'Could not download receipt: $e';
|
||||
_loading = false;
|
||||
});
|
||||
} finally {
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
|
@ -17,11 +72,26 @@ class ReceiptImageScreen extends StatelessWidget {
|
|||
foregroundColor: Colors.white,
|
||||
title: const Text('Receipt'),
|
||||
),
|
||||
body: Center(
|
||||
child: InteractiveViewer(
|
||||
child: Image.file(File(imagePath)),
|
||||
),
|
||||
),
|
||||
body: Center(child: _buildBody()),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody() {
|
||||
if (widget.localImagePath != null) {
|
||||
return InteractiveViewer(child: Image.file(File(widget.localImagePath!)));
|
||||
}
|
||||
if (_loading) {
|
||||
return const CircularProgressIndicator(color: Colors.white);
|
||||
}
|
||||
if (_error != null) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Text(_error!, style: const TextStyle(color: Colors.white)),
|
||||
);
|
||||
}
|
||||
if (_downloadedBytes != null) {
|
||||
return InteractiveViewer(child: Image.memory(_downloadedBytes!));
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../services/app_state.dart';
|
||||
import 'drive_folder_browser_screen.dart';
|
||||
|
||||
class SettingsScreen extends StatefulWidget {
|
||||
const SettingsScreen({super.key});
|
||||
|
|
@ -12,31 +13,50 @@ class SettingsScreen extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _SettingsScreenState extends State<SettingsScreen> {
|
||||
bool _changing = false;
|
||||
bool _busy = false;
|
||||
String? _error;
|
||||
|
||||
Future<void> _chooseFolder() async {
|
||||
setState(() => _error = null);
|
||||
|
||||
final selectedPath = await FilePicker.platform.getDirectoryPath(
|
||||
dialogTitle: 'Choose a folder for receipts and data',
|
||||
);
|
||||
if (selectedPath == null || !mounted) return;
|
||||
|
||||
setState(() => _changing = true);
|
||||
Future<void> _connect() async {
|
||||
setState(() {
|
||||
_busy = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
await context.read<AppState>().changeSaveDirectory(selectedPath);
|
||||
await context.read<AppState>().connectDrive();
|
||||
} catch (e) {
|
||||
setState(() => _error = 'Could not switch to that folder: $e');
|
||||
setState(() => _error = 'Could not sign in: $e');
|
||||
} finally {
|
||||
if (mounted) setState(() => _changing = false);
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _disconnect() async {
|
||||
setState(() => _busy = true);
|
||||
try {
|
||||
await context.read<AppState>().disconnectDrive();
|
||||
} finally {
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
}
|
||||
|
||||
void _chooseFolder() {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => const DriveFolderBrowserScreen()),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _syncNow() async {
|
||||
setState(() => _busy = true);
|
||||
try {
|
||||
await context.read<AppState>().syncNow();
|
||||
} finally {
|
||||
if (mounted) setState(() => _busy = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final appState = context.watch<AppState>();
|
||||
final saveDir = appState.storage.saveDirectory.path;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Settings')),
|
||||
|
|
@ -49,35 +69,79 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
|||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Save Location', style: Theme.of(context).textTheme.titleMedium),
|
||||
Text('Google Drive', style: Theme.of(context).textTheme.titleMedium),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'Receipt photos and the data file are stored here:',
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
SelectableText(
|
||||
saveDir,
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
fontFamily: 'monospace',
|
||||
if (!appState.isDriveConnected) ...[
|
||||
Text(
|
||||
'Connect Google Drive to share vehicles and fuel receipts with '
|
||||
'other people, and to keep a backup off this device.',
|
||||
style: Theme.of(context).textTheme.bodyMedium,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
if (_error != null) ...[
|
||||
Text(_error!, style: TextStyle(color: Theme.of(context).colorScheme.error)),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
FilledButton.icon(
|
||||
onPressed: _busy ? null : _connect,
|
||||
icon: const Icon(Icons.login),
|
||||
label: const Text('Connect Google Drive'),
|
||||
),
|
||||
] else ...[
|
||||
Text('Signed in as ${appState.driveAccountEmail}'),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
appState.driveFolderPath == null
|
||||
? 'No folder selected yet.'
|
||||
: 'Folder: ${appState.driveFolderPath}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
OutlinedButton.icon(
|
||||
onPressed: _busy ? null : _chooseFolder,
|
||||
icon: const Icon(Icons.folder_open),
|
||||
label: Text(appState.driveFolderPath == null
|
||||
? 'Choose Folder'
|
||||
: 'Change Folder'),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
if (_error != null) ...[
|
||||
Text(_error!, style: TextStyle(color: Theme.of(context).colorScheme.error)),
|
||||
OutlinedButton.icon(
|
||||
onPressed: (_busy || appState.driveFolderPath == null)
|
||||
? null
|
||||
: _syncNow,
|
||||
icon: appState.isSyncing
|
||||
? const SizedBox(
|
||||
height: 16,
|
||||
width: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2))
|
||||
: const Icon(Icons.sync),
|
||||
label: const Text('Sync Now'),
|
||||
),
|
||||
TextButton.icon(
|
||||
onPressed: _busy ? null : _disconnect,
|
||||
icon: const Icon(Icons.logout),
|
||||
label: const Text('Disconnect'),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
if (appState.lastSyncedAt != null)
|
||||
Text(
|
||||
'Last synced ${DateFormat.yMMMd().add_jm().format(appState.lastSyncedAt!)}',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
if (appState.lastSyncError != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 4),
|
||||
child: Text(
|
||||
'Last sync failed: ${appState.lastSyncError}',
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||||
),
|
||||
),
|
||||
],
|
||||
FilledButton.icon(
|
||||
onPressed: _changing ? null : _chooseFolder,
|
||||
icon: _changing
|
||||
? const SizedBox(
|
||||
height: 16,
|
||||
width: 16,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.folder_open),
|
||||
label: const Text('Choose Folder'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
|
|
|||
|
|
@ -88,6 +88,38 @@ class VehicleDetailScreen extends StatelessWidget {
|
|||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final appState = context.watch<AppState>();
|
||||
|
|
@ -144,24 +176,7 @@ class VehicleDetailScreen extends StatelessWidget {
|
|||
return Card(
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: ListTile(
|
||||
leading: entry.receiptImagePath != null
|
||||
? GestureDetector(
|
||||
onTap: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => ReceiptImageScreen(imagePath: entry.receiptImagePath!),
|
||||
),
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
child: Image.file(
|
||||
File(entry.receiptImagePath!),
|
||||
width: 48,
|
||||
height: 48,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
)
|
||||
: const CircleAvatar(child: Icon(Icons.receipt_long)),
|
||||
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)}',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue