fix(resources): refresh geo file info once the core finishes updating

The file info was re-read as soon as the update RPC returned, before the
core had finished writing the file, so the list kept showing the old size
and time. Refresh when the updating key clears instead, which also covers
automatic and core-initiated updates.

Changelog: Refresh the geo file size and time after an update finishes
This commit is contained in:
chen08209
2026-09-12 00:44:21 +08:00
parent 60f371afd8
commit c5bf5bd487
3 changed files with 100 additions and 6 deletions
+3
View File
@@ -37,6 +37,9 @@ class GeoResourceAction extends _$GeoResourceAction {
.stop(geoResource.updatingKey, current);
}
/// Completes once the Core has accepted the update, not once it finishes.
/// Completion arrives as a geo-update event through [handleCoreUpdate];
/// callers that need it watch [isUpdatingProvider] for the key to clear.
Future<void> updateGeoResource(GeoResource geoResource) async {
_manualUpdates.add(geoResource);
final operation = _startUpdating(geoResource);
+12 -6
View File
@@ -189,15 +189,15 @@ class _GeoResourceListItemState extends ConsumerState<_GeoResourceListItem> {
return file.getFileInfo();
}
Future<void> _handleUpdateGeoDataItem() async {
await globalState.safeRun<void>(() async {
Future<void> _handleUpdateGeoDataItem() {
return globalState.safeRun<void>(() async {
await ref
.read(geoResourceActionProvider.notifier)
.updateGeoResource(widget.type);
}, silence: false);
if (!mounted) {
return;
}
}
void _refreshFileInfo() {
setState(() {
_fileInfoFuture = _getGeoFileInfo(fileName);
});
@@ -223,7 +223,13 @@ class _GeoResourceListItemState extends ConsumerState<_GeoResourceListItem> {
@override
Widget build(BuildContext context) {
final isUpdating = ref.watch(isUpdatingProvider(widget.type.updatingKey));
final updatingKey = widget.type.updatingKey;
ref.listen(isUpdatingProvider(updatingKey), (previous, next) {
if (previous == true && !next) {
_refreshFileInfo();
}
});
final isUpdating = ref.watch(isUpdatingProvider(updatingKey));
final url = ref.watch(
patchClashConfigProvider.select((state) => state.geoXUrl[widget.type]),
);
+85
View File
@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/enum/enum.dart';
import 'package:fl_clash/models/models.dart';
@@ -8,12 +10,53 @@ import 'package:fl_clash/widgets/widgets.dart';
import 'package:material_ui/material_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' hide context;
import '../helpers/test_app.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late Directory home;
final supportDirectory = AppPath.supportDirectory;
final temporaryDirectory = AppPath.temporaryDirectory;
final cacheDirectory = AppPath.cacheDirectory;
setUpAll(() async {
home = Directory.systemTemp.createTempSync('flclash-resources-');
AppPath.supportDirectory = () async => home;
AppPath.temporaryDirectory = () async => home;
AppPath.cacheDirectory = () async => home;
await appPath.homeDirPath;
});
tearDownAll(() {
AppPath.supportDirectory = supportDirectory;
AppPath.temporaryDirectory = temporaryDirectory;
AppPath.cacheDirectory = cacheDirectory;
if (home.existsSync()) home.deleteSync(recursive: true);
});
// `getFileInfo` stats the file on the real event loop, outside fake-async.
Future<void> settle(WidgetTester tester, {int rounds = 1}) async {
for (var i = 0; i < rounds; i++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 20)),
);
await tester.pump(const Duration(milliseconds: 50));
}
}
Future<void> pumpUntilFound(WidgetTester tester, Finder finder) async {
for (var i = 0; i < 100; i++) {
await settle(tester);
if (finder.evaluate().isNotEmpty) {
return;
}
}
fail('timed out waiting for $finder');
}
testWidgets('uses decorated sections without displaying resource URLs', (
tester,
) async {
@@ -60,4 +103,46 @@ void main() {
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('refreshes the file info once the core finishes updating', (
tester,
) async {
const size = Size(1000, 1000);
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final file = File(join(home.path, MMDB))..writeAsBytesSync([0]);
addTearDown(file.deleteSync);
final container = ProviderContainer();
addTearDown(container.dispose);
globalState.container = container;
container.read(viewSizeProvider.notifier).update((_) => size);
await tester.pumpWidget(
UncontrolledProviderScope(
container: container,
child: const TestApp(child: ResourcesView()),
),
);
await pumpUntilFound(tester, find.text(1.traffic.show));
final updatingKeys = container.read(updatingKeysProvider.notifier);
final key = GeoResource.MMDB.updatingKey;
final operation = updatingKeys.start(key, scope: UpdatingScope.core);
await settle(tester);
file.writeAsBytesSync(List.filled(2048, 0));
await settle(tester, rounds: 5);
expect(find.text(1.traffic.show), findsOneWidget);
updatingKeys.stop(key, operation);
await pumpUntilFound(tester, find.text(2048.traffic.show));
expect(find.text(1.traffic.show), findsNothing);
expect(tester.takeException(), null);
await tester.pumpWidget(const SizedBox.shrink());
});
}