From a1763087cc06870aa28eb745b012a8581bc6415b Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Fri, 27 Oct 2023 17:52:04 +0330 Subject: [PATCH] Add selected tag for selector outbounds --- lib/domain/singbox/outbounds.dart | 7 ++++--- .../proxies/notifier/proxies_notifier.dart | 20 ++++++++++++++++++- lib/features/proxies/widgets/proxy_tile.dart | 15 +++++++++++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/lib/domain/singbox/outbounds.dart b/lib/domain/singbox/outbounds.dart index a9871256..e99d0b11 100644 --- a/lib/domain/singbox/outbounds.dart +++ b/lib/domain/singbox/outbounds.dart @@ -28,11 +28,9 @@ class OutboundGroupItem with _$OutboundGroupItem { required String tag, @JsonKey(fromJson: _typeFromJson) required ProxyType type, required int urlTestDelay, + String? selectedTag, }) = _OutboundGroupItem; - String get sanitizedTag => - tag.replaceFirst(RegExp(r"\§[^]*"), "").trimRight(); - factory OutboundGroupItem.fromJson(Map json) => _$OutboundGroupItemFromJson(json); } @@ -41,3 +39,6 @@ ProxyType _typeFromJson(dynamic type) => ProxyType.values .firstOrNullWhere((e) => e.key == (type as String?)?.toLowerCase()) ?? ProxyType.unknown; + +String sanitizedTag(String tag) => + tag.replaceFirst(RegExp(r"\§[^]*"), "").trimRight(); diff --git a/lib/features/proxies/notifier/proxies_notifier.dart b/lib/features/proxies/notifier/proxies_notifier.dart index e7904397..743d3d20 100644 --- a/lib/features/proxies/notifier/proxies_notifier.dart +++ b/lib/features/proxies/notifier/proxies_notifier.dart @@ -11,6 +11,7 @@ import 'package:hiddify/utils/pref_notifier.dart'; import 'package:hiddify/utils/riverpod_utils.dart'; import 'package:hiddify/utils/utils.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; +import 'package:rxdart/rxdart.dart'; part 'proxies_notifier.g.dart'; @@ -58,6 +59,11 @@ class ProxiesNotifier extends _$ProxiesNotifier with AppLogger { yield* ref .watch(coreFacadeProvider) .watchOutbounds() + .throttleTime( + const Duration(milliseconds: 100), + leading: false, + trailing: true, + ) .map( (event) => event.getOrElse( (err) { @@ -75,9 +81,12 @@ class ProxiesNotifier extends _$ProxiesNotifier with AppLogger { ) async { return CombineWorker().execute( () { + final groupWithSelected = { + for (final o in outbounds) o.tag: o.selected, + }; final sortedOutbounds = []; for (final group in outbounds) { - final items = switch (sortBy) { + final sortedItems = switch (sortBy) { ProxiesSort.name => group.items.sortedBy((e) => e.tag), ProxiesSort.delay => group.items.sortedWith((a, b) { final ai = a.urlTestDelay; @@ -90,6 +99,15 @@ class ProxiesNotifier extends _$ProxiesNotifier with AppLogger { }), ProxiesSort.unsorted => group.items, }; + final items = []; + for (final item in sortedItems) { + if (groupWithSelected.keys.contains(item.tag)) { + items + .add(item.copyWith(selectedTag: groupWithSelected[item.tag])); + } else { + items.add(item); + } + } sortedOutbounds.add(group.copyWith(items: items)); } return sortedOutbounds; diff --git a/lib/features/proxies/widgets/proxy_tile.dart b/lib/features/proxies/widgets/proxy_tile.dart index 9396c730..fe2a2375 100644 --- a/lib/features/proxies/widgets/proxy_tile.dart +++ b/lib/features/proxies/widgets/proxy_tile.dart @@ -21,7 +21,7 @@ class ProxyTile extends HookConsumerWidget { return ListTile( shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), title: Text( - proxy.sanitizedTag, + sanitizedTag(proxy.tag), overflow: TextOverflow.ellipsis, ), leading: Padding( @@ -35,8 +35,17 @@ class ProxyTile extends HookConsumerWidget { ), ), ), - subtitle: Text( - proxy.type.label, + subtitle: Text.rich( + TextSpan( + text: proxy.type.label, + children: [ + if (proxy.selectedTag != null) + TextSpan( + text: ' (${sanitizedTag(proxy.selectedTag!)})', + style: Theme.of(context).textTheme.bodySmall, + ), + ], + ), overflow: TextOverflow.ellipsis, ), trailing: proxy.urlTestDelay != 0