Add chat files panel explorer shell with sorting and views
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../data/directory_listing.dart';
|
||||
import 'files_models.dart';
|
||||
|
||||
class FilesController extends ChangeNotifier {
|
||||
FilesController({required this.gateway, required this.initialPath});
|
||||
final ApiGateway gateway;
|
||||
final String initialPath;
|
||||
DirectoryListing? listing;
|
||||
String? error;
|
||||
bool loading = false;
|
||||
bool showHidden = false;
|
||||
String filter = '';
|
||||
FilesSort sort = const FilesSort(FilesSortKey.name);
|
||||
FilesViewMode view = FilesViewMode.list;
|
||||
int _request = 0;
|
||||
bool _disposed = false;
|
||||
|
||||
bool get missingDirectory => initialPath.trim().isEmpty;
|
||||
List<DirectoryEntry> get entries {
|
||||
final current = listing;
|
||||
if (current == null) return const [];
|
||||
final needle = filter.trim().toLowerCase();
|
||||
return sortFileEntries(
|
||||
current.entries.where(
|
||||
(entry) =>
|
||||
(showHidden || !entry.hidden) &&
|
||||
(needle.isEmpty || entry.name.toLowerCase().contains(needle)),
|
||||
),
|
||||
sort,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> start() => open(initialPath);
|
||||
|
||||
Future<void> open(String? path) async {
|
||||
if (path == null || path.trim().isEmpty) return;
|
||||
final request = ++_request;
|
||||
loading = true;
|
||||
error = null;
|
||||
_notify();
|
||||
try {
|
||||
final next = await listDirectory(gateway, path);
|
||||
if (_disposed || request != _request) return;
|
||||
listing = next;
|
||||
filter = '';
|
||||
} catch (e) {
|
||||
if (_disposed || request != _request) return;
|
||||
error = '$e';
|
||||
} finally {
|
||||
if (!_disposed && request == _request) {
|
||||
loading = false;
|
||||
_notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> retry() => open(listing?.path ?? initialPath);
|
||||
void toggleHidden(bool value) {
|
||||
showHidden = value;
|
||||
_notify();
|
||||
}
|
||||
|
||||
void setFilter(String value) {
|
||||
filter = value;
|
||||
_notify();
|
||||
}
|
||||
|
||||
void setSort(FilesSort value) {
|
||||
sort = value;
|
||||
_notify();
|
||||
}
|
||||
|
||||
void setView(FilesViewMode value) {
|
||||
view = value;
|
||||
_notify();
|
||||
}
|
||||
|
||||
void _notify() {
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
_request++;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../artifacts/domain/artifact_kind.dart';
|
||||
import '../data/directory_listing.dart';
|
||||
|
||||
/// Chooses the same broad kind glyphs used by the artifact gallery.
|
||||
IconData fileIcon(DirectoryEntry entry) {
|
||||
if (entry.isDir) return Icons.folder_outlined;
|
||||
|
||||
switch (artifactKind('', entry.name)) {
|
||||
case ArtifactKind.image:
|
||||
return Icons.image_outlined;
|
||||
case ArtifactKind.video:
|
||||
return Icons.movie_outlined;
|
||||
case ArtifactKind.audio:
|
||||
return Icons.audio_file_outlined;
|
||||
case ArtifactKind.pdf:
|
||||
return Icons.picture_as_pdf_outlined;
|
||||
case ArtifactKind.json:
|
||||
case ArtifactKind.code:
|
||||
return Icons.code_outlined;
|
||||
case ArtifactKind.archive:
|
||||
return Icons.archive_outlined;
|
||||
case ArtifactKind.spreadsheet:
|
||||
return Icons.table_chart_outlined;
|
||||
case ArtifactKind.log:
|
||||
case ArtifactKind.document:
|
||||
return Icons.description_outlined;
|
||||
case ArtifactKind.other:
|
||||
return Icons.insert_drive_file_outlined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import '../data/directory_listing.dart';
|
||||
import '../../artifacts/domain/artifact_kind.dart';
|
||||
|
||||
enum FilesViewMode { list, grid }
|
||||
|
||||
enum FilesSortKey { name, modified, size, kind }
|
||||
|
||||
class FilesSort {
|
||||
const FilesSort(this.key, {this.ascending = true});
|
||||
final FilesSortKey key;
|
||||
final bool ascending;
|
||||
|
||||
FilesSort reverseOr(FilesSortKey next) =>
|
||||
FilesSort(next, ascending: next == key ? !ascending : true);
|
||||
}
|
||||
|
||||
List<DirectoryEntry> sortFileEntries(
|
||||
Iterable<DirectoryEntry> entries,
|
||||
FilesSort sort,
|
||||
) {
|
||||
final result = entries.toList();
|
||||
result.sort((a, b) {
|
||||
// Keep directories above files regardless of the selected direction.
|
||||
if (a.isDir != b.isDir) return a.isDir ? -1 : 1;
|
||||
int compare;
|
||||
switch (sort.key) {
|
||||
case FilesSortKey.name:
|
||||
compare = a.name.toLowerCase().compareTo(b.name.toLowerCase());
|
||||
case FilesSortKey.modified:
|
||||
compare = (a.modified ?? '').compareTo(b.modified ?? '');
|
||||
case FilesSortKey.size:
|
||||
compare = (a.size ?? -1).compareTo(b.size ?? -1);
|
||||
case FilesSortKey.kind:
|
||||
compare = fileKind(a).compareTo(fileKind(b));
|
||||
}
|
||||
if (compare == 0) compare = a.name.compareTo(b.name);
|
||||
return sort.ascending ? compare : -compare;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
String fileKind(DirectoryEntry entry) =>
|
||||
entry.isDir ? 'Folder' : artifactKind('', entry.name).name;
|
||||
|
||||
String fileSizeLabel(int? size) {
|
||||
if (size == null) return '—';
|
||||
if (size < 1024) return '$size B';
|
||||
final units = ['KiB', 'MiB', 'GiB', 'TiB'];
|
||||
var value = size.toDouble();
|
||||
var unit = 0;
|
||||
while (value >= 1024 && unit < units.length - 1) {
|
||||
value /= 1024;
|
||||
unit++;
|
||||
}
|
||||
return '${value.toStringAsFixed(value >= 10 ? 0 : 1)} ${units[unit]}';
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../data/directory_listing.dart';
|
||||
import '../ui/sessions/chat_adaptive.dart';
|
||||
import 'files_controller.dart';
|
||||
import 'files_icons.dart';
|
||||
import 'files_models.dart';
|
||||
|
||||
const double filesPanelMinWidth = 280;
|
||||
const double filesPanelMaxWidth = 420;
|
||||
const double filesPanelFraction = .30;
|
||||
|
||||
double filesPanelWidth(double available) => (available * filesPanelFraction)
|
||||
.clamp(filesPanelMinWidth, filesPanelMaxWidth);
|
||||
|
||||
class FilesPanel extends StatefulWidget {
|
||||
const FilesPanel({super.key, required this.controller, this.onOpen});
|
||||
final FilesController controller;
|
||||
final ValueChanged<DirectoryEntry>? onOpen;
|
||||
@override
|
||||
State<FilesPanel> createState() => _FilesPanelState();
|
||||
}
|
||||
|
||||
class _FilesPanelState extends State<FilesPanel> {
|
||||
late final TextEditingController _filter = TextEditingController();
|
||||
FilesController get controller => widget.controller;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
controller.addListener(_changed);
|
||||
controller.start();
|
||||
}
|
||||
|
||||
void _changed() {
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
controller.removeListener(_changed);
|
||||
_filter.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final listing = controller.listing;
|
||||
if (controller.missingDirectory) {
|
||||
return const _FilesMessage('This chat has no working directory.');
|
||||
}
|
||||
return Material(
|
||||
color: Theme.of(context).colorScheme.surface,
|
||||
child: Column(
|
||||
children: [
|
||||
_header(context, listing),
|
||||
if (controller.error != null)
|
||||
ChatErrorNotice(
|
||||
message: controller.error!,
|
||||
onRetry: controller.retry,
|
||||
),
|
||||
if (controller.loading && listing == null)
|
||||
const Expanded(child: ChatLoadingIndicator())
|
||||
else if (listing != null &&
|
||||
controller.entries.isEmpty &&
|
||||
!controller.loading)
|
||||
const Expanded(child: _FilesMessage('This folder is empty.'))
|
||||
else
|
||||
Expanded(
|
||||
child: controller.view == FilesViewMode.grid ? _grid() : _list(),
|
||||
),
|
||||
if (listing?.truncated == true)
|
||||
const Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: Text('Some entries are not shown.'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _header(BuildContext context, DirectoryListing? listing) => Padding(
|
||||
padding: const EdgeInsets.fromLTRB(12, 8, 8, 4),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
if (listing?.parent != null)
|
||||
IconButton(
|
||||
tooltip: 'Back',
|
||||
onPressed: controller.loading
|
||||
? null
|
||||
: () => controller.open(listing!.parent),
|
||||
icon: const Icon(Icons.arrow_back),
|
||||
),
|
||||
Expanded(
|
||||
child: Text(
|
||||
homeRelativeLabel(
|
||||
listing?.home ?? '',
|
||||
listing?.path ?? controller.initialPath,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Refresh',
|
||||
onPressed: controller.loading ? null : controller.retry,
|
||||
icon: const Icon(Icons.refresh),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: controller.view == FilesViewMode.list
|
||||
? 'Grid view'
|
||||
: 'List view',
|
||||
onPressed: () => controller.setView(
|
||||
controller.view == FilesViewMode.list
|
||||
? FilesViewMode.grid
|
||||
: FilesViewMode.list,
|
||||
),
|
||||
icon: Icon(
|
||||
controller.view == FilesViewMode.list
|
||||
? Icons.grid_view
|
||||
: Icons.view_list,
|
||||
),
|
||||
),
|
||||
PopupMenuButton<FilesSortKey>(
|
||||
initialValue: controller.sort.key,
|
||||
tooltip: 'Sort',
|
||||
onSelected: (key) =>
|
||||
controller.setSort(controller.sort.reverseOr(key)),
|
||||
itemBuilder: (_) => [
|
||||
for (final key in FilesSortKey.values)
|
||||
PopupMenuItem(value: key, child: Text(key.name)),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
||||
? CupertinoSearchTextField(
|
||||
controller: _filter,
|
||||
onChanged: controller.setFilter,
|
||||
placeholder: 'Filter files',
|
||||
)
|
||||
: TextField(
|
||||
controller: _filter,
|
||||
onChanged: controller.setFilter,
|
||||
decoration: const InputDecoration(
|
||||
prefixIcon: Icon(Icons.search),
|
||||
hintText: 'Filter files',
|
||||
isDense: true,
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
tooltip: 'Show hidden files',
|
||||
onPressed: () => controller.toggleHidden(!controller.showHidden),
|
||||
icon: Icon(
|
||||
controller.showHidden ? Icons.visibility : Icons.visibility_off,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
Widget _list() => ListView.builder(
|
||||
itemCount: controller.entries.length,
|
||||
itemBuilder: (_, index) {
|
||||
final entry = controller.entries[index];
|
||||
return ListTile(
|
||||
onTap: () => entry.isDir
|
||||
? controller.open(entry.path)
|
||||
: widget.onOpen?.call(entry),
|
||||
leading: Icon(
|
||||
fileIcon(entry),
|
||||
),
|
||||
title: Text(entry.name),
|
||||
subtitle: Text(
|
||||
entry.isDir
|
||||
? 'Folder'
|
||||
: '${entry.modified ?? 'Unknown'} · ${fileSizeLabel(entry.size)}',
|
||||
),
|
||||
trailing: entry.isDir ? null : Text(fileKind(entry)),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Widget _grid() => GridView.builder(
|
||||
padding: const EdgeInsets.all(12),
|
||||
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
||||
maxCrossAxisExtent: 180,
|
||||
childAspectRatio: 1.25,
|
||||
),
|
||||
itemCount: controller.entries.length,
|
||||
itemBuilder: (_, index) {
|
||||
final entry = controller.entries[index];
|
||||
return InkWell(
|
||||
onTap: () => entry.isDir
|
||||
? controller.open(entry.path)
|
||||
: widget.onOpen?.call(entry),
|
||||
child: Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
fileIcon(entry),
|
||||
size: 32,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
entry.name,
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
class _FilesMessage extends StatelessWidget {
|
||||
const _FilesMessage(this.text);
|
||||
final String text;
|
||||
@override
|
||||
Widget build(BuildContext context) => Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Text(text, textAlign: TextAlign.center),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'files_models.dart';
|
||||
|
||||
class FilesPreferences {
|
||||
const FilesPreferences(this.serverScope);
|
||||
final String serverScope;
|
||||
|
||||
String get _prefix => 'pipelines.chat.files.v1.$serverScope';
|
||||
|
||||
Future<SharedPreferences> _prefs() => SharedPreferences.getInstance();
|
||||
|
||||
Future<bool> visible() async =>
|
||||
(await _prefs()).getBool('$_prefix.visible') ?? false;
|
||||
Future<void> setVisible(bool value) async =>
|
||||
(await _prefs()).setBool('$_prefix.visible', value);
|
||||
|
||||
Future<FilesViewMode> view() async {
|
||||
final value = (await _prefs()).getString('$_prefix.view');
|
||||
return FilesViewMode.values.firstWhere(
|
||||
(v) => v.name == value,
|
||||
orElse: () => FilesViewMode.list,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> setView(FilesViewMode value) async =>
|
||||
(await _prefs()).setString('$_prefix.view', value.name);
|
||||
|
||||
Future<FilesSort> sort() async {
|
||||
final prefs = await _prefs();
|
||||
final key = FilesSortKey.values.firstWhere(
|
||||
(v) => v.name == prefs.getString('$_prefix.sort'),
|
||||
orElse: () => FilesSortKey.name,
|
||||
);
|
||||
return FilesSort(key, ascending: prefs.getBool('$_prefix.asc') ?? true);
|
||||
}
|
||||
|
||||
Future<void> setSort(FilesSort value) async {
|
||||
final prefs = await _prefs();
|
||||
await prefs.setString('$_prefix.sort', value.key.name);
|
||||
await prefs.setBool('$_prefix.asc', value.ascending);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user