Add proxies sort
This commit is contained in:
@@ -79,7 +79,12 @@
|
|||||||
"pageTitle": "proxies",
|
"pageTitle": "proxies",
|
||||||
"emptyProxiesMsg": "no proxies",
|
"emptyProxiesMsg": "no proxies",
|
||||||
"delayTestTooltip": "delay test",
|
"delayTestTooltip": "delay test",
|
||||||
"cancelTestButtonText": "cancel test"
|
"sortTooltip": "sort proxies",
|
||||||
|
"sortOptions": {
|
||||||
|
"unsorted": "Default",
|
||||||
|
"name": "by Name",
|
||||||
|
"delay": "by Delay"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"logs": {
|
"logs": {
|
||||||
"pageTitle": "logs",
|
"pageTitle": "logs",
|
||||||
|
|||||||
@@ -79,7 +79,12 @@
|
|||||||
"pageTitle": "پراکسیها",
|
"pageTitle": "پراکسیها",
|
||||||
"emptyProxiesMsg": "پراکسی وجود ندارد",
|
"emptyProxiesMsg": "پراکسی وجود ندارد",
|
||||||
"delayTestTooltip": "تست تاخیر",
|
"delayTestTooltip": "تست تاخیر",
|
||||||
"cancelTestButtonText": "لغو تست"
|
"sortTooltip": "مرتبسازی پراکسیها",
|
||||||
|
"sortOptions": {
|
||||||
|
"unsorted": "پیشفرض",
|
||||||
|
"name": "براساس نام",
|
||||||
|
"delay": "براساس تاخیر"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"logs": {
|
"logs": {
|
||||||
"pageTitle": "لاگها",
|
"pageTitle": "لاگها",
|
||||||
|
|||||||
@@ -28,4 +28,6 @@ enum ProxyType {
|
|||||||
String get key => name;
|
String get key => name;
|
||||||
|
|
||||||
static List<ProxyType> groupValues = [selector, urltest];
|
static List<ProxyType> groupValues = [selector, urltest];
|
||||||
|
|
||||||
|
bool get isGroup => ProxyType.groupValues.contains(this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,85 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:combine/combine.dart';
|
||||||
|
import 'package:dartx/dartx.dart';
|
||||||
|
import 'package:hiddify/core/prefs/prefs.dart';
|
||||||
import 'package:hiddify/data/data_providers.dart';
|
import 'package:hiddify/data/data_providers.dart';
|
||||||
import 'package:hiddify/domain/core_service_failure.dart';
|
import 'package:hiddify/domain/core_service_failure.dart';
|
||||||
import 'package:hiddify/domain/singbox/singbox.dart';
|
import 'package:hiddify/domain/singbox/singbox.dart';
|
||||||
import 'package:hiddify/features/common/connectivity/connectivity_controller.dart';
|
import 'package:hiddify/features/common/connectivity/connectivity_controller.dart';
|
||||||
|
import 'package:hiddify/utils/pref_notifier.dart';
|
||||||
import 'package:hiddify/utils/utils.dart';
|
import 'package:hiddify/utils/utils.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
|
|
||||||
part 'proxies_notifier.g.dart';
|
part 'proxies_notifier.g.dart';
|
||||||
|
|
||||||
@Riverpod(keepAlive: true)
|
enum ProxiesSort {
|
||||||
|
unsorted,
|
||||||
|
name,
|
||||||
|
delay;
|
||||||
|
|
||||||
|
String present(TranslationsEn t) => switch (this) {
|
||||||
|
ProxiesSort.unsorted => t.proxies.sortOptions.unsorted,
|
||||||
|
ProxiesSort.name => t.proxies.sortOptions.name,
|
||||||
|
ProxiesSort.delay => t.proxies.sortOptions.delay,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
final proxiesSortProvider = AlwaysAlivePrefNotifier.provider(
|
||||||
|
"proxies_sort_mode",
|
||||||
|
ProxiesSort.unsorted,
|
||||||
|
mapFrom: ProxiesSort.values.byName,
|
||||||
|
mapTo: (value) => value.name,
|
||||||
|
);
|
||||||
|
|
||||||
|
@riverpod
|
||||||
class ProxiesNotifier extends _$ProxiesNotifier with AppLogger {
|
class ProxiesNotifier extends _$ProxiesNotifier with AppLogger {
|
||||||
@override
|
@override
|
||||||
Stream<List<OutboundGroup>> build() async* {
|
Stream<List<OutboundGroup>> build() async* {
|
||||||
loggy.debug("building");
|
|
||||||
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
||||||
if (!serviceRunning) {
|
if (!serviceRunning) {
|
||||||
throw const CoreServiceNotRunning();
|
throw const CoreServiceNotRunning();
|
||||||
}
|
}
|
||||||
yield* ref.watch(coreFacadeProvider).watchOutbounds().map(
|
final sortBy = ref.watch(proxiesSortProvider);
|
||||||
|
yield* ref
|
||||||
|
.watch(coreFacadeProvider)
|
||||||
|
.watchOutbounds()
|
||||||
|
.map(
|
||||||
(event) => event.getOrElse(
|
(event) => event.getOrElse(
|
||||||
(f) {
|
(f) {
|
||||||
loggy.warning("error receiving proxies", f);
|
loggy.warning("error receiving proxies: $f");
|
||||||
throw f;
|
throw f;
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
)
|
||||||
|
.asyncMap((proxies) async => _sortOutbounds(proxies, sortBy));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<OutboundGroup>> _sortOutbounds(
|
||||||
|
List<OutboundGroup> outbounds,
|
||||||
|
ProxiesSort sortBy,
|
||||||
|
) async {
|
||||||
|
return CombineWorker().execute(
|
||||||
|
() {
|
||||||
|
final sortedOutbounds = <OutboundGroup>[];
|
||||||
|
for (final group in outbounds) {
|
||||||
|
final items = switch (sortBy) {
|
||||||
|
ProxiesSort.name => group.items.sortedBy((e) => e.tag),
|
||||||
|
ProxiesSort.delay => group.items.sortedWith((a, b) {
|
||||||
|
final ai = a.urlTestDelay;
|
||||||
|
final bi = b.urlTestDelay;
|
||||||
|
if (ai == 0 && bi == 0) return -1;
|
||||||
|
if (ai == 0 && bi > 0) return 1;
|
||||||
|
if (ai > 0 && bi == 0) return -1;
|
||||||
|
if (ai == bi && a.type.isGroup) return -1;
|
||||||
|
return ai.compareTo(bi);
|
||||||
|
}),
|
||||||
|
ProxiesSort.unsorted => group.items,
|
||||||
|
};
|
||||||
|
sortedOutbounds.add(group.copyWith(items: items));
|
||||||
|
}
|
||||||
|
return sortedOutbounds;
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ class ProxiesPage extends HookConsumerWidget with PresLogger {
|
|||||||
|
|
||||||
final asyncProxies = ref.watch(proxiesNotifierProvider);
|
final asyncProxies = ref.watch(proxiesNotifierProvider);
|
||||||
final notifier = ref.watch(proxiesNotifierProvider.notifier);
|
final notifier = ref.watch(proxiesNotifierProvider.notifier);
|
||||||
|
final sortBy = ref.watch(proxiesSortProvider);
|
||||||
|
|
||||||
final selectActiveProxyMutation = useMutation(
|
final selectActiveProxyMutation = useMutation(
|
||||||
initialOnFailure: (error) =>
|
initialOnFailure: (error) =>
|
||||||
@@ -50,7 +51,27 @@ class ProxiesPage extends HookConsumerWidget with PresLogger {
|
|||||||
return Scaffold(
|
return Scaffold(
|
||||||
body: CustomScrollView(
|
body: CustomScrollView(
|
||||||
slivers: [
|
slivers: [
|
||||||
NestedTabAppBar(title: Text(t.proxies.pageTitle.titleCase)),
|
NestedTabAppBar(
|
||||||
|
title: Text(t.proxies.pageTitle.titleCase),
|
||||||
|
actions: [
|
||||||
|
PopupMenuButton<ProxiesSort>(
|
||||||
|
initialValue: sortBy,
|
||||||
|
onSelected: ref.read(proxiesSortProvider.notifier).update,
|
||||||
|
icon: const Icon(Icons.sort),
|
||||||
|
tooltip: t.proxies.sortTooltip,
|
||||||
|
itemBuilder: (context) {
|
||||||
|
return [
|
||||||
|
...ProxiesSort.values.map(
|
||||||
|
(e) => PopupMenuItem(
|
||||||
|
value: e,
|
||||||
|
child: Text(e.present(t)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
SliverLayoutBuilder(
|
SliverLayoutBuilder(
|
||||||
builder: (context, constraints) {
|
builder: (context, constraints) {
|
||||||
final width = constraints.crossAxisExtent;
|
final width = constraints.crossAxisExtent;
|
||||||
|
|||||||
Reference in New Issue
Block a user