Files
blumia 8ac36b1fb1 feat: the archive picker offers only projects that hold archived sessions
- the archive page passes its project set to the shared picker, built from
  the archived sessions' directories (deduped and sorted by name), so the
  sheet no longer lists every server project
- the restricted picker hides the add-project action: the archive page never
  archives anything, so adding a project there would be pointless
- the picker gained an optional onlyProjects argument; chat archives remain
  reachable through the Chat row
- version 0.6.2+602
2026-11-27 13:36:11 +08:00

285 lines
10 KiB
Dart

// Choosing a project, shared by the new-session draft and scheduled tasks.
// The sheet opens at once and refreshes the project list behind it: a tap on
// the project chip should never wait for the server before anything appears.
import 'package:flutter/material.dart';
import '../api/models.dart';
import '../l10n.dart';
import '../state.dart';
import 'directory_picker.dart';
import 'project_icon.dart';
/// The "All projects" row of the stats scope picker, where a report is not
/// narrowed to one checkout.
const allProjectsTarget = '\u0000all-projects';
/// What the picker returns: the Chat row, the All projects row, or a checkout.
class ProjectChoice {
/// Null for the Chat and All projects rows.
final ProjectInfo? project;
final bool all;
const ProjectChoice.chat()
: project = null,
all = false;
const ProjectChoice.allProjects()
: project = null,
all = true;
const ProjectChoice.project(this.project) : all = false;
bool get isChat => project == null && !all;
bool get isAll => all;
String get canonical =>
all ? allProjectsTarget : (project?.canonical ?? AppState.chatTarget);
String get label => all ? tr('All projects') : (project?.label ?? tr('Chat'));
}
/// Opens the project sheet and returns the chosen target, or null when the
/// person dismissed it. [selected] is a checkout path, or [AppState.chatTarget]
/// for the Chat row. [onlyProjects] limits the list to exactly those projects
/// (the archive page offers only projects that hold archived sessions, since
/// it never archives anything itself); null lists every selectable checkout.
Future<ProjectChoice?> showProjectPicker(
BuildContext context, {
required AppState app,
required String selected,
bool includeAll = false,
bool includeChat = true,
List<ProjectInfo>? onlyProjects,
}) {
return showModalBottomSheet<ProjectChoice>(
context: context,
isScrollControlled: true,
builder: (context) => DraggableScrollableSheet(
expand: false,
initialChildSize: 0.6,
maxChildSize: 0.9,
builder: (context, controller) => _ProjectPickerSheet(
app: app,
selected: selected,
includeAll: includeAll,
includeChat: includeChat,
projects: onlyProjects,
controller: controller,
),
),
);
}
/// The projects a picker offers: checkouts, not the server's chat folders.
/// The server can list the same path twice (a project record and a sandbox),
/// so one path is one row, preferring the one that knows its VCS.
List<ProjectInfo> selectableProjects(AppState app) {
final byPath = <String, ProjectInfo>{};
for (final project in app.projects) {
if (project.canonical.isEmpty) continue;
if (app.isChatDirectory(project.canonical)) continue;
final existing = byPath[project.canonical];
if (existing == null ||
(existing.vcs.isEmpty && project.vcs.isNotEmpty)) {
byPath[project.canonical] = project;
}
}
return byPath.values.toList();
}
class _ProjectPickerSheet extends StatefulWidget {
final AppState app;
final String selected;
final bool includeAll;
final bool includeChat;
/// Null offers every selectable checkout; a list limits the picker to it
/// (the archive page) and hides the add-project action.
final List<ProjectInfo>? projects;
final ScrollController controller;
const _ProjectPickerSheet({
required this.app,
required this.selected,
required this.includeAll,
required this.includeChat,
required this.projects,
required this.controller,
});
@override
State<_ProjectPickerSheet> createState() => _ProjectPickerSheetState();
}
class _ProjectPickerSheetState extends State<_ProjectPickerSheet> {
/// True until the list behind the sheet has been refreshed. The sheet is
/// already up by then; only its body shows the spinner.
bool _refreshing = true;
final TextEditingController _searchCtrl = TextEditingController();
String _filter = '';
@override
void initState() {
super.initState();
_refresh();
}
@override
void dispose() {
_searchCtrl.dispose();
super.dispose();
}
Future<void> _refresh() async {
await Future.wait([
widget.app.refreshProjects(),
widget.app.refreshFsHome(),
]);
if (!mounted) return;
setState(() => _refreshing = false);
}
/// Browses the server's folders and returns with the new project selected.
Future<void> _addProject() async {
final path = await showDirectoryPicker(context, app: widget.app);
if (path == null || !mounted) return;
final project = widget.app.projectFor(path) ??
ProjectInfo(id: '', canonical: path, name: pathLeaf(path));
Navigator.pop(context, ProjectChoice.project(project));
}
@override
Widget build(BuildContext context) {
final chat = widget.selected == AppState.chatTarget;
// A cached list is shown at once; the refresh updates it in place.
final projects = widget.projects ?? selectableProjects(widget.app);
final query = _filter.trim().toLowerCase();
final visible = query.isEmpty
? projects
: [
for (final project in projects)
if (project.label.toLowerCase().contains(query) ||
project.canonical.toLowerCase().contains(query))
project,
];
final loading = _refreshing && projects.isEmpty;
return Column(
children: [
Padding(
padding: const EdgeInsets.fromLTRB(16, 6, 8, 0),
child: Row(
children: [
const Expanded(
child: AppText('Choose project',
style:
TextStyle(fontSize: 17, fontWeight: FontWeight.w600)),
),
if (widget.projects == null)
TextButton.icon(
onPressed: _addProject,
style: TextButton.styleFrom(
visualDensity: VisualDensity.compact,
padding: const EdgeInsets.symmetric(horizontal: 8)),
icon: const Icon(Icons.create_new_folder_outlined, size: 18),
label: const AppText('Add project'),
),
],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(12, 2, 12, 6),
child: TextField(
controller: _searchCtrl,
onChanged: (value) => setState(() => _filter = value),
decoration: InputDecoration(
hintText: tr('Search projects'),
prefixIcon: const Icon(Icons.search, size: 18),
isDense: true,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10)),
),
),
),
Expanded(
child: loading
? const Center(
child: Padding(
padding: EdgeInsets.all(24),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
width: 22,
height: 22,
child: CircularProgressIndicator(strokeWidth: 2)),
SizedBox(height: 12),
AppText('Loading projects…'),
],
),
),
)
: ListView(
controller: widget.controller,
children: [
if (widget.includeAll)
ListTile(
dense: true,
leading: Icon(Icons.folder_copy_outlined,
size: 20,
color: Theme.of(context).colorScheme.onSurfaceVariant),
title: const AppText('All projects'),
trailing: widget.selected == allProjectsTarget
? Icon(Icons.check,
size: 18,
color: Theme.of(context).colorScheme.primary)
: null,
onTap: () => Navigator.pop(
context, const ProjectChoice.allProjects()),
),
if (widget.includeChat)
ListTile(
dense: true,
leading:
ProjectIcon(app: widget.app, chat: true, size: 20),
title: const AppText('Chat'),
subtitle: const AppText(
'Not tied to a project, saved in OpenChamber\'s chat directory'),
trailing: chat
? Icon(Icons.check,
size: 18,
color: Theme.of(context).colorScheme.primary)
: null,
onTap: () =>
Navigator.pop(context, const ProjectChoice.chat()),
),
for (final project in visible)
ListTile(
dense: true,
leading: ProjectIcon(
app: widget.app, project: project, size: 20),
title: Text(project.label,
maxLines: 1, overflow: TextOverflow.ellipsis),
subtitle: Text(project.canonical,
maxLines: 1, overflow: TextOverflow.ellipsis),
trailing: samePath(project.canonical, widget.selected)
? Icon(Icons.check,
size: 18,
color: Theme.of(context).colorScheme.primary)
: null,
onTap: () => Navigator.pop(
context, ProjectChoice.project(project)),
),
if (!_refreshing && visible.isEmpty)
Padding(
padding: const EdgeInsets.all(24),
child: AppText(
query.isEmpty
? 'No projects available'
: 'No matching projects',
textAlign: TextAlign.center),
),
],
),
),
],
);
}
}