Files
openchamber-android-native/lib/widgets/project_picker.dart
T
blumia c6b5a870c6 feat: add a project from the picker, and keep the context ring
- the project picker has an "Add project" action: a folder browser starts
  at the server user's home, navigates (typed path, up, new folder) and
  adds the chosen folder through POST /api/openchamber/directory; the new
  project becomes the draft's target at once
- refreshProjects merges settings-only projects (newest added first), so a
  project added before its first session still appears in the picker; the
  first session moves it into /api/project as usual
- usageReading no longer hides the composer's context ring when the
  session's model has left the server catalog: a 200K window is assumed,
  the percentage is prefixed with "~", and the usage sheet says the window
  is estimated. A failed model fetch at connect is retried when a chat
  opens, so a normal session shows the real percentage
- tests: the add-project browser flow, settings-only projects, and the
  estimated/known context readings
2026-11-27 09:26:14 +08:00

240 lines
8.6 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.
Future<ProjectChoice?> showProjectPicker(
BuildContext context, {
required AppState app,
required String selected,
bool includeAll = false,
bool includeChat = true,
}) {
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,
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;
final ScrollController controller;
const _ProjectPickerSheet({
required this.app,
required this.selected,
required this.includeAll,
required this.includeChat,
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;
@override
void initState() {
super.initState();
_refresh();
}
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 = selectableProjects(widget.app);
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)),
),
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'),
),
],
),
),
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 projects)
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 && projects.isEmpty)
const Padding(
padding: EdgeInsets.all(24),
child: AppText('No projects available',
textAlign: TextAlign.center),
),
],
),
),
],
);
}
}