Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/feature/project/parts/project_list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@ class ProjectListTile extends StatelessWidget {
final theme = Theme.of(context);
final colorScheme = theme.colorScheme;

// A tinted background alone is easy to miss, and a pack icon hides the
// filled folder that marks the selection otherwise. The bar on the left
// makes it clear which project Open Project opens. Unselected tiles keep
// a transparent bar, so their content doesn't shift on selection.
return Material(
color: isSelected
? colorScheme.surfaceContainerHighest.withValues(alpha: 0.5)
? colorScheme.primaryContainer.withValues(alpha: 0.35)
: Colors.transparent,
shape: Border(
left: BorderSide(
color: isSelected ? colorScheme.primary : Colors.transparent,
width: 3,
),
),
child: ListTile(
dense: true,
contentPadding: const EdgeInsets.symmetric(horizontal: 12, vertical: 2),
Expand Down
13 changes: 12 additions & 1 deletion lib/feature/project/parts/project_selection_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ class ProjectSelectionCard extends StatelessWidget {
final VoidCallback onCreateProject;
final VoidCallback onProceed;

/// Loads the projects again, null hides the refresh button.
final VoidCallback? onRefresh;

/// Whether a refresh is under way.
final bool isRefreshing;

/// Whether the projects are still loading, which shows a placeholder in
/// place of the list.
final bool isLoading;
Expand All @@ -33,6 +39,8 @@ class ProjectSelectionCard extends StatelessWidget {
required this.onCreateProject,
required this.onProceed,
this.isLoading = false,
this.onRefresh,
this.isRefreshing = false,
this.packIconOf,
super.key,
});
Expand All @@ -55,7 +63,10 @@ class ProjectSelectionCard extends StatelessWidget {
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const ProjectSelectionHeader(),
ProjectSelectionHeader(
onRefresh: isLoading ? null : onRefresh,
isRefreshing: isRefreshing,
),
const Divider(height: 32),
if (isLoading) ...[
const ProjectListSkeleton(),
Expand Down
49 changes: 42 additions & 7 deletions lib/feature/project/parts/project_selection_header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,53 @@ import 'package:material_ui/material_ui.dart';
import 'package:stelaris/util/l10n_ext.dart';

class ProjectSelectionHeader extends StatelessWidget {
const ProjectSelectionHeader({super.key});
const ProjectSelectionHeader({
this.onRefresh,
this.isRefreshing = false,
super.key,
});

/// Loads the projects again, null hides the refresh button.
final VoidCallback? onRefresh;

/// Whether a refresh is under way, which disables the button.
final bool isRefreshing;

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);

return Text(
context.l10n.project_selection_title,
textAlign: TextAlign.center,
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
return Stack(
alignment: Alignment.center,
children: [
// Padded on both sides, so a long title stays centered and clear of
// the button.
Padding(
padding: const EdgeInsets.symmetric(horizontal: 48),
child: Text(
context.l10n.project_selection_title,
textAlign: TextAlign.center,
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
),
if (onRefresh != null)
Align(
alignment: Alignment.centerRight,
child: IconButton(
tooltip: context.l10n.project_selection_refresh_tooltip,
onPressed: isRefreshing ? null : onRefresh,
// Same size as the icon, so the button doesn't jump.
icon: isRefreshing
? const SizedBox.square(
dimension: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.refresh),
),
),
],
);
}
}
17 changes: 17 additions & 0 deletions lib/feature/project/project_selection_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class _ProjectSelectionPageState extends State<ProjectSelectionPage> {
/// unavailable view up meanwhile instead of flashing the selection.
bool _retrying = false;

/// Whether the projects are loaded again on request while they are shown.
bool _refreshing = false;

/// The pack icon of each project by its id, loaded once while the page is
/// open and again after the project was edited.
final Map<String, Future<ProjectPackIcon?>> _packIcons = {};
Expand Down Expand Up @@ -117,6 +120,8 @@ class _ProjectSelectionPageState extends State<ProjectSelectionPage> {
_openEditDialog(context, project),
onCreateProject: () => _openCreateDialog(context),
onProceed: () => _proceedWithProject(vm, vm.selected!),
onRefresh: _refresh,
isRefreshing: _refreshing,
packIconOf: _packIconOf,
),
],
Expand Down Expand Up @@ -147,6 +152,18 @@ class _ProjectSelectionPageState extends State<ProjectSelectionPage> {
setState(() => _retrying = false);
}

/// Loads the projects again, for example after someone else created one.
/// The pack icons load again as well, they may have changed meanwhile.
Future<void> _refresh() async {
setState(() {
_refreshing = true;
_packIcons.clear();
});
await _loadProjects((action) => context.dispatchAndWait(action));
if (!mounted) return;
setState(() => _refreshing = false);
}

/// Opens the create dialog. A created project is selected, and a snackbar
/// offers to set up its pack right away.
Future<void> _openCreateDialog(BuildContext context) async {
Expand Down
6 changes: 6 additions & 0 deletions lib/l10n/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,12 @@ abstract class AppLocalizations {
/// **'Loading projects'**
String get project_selection_loading_label;

/// No description provided for @project_selection_refresh_tooltip.
///
/// In en, this message translates to:
/// **'Refresh projects'**
String get project_selection_refresh_tooltip;

/// No description provided for @dialog_project_create_title.
///
/// In en, this message translates to:
Expand Down
3 changes: 3 additions & 0 deletions lib/l10n/app_localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get project_selection_loading_label => 'Loading projects';

@override
String get project_selection_refresh_tooltip => 'Refresh projects';

@override
String get dialog_project_create_title => 'Create new project';

Expand Down
1 change: 1 addition & 0 deletions lib/l10n/stelaris_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@
"project_selection_unavailable_message": "Stelaris could not reach its server. Check that the backend is running, then try again.",
"project_selection_unavailable_retry": "Try again",
"project_selection_loading_label": "Loading projects",
"project_selection_refresh_tooltip": "Refresh projects",
"dialog_project_create_title": "Create new project",
"dialog_project_display_name": "Display Name",
"dialog_project_key": "Key / Namespace",
Expand Down
63 changes: 63 additions & 0 deletions test/feature/project/parts/project_list_tile_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:material_ui/material_ui.dart';
import 'package:stelaris/feature/project/parts/project_list_tile.dart';
import 'package:stelaris/l10n/app_localizations.dart';
import 'package:stelaris/l10n/app_localizations_delegates.dart';
import 'package:stelaris_models/stelaris_models.dart';

const _project = Project(id: 'p-1', displayName: 'Vulpes', key: 'vulpes');

Future<ColorScheme> _pump(WidgetTester tester, {required bool selected}) async {
await tester.pumpWidget(
MaterialApp(
localizationsDelegates: appLocalizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: ProjectListTile(
project: _project,
isSelected: selected,
onSelect: () {},
onEdit: () {},
),
),
),
);
return Theme.of(tester.element(find.byType(ProjectListTile))).colorScheme;
}

Material _tileMaterial(WidgetTester tester) => tester.widget<Material>(
find
.descendant(
of: find.byType(ProjectListTile),
matching: find.byType(Material),
)
.first,
);

void main() {
testWidgets('marks the selected project with a bar on the left', (
tester,
) async {
final colorScheme = await _pump(tester, selected: true);

final material = _tileMaterial(tester);
final border = material.shape! as Border;
expect(border.left.color, colorScheme.primary);
expect(border.left.width, 3);
expect(material.color, isNot(Colors.transparent));
});

testWidgets('keeps the bar transparent when not selected', (tester) async {
await _pump(tester, selected: false);

final material = _tileMaterial(tester);
final border = material.shape! as Border;
expect(border.left.color, Colors.transparent);
expect(
border.left.width,
3,
reason: 'the same width keeps the content from shifting',
);
expect(material.color, Colors.transparent);
});
}
102 changes: 102 additions & 0 deletions test/feature/project/project_selection_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,108 @@ void main() {
});
});

group('ProjectSelectionPage refresh', () {
const projectA = Project(
id: 'test_id_a',
displayName: 'Test Project A',
key: 'test_key_a',
);
const projectB = Project(
id: 'test_id_b',
displayName: 'Test Project B',
key: 'test_key_b',
);

void serveProjects(List<Project> projects) {
ApiService().projectApi.apiClient.dio.httpClientAdapter =
FakeHttpClientAdapter.json(
PaginatedResult<Project>(
items: projects,
totalItems: projects.length,
totalPages: 1,
currentPage: 1,
pageSize: 50,
).toJson((p) => p.toJson()),
);
}

Future<void> pumpPage(
WidgetTester tester, {
ProjectPackIconLoader? loadPackIcon,
}) async {
await tester.pumpWidget(
StoreProvider<AppState>(
store: Store<AppState>(initialState: const AppState()),
child: MaterialApp(
localizationsDelegates: appLocalizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: ProjectSelectionPage(
loadPackIcon: loadPackIcon ?? (_) async => null,
),
),
),
);
await tester.pumpAndSettle();
}

testWidgets('shows a project created elsewhere meanwhile', (tester) async {
serveProjects([projectA]);
final loaded = <String>[];
await pumpPage(
tester,
loadPackIcon: (id) async {
loaded.add(id);
return null;
},
);
expect(find.text('Test Project B'), findsNothing);

serveProjects([projectA, projectB]);
await tester.tap(find.byTooltip('Refresh projects'));
await tester.pumpAndSettle();

expect(find.text('Test Project A'), findsOneWidget);
expect(find.text('Test Project B'), findsOneWidget);
expect(
loaded,
['test_id_a', 'test_id_a', 'test_id_b'],
reason: 'the pack icons load again',
);
});

testWidgets('is offered on the empty page too', (tester) async {
serveProjects([]);
await pumpPage(tester);
expect(find.text('No projects found'), findsOneWidget);

serveProjects([projectA]);
await tester.tap(find.byTooltip('Refresh projects'));
await tester.pumpAndSettle();

expect(find.text('No projects found'), findsNothing);
expect(find.text('Test Project A'), findsOneWidget);
});

testWidgets('shows the unavailable page when the refresh fails', (
tester,
) async {
serveProjects([projectA]);
await pumpPage(tester);

ApiService().projectApi.apiClient.dio.httpClientAdapter =
FakeHttpClientAdapter(
(options) => throw DioException.connectionError(
requestOptions: options,
reason: 'Connection refused',
),
);
await tester.tap(find.byTooltip('Refresh projects'));
await tester.pumpAndSettle();

expect(find.text('Server not reachable'), findsOneWidget);
});
});

group('ProjectSelectionPage without a backend', () {
const project = Project(
id: 'test_id_a',
Expand Down
Loading