Skip to content

Commit

Permalink
add changeProfileDebounce
Browse files Browse the repository at this point in the history
  • Loading branch information
chen08209 committed May 4, 2024
1 parent 184d2d1 commit ecd1bca
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
1 change: 0 additions & 1 deletion core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ func applyConfig(isPatch bool) bool {
patchConfig(cfg.General)
} else {
executor.ApplyConfig(cfg, true)

}
return true
}
20 changes: 13 additions & 7 deletions lib/clash/core.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions lib/common/function.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Function debounce<F extends Function>(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);
});
};
}
45 changes: 34 additions & 11 deletions lib/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
if (value == config.currentProfileId) return;
config.currentProfileId = value;
Expand All @@ -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();
Expand All @@ -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));
}
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -283,7 +300,7 @@ class AppController {

initLink() {
linkManager.initAppLinksListen(
(url) {
(url) {
globalState.showMessage(
title: "${appLocalizations.add}${appLocalizations.profile}",
message: TextSpan(
Expand All @@ -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: () {
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion lib/fragments/profiles/profiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ProfilesFragment extends StatefulWidget {
}

class _ProfilesFragmentState extends State<ProfilesFragment> {

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

0 comments on commit ecd1bca

Please sign in to comment.