feat: Consolidate note and category loading into a single method for improved efficiency
This commit is contained in:
@@ -83,8 +83,51 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadNotes();
|
||||
_loadCategories();
|
||||
_loadNotesAndCategories();
|
||||
}
|
||||
|
||||
Future<void> _loadNotesAndCategories({bool showLoadingIndicator = true}) async {
|
||||
if (showLoadingIndicator) {
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
}
|
||||
|
||||
final Future<List<Note>> notesFuture = _showDeletedNotes
|
||||
? widget.repository.loadDeletedNotes()
|
||||
: widget.repository.loadNotes();
|
||||
final Future<List<Category>> categoriesFuture =
|
||||
widget.repository.loadCategories();
|
||||
|
||||
List<Note> notesResult = <Note>[];
|
||||
List<Category> categoriesResult = <Category>[];
|
||||
|
||||
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 = <Category>[];
|
||||
}
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
setState(() {
|
||||
_notes = notesResult;
|
||||
_categories = categoriesResult;
|
||||
if (showLoadingIndicator) {
|
||||
_isLoading = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _loadCategories() async {
|
||||
@@ -118,8 +161,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
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<HomeScreen> {
|
||||
: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
await widget.onRequestSync();
|
||||
await _loadNotesAndCategories(showLoadingIndicator: false);
|
||||
},
|
||||
child: MouseRegion(
|
||||
cursor: _isDragging
|
||||
|
||||
Reference in New Issue
Block a user