2024-06-03 18:02:05 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:fl_clash/clash/clash.dart';
|
|
|
|
|
import 'package:fl_clash/common/common.dart';
|
2024-07-07 10:02:10 +08:00
|
|
|
import 'package:fl_clash/models/models.dart';
|
|
|
|
|
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';
|
|
|
|
|
import 'package:path/path.dart' hide context;
|
2024-07-08 17:34:14 +08:00
|
|
|
import 'package:provider/provider.dart';
|
2024-06-03 18:02:05 +08:00
|
|
|
|
|
|
|
|
@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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-04 08:21:14 +08:00
|
|
|
class Resources extends StatelessWidget {
|
2024-06-03 18:02:05 +08:00
|
|
|
const Resources({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
2024-08-04 08:21:14 +08:00
|
|
|
Widget build(BuildContext context) {
|
2024-06-03 18:02:05 +08:00
|
|
|
const geoItems = <GeoItem>[
|
2024-07-08 17:34:14 +08:00
|
|
|
GeoItem(
|
|
|
|
|
label: "GeoIp",
|
|
|
|
|
fileName: geoIpFileName,
|
|
|
|
|
key: "geoip",
|
|
|
|
|
),
|
|
|
|
|
GeoItem(label: "GeoSite", fileName: geoSiteFileName, key: "geosite"),
|
|
|
|
|
GeoItem(
|
|
|
|
|
label: "MMDB",
|
|
|
|
|
fileName: mmdbFileName,
|
|
|
|
|
key: "mmdb",
|
|
|
|
|
),
|
|
|
|
|
GeoItem(label: "ASN", fileName: asnFileName, key: "asn"),
|
2024-06-03 18:02:05 +08:00
|
|
|
];
|
2024-07-08 17:34:14 +08:00
|
|
|
|
2024-08-04 08:21:14 +08:00
|
|
|
return ListView.separated(
|
|
|
|
|
itemBuilder: (_, index) {
|
|
|
|
|
final geoItem = geoItems[index];
|
|
|
|
|
return GeoDataListItem(
|
2024-07-07 10:02:10 +08:00
|
|
|
geoItem: geoItem,
|
2024-08-04 08:21:14 +08:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
separatorBuilder: (BuildContext context, int index) {
|
|
|
|
|
return const Divider(
|
|
|
|
|
height: 0,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
itemCount: geoItems.length,
|
2024-07-07 10:02:10 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class GeoDataListItem extends StatefulWidget {
|
|
|
|
|
final GeoItem geoItem;
|
|
|
|
|
|
|
|
|
|
const GeoDataListItem({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.geoItem,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<GeoDataListItem> createState() => _GeoDataListItemState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _GeoDataListItemState extends State<GeoDataListItem> {
|
|
|
|
|
final isUpdating = ValueNotifier<bool>(false);
|
|
|
|
|
|
|
|
|
|
GeoItem get geoItem => widget.geoItem;
|
|
|
|
|
|
2024-07-08 17:34:14 +08:00
|
|
|
_updateUrl(String url) async {
|
|
|
|
|
final newUrl = await globalState.showCommonDialog<String>(
|
|
|
|
|
child: UpdateGeoUrlFormDialog(
|
|
|
|
|
title: geoItem.label,
|
|
|
|
|
url: url,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
if (newUrl != null && newUrl != url && mounted) {
|
|
|
|
|
try {
|
|
|
|
|
if (!newUrl.isUrl) {
|
|
|
|
|
throw "Invalid url";
|
|
|
|
|
}
|
|
|
|
|
final appController = globalState.appController;
|
|
|
|
|
appController.clashConfig.geoXUrl =
|
|
|
|
|
Map.from(appController.clashConfig.geoXUrl)..[geoItem.key] = newUrl;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
globalState.showMessage(
|
|
|
|
|
title: geoItem.label,
|
|
|
|
|
message: TextSpan(
|
|
|
|
|
text: e.toString(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-07 10:02:10 +08:00
|
|
|
Future<FileInfo> _getGeoFileLastModified(String fileName) async {
|
|
|
|
|
final homePath = await appPath.getHomeDirPath();
|
|
|
|
|
final file = File(join(homePath, fileName));
|
|
|
|
|
final lastModified = await file.lastModified();
|
|
|
|
|
final size = await file.length();
|
|
|
|
|
return FileInfo(
|
2024-07-26 08:05:22 +08:00
|
|
|
size: size,
|
2024-07-07 10:02:10 +08:00
|
|
|
lastModified: lastModified,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 17:34:14 +08:00
|
|
|
Widget _buildSubtitle(String url) {
|
2024-07-07 10:02:10 +08:00
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 4,
|
|
|
|
|
),
|
|
|
|
|
FutureBuilder<FileInfo>(
|
|
|
|
|
future: _getGeoFileLastModified(geoItem.fileName),
|
|
|
|
|
builder: (_, snapshot) {
|
|
|
|
|
return SizedBox(
|
|
|
|
|
height: 24,
|
|
|
|
|
child: FadeBox(
|
|
|
|
|
key: Key("fade_box_${geoItem.label}"),
|
|
|
|
|
child: snapshot.data == null
|
|
|
|
|
? const SizedBox(
|
|
|
|
|
width: 24,
|
|
|
|
|
height: 24,
|
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
|
strokeWidth: 2,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
: Text(
|
2024-07-26 08:05:22 +08:00
|
|
|
snapshot.data!.desc,
|
2024-07-07 10:02:10 +08:00
|
|
|
),
|
2024-06-03 18:02:05 +08:00
|
|
|
),
|
2024-07-07 10:02:10 +08:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-07-08 17:34:14 +08:00
|
|
|
Text(
|
|
|
|
|
url,
|
|
|
|
|
style: context.textTheme.bodyMedium?.toLight,
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
height: 8,
|
|
|
|
|
),
|
2024-07-07 10:02:10 +08:00
|
|
|
Wrap(
|
|
|
|
|
runSpacing: 6,
|
|
|
|
|
spacing: 12,
|
|
|
|
|
children: [
|
2024-07-08 17:34:14 +08:00
|
|
|
CommonChip(
|
|
|
|
|
avatar: const Icon(Icons.edit),
|
|
|
|
|
label: appLocalizations.edit,
|
|
|
|
|
onPressed: () {
|
|
|
|
|
_updateUrl(url);
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-07-07 10:02:10 +08:00
|
|
|
CommonChip(
|
|
|
|
|
avatar: const Icon(Icons.sync),
|
|
|
|
|
label: appLocalizations.sync,
|
|
|
|
|
onPressed: () {
|
|
|
|
|
_handleUpdateGeoDataItem();
|
|
|
|
|
},
|
2024-06-03 18:02:05 +08:00
|
|
|
),
|
2024-07-07 10:02:10 +08:00
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
],
|
2024-06-03 18:02:05 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-07 10:02:10 +08:00
|
|
|
_handleUpdateGeoDataItem() async {
|
|
|
|
|
await globalState.safeRun<void>(updateGeoDateItem);
|
|
|
|
|
setState(() {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateGeoDateItem() async {
|
|
|
|
|
isUpdating.value = true;
|
|
|
|
|
try {
|
2024-08-05 19:25:35 +08:00
|
|
|
final message = await clashCore.updateGeoData(
|
|
|
|
|
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;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: 16,
|
|
|
|
|
vertical: 4,
|
|
|
|
|
),
|
|
|
|
|
title: Text(geoItem.label),
|
2024-07-08 17:34:14 +08:00
|
|
|
subtitle: Selector<ClashConfig, String>(
|
|
|
|
|
selector: (_, clashConfig) => clashConfig.geoXUrl[geoItem.key]!,
|
|
|
|
|
builder: (_, value, __) {
|
|
|
|
|
return _buildSubtitle(value);
|
|
|
|
|
},
|
|
|
|
|
),
|
2024-07-07 10:02:10 +08:00
|
|
|
trailing: SizedBox(
|
|
|
|
|
height: 48,
|
|
|
|
|
width: 48,
|
|
|
|
|
child: ValueListenableBuilder(
|
|
|
|
|
valueListenable: isUpdating,
|
|
|
|
|
builder: (_, isUpdating, ___) {
|
|
|
|
|
return FadeBox(
|
|
|
|
|
child: isUpdating
|
|
|
|
|
? const Padding(
|
|
|
|
|
padding: EdgeInsets.all(8),
|
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
|
)
|
|
|
|
|
: const SizedBox(),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 17:34:14 +08:00
|
|
|
class UpdateGeoUrlFormDialog extends StatefulWidget {
|
|
|
|
|
final String title;
|
|
|
|
|
final String url;
|
|
|
|
|
|
|
|
|
|
const UpdateGeoUrlFormDialog({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.title,
|
|
|
|
|
required this.url,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<UpdateGeoUrlFormDialog> createState() => _UpdateGeoUrlFormDialogState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _UpdateGeoUrlFormDialogState extends State<UpdateGeoUrlFormDialog> {
|
|
|
|
|
late TextEditingController urlController;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
urlController = TextEditingController(text: widget.url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_handleUpdate() async {
|
|
|
|
|
final url = urlController.value.text;
|
|
|
|
|
if (url.isEmpty) return;
|
|
|
|
|
Navigator.of(context).pop<String>(url);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return AlertDialog(
|
|
|
|
|
title: Text(widget.title),
|
|
|
|
|
content: SizedBox(
|
|
|
|
|
width: 300,
|
|
|
|
|
child: Wrap(
|
|
|
|
|
runSpacing: 16,
|
|
|
|
|
children: [
|
|
|
|
|
TextField(
|
|
|
|
|
maxLines: 5,
|
|
|
|
|
minLines: 1,
|
|
|
|
|
controller: urlController,
|
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: _handleUpdate,
|
|
|
|
|
child: Text(appLocalizations.submit),
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-07-26 08:05:22 +08:00
|
|
|
}
|