add changeProfileDebounce

This commit is contained in:
chen08209
2024-05-04 21:51:40 +08:00
parent 184d2d117a
commit ecd1bcafd5
5 changed files with 51 additions and 22 deletions

View File

@@ -176,7 +176,6 @@ func applyConfig(isPatch bool) bool {
patchConfig(cfg.General) patchConfig(cfg.General)
} else { } else {
executor.ApplyConfig(cfg, true) executor.ApplyConfig(cfg, true)
} }
return true return true
} }

View File

@@ -1,9 +1,11 @@
import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:ffi'; import 'dart:ffi';
import 'dart:io'; import 'dart:io';
import 'dart:isolate'; import 'dart:isolate';
import 'package:ffi/ffi.dart'; import 'package:ffi/ffi.dart';
import 'package:flutter/cupertino.dart';
import '../enum/enum.dart'; import '../enum/enum.dart';
import '../models/models.dart'; import '../models/models.dart';
import '../common/common.dart'; import '../common/common.dart';
@@ -16,19 +18,23 @@ class ClashCore {
late final ClashFFI clashFFI; late final ClashFFI clashFFI;
late final DynamicLibrary lib; late final DynamicLibrary lib;
ClashCore._internal() { DynamicLibrary _getClashLib() {
debugPrint("OpenClash");
if (Platform.isWindows) { if (Platform.isWindows) {
lib = DynamicLibrary.open("libclash.dll"); return DynamicLibrary.open("libclash.dll");
clashFFI = ClashFFI(lib);
} }
if (Platform.isMacOS) { if (Platform.isMacOS) {
lib = DynamicLibrary.open("libclash.dylib"); return DynamicLibrary.open("libclash.dylib");
clashFFI = ClashFFI(lib);
} }
if (Platform.isAndroid || Platform.isLinux) { if (Platform.isAndroid || Platform.isLinux) {
lib = DynamicLibrary.open("libclash.so"); return DynamicLibrary.open("libclash.so");
clashFFI = ClashFFI(lib);
} }
throw "Platform is not supported";
}
ClashCore._internal() {
lib = _getClashLib();
clashFFI = ClashFFI(lib);
clashFFI.initNativeApiBridge( clashFFI.initNativeApiBridge(
NativeApi.initializeApiDLData, NativeApi.initializeApiDLData,
receiver.sendPort.nativePort, receiver.sendPort.nativePort,

View File

@@ -19,8 +19,8 @@ Function debounce<F extends Function>(F func,{int milliseconds = 600}) {
if (timer != null) { if (timer != null) {
timer!.cancel(); timer!.cancel();
} }
timer = Timer(Duration(milliseconds: milliseconds), () { timer = Timer(Duration(milliseconds: milliseconds), () async {
Function.apply(func, args ?? [], namedArgs); await Function.apply(func, args ?? [], namedArgs);
}); });
}; };
} }

View File

@@ -56,7 +56,9 @@ class AppController {
updateRunTime() { updateRunTime() {
if (proxyManager.startTime != null) { if (proxyManager.startTime != null) {
final startTimeStamp = proxyManager.startTime!.millisecondsSinceEpoch; final startTimeStamp = proxyManager.startTime!.millisecondsSinceEpoch;
final nowTimeStamp = DateTime.now().millisecondsSinceEpoch; final nowTimeStamp = DateTime
.now()
.millisecondsSinceEpoch;
appState.runTime = nowTimeStamp - startTimeStamp; appState.runTime = nowTimeStamp - startTimeStamp;
} else { } else {
appState.runTime = null; appState.runTime = null;
@@ -131,6 +133,21 @@ class AppController {
); );
} }
Function? _changeProfileDebounce;
changeProfileDebounce(String? profileId) {
if (profileId == config.currentProfileId) return;
config.currentProfileId = profileId;
_changeProfileDebounce ??= debounce<Function(String?)>((profileId) async {
if (context.mounted) {
await applyProfile();
appState.delayMap = {};
saveConfigPreferences();
}
});
_changeProfileDebounce!([profileId]);
}
changeProfile(String? value) async { changeProfile(String? value) async {
if (value == config.currentProfileId) return; if (value == config.currentProfileId) return;
config.currentProfileId = value; config.currentProfileId = value;
@@ -144,8 +161,8 @@ class AppController {
if (!profile.autoUpdate) return; if (!profile.autoUpdate) return;
final isNotNeedUpdate = profile.lastUpdateDate final isNotNeedUpdate = profile.lastUpdateDate
?.add( ?.add(
profile.autoUpdateDuration, profile.autoUpdateDuration,
) )
.isBeforeNow(); .isBeforeNow();
if (isNotNeedUpdate == false) continue; if (isNotNeedUpdate == false) continue;
await profile.update(); await profile.update();
@@ -162,7 +179,7 @@ class AppController {
clearCurrentDelay() { clearCurrentDelay() {
final currentProxyName = final currentProxyName =
appState.getCurrentProxyName(config.currentProxyName, clashConfig.mode); appState.getCurrentProxyName(config.currentProxyName, clashConfig.mode);
if (currentProxyName == null) return; if (currentProxyName == null) return;
appState.setDelay(Delay(name: currentProxyName, value: null)); appState.setDelay(Delay(name: currentProxyName, value: null));
} }
@@ -249,7 +266,7 @@ class AppController {
toProfiles() { toProfiles() {
final index = globalState.currentNavigationItems.indexWhere( final index = globalState.currentNavigationItems.indexWhere(
(element) => element.label == "profiles", (element) => element.label == "profiles",
); );
if (index != -1) { if (index != -1) {
toPage(index); toPage(index);
@@ -262,7 +279,7 @@ class AppController {
final commonScaffoldState = globalState.homeScaffoldKey.currentState; final commonScaffoldState = globalState.homeScaffoldKey.currentState;
if (commonScaffoldState?.mounted != true) return; if (commonScaffoldState?.mounted != true) return;
commonScaffoldState?.loadingRun( commonScaffoldState?.loadingRun(
() async { () async {
await Future.delayed(const Duration(milliseconds: 300)); await Future.delayed(const Duration(milliseconds: 300));
final profile = Profile( final profile = Profile(
url: url, url: url,
@@ -283,7 +300,7 @@ class AppController {
initLink() { initLink() {
linkManager.initAppLinksListen( linkManager.initAppLinksListen(
(url) { (url) {
globalState.showMessage( globalState.showMessage(
title: "${appLocalizations.add}${appLocalizations.profile}", title: "${appLocalizations.add}${appLocalizations.profile}",
message: TextSpan( message: TextSpan(
@@ -292,14 +309,20 @@ class AppController {
TextSpan( TextSpan(
text: " $url ", text: " $url ",
style: TextStyle( style: TextStyle(
color: Theme.of(context).colorScheme.primary, color: Theme
.of(context)
.colorScheme
.primary,
decoration: TextDecoration.underline, decoration: TextDecoration.underline,
decorationColor: Theme.of(context).colorScheme.primary, decorationColor: Theme
.of(context)
.colorScheme
.primary,
), ),
), ),
TextSpan( TextSpan(
text: text:
"${appLocalizations.create}${appLocalizations.profile}"), "${appLocalizations.create}${appLocalizations.profile}"),
], ],
), ),
onTab: () { onTab: () {
@@ -319,7 +342,7 @@ class AppController {
final commonScaffoldState = globalState.homeScaffoldKey.currentState; final commonScaffoldState = globalState.homeScaffoldKey.currentState;
if (commonScaffoldState?.mounted != true) return; if (commonScaffoldState?.mounted != true) return;
commonScaffoldState?.loadingRun( commonScaffoldState?.loadingRun(
() async { () async {
await Future.delayed(const Duration(milliseconds: 300)); await Future.delayed(const Duration(milliseconds: 300));
final bytes = result.data?.bytes; final bytes = result.data?.bytes;
if (bytes == null) { if (bytes == null) {

View File

@@ -24,6 +24,7 @@ class ProfilesFragment extends StatefulWidget {
} }
class _ProfilesFragmentState extends State<ProfilesFragment> { class _ProfilesFragmentState extends State<ProfilesFragment> {
String _getLastUpdateTimeDifference(DateTime lastDateTime) { String _getLastUpdateTimeDifference(DateTime lastDateTime) {
final currentDateTime = DateTime.now(); final currentDateTime = DateTime.now();
final difference = currentDateTime.difference(lastDateTime); final difference = currentDateTime.difference(lastDateTime);
@@ -209,7 +210,7 @@ class _ProfilesFragmentState extends State<ProfilesFragment> {
child: _profileItem( child: _profileItem(
profile: profile, profile: profile,
groupValue: state.currentProfileId, groupValue: state.currentProfileId,
onChanged: context.appController.changeProfile, onChanged: context.appController.changeProfileDebounce,
), ),
), ),
], ],