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
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadNotes();
|
_loadNotesAndCategories();
|
||||||
_loadCategories();
|
}
|
||||||
|
|
||||||
|
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 {
|
Future<void> _loadCategories() async {
|
||||||
@@ -118,8 +161,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
void didUpdateWidget(covariant HomeScreen oldWidget) {
|
void didUpdateWidget(covariant HomeScreen oldWidget) {
|
||||||
super.didUpdateWidget(oldWidget);
|
super.didUpdateWidget(oldWidget);
|
||||||
if (oldWidget.refreshToken != widget.refreshToken) {
|
if (oldWidget.refreshToken != widget.refreshToken) {
|
||||||
_loadNotes();
|
// Refresh in background without showing the full-screen loader
|
||||||
_loadCategories();
|
_loadNotesAndCategories(showLoadingIndicator: false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,6 +369,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
|||||||
: RefreshIndicator(
|
: RefreshIndicator(
|
||||||
onRefresh: () async {
|
onRefresh: () async {
|
||||||
await widget.onRequestSync();
|
await widget.onRequestSync();
|
||||||
|
await _loadNotesAndCategories(showLoadingIndicator: false);
|
||||||
},
|
},
|
||||||
child: MouseRegion(
|
child: MouseRegion(
|
||||||
cursor: _isDragging
|
cursor: _isDragging
|
||||||
|
|||||||
Reference in New Issue
Block a user