diff --git a/core/common.go b/core/common.go index 25ff458..55e8a53 100644 --- a/core/common.go +++ b/core/common.go @@ -176,7 +176,6 @@ func applyConfig(isPatch bool) bool { patchConfig(cfg.General) } else { executor.ApplyConfig(cfg, true) - } return true } diff --git a/lib/clash/core.dart b/lib/clash/core.dart index b3b663a..13de312 100644 --- a/lib/clash/core.dart +++ b/lib/clash/core.dart @@ -1,9 +1,11 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:ffi'; import 'dart:io'; import 'dart:isolate'; import 'package:ffi/ffi.dart'; +import 'package:flutter/cupertino.dart'; import '../enum/enum.dart'; import '../models/models.dart'; import '../common/common.dart'; @@ -16,19 +18,23 @@ class ClashCore { late final ClashFFI clashFFI; late final DynamicLibrary lib; - ClashCore._internal() { + DynamicLibrary _getClashLib() { + debugPrint("OpenClash"); if (Platform.isWindows) { - lib = DynamicLibrary.open("libclash.dll"); - clashFFI = ClashFFI(lib); + return DynamicLibrary.open("libclash.dll"); } if (Platform.isMacOS) { - lib = DynamicLibrary.open("libclash.dylib"); - clashFFI = ClashFFI(lib); + return DynamicLibrary.open("libclash.dylib"); } if (Platform.isAndroid || Platform.isLinux) { - lib = DynamicLibrary.open("libclash.so"); - clashFFI = ClashFFI(lib); + return DynamicLibrary.open("libclash.so"); } + throw "Platform is not supported"; + } + + ClashCore._internal() { + lib = _getClashLib(); + clashFFI = ClashFFI(lib); clashFFI.initNativeApiBridge( NativeApi.initializeApiDLData, receiver.sendPort.nativePort, diff --git a/lib/common/function.dart b/lib/common/function.dart index 7c2f208..6b8c8d5 100644 --- a/lib/common/function.dart +++ b/lib/common/function.dart @@ -19,8 +19,8 @@ Function debounce(F func,{int milliseconds = 600}) { if (timer != null) { timer!.cancel(); } - timer = Timer(Duration(milliseconds: milliseconds), () { - Function.apply(func, args ?? [], namedArgs); + timer = Timer(Duration(milliseconds: milliseconds), () async { + await Function.apply(func, args ?? [], namedArgs); }); }; } \ No newline at end of file diff --git a/lib/controller.dart b/lib/controller.dart index 907b583..4d1a4a2 100644 --- a/lib/controller.dart +++ b/lib/controller.dart @@ -56,7 +56,9 @@ class AppController { updateRunTime() { if (proxyManager.startTime != null) { final startTimeStamp = proxyManager.startTime!.millisecondsSinceEpoch; - final nowTimeStamp = DateTime.now().millisecondsSinceEpoch; + final nowTimeStamp = DateTime + .now() + .millisecondsSinceEpoch; appState.runTime = nowTimeStamp - startTimeStamp; } else { appState.runTime = null; @@ -131,6 +133,21 @@ class AppController { ); } + Function? _changeProfileDebounce; + + changeProfileDebounce(String? profileId) { + if (profileId == config.currentProfileId) return; + config.currentProfileId = profileId; + _changeProfileDebounce ??= debounce((profileId) async { + if (context.mounted) { + await applyProfile(); + appState.delayMap = {}; + saveConfigPreferences(); + } + }); + _changeProfileDebounce!([profileId]); + } + changeProfile(String? value) async { if (value == config.currentProfileId) return; config.currentProfileId = value; @@ -144,8 +161,8 @@ class AppController { if (!profile.autoUpdate) return; final isNotNeedUpdate = profile.lastUpdateDate ?.add( - profile.autoUpdateDuration, - ) + profile.autoUpdateDuration, + ) .isBeforeNow(); if (isNotNeedUpdate == false) continue; await profile.update(); @@ -162,7 +179,7 @@ class AppController { clearCurrentDelay() { final currentProxyName = - appState.getCurrentProxyName(config.currentProxyName, clashConfig.mode); + appState.getCurrentProxyName(config.currentProxyName, clashConfig.mode); if (currentProxyName == null) return; appState.setDelay(Delay(name: currentProxyName, value: null)); } @@ -249,7 +266,7 @@ class AppController { toProfiles() { final index = globalState.currentNavigationItems.indexWhere( - (element) => element.label == "profiles", + (element) => element.label == "profiles", ); if (index != -1) { toPage(index); @@ -262,7 +279,7 @@ class AppController { final commonScaffoldState = globalState.homeScaffoldKey.currentState; if (commonScaffoldState?.mounted != true) return; commonScaffoldState?.loadingRun( - () async { + () async { await Future.delayed(const Duration(milliseconds: 300)); final profile = Profile( url: url, @@ -283,7 +300,7 @@ class AppController { initLink() { linkManager.initAppLinksListen( - (url) { + (url) { globalState.showMessage( title: "${appLocalizations.add}${appLocalizations.profile}", message: TextSpan( @@ -292,14 +309,20 @@ class AppController { TextSpan( text: " $url ", style: TextStyle( - color: Theme.of(context).colorScheme.primary, + color: Theme + .of(context) + .colorScheme + .primary, decoration: TextDecoration.underline, - decorationColor: Theme.of(context).colorScheme.primary, + decorationColor: Theme + .of(context) + .colorScheme + .primary, ), ), TextSpan( text: - "${appLocalizations.create}${appLocalizations.profile}"), + "${appLocalizations.create}${appLocalizations.profile}"), ], ), onTab: () { @@ -319,7 +342,7 @@ class AppController { final commonScaffoldState = globalState.homeScaffoldKey.currentState; if (commonScaffoldState?.mounted != true) return; commonScaffoldState?.loadingRun( - () async { + () async { await Future.delayed(const Duration(milliseconds: 300)); final bytes = result.data?.bytes; if (bytes == null) { diff --git a/lib/fragments/profiles/profiles.dart b/lib/fragments/profiles/profiles.dart index 3c978e8..a7b459f 100644 --- a/lib/fragments/profiles/profiles.dart +++ b/lib/fragments/profiles/profiles.dart @@ -24,6 +24,7 @@ class ProfilesFragment extends StatefulWidget { } class _ProfilesFragmentState extends State { + String _getLastUpdateTimeDifference(DateTime lastDateTime) { final currentDateTime = DateTime.now(); final difference = currentDateTime.difference(lastDateTime); @@ -209,7 +210,7 @@ class _ProfilesFragmentState extends State { child: _profileItem( profile: profile, groupValue: state.currentProfileId, - onChanged: context.appController.changeProfile, + onChanged: context.appController.changeProfileDebounce, ), ), ],