Files
openchamber-android-native/lib/widgets/project_picker.dart
T

214 lines
7.6 KiB
Dart
Raw Normal View History

// 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';
/// 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);
}
@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: [
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),
),
],
),
),
],
);
}
}