Add misc preferences
This commit is contained in:
10
lib/core/prefs/misc_prefs.dart
Normal file
10
lib/core/prefs/misc_prefs.dart
Normal file
@@ -0,0 +1,10 @@
|
||||
import 'package:hiddify/domain/constants.dart';
|
||||
import 'package:hiddify/utils/pref_notifier.dart';
|
||||
|
||||
final connectionTestUrlProvider =
|
||||
PrefNotifier.provider("connection_test_url", Defaults.connectionTestUrl);
|
||||
|
||||
final concurrentTestCountProvider = PrefNotifier.provider(
|
||||
"concurrent_test_count",
|
||||
Defaults.concurrentTestCount,
|
||||
);
|
||||
@@ -149,7 +149,7 @@ class CoreFacadeImpl with ExceptionHandler, InfraLogger implements CoreFacade {
|
||||
@override
|
||||
TaskEither<CoreServiceFailure, int> testDelay(
|
||||
String proxyName, {
|
||||
String testUrl = Defaults.delayTestUrl,
|
||||
String testUrl = Defaults.connectionTestUrl,
|
||||
}) {
|
||||
return exceptionHandler(
|
||||
() async {
|
||||
|
||||
@@ -17,7 +17,7 @@ abstract class ClashFacade {
|
||||
|
||||
TaskEither<CoreServiceFailure, int> testDelay(
|
||||
String proxyName, {
|
||||
String testUrl = Defaults.delayTestUrl,
|
||||
String testUrl = Defaults.connectionTestUrl,
|
||||
});
|
||||
|
||||
Stream<Either<CoreServiceFailure, ClashTraffic>> watchTraffic();
|
||||
|
||||
@@ -14,5 +14,6 @@ abstract class Constants {
|
||||
abstract class Defaults {
|
||||
static const clashApiPort = 9090;
|
||||
static const mixedPort = 2334;
|
||||
static const delayTestUrl = "https://www.gstatic.com/generate_204";
|
||||
static const connectionTestUrl = "https://www.gstatic.com/generate_204";
|
||||
static const concurrentTestCount = 5;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:hiddify/core/prefs/misc_prefs.dart';
|
||||
import 'package:hiddify/data/data_providers.dart';
|
||||
import 'package:hiddify/domain/clash/clash.dart';
|
||||
import 'package:hiddify/features/common/active_profile/active_profile_notifier.dart';
|
||||
@@ -36,7 +37,12 @@ class ProxiesDelayNotifier extends _$ProxiesDelayNotifier with AppLogger {
|
||||
StreamSubscription? _currentTest;
|
||||
|
||||
Future<void> testDelay(Iterable<String> proxies) async {
|
||||
loggy.debug('testing delay for [${proxies.length}] proxies');
|
||||
final testUrl = ref.read(connectionTestUrlProvider);
|
||||
final concurrent = ref.read(concurrentTestCountProvider);
|
||||
|
||||
loggy.debug(
|
||||
'testing delay for [${proxies.length}] proxies with [$testUrl], [$concurrent] at a time',
|
||||
);
|
||||
|
||||
// cancel possible running test
|
||||
await _currentTest?.cancel();
|
||||
@@ -51,18 +57,21 @@ class ProxiesDelayNotifier extends _$ProxiesDelayNotifier with AppLogger {
|
||||
name,
|
||||
(_) => delay,
|
||||
ifAbsent: () => delay,
|
||||
)
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
_currentTest = Stream.fromIterable(proxies)
|
||||
.bufferCount(5)
|
||||
.bufferCount(concurrent)
|
||||
.asyncMap(
|
||||
(chunk) => Future.wait(
|
||||
chunk.map(
|
||||
(e) async => setDelay(
|
||||
e,
|
||||
await _clash.testDelay(e).getOrElse((l) => -1).run(),
|
||||
await _clash
|
||||
.testDelay(e, testUrl: testUrl)
|
||||
.getOrElse((l) => -1)
|
||||
.run(),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
41
lib/utils/pref_notifier.dart
Normal file
41
lib/utils/pref_notifier.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:hiddify/data/data_providers.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class PrefNotifier<T> extends AutoDisposeNotifier<T> {
|
||||
PrefNotifier(this._key, this._defaultValue);
|
||||
|
||||
final String _key;
|
||||
final T _defaultValue;
|
||||
|
||||
static AutoDisposeNotifierProvider<PrefNotifier<T>, T> provider<T>(
|
||||
String key,
|
||||
T defaultValue,
|
||||
) =>
|
||||
NotifierProvider.autoDispose(
|
||||
() => PrefNotifier(key, defaultValue),
|
||||
);
|
||||
|
||||
SharedPreferences get _prefs => ref.read(sharedPreferencesProvider);
|
||||
|
||||
/// Updates the value asynchronously.
|
||||
Future<void> update(T value) async {
|
||||
if (value is String) {
|
||||
await _prefs.setString(_key, value);
|
||||
} else if (value is bool) {
|
||||
await _prefs.setBool(_key, value);
|
||||
} else if (value is int) {
|
||||
await _prefs.setInt(_key, value);
|
||||
} else if (value is double) {
|
||||
await _prefs.setDouble(_key, value);
|
||||
} else if (value is List<String>) {
|
||||
await _prefs.setStringList(_key, value);
|
||||
}
|
||||
super.state = value;
|
||||
}
|
||||
|
||||
@override
|
||||
T build() {
|
||||
return _prefs.get(_key) as T? ?? _defaultValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user