From 83344a06d5a18058145e6c262b8c4be7aa6b5297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20M=C3=BCllner?= Date: Thu, 5 Mar 2026 08:34:14 +0100 Subject: [PATCH] perf: avoid redundant tab refresh traffic --- lib/screens/collections_screen.dart | 2 ++ lib/screens/home_shell.dart | 6 ------ lib/screens/scan_tab.dart | 13 +++++++++++++ lib/services/main_collection_sync.dart | 11 +++++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 lib/services/main_collection_sync.dart diff --git a/lib/screens/collections_screen.dart b/lib/screens/collections_screen.dart index d57c9e3..4d75ad2 100644 --- a/lib/screens/collections_screen.dart +++ b/lib/screens/collections_screen.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../main.dart'; import '../services/collection_service.dart'; +import '../services/main_collection_sync.dart'; import '../theme/app_colors.dart'; import 'garage_screen.dart'; import 'manage_collection_screen.dart'; @@ -172,6 +173,7 @@ class CollectionsScreenState extends State { await prefs.setString(_activeCollectionPrefKey, collectionId); if (!mounted) return; setState(() => _activeCollectionId = collectionId); + MainCollectionSync.notifyChanged(); showGlobalSnackBar('Main collection set for scanning.'); } diff --git a/lib/screens/home_shell.dart b/lib/screens/home_shell.dart index f25e32c..900bb73 100644 --- a/lib/screens/home_shell.dart +++ b/lib/screens/home_shell.dart @@ -24,12 +24,6 @@ class _HomeShellState extends State { void _onTabSelected(int i) { setState(() => _currentIndex = i); - - if (i == 0) { - _collectionsKey.currentState?.refresh(); - } else if (i == 1) { - _scanKey.currentState?.refresh(); - } } @override diff --git a/lib/screens/scan_tab.dart b/lib/screens/scan_tab.dart index e2b2185..6281fdd 100644 --- a/lib/screens/scan_tab.dart +++ b/lib/screens/scan_tab.dart @@ -3,6 +3,7 @@ import 'package:shared_preferences/shared_preferences.dart'; import '../main.dart'; import '../scanner_screen.dart'; import '../services/collection_service.dart'; +import '../services/main_collection_sync.dart'; import '../theme/app_colors.dart'; class ScanTab extends StatefulWidget { @@ -26,11 +27,23 @@ class ScanTabState extends State { @override void initState() { super.initState(); + MainCollectionSync.changeToken.addListener(_handleMainCollectionChanged); _loadCollections(); } + @override + void dispose() { + MainCollectionSync.changeToken.removeListener(_handleMainCollectionChanged); + super.dispose(); + } + void refresh() => _loadCollections(); + void _handleMainCollectionChanged() { + if (!mounted) return; + _loadCollections(); + } + Future _loadCollections() async { try { final list = await CollectionService.getMyCollections(); diff --git a/lib/services/main_collection_sync.dart b/lib/services/main_collection_sync.dart new file mode 100644 index 0000000..5a53e74 --- /dev/null +++ b/lib/services/main_collection_sync.dart @@ -0,0 +1,11 @@ +import 'package:flutter/foundation.dart'; + +class MainCollectionSync { + MainCollectionSync._(); + + static final ValueNotifier changeToken = ValueNotifier(0); + + static void notifyChanged() { + changeToken.value = changeToken.value + 1; + } +}