Add proxies sort

This commit is contained in:
problematicconsumer
2023-09-06 17:14:24 +03:30
parent ef1846e553
commit 490b116427
5 changed files with 96 additions and 8 deletions

View File

@@ -28,4 +28,6 @@ enum ProxyType {
String get key => name;
static List<ProxyType> groupValues = [selector, urltest];
bool get isGroup => ProxyType.groupValues.contains(this);
}

View File

@@ -1,31 +1,86 @@
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;
},
);
}
Future<void> changeProxy(String groupTag, String outboundTag) async {

View File

@@ -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;