Skip to content

Commit bd5667b

Browse files
committed
cache
1 parent dc7a2ee commit bd5667b

File tree

5 files changed

+76
-50
lines changed

5 files changed

+76
-50
lines changed

lib/common/tray.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ class Tray {
105105
subMenuItems.add(
106106
MenuItem.checkbox(
107107
label: proxy.name,
108-
checked: trayState.selectedMap[group.name] == proxy.name,
108+
checked:
109+
globalState.getSelectedProxyName(group.name) == proxy.name,
109110
onClick: (_) {
110111
final appController = globalState.appController;
111112
appController.updateCurrentSelectedMap(group.name, proxy.name);

lib/providers/generated/state.g.dart

Lines changed: 13 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/providers/state.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,16 @@ GroupsState currentGroupsState(Ref ref) {
5151
final mode = ref.watch(
5252
patchClashConfigProvider.select((state) => state.mode),
5353
);
54-
final groups = ref.watch(groupsProvider);
54+
final groups = ref.watch(
55+
groupsProvider.select(
56+
(state) => state.map((item) {
57+
return item.copyWith(
58+
now: '',
59+
all: item.all.map((proxy) => proxy.copyWith(now: '')).toList(),
60+
);
61+
}),
62+
),
63+
);
5564
return GroupsState(
5665
value: switch (mode) {
5766
Mode.direct => [],
@@ -290,7 +299,7 @@ GroupsState filterGroupsState(Ref ref, String query) {
290299
})
291300
.where((group) => group.all.isNotEmpty)
292301
.toList();
293-
return GroupsState(value: groups);
302+
return currentGroups.copyWith(value: groups);
294303
}
295304

296305
@riverpod
@@ -635,12 +644,15 @@ VM2<bool, bool> autoSetSystemDnsState(Ref ref) {
635644
}
636645

637646
@riverpod
638-
VM2<int, ProxiesSortType> needUpdateGroups(Ref ref) {
647+
VM3<bool, int, ProxiesSortType> needUpdateGroups(Ref ref) {
648+
final isProxies = ref.watch(
649+
currentPageLabelProvider.select((state) => state == PageLabel.proxies),
650+
);
639651
final sortNum = ref.watch(sortNumProvider);
640652
final sortType = ref.watch(
641653
proxiesStyleSettingProvider.select((state) => state.sortType),
642654
);
643-
return VM2(a: sortNum, b: sortType);
655+
return VM3(a: isProxies, b: sortNum, c: sortType);
644656
}
645657

646658
@riverpod

lib/state.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ class GlobalState {
367367
);
368368
}
369369

370+
String getSelectedProxyName(String groupName) {
371+
final group = appState.groups.getGroup(groupName);
372+
final proxyName = config.currentProfile?.selectedMap[groupName];
373+
return group?.getCurrentSelectedName(proxyName ?? '') ?? '';
374+
}
375+
370376
Future<String> setupConfig({
371377
required SetupState setupState,
372378
required ClashConfig patchConfig,

lib/views/proxies/tab.dart

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ class ProxiesTabViewState extends ConsumerState<ProxiesTabView>
4040
}
4141
if (!stringListEquality.equals(prev?.a, next.a)) {
4242
_destroyTabController();
43-
final index = next.a.indexWhere((item) => item == next.b);
44-
_updateTabController(next.a.length, index);
43+
final groupNames = next.a;
44+
final currentGroupName = next.b;
45+
final index = groupNames.indexWhere((item) => item == currentGroupName);
46+
_updateTabController(groupNames.length, index);
4547
}
4648
}, fireImmediately: true);
4749
}
@@ -127,22 +129,22 @@ class ProxiesTabViewState extends ConsumerState<ProxiesTabView>
127129
}
128130

129131
void _tabControllerListener([int? index]) {
130-
int? groupIndex = index;
131-
if (groupIndex == -1) {
132-
return;
133-
}
134-
final appController = globalState.appController;
135-
if (groupIndex == null) {
136-
final currentIndex = _tabController?.index;
137-
groupIndex = currentIndex;
138-
}
139-
final currentGroups = appController.getCurrentGroups();
140-
if (groupIndex == null || groupIndex > currentGroups.length) {
141-
return;
142-
}
143-
final currentGroup = currentGroups[groupIndex];
144132
WidgetsBinding.instance.addPostFrameCallback((_) {
145-
globalState.appController.updateCurrentGroupName(currentGroup.name);
133+
int? groupIndex = index;
134+
if (groupIndex == -1) {
135+
return;
136+
}
137+
final appController = globalState.appController;
138+
if (groupIndex == null) {
139+
final currentIndex = _tabController?.index;
140+
groupIndex = currentIndex;
141+
}
142+
final currentGroups = appController.getCurrentGroups();
143+
if (groupIndex == null || groupIndex > currentGroups.length) {
144+
return;
145+
}
146+
final currentGroup = currentGroups[groupIndex];
147+
appController.updateCurrentGroupName(currentGroup.name);
146148
});
147149
}
148150

@@ -153,8 +155,8 @@ class ProxiesTabViewState extends ConsumerState<ProxiesTabView>
153155
}
154156

155157
void _updateTabController(int length, int index) {
158+
_destroyTabController();
156159
if (length == 0) {
157-
_destroyTabController();
158160
return;
159161
}
160162
final realIndex = index == -1 ? 0 : index;
@@ -170,26 +172,15 @@ class ProxiesTabViewState extends ConsumerState<ProxiesTabView>
170172
@override
171173
Widget build(BuildContext context) {
172174
ref.watch(themeSettingProvider.select((state) => state.textScale));
173-
final state = ref.watch(proxiesTabStateProvider);
175+
final state = ref.watch(proxiesTabStateProvider.select((state) => state));
174176
final groups = state.groups;
175-
if (groups.isEmpty) {
177+
if (groups.isEmpty || _tabController == null) {
176178
return NullStatus(
177179
illustration: ProxyEmptyIllustration(),
178180
label: appLocalizations.nullTip(appLocalizations.proxies),
179181
);
180182
}
181-
final ProxyGroupViewKeyMap keyMap = {};
182-
final children = groups.map((group) {
183-
final key = GlobalObjectKey<_ProxyGroupViewState>(group.name);
184-
keyMap[group.name] = key;
185-
return ProxyGroupView(
186-
key: key,
187-
group: group,
188-
columns: state.columns,
189-
cardType: state.proxyCardType,
190-
);
191-
}).toList();
192-
_keyMap = keyMap;
183+
_keyMap = {};
193184
return Column(
194185
mainAxisAlignment: MainAxisAlignment.start,
195186
crossAxisAlignment: CrossAxisAlignment.start,
@@ -245,15 +236,29 @@ class ProxiesTabViewState extends ConsumerState<ProxiesTabView>
245236
context.colorScheme.surface.opacity10,
246237
context.colorScheme.surface,
247238
],
248-
stops: const [0.0, 0.1],
239+
stops: const [0.0, 0.5],
249240
),
250241
),
251242
child: _buildMoreButton(),
252243
),
253244
),
254245
),
255246
Expanded(
256-
child: TabBarView(controller: _tabController, children: children),
247+
child: TabBarView(
248+
controller: _tabController,
249+
children: [
250+
for (final group in groups)
251+
ProxyGroupView(
252+
key: _keyMap.updateCacheValue(
253+
group.name,
254+
() => GlobalObjectKey<_ProxyGroupViewState>(group.name),
255+
),
256+
group: group,
257+
columns: state.columns,
258+
cardType: state.proxyCardType,
259+
),
260+
],
261+
),
257262
),
258263
],
259264
);

0 commit comments

Comments
 (0)