From d7495a461a447da80c00f1409cd91d26cc04249a Mon Sep 17 00:00:00 2001 From: Marcos Date: Thu, 21 May 2026 19:37:21 +0200 Subject: [PATCH] feat: Consolidate note and category loading into a single method for improved efficiency --- lib/screens/home_screen.dart | 52 +++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index 9826ea1..a14b9c6 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -83,8 +83,51 @@ class _HomeScreenState extends State { @override void initState() { super.initState(); - _loadNotes(); - _loadCategories(); + _loadNotesAndCategories(); + } + + Future _loadNotesAndCategories({bool showLoadingIndicator = true}) async { + if (showLoadingIndicator) { + setState(() { + _isLoading = true; + }); + } + + final Future> notesFuture = _showDeletedNotes + ? widget.repository.loadDeletedNotes() + : widget.repository.loadNotes(); + final Future> categoriesFuture = + widget.repository.loadCategories(); + + List notesResult = []; + List categoriesResult = []; + + try { + notesResult = await notesFuture; + } catch (e, st) { + debugPrint('Failed to load notes: $e\n$st'); + if (widget.onVaultInvalid != null) { + await widget.onVaultInvalid!(); + } + return; + } + + try { + categoriesResult = await categoriesFuture; + } catch (e) { + debugPrint('Failed to load categories: $e'); + categoriesResult = []; + } + + if (!mounted) return; + + setState(() { + _notes = notesResult; + _categories = categoriesResult; + if (showLoadingIndicator) { + _isLoading = false; + } + }); } Future _loadCategories() async { @@ -118,8 +161,8 @@ class _HomeScreenState extends State { void didUpdateWidget(covariant HomeScreen oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.refreshToken != widget.refreshToken) { - _loadNotes(); - _loadCategories(); + // Refresh in background without showing the full-screen loader + _loadNotesAndCategories(showLoadingIndicator: false); } } @@ -326,6 +369,7 @@ class _HomeScreenState extends State { : RefreshIndicator( onRefresh: () async { await widget.onRequestSync(); + await _loadNotesAndCategories(showLoadingIndicator: false); }, child: MouseRegion( cursor: _isDragging