2024-06-03 18:02:05 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:fl_clash/common/common.dart';
|
2025-12-16 11:23:09 +08:00
|
|
|
import 'package:fl_clash/controller.dart';
|
2025-07-31 17:09:18 +08:00
|
|
|
import 'package:fl_clash/core/core.dart';
|
2024-07-07 10:02:10 +08:00
|
|
|
import 'package:fl_clash/models/models.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:fl_clash/providers/config.dart';
|
2024-07-07 10:02:10 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
2024-06-03 18:02:05 +08:00
|
|
|
import 'package:fl_clash/widgets/widgets.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2024-06-03 18:02:05 +08:00
|
|
|
import 'package:path/path.dart' hide context;
|
|
|
|
|
|
|
|
|
|
@immutable
|
|
|
|
|
class GeoItem {
|
|
|
|
|
final String label;
|
2024-07-08 17:34:14 +08:00
|
|
|
final String key;
|
2024-06-03 18:02:05 +08:00
|
|
|
final String fileName;
|
|
|
|
|
|
|
|
|
|
const GeoItem({
|
|
|
|
|
required this.label,
|
2024-07-08 17:34:14 +08:00
|
|
|
required this.key,
|
2024-06-03 18:02:05 +08:00
|
|
|
required this.fileName,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 02:24:12 +08:00
|
|
|
class ResourcesView extends StatelessWidget {
|
|
|
|
|
const ResourcesView({super.key});
|
2024-06-03 18:02:05 +08:00
|
|
|
|
|
|
|
|
@override
|
2024-08-04 08:21:14 +08:00
|
|
|
Widget build(BuildContext context) {
|
2024-06-03 18:02:05 +08:00
|
|
|
const geoItems = <GeoItem>[
|
2025-07-31 17:09:18 +08:00
|
|
|
GeoItem(label: 'GEOIP', fileName: GEOIP, key: 'geoip'),
|
|
|
|
|
GeoItem(label: 'GEOSITE', fileName: GEOSITE, key: 'geosite'),
|
|
|
|
|
GeoItem(label: 'MMDB', fileName: MMDB, key: 'mmdb'),
|
|
|
|
|
GeoItem(label: 'ASN', fileName: ASN, key: 'asn'),
|
2024-06-03 18:02:05 +08:00
|
|
|
];
|
2024-07-08 17:34:14 +08:00
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
return CommonScaffold(
|
|
|
|
|
title: appLocalizations.resources,
|
|
|
|
|
body: ListView.separated(
|
|
|
|
|
itemBuilder: (_, index) {
|
|
|
|
|
final geoItem = geoItems[index];
|
2025-07-31 17:09:18 +08:00
|
|
|
return GeoDataListItem(geoItem: geoItem);
|
2025-06-07 01:48:34 +08:00
|
|
|
},
|
|
|
|
|
separatorBuilder: (BuildContext context, int index) {
|
2025-07-31 17:09:18 +08:00
|
|
|
return const Divider(height: 0);
|
2025-06-07 01:48:34 +08:00
|
|
|
},
|
|
|
|
|
itemCount: geoItems.length,
|
|
|
|
|
),
|
2024-07-07 10:02:10 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class GeoDataListItem extends StatefulWidget {
|
|
|
|
|
final GeoItem geoItem;
|
|
|
|
|
|
2025-07-31 17:09:18 +08:00
|
|
|
const GeoDataListItem({super.key, required this.geoItem});
|
2024-07-07 10:02:10 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<GeoDataListItem> createState() => _GeoDataListItemState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _GeoDataListItemState extends State<GeoDataListItem> {
|
|
|
|
|
final isUpdating = ValueNotifier<bool>(false);
|
|
|
|
|
|
|
|
|
|
GeoItem get geoItem => widget.geoItem;
|
|
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
Future<void> _updateUrl(String url, WidgetRef ref) async {
|
2025-02-09 18:39:38 +08:00
|
|
|
final defaultMap = defaultGeoXUrl.toJson();
|
2024-07-08 17:34:14 +08:00
|
|
|
final newUrl = await globalState.showCommonDialog<String>(
|
|
|
|
|
child: UpdateGeoUrlFormDialog(
|
|
|
|
|
title: geoItem.label,
|
|
|
|
|
url: url,
|
2025-02-09 18:39:38 +08:00
|
|
|
defaultValue: defaultMap[geoItem.key],
|
2024-07-08 17:34:14 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
if (newUrl != null && newUrl != url && mounted) {
|
|
|
|
|
try {
|
|
|
|
|
if (!newUrl.isUrl) {
|
2025-06-07 01:48:34 +08:00
|
|
|
throw 'Invalid url';
|
2024-07-08 17:34:14 +08:00
|
|
|
}
|
2025-12-16 11:23:09 +08:00
|
|
|
ref.read(patchClashConfigProvider.notifier).update((state) {
|
2025-02-09 18:39:38 +08:00
|
|
|
final map = state.geoXUrl.toJson();
|
|
|
|
|
map[geoItem.key] = newUrl;
|
2025-07-31 17:09:18 +08:00
|
|
|
return state.copyWith(geoXUrl: GeoXUrl.fromJson(map));
|
2025-02-09 18:39:38 +08:00
|
|
|
});
|
2024-07-08 17:34:14 +08:00
|
|
|
} catch (e) {
|
|
|
|
|
globalState.showMessage(
|
|
|
|
|
title: geoItem.label,
|
2025-07-31 17:09:18 +08:00
|
|
|
message: TextSpan(text: e.toString()),
|
2024-07-08 17:34:14 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-07 10:02:10 +08:00
|
|
|
Future<FileInfo> _getGeoFileLastModified(String fileName) async {
|
2025-02-03 23:32:00 +08:00
|
|
|
final homePath = await appPath.homeDirPath;
|
2024-07-07 10:02:10 +08:00
|
|
|
final file = File(join(homePath, fileName));
|
|
|
|
|
final lastModified = await file.lastModified();
|
|
|
|
|
final size = await file.length();
|
2025-07-31 17:09:18 +08:00
|
|
|
return FileInfo(size: size, lastModified: lastModified);
|
2024-07-07 10:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
Widget _buildSubtitle() {
|
|
|
|
|
return Consumer(
|
2025-07-31 17:09:18 +08:00
|
|
|
builder: (_, ref, _) {
|
2025-02-09 18:39:38 +08:00
|
|
|
final url = ref.watch(
|
2025-07-31 17:09:18 +08:00
|
|
|
patchClashConfigProvider.select(
|
|
|
|
|
(state) => state.geoXUrl.toJson()[geoItem.key],
|
|
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
);
|
|
|
|
|
if (url == null) {
|
|
|
|
|
return SizedBox();
|
|
|
|
|
}
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2024-07-07 10:02:10 +08:00
|
|
|
children: [
|
2025-07-31 17:09:18 +08:00
|
|
|
const SizedBox(height: 6),
|
2025-02-09 18:39:38 +08:00
|
|
|
FutureBuilder<FileInfo>(
|
|
|
|
|
future: _getGeoFileLastModified(geoItem.fileName),
|
|
|
|
|
builder: (_, snapshot) {
|
2025-04-18 17:50:46 +08:00
|
|
|
final height = globalState.measure.bodyMediumHeight;
|
2025-02-09 18:39:38 +08:00
|
|
|
return SizedBox(
|
2025-04-18 17:50:46 +08:00
|
|
|
height: height,
|
|
|
|
|
child: snapshot.data == null
|
2025-07-31 17:09:18 +08:00
|
|
|
? SizedBox(width: height, height: height)
|
2025-04-18 17:50:46 +08:00
|
|
|
: Text(
|
|
|
|
|
snapshot.data!.desc,
|
|
|
|
|
style: context.textTheme.bodyMedium,
|
|
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
);
|
2024-07-07 10:02:10 +08:00
|
|
|
},
|
2024-06-03 18:02:05 +08:00
|
|
|
),
|
2025-07-31 17:09:18 +08:00
|
|
|
const SizedBox(height: 4),
|
|
|
|
|
Text(url, style: context.textTheme.bodyMedium?.toLight),
|
|
|
|
|
const SizedBox(height: 12),
|
2025-02-09 18:39:38 +08:00
|
|
|
Wrap(
|
|
|
|
|
runSpacing: 6,
|
|
|
|
|
spacing: 12,
|
2025-06-07 01:48:34 +08:00
|
|
|
runAlignment: WrapAlignment.center,
|
2025-02-09 18:39:38 +08:00
|
|
|
children: [
|
|
|
|
|
CommonChip(
|
|
|
|
|
avatar: const Icon(Icons.edit),
|
|
|
|
|
label: appLocalizations.edit,
|
|
|
|
|
onPressed: () {
|
|
|
|
|
_updateUrl(url, ref);
|
|
|
|
|
},
|
|
|
|
|
),
|
2025-06-07 01:48:34 +08:00
|
|
|
Row(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
SizedBox(
|
|
|
|
|
child: ValueListenableBuilder(
|
|
|
|
|
valueListenable: isUpdating,
|
2025-07-31 17:09:18 +08:00
|
|
|
builder: (_, isUpdating, _) {
|
2025-06-07 01:48:34 +08:00
|
|
|
return isUpdating
|
|
|
|
|
? SizedBox(
|
|
|
|
|
height: 30,
|
|
|
|
|
width: 30,
|
|
|
|
|
child: const Padding(
|
|
|
|
|
padding: EdgeInsets.all(2),
|
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
: CommonChip(
|
|
|
|
|
avatar: const Icon(Icons.sync),
|
|
|
|
|
label: appLocalizations.sync,
|
|
|
|
|
onPressed: () {
|
|
|
|
|
_handleUpdateGeoDataItem();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
2025-07-31 17:09:18 +08:00
|
|
|
),
|
2025-06-07 01:48:34 +08:00
|
|
|
],
|
2025-07-31 17:09:18 +08:00
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
],
|
|
|
|
|
),
|
2025-07-31 17:09:18 +08:00
|
|
|
const SizedBox(height: 6),
|
2024-07-07 10:02:10 +08:00
|
|
|
],
|
2025-02-09 18:39:38 +08:00
|
|
|
);
|
|
|
|
|
},
|
2024-06-03 18:02:05 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
Future<void> _handleUpdateGeoDataItem() async {
|
2025-12-16 11:23:09 +08:00
|
|
|
await appController.safeRun<void>(() async {
|
|
|
|
|
await updateGeoDateItem();
|
|
|
|
|
}, silence: false);
|
2025-07-31 17:09:18 +08:00
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {});
|
|
|
|
|
}
|
2024-07-07 10:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
Future<void> updateGeoDateItem() async {
|
2024-07-07 10:02:10 +08:00
|
|
|
isUpdating.value = true;
|
|
|
|
|
try {
|
2025-07-31 17:09:18 +08:00
|
|
|
final message = await coreController.updateGeoData(
|
|
|
|
|
UpdateGeoDataParams(geoName: geoItem.fileName, geoType: geoItem.label),
|
2024-07-07 10:02:10 +08:00
|
|
|
);
|
|
|
|
|
if (message.isNotEmpty) throw message;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
isUpdating.value = false;
|
|
|
|
|
rethrow;
|
|
|
|
|
}
|
|
|
|
|
isUpdating.value = false;
|
2025-06-07 01:48:34 +08:00
|
|
|
return;
|
2024-07-07 10:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
|
|
|
|
isUpdating.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2024-07-07 10:02:10 +08:00
|
|
|
return ListItem(
|
2025-07-31 17:09:18 +08:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
2024-07-07 10:02:10 +08:00
|
|
|
title: Text(geoItem.label),
|
2025-02-09 18:39:38 +08:00
|
|
|
subtitle: _buildSubtitle(),
|
2024-07-07 10:02:10 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 17:34:14 +08:00
|
|
|
class UpdateGeoUrlFormDialog extends StatefulWidget {
|
|
|
|
|
final String title;
|
|
|
|
|
final String url;
|
2024-09-05 08:59:45 +08:00
|
|
|
final String? defaultValue;
|
2024-07-08 17:34:14 +08:00
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
const UpdateGeoUrlFormDialog({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.title,
|
|
|
|
|
required this.url,
|
|
|
|
|
this.defaultValue,
|
|
|
|
|
});
|
2024-07-08 17:34:14 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<UpdateGeoUrlFormDialog> createState() => _UpdateGeoUrlFormDialogState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _UpdateGeoUrlFormDialogState extends State<UpdateGeoUrlFormDialog> {
|
2025-12-16 11:23:09 +08:00
|
|
|
late final TextEditingController _urlController;
|
2024-07-08 17:34:14 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2025-12-16 11:23:09 +08:00
|
|
|
_urlController = TextEditingController(text: widget.url);
|
2024-07-08 17:34:14 +08:00
|
|
|
}
|
|
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
Future<void> _handleReset() async {
|
2024-09-05 08:59:45 +08:00
|
|
|
if (widget.defaultValue == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Navigator.of(context).pop<String>(widget.defaultValue);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-07 01:48:34 +08:00
|
|
|
Future<void> _handleUpdate() async {
|
2025-12-16 11:23:09 +08:00
|
|
|
final url = _urlController.value.text;
|
2024-07-08 17:34:14 +08:00
|
|
|
if (url.isEmpty) return;
|
|
|
|
|
Navigator.of(context).pop<String>(url);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-16 11:23:09 +08:00
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_urlController.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 17:34:14 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-03-12 17:15:31 +08:00
|
|
|
return CommonDialog(
|
|
|
|
|
title: widget.title,
|
2024-07-08 17:34:14 +08:00
|
|
|
actions: [
|
2024-09-05 08:59:45 +08:00
|
|
|
if (widget.defaultValue != null &&
|
2025-12-16 11:23:09 +08:00
|
|
|
_urlController.value.text != widget.defaultValue) ...[
|
2024-09-05 08:59:45 +08:00
|
|
|
TextButton(
|
|
|
|
|
onPressed: _handleReset,
|
|
|
|
|
child: Text(appLocalizations.reset),
|
|
|
|
|
),
|
2025-07-31 17:09:18 +08:00
|
|
|
const SizedBox(width: 4),
|
2024-09-05 08:59:45 +08:00
|
|
|
],
|
2024-07-08 17:34:14 +08:00
|
|
|
TextButton(
|
|
|
|
|
onPressed: _handleUpdate,
|
|
|
|
|
child: Text(appLocalizations.submit),
|
2025-07-31 17:09:18 +08:00
|
|
|
),
|
2024-07-08 17:34:14 +08:00
|
|
|
],
|
2025-03-12 17:15:31 +08:00
|
|
|
child: Wrap(
|
|
|
|
|
runSpacing: 16,
|
|
|
|
|
children: [
|
|
|
|
|
TextField(
|
|
|
|
|
maxLines: 5,
|
|
|
|
|
minLines: 1,
|
2025-12-16 11:23:09 +08:00
|
|
|
controller: _urlController,
|
2025-07-31 17:09:18 +08:00
|
|
|
decoration: const InputDecoration(border: OutlineInputBorder()),
|
2025-03-12 17:15:31 +08:00
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2024-07-08 17:34:14 +08:00
|
|
|
);
|
|
|
|
|
}
|
2024-07-26 08:05:22 +08:00
|
|
|
}
|