219 lines
6.4 KiB
Dart
219 lines
6.4 KiB
Dart
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),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|