178 lines
6.2 KiB
Dart
178 lines
6.2 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 'project_icon.dart';
|
||
|
|
|
||
|
|
/// What the picker returns: the Chat row or a project checkout.
|
||
|
|
class ProjectChoice {
|
||
|
|
/// Null for the Chat row: a chat is not tied to a project.
|
||
|
|
final ProjectInfo? project;
|
||
|
|
|
||
|
|
const ProjectChoice.chat() : project = null;
|
||
|
|
const ProjectChoice.project(this.project);
|
||
|
|
|
||
|
|
bool get isChat => project == null;
|
||
|
|
String get canonical => project?.canonical ?? AppState.chatTarget;
|
||
|
|
String get label => 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,
|
||
|
|
}) {
|
||
|
|
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,
|
||
|
|
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 ScrollController controller;
|
||
|
|
|
||
|
|
const _ProjectPickerSheet({
|
||
|
|
required this.app,
|
||
|
|
required this.selected,
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
|
||
|
|
@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: [
|
||
|
|
const Padding(
|
||
|
|
padding: EdgeInsets.fromLTRB(16, 14, 16, 6),
|
||
|
|
child: AppText('Choose project',
|
||
|
|
style: TextStyle(fontSize: 17, fontWeight: FontWeight.w600)),
|
||
|
|
),
|
||
|
|
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: [
|
||
|
|
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),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
),
|
||
|
|
),
|
||
|
|
],
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|