perf: avoid redundant tab refresh traffic

This commit is contained in:
Lukas Müllner 2026-03-05 08:34:14 +01:00
parent 33e24bd500
commit 83344a06d5
4 changed files with 26 additions and 6 deletions

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import '../main.dart'; import '../main.dart';
import '../services/collection_service.dart'; import '../services/collection_service.dart';
import '../services/main_collection_sync.dart';
import '../theme/app_colors.dart'; import '../theme/app_colors.dart';
import 'garage_screen.dart'; import 'garage_screen.dart';
import 'manage_collection_screen.dart'; import 'manage_collection_screen.dart';
@ -172,6 +173,7 @@ class CollectionsScreenState extends State<CollectionsScreen> {
await prefs.setString(_activeCollectionPrefKey, collectionId); await prefs.setString(_activeCollectionPrefKey, collectionId);
if (!mounted) return; if (!mounted) return;
setState(() => _activeCollectionId = collectionId); setState(() => _activeCollectionId = collectionId);
MainCollectionSync.notifyChanged();
showGlobalSnackBar('Main collection set for scanning.'); showGlobalSnackBar('Main collection set for scanning.');
} }

View file

@ -24,12 +24,6 @@ class _HomeShellState extends State<HomeShell> {
void _onTabSelected(int i) { void _onTabSelected(int i) {
setState(() => _currentIndex = i); setState(() => _currentIndex = i);
if (i == 0) {
_collectionsKey.currentState?.refresh();
} else if (i == 1) {
_scanKey.currentState?.refresh();
}
} }
@override @override

View file

@ -3,6 +3,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import '../main.dart'; import '../main.dart';
import '../scanner_screen.dart'; import '../scanner_screen.dart';
import '../services/collection_service.dart'; import '../services/collection_service.dart';
import '../services/main_collection_sync.dart';
import '../theme/app_colors.dart'; import '../theme/app_colors.dart';
class ScanTab extends StatefulWidget { class ScanTab extends StatefulWidget {
@ -26,11 +27,23 @@ class ScanTabState extends State<ScanTab> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
MainCollectionSync.changeToken.addListener(_handleMainCollectionChanged);
_loadCollections(); _loadCollections();
} }
@override
void dispose() {
MainCollectionSync.changeToken.removeListener(_handleMainCollectionChanged);
super.dispose();
}
void refresh() => _loadCollections(); void refresh() => _loadCollections();
void _handleMainCollectionChanged() {
if (!mounted) return;
_loadCollections();
}
Future<void> _loadCollections() async { Future<void> _loadCollections() async {
try { try {
final list = await CollectionService.getMyCollections(); final list = await CollectionService.getMyCollections();

View file

@ -0,0 +1,11 @@
import 'package:flutter/foundation.dart';
class MainCollectionSync {
MainCollectionSync._();
static final ValueNotifier<int> changeToken = ValueNotifier<int>(0);
static void notifyChanged() {
changeToken.value = changeToken.value + 1;
}
}