Files
MWClash/lib/fragments/dashboard/network_detection.dart

230 lines
8.2 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:dio/dio.dart';
2024-04-30 23:38:49 +08:00
import 'package:fl_clash/common/common.dart';
import 'package:fl_clash/enum/enum.dart';
2024-04-30 23:38:49 +08:00
import 'package:fl_clash/models/models.dart';
import 'package:fl_clash/state.dart';
2024-04-30 23:38:49 +08:00
import 'package:fl_clash/widgets/widgets.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
class NetworkDetection extends StatefulWidget {
const NetworkDetection({super.key});
@override
State<NetworkDetection> createState() => _NetworkDetectionState();
}
class _NetworkDetectionState extends State<NetworkDetection> {
final networkDetectionState = ValueNotifier<NetworkDetectionState>(
const NetworkDetectionState(
isTesting: true,
ipInfo: null,
),
);
bool? _preIsStart;
Function? _checkIpDebounce;
Timer? _setTimeoutTimer;
CancelToken? cancelToken;
2024-06-06 16:31:08 +08:00
_checkIp() async {
final appState = globalState.appController.appState;
final appFlowingState = globalState.appController.appFlowingState;
final isInit = appState.isInit;
2024-06-06 16:31:08 +08:00
if (!isInit) return;
final isStart = appFlowingState.isStart;
if (_preIsStart == false && _preIsStart == isStart) return;
_clearSetTimeoutTimer();
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: true,
ipInfo: null,
);
_preIsStart = isStart;
if (cancelToken != null) {
cancelToken!.cancel();
cancelToken = null;
}
cancelToken = CancelToken();
try {
final ipInfo = await request.checkIp(cancelToken: cancelToken);
if (ipInfo != null) {
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: false,
ipInfo: ipInfo,
);
return;
}
_setTimeoutTimer = Timer(const Duration(milliseconds: 2000), () {
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: false,
ipInfo: null,
);
});
} catch (_) {
networkDetectionState.value = networkDetectionState.value.copyWith(
isTesting: true,
ipInfo: null,
);
}
}
_clearSetTimeoutTimer() {
if (_setTimeoutTimer != null) {
_setTimeoutTimer?.cancel();
_setTimeoutTimer = null;
}
2024-06-06 16:31:08 +08:00
}
_checkIpContainer(Widget child) {
return Selector<AppState, num>(
selector: (_, appState) {
return appState.checkIpNum;
2024-06-06 16:31:08 +08:00
},
builder: (_, checkIpNum, child) {
if (_checkIpDebounce != null) {
_checkIpDebounce!();
}
return child!;
2024-06-06 16:31:08 +08:00
},
child: child,
2024-04-30 23:38:49 +08:00
);
}
@override
void dispose() {
super.dispose();
networkDetectionState.dispose();
}
String countryCodeToEmoji(String countryCode) {
final String code = countryCode.toUpperCase();
if (code.length != 2) {
return countryCode;
}
final int firstLetter = code.codeUnitAt(0) - 0x41 + 0x1F1E6;
final int secondLetter = code.codeUnitAt(1) - 0x41 + 0x1F1E6;
return String.fromCharCode(firstLetter) + String.fromCharCode(secondLetter);
}
@override
2024-04-30 23:38:49 +08:00
Widget build(BuildContext context) {
_checkIpDebounce ??= debounce(_checkIp);
2024-06-06 16:31:08 +08:00
return _checkIpContainer(
ValueListenableBuilder<NetworkDetectionState>(
valueListenable: networkDetectionState,
builder: (_, state, __) {
final ipInfo = state.ipInfo;
final isTesting = state.isTesting;
2024-06-06 16:31:08 +08:00
return CommonCard(
onPressed: () {},
2024-05-17 15:39:41 +08:00
child: Column(
children: [
Flexible(
flex: 0,
child: Container(
2024-06-06 16:31:08 +08:00
padding: const EdgeInsets.all(16),
child: Row(
children: [
Icon(
Icons.network_check,
color: Theme.of(context).colorScheme.primary,
),
const SizedBox(
width: 8,
),
Flexible(
flex: 1,
child: FadeBox(
child: isTesting
? Text(
appLocalizations.checking,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style:
Theme.of(context).textTheme.titleMedium,
2024-06-06 17:13:32 +08:00
)
: ipInfo != null
? Container(
alignment: Alignment.centerLeft,
height: globalState
.measure.titleMediumHeight,
child: Text(
countryCodeToEmoji(
ipInfo.countryCode),
style: Theme.of(context)
.textTheme
.titleLarge
?.copyWith(
fontFamily: FontFamily.twEmoji.value,
),
),
)
: Text(
appLocalizations.checkError,
style: Theme.of(context)
.textTheme
.titleMedium,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
2024-06-06 16:31:08 +08:00
),
),
],
2024-04-30 23:38:49 +08:00
),
),
2024-05-17 15:39:41 +08:00
),
2024-06-06 16:31:08 +08:00
Container(
height: globalState.measure.titleLargeHeight + 24 - 2,
2024-06-06 16:31:08 +08:00
alignment: Alignment.centerLeft,
padding: const EdgeInsets.all(16).copyWith(top: 0),
child: FadeBox(
child: ipInfo != null
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
flex: 1,
child: TooltipText(
text: Text(
ipInfo.ip,
style: context.textTheme.titleLarge
?.toSoftBold.toMinus,
2024-06-06 16:31:08 +08:00
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
),
],
)
: FadeBox(
child: isTesting == false && ipInfo == null
? Text(
"timeout",
style: context.textTheme.titleLarge
?.copyWith(color: Colors.red)
.toSoftBold
.toMinus,
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
: Container(
padding: const EdgeInsets.all(2),
child: const AspectRatio(
aspectRatio: 1,
child: CircularProgressIndicator(),
),
),
2024-06-06 16:31:08 +08:00
),
),
)
2024-05-17 15:39:41 +08:00
],
),
);
},
2024-04-30 23:38:49 +08:00
),
);
}
}