Add proxies sort
This commit is contained in:
@@ -79,7 +79,12 @@
|
||||
"pageTitle": "proxies",
|
||||
"emptyProxiesMsg": "no proxies",
|
||||
"delayTestTooltip": "delay test",
|
||||
"cancelTestButtonText": "cancel test"
|
||||
"sortTooltip": "sort proxies",
|
||||
"sortOptions": {
|
||||
"unsorted": "Default",
|
||||
"name": "by Name",
|
||||
"delay": "by Delay"
|
||||
}
|
||||
},
|
||||
"logs": {
|
||||
"pageTitle": "logs",
|
||||
|
||||
@@ -79,7 +79,12 @@
|
||||
"pageTitle": "پراکسیها",
|
||||
"emptyProxiesMsg": "پراکسی وجود ندارد",
|
||||
"delayTestTooltip": "تست تاخیر",
|
||||
"cancelTestButtonText": "لغو تست"
|
||||
"sortTooltip": "مرتبسازی پراکسیها",
|
||||
"sortOptions": {
|
||||
"unsorted": "پیشفرض",
|
||||
"name": "براساس نام",
|
||||
"delay": "براساس تاخیر"
|
||||
}
|
||||
},
|
||||
"logs": {
|
||||
"pageTitle": "لاگها",
|
||||
|
||||
@@ -28,4 +28,6 @@ enum ProxyType {
|
||||
String get key => name;
|
||||
|
||||
static List<ProxyType> groupValues = [selector, urltest];
|
||||
|
||||
bool get isGroup => ProxyType.groupValues.contains(this);
|
||||
}
|
||||
|
||||
@@ -1,30 +1,85 @@
|
||||
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/domain/core_service_failure.dart';
|
||||
import 'package:hiddify/domain/singbox/singbox.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:riverpod_annotation/riverpod_annotation.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 {
|
||||
@override
|
||||
Stream<List<OutboundGroup>> build() async* {
|
||||
loggy.debug("building");
|
||||
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
||||
if (!serviceRunning) {
|
||||
throw const CoreServiceNotRunning();
|
||||
}
|
||||
yield* ref.watch(coreFacadeProvider).watchOutbounds().map(
|
||||
final sortBy = ref.watch(proxiesSortProvider);
|
||||
yield* ref
|
||||
.watch(coreFacadeProvider)
|
||||
.watchOutbounds()
|
||||
.map(
|
||||
(event) => event.getOrElse(
|
||||
(f) {
|
||||
loggy.warning("error receiving proxies", f);
|
||||
loggy.warning("error receiving proxies: $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 notifier = ref.watch(proxiesNotifierProvider.notifier);
|
||||
final sortBy = ref.watch(proxiesSortProvider);
|
||||
|
||||
final selectActiveProxyMutation = useMutation(
|
||||
initialOnFailure: (error) =>
|
||||
@@ -50,7 +51,27 @@ class ProxiesPage extends HookConsumerWidget with PresLogger {
|
||||
return Scaffold(
|
||||
body: CustomScrollView(
|
||||
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(
|
||||
builder: (context, constraints) {
|
||||
final width = constraints.crossAxisExtent;
|
||||
|
||||
Reference in New Issue
Block a user