2024-04-30 23:38:49 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
|
|
import 'package:animations/animations.dart';
|
|
|
|
|
import 'package:fl_clash/clash/clash.dart';
|
2024-06-23 00:26:24 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/widgets/scaffold.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
2024-05-11 17:02:34 +08:00
|
|
|
import 'controller.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'models/models.dart';
|
|
|
|
|
import 'common/common.dart';
|
|
|
|
|
|
|
|
|
|
class GlobalState {
|
|
|
|
|
Timer? timer;
|
2024-05-10 10:11:27 +08:00
|
|
|
Timer? groupsUpdateTimer;
|
|
|
|
|
Function? updateCurrentDelayDebounce;
|
2024-04-30 23:38:49 +08:00
|
|
|
PageController? pageController;
|
|
|
|
|
final navigatorKey = GlobalKey<NavigatorState>();
|
2024-05-11 17:02:34 +08:00
|
|
|
late AppController appController;
|
2024-04-30 23:38:49 +08:00
|
|
|
GlobalKey<CommonScaffoldState> homeScaffoldKey = GlobalKey();
|
|
|
|
|
List<Function> updateFunctionLists = [];
|
|
|
|
|
|
|
|
|
|
startListenUpdate() {
|
|
|
|
|
if (timer != null && timer!.isActive == true) return;
|
|
|
|
|
timer = Timer.periodic(const Duration(seconds: 1), (Timer t) {
|
|
|
|
|
for (final function in updateFunctionLists) {
|
|
|
|
|
function();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopListenUpdate() {
|
|
|
|
|
if (timer == null || timer?.isActive == false) return;
|
|
|
|
|
timer?.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
Future<void> updateClashConfig({
|
2024-04-30 23:38:49 +08:00
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
required Config config,
|
|
|
|
|
bool isPatch = true,
|
|
|
|
|
}) async {
|
|
|
|
|
final profilePath = await appPath.getProfilePath(config.currentProfileId);
|
2024-05-20 15:15:09 +08:00
|
|
|
await config.currentProfile?.checkAndUpdate();
|
2024-06-29 21:42:00 +08:00
|
|
|
final res = await clashCore.updateConfig(
|
|
|
|
|
UpdateConfigParams(
|
|
|
|
|
profilePath: profilePath,
|
|
|
|
|
config: clashConfig,
|
|
|
|
|
isPatch: isPatch,
|
|
|
|
|
isCompatible: config.isCompatible,
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-06-03 18:02:05 +08:00
|
|
|
if (res.isNotEmpty) throw res;
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateCoreVersionInfo(AppState appState) {
|
|
|
|
|
appState.versionInfo = clashCore.getVersionInfo();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> startSystemProxy({
|
2024-06-16 19:04:33 +08:00
|
|
|
required AppState appState,
|
2024-04-30 23:38:49 +08:00
|
|
|
required Config config,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
}) async {
|
2024-06-10 00:47:48 +08:00
|
|
|
final args = config.isAccessControl
|
|
|
|
|
? json.encode(
|
|
|
|
|
Props(
|
|
|
|
|
accessControl: config.accessControl,
|
|
|
|
|
allowBypass: config.allowBypass,
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
: null;
|
2024-04-30 23:38:49 +08:00
|
|
|
await proxyManager.startProxy(
|
|
|
|
|
port: clashConfig.mixedPort,
|
|
|
|
|
args: args,
|
|
|
|
|
);
|
|
|
|
|
startListenUpdate();
|
2024-06-16 19:04:33 +08:00
|
|
|
applyProfile(
|
|
|
|
|
appState: appState,
|
|
|
|
|
config: config,
|
|
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> stopSystemProxy() async {
|
|
|
|
|
await proxyManager.stopProxy();
|
|
|
|
|
stopListenUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
Future<void> applyProfile({
|
2024-05-04 16:39:21 +08:00
|
|
|
required AppState appState,
|
|
|
|
|
required Config config,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
}) async {
|
2024-06-03 18:02:05 +08:00
|
|
|
await updateClashConfig(
|
2024-05-04 16:39:21 +08:00
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
config: config,
|
|
|
|
|
isPatch: false,
|
|
|
|
|
);
|
|
|
|
|
await updateGroups(appState);
|
|
|
|
|
changeProxy(
|
|
|
|
|
appState: appState,
|
|
|
|
|
config: config,
|
|
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
init({
|
|
|
|
|
required AppState appState,
|
|
|
|
|
required Config config,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
}) async {
|
|
|
|
|
appState.isInit = clashCore.isInit;
|
|
|
|
|
if (!appState.isInit) {
|
|
|
|
|
appState.isInit = await clashService.init(
|
|
|
|
|
config: config,
|
|
|
|
|
clashConfig: clashConfig,
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-05-04 16:39:21 +08:00
|
|
|
updateCoreVersionInfo(appState);
|
2024-05-02 00:32:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
changeProxy({
|
|
|
|
|
required AppState appState,
|
|
|
|
|
required Config config,
|
|
|
|
|
required ClashConfig clashConfig,
|
|
|
|
|
}) {
|
2024-06-05 17:59:21 +08:00
|
|
|
if (config.profiles.isEmpty) {
|
|
|
|
|
stopSystemProxy();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
config.currentSelectedMap.forEach((key, value) {
|
|
|
|
|
clashCore.changeProxy(
|
|
|
|
|
ChangeProxyParams(
|
|
|
|
|
groupName: key,
|
|
|
|
|
proxyName: value,
|
|
|
|
|
),
|
|
|
|
|
);
|
2024-05-07 13:50:00 +08:00
|
|
|
});
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
2024-05-03 14:31:10 +08:00
|
|
|
Future<void> updateGroups(AppState appState) async {
|
2024-05-04 16:39:21 +08:00
|
|
|
appState.groups = await clashCore.getProxiesGroups();
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showMessage({
|
|
|
|
|
required String title,
|
|
|
|
|
required InlineSpan message,
|
|
|
|
|
Function()? onTab,
|
2024-05-31 09:59:18 +08:00
|
|
|
String? confirmText,
|
2024-04-30 23:38:49 +08:00
|
|
|
}) {
|
|
|
|
|
showCommonDialog(
|
|
|
|
|
child: Builder(
|
|
|
|
|
builder: (context) {
|
|
|
|
|
return AlertDialog(
|
|
|
|
|
title: Text(title),
|
2024-05-31 09:59:18 +08:00
|
|
|
content: Container(
|
2024-04-30 23:38:49 +08:00
|
|
|
width: 300,
|
2024-06-03 18:02:05 +08:00
|
|
|
constraints: const BoxConstraints(maxHeight: 200),
|
2024-05-31 09:59:18 +08:00
|
|
|
child: SingleChildScrollView(
|
|
|
|
|
child: RichText(
|
|
|
|
|
overflow: TextOverflow.visible,
|
|
|
|
|
text: TextSpan(
|
|
|
|
|
style: Theme.of(context).textTheme.labelLarge,
|
|
|
|
|
children: [message],
|
|
|
|
|
),
|
2024-05-20 15:15:09 +08:00
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: onTab ??
|
|
|
|
|
() {
|
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
|
},
|
2024-05-31 09:59:18 +08:00
|
|
|
child: Text(confirmText ?? appLocalizations.confirm),
|
2024-04-30 23:38:49 +08:00
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 15:15:09 +08:00
|
|
|
Future<T?> showCommonDialog<T>({
|
2024-04-30 23:38:49 +08:00
|
|
|
required Widget child,
|
|
|
|
|
}) async {
|
|
|
|
|
return await showModal<T>(
|
|
|
|
|
context: navigatorKey.currentState!.context,
|
|
|
|
|
configuration: const FadeScaleTransitionConfiguration(
|
|
|
|
|
barrierColor: Colors.black38,
|
|
|
|
|
),
|
|
|
|
|
builder: (_) => child,
|
2024-05-20 15:15:09 +08:00
|
|
|
filter: filter,
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
}
|
2024-05-31 09:59:18 +08:00
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
updateTraffic({
|
|
|
|
|
AppState? appState,
|
|
|
|
|
required Config config,
|
|
|
|
|
}) {
|
|
|
|
|
final traffic = clashCore.getTraffic();
|
2024-05-02 00:32:11 +08:00
|
|
|
if (appState != null) {
|
2024-04-30 23:38:49 +08:00
|
|
|
appState.addTraffic(traffic);
|
2024-06-16 19:04:33 +08:00
|
|
|
appState.totalTraffic = clashCore.getTotalTraffic();
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
|
final currentProfile = config.currentProfile;
|
|
|
|
|
if (currentProfile == null) return;
|
|
|
|
|
proxyManager.startForeground(
|
|
|
|
|
title: currentProfile.label ?? currentProfile.id,
|
|
|
|
|
content: "$traffic",
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-10 10:11:27 +08:00
|
|
|
|
|
|
|
|
showSnackBar(
|
|
|
|
|
BuildContext context, {
|
|
|
|
|
required String message,
|
|
|
|
|
SnackBarAction? action,
|
|
|
|
|
}) {
|
2024-06-13 23:43:42 +08:00
|
|
|
// final width = context.width;
|
|
|
|
|
// EdgeInsets margin;
|
|
|
|
|
// if (width < 600) {
|
|
|
|
|
// margin = const EdgeInsets.only(
|
|
|
|
|
// bottom: 96,
|
|
|
|
|
// right: 16,
|
|
|
|
|
// left: 16,
|
|
|
|
|
// );
|
|
|
|
|
// } else {
|
|
|
|
|
// margin = EdgeInsets.only(
|
|
|
|
|
// bottom: 16,
|
|
|
|
|
// left: 16,
|
|
|
|
|
// right: width - 316,
|
|
|
|
|
// );
|
|
|
|
|
// }
|
|
|
|
|
// ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
// SnackBar(
|
|
|
|
|
// action: action,
|
|
|
|
|
// content: Text(message),
|
|
|
|
|
// behavior: SnackBarBehavior.floating,
|
|
|
|
|
// duration: const Duration(milliseconds: 1500),
|
|
|
|
|
// margin: margin,
|
|
|
|
|
// ),
|
|
|
|
|
// );
|
2024-05-10 10:11:27 +08:00
|
|
|
}
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
Future<T?> safeRun<T>(
|
|
|
|
|
FutureOr<T> Function() futureFunction, {
|
|
|
|
|
String? title,
|
|
|
|
|
}) async {
|
|
|
|
|
try {
|
|
|
|
|
final res = await futureFunction();
|
|
|
|
|
return res;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
showMessage(
|
|
|
|
|
title: title ?? appLocalizations.tip,
|
|
|
|
|
message: TextSpan(
|
|
|
|
|
text: e.toString(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-06-23 00:26:24 +08:00
|
|
|
|
2024-06-29 21:42:00 +08:00
|
|
|
int getColumns(ViewMode viewMode, int currentColumns) {
|
2024-06-23 00:26:24 +08:00
|
|
|
final targetColumnsArray = switch (viewMode) {
|
|
|
|
|
ViewMode.mobile => [2, 1],
|
|
|
|
|
ViewMode.laptop => [3, 2],
|
|
|
|
|
ViewMode.desktop => [4, 3],
|
|
|
|
|
};
|
|
|
|
|
if (targetColumnsArray.contains(currentColumns)) {
|
|
|
|
|
return currentColumns;
|
|
|
|
|
}
|
|
|
|
|
return targetColumnsArray.first;
|
|
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final globalState = GlobalState();
|