Skip to content
Open
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
20 changes: 17 additions & 3 deletions lib/ui/pages/landing_page/landing_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class _LandingPageState extends ConsumerState<LandingPage> {
late ProjectDatabase database;
late IFileService fileService;
late IImageService imageService;
Future<List<Project>>? _projectsFuture;
ProjectDatabase? _projectsLoadedFrom;

@override
void initState() {
Expand Down Expand Up @@ -96,9 +98,15 @@ class _LandingPageState extends ConsumerState<LandingPage> {
return database.projectDAO.getProjects();
}

void _refreshProjectsFuture() {
_projectsFuture = _getProjects();
}

Future<void> _navigateToPocketPaint() async {
await Navigator.pushNamed(context, '/PocketPaint');
if (mounted){setState(() {});}
if (mounted) {
setState(_refreshProjectsFuture);
}
}

Future<bool> _loadProject(IOHandler ioHandler, Project project) async {
Expand Down Expand Up @@ -141,7 +149,13 @@ class _LandingPageState extends ConsumerState<LandingPage> {

final db = ref.watch(ProjectDatabase.provider);
db.when(
data: (value) => database = value,
data: (value) {
database = value;
if (!identical(_projectsLoadedFrom, value)) {
_projectsLoadedFrom = value;
_refreshProjectsFuture();
}
},
error: (err, stacktrace) =>
ToastUtils.showShortToast(message: 'Error: $err'),
loading: () {},
Expand All @@ -158,7 +172,7 @@ class _LandingPageState extends ConsumerState<LandingPage> {
actions: const [MainOverflowMenu()],
),
body: FutureBuilder(
future: _getProjects(),
future: _projectsFuture,
builder: (BuildContext context, AsyncSnapshot<List<Project>> snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
Expand Down
25 changes: 25 additions & 0 deletions test/widget/landing_page/landing_page_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'dart:ui' as ui;

Expand Down Expand Up @@ -738,4 +739,28 @@ void main() {
expect(find.byType(CircularProgressIndicator), findsOneWidget);
},
);

testWidgets(
'Should reuse the projects future across rebuilds while the list is loading',
(tester) async {
final projectsCompleter = Completer<List<Project>>();
when(database.projectDAO).thenReturn(dao);
when(dao.getProjects()).thenAnswer((_) => projectsCompleter.future);

await tester.pumpWidget(sut);
await tester.pump();

expect(find.byType(CircularProgressIndicator), findsWidgets);

await tester.pump();
await tester.pump(const Duration(milliseconds: 16));

expect(find.byType(CircularProgressIndicator), findsWidgets);

projectsCompleter.complete([]);
await tester.pumpAndSettle();

verify(dao.getProjects()).called(1);
},
);
}