perf: avoid redundant tab refresh traffic
This commit is contained in:
parent
33e24bd500
commit
83344a06d5
4 changed files with 26 additions and 6 deletions
|
|
@ -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<CollectionsScreen> {
|
|||
await prefs.setString(_activeCollectionPrefKey, collectionId);
|
||||
if (!mounted) return;
|
||||
setState(() => _activeCollectionId = collectionId);
|
||||
MainCollectionSync.notifyChanged();
|
||||
showGlobalSnackBar('Main collection set for scanning.');
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -24,12 +24,6 @@ class _HomeShellState extends State<HomeShell> {
|
|||
|
||||
void _onTabSelected(int i) {
|
||||
setState(() => _currentIndex = i);
|
||||
|
||||
if (i == 0) {
|
||||
_collectionsKey.currentState?.refresh();
|
||||
} else if (i == 1) {
|
||||
_scanKey.currentState?.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
|||
|
|
@ -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<ScanTab> {
|
|||
@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<void> _loadCollections() async {
|
||||
try {
|
||||
final list = await CollectionService.getMyCollections();
|
||||
|
|
|
|||
11
lib/services/main_collection_sync.dart
Normal file
11
lib/services/main_collection_sync.dart
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue