Fix connection info
This commit is contained in:
16
lib/core/utils/throttler.dart
Normal file
16
lib/core/utils/throttler.dart
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
|
||||||
|
class Throttler {
|
||||||
|
Throttler(this.throttleFor);
|
||||||
|
|
||||||
|
final Duration throttleFor;
|
||||||
|
DateTime? _lastCall;
|
||||||
|
|
||||||
|
void call(VoidCallback callback) {
|
||||||
|
if (_lastCall == null ||
|
||||||
|
DateTime.now().difference(_lastCall!) > throttleFor) {
|
||||||
|
callback();
|
||||||
|
_lastCall = DateTime.now();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,22 +12,21 @@ class ActiveProxyDelayIndicator extends HookConsumerWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
final asyncState = ref.watch(activeProxyNotifierProvider);
|
final activeProxy = ref.watch(activeProxyNotifierProvider);
|
||||||
|
|
||||||
return AnimatedVisibility(
|
return AnimatedVisibility(
|
||||||
axis: Axis.vertical,
|
axis: Axis.vertical,
|
||||||
visible: asyncState is AsyncData,
|
visible: activeProxy is AsyncData,
|
||||||
child: () {
|
child: () {
|
||||||
switch (asyncState) {
|
switch (activeProxy) {
|
||||||
case AsyncData(:final value):
|
case AsyncData(value: final proxy):
|
||||||
final delay = value.proxy.urlTestDelay;
|
final delay = proxy.urlTestDelay;
|
||||||
return Center(
|
return Center(
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
await ref
|
await ref
|
||||||
.read(activeProxyNotifierProvider.notifier)
|
.read(activeProxyNotifierProvider.notifier)
|
||||||
// .urlTest(value.proxy.tag);
|
.urlTest(proxy.tag);
|
||||||
.urlTest("auto");
|
|
||||||
},
|
},
|
||||||
borderRadius: BorderRadius.circular(24),
|
borderRadius: BorderRadius.circular(24),
|
||||||
child: Padding(
|
child: Padding(
|
||||||
|
|||||||
@@ -17,13 +17,14 @@ class ActiveProxyFooter extends HookConsumerWidget {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final t = ref.watch(translationsProvider);
|
final t = ref.watch(translationsProvider);
|
||||||
final asyncState = ref.watch(activeProxyNotifierProvider);
|
final activeProxy = ref.watch(activeProxyNotifierProvider);
|
||||||
|
final ipInfo = ref.watch(ipInfoNotifierProvider);
|
||||||
|
|
||||||
return AnimatedVisibility(
|
return AnimatedVisibility(
|
||||||
axis: Axis.vertical,
|
axis: Axis.vertical,
|
||||||
visible: asyncState is AsyncData,
|
visible: activeProxy is AsyncData,
|
||||||
child: switch (asyncState) {
|
child: switch (activeProxy) {
|
||||||
AsyncData(value: final info) => Padding(
|
AsyncData(value: final proxy) => Padding(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
||||||
child: Row(
|
child: Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
@@ -34,24 +35,22 @@ class ActiveProxyFooter extends HookConsumerWidget {
|
|||||||
children: [
|
children: [
|
||||||
_InfoProp(
|
_InfoProp(
|
||||||
icon: FluentIcons.arrow_routing_20_regular,
|
icon: FluentIcons.arrow_routing_20_regular,
|
||||||
text: info.proxy.selectedName.isNotNullOrBlank
|
text: proxy.selectedName.isNotNullOrBlank
|
||||||
? info.proxy.selectedName!
|
? proxy.selectedName!
|
||||||
: info.proxy.name,
|
: proxy.name,
|
||||||
),
|
),
|
||||||
const Gap(8),
|
const Gap(8),
|
||||||
switch (info.ipInfo) {
|
switch (ipInfo) {
|
||||||
AsyncData(value: final ipInfo?) => Row(
|
AsyncData(value: final info) => Row(
|
||||||
children: [
|
children: [
|
||||||
IPCountryFlag(countryCode: ipInfo.countryCode),
|
IPCountryFlag(countryCode: info.countryCode),
|
||||||
const Gap(8),
|
const Gap(8),
|
||||||
IPText(
|
IPText(
|
||||||
ip: ipInfo.ip,
|
ip: info.ip,
|
||||||
onLongPress: () async {
|
onLongPress: () async {
|
||||||
ref
|
ref
|
||||||
.read(
|
.read(ipInfoNotifierProvider.notifier)
|
||||||
activeProxyNotifierProvider.notifier,
|
.refresh();
|
||||||
)
|
|
||||||
.refreshIpInfo();
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:hiddify/core/utils/throttler.dart';
|
||||||
import 'package:hiddify/features/connection/notifier/connection_notifier.dart';
|
import 'package:hiddify/features/connection/notifier/connection_notifier.dart';
|
||||||
import 'package:hiddify/features/proxy/data/proxy_data_providers.dart';
|
import 'package:hiddify/features/proxy/data/proxy_data_providers.dart';
|
||||||
import 'package:hiddify/features/proxy/model/ip_info_entity.dart';
|
import 'package:hiddify/features/proxy/model/ip_info_entity.dart';
|
||||||
@@ -6,98 +7,65 @@ import 'package:hiddify/features/proxy/model/proxy_entity.dart';
|
|||||||
import 'package:hiddify/features/proxy/model/proxy_failure.dart';
|
import 'package:hiddify/features/proxy/model/proxy_failure.dart';
|
||||||
import 'package:hiddify/utils/riverpod_utils.dart';
|
import 'package:hiddify/utils/riverpod_utils.dart';
|
||||||
import 'package:hiddify/utils/utils.dart';
|
import 'package:hiddify/utils/utils.dart';
|
||||||
import 'package:loggy/loggy.dart';
|
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||||
|
|
||||||
part 'active_proxy_notifier.g.dart';
|
part 'active_proxy_notifier.g.dart';
|
||||||
|
|
||||||
typedef ActiveProxyInfo = ({
|
|
||||||
ProxyItemEntity proxy,
|
|
||||||
AsyncValue<IpInfo?> ipInfo,
|
|
||||||
});
|
|
||||||
|
|
||||||
@riverpod
|
@riverpod
|
||||||
Stream<ProxyItemEntity?> activeProxyGroup(ActiveProxyGroupRef ref) async* {
|
class IpInfoNotifier extends _$IpInfoNotifier with AppLogger {
|
||||||
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
@override
|
||||||
if (!serviceRunning) {
|
Future<IpInfo> build() async {
|
||||||
throw const ServiceNotRunning();
|
ref.disposeDelay(const Duration(seconds: 20));
|
||||||
}
|
final cancelToken = CancelToken();
|
||||||
yield* ref
|
ref.onDispose(() {
|
||||||
.watch(proxyRepositoryProvider)
|
loggy.debug("disposing");
|
||||||
.watchActiveProxies()
|
cancelToken.cancel();
|
||||||
.map((event) => event.getOrElse((l) => throw l))
|
|
||||||
.map((event) {
|
|
||||||
final Map<String, ProxyGroupEntity> itemMap =
|
|
||||||
event.fold({}, (Map<String, ProxyGroupEntity> map, item) {
|
|
||||||
map[item.tag] = item;
|
|
||||||
return map;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start from the item with the tag "Select"
|
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
||||||
var current = itemMap["select"];
|
if (!serviceRunning) {
|
||||||
ProxyItemEntity? selected;
|
throw const ServiceNotRunning();
|
||||||
|
|
||||||
// Traverse through the map based on the selected tag until reaching the end
|
|
||||||
while (current != null) {
|
|
||||||
selected = current.items.firstOrNull;
|
|
||||||
current = itemMap[selected?.tag];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return selected;
|
return ref
|
||||||
});
|
.watch(proxyRepositoryProvider)
|
||||||
}
|
.getCurrentIpInfo(cancelToken)
|
||||||
|
.getOrElse(
|
||||||
@riverpod
|
(err) {
|
||||||
Future<IpInfo?> proxyIpInfo(ProxyIpInfoRef ref) async {
|
loggy.error("error getting proxy ip info", err);
|
||||||
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
throw err;
|
||||||
if (!serviceRunning) {
|
},
|
||||||
return null;
|
).run();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> refresh() async {
|
||||||
|
loggy.debug("refreshing");
|
||||||
|
ref.invalidateSelf();
|
||||||
}
|
}
|
||||||
final cancelToken = CancelToken();
|
|
||||||
ref.onDispose(() {
|
|
||||||
Loggy("ProxyIpInfo").debug("canceling");
|
|
||||||
cancelToken.cancel();
|
|
||||||
});
|
|
||||||
return ref
|
|
||||||
.watch(proxyRepositoryProvider)
|
|
||||||
.getCurrentIpInfo(cancelToken)
|
|
||||||
.getOrElse(
|
|
||||||
(err) {
|
|
||||||
Loggy("ProxyIpInfo").error("error getting proxy ip info", err);
|
|
||||||
throw err;
|
|
||||||
},
|
|
||||||
).run();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@riverpod
|
@riverpod
|
||||||
class ActiveProxyNotifier extends _$ActiveProxyNotifier with AppLogger {
|
class ActiveProxyNotifier extends _$ActiveProxyNotifier with AppLogger {
|
||||||
@override
|
@override
|
||||||
AsyncValue<ActiveProxyInfo> build() {
|
Stream<ProxyItemEntity> build() async* {
|
||||||
ref.disposeDelay(const Duration(seconds: 20));
|
ref.disposeDelay(const Duration(seconds: 20));
|
||||||
ref.onDispose(() {
|
|
||||||
_urlTestDebouncer.dispose();
|
|
||||||
});
|
|
||||||
final ipInfo = ref.watch(proxyIpInfoProvider);
|
|
||||||
final activeProxies = ref.watch(activeProxyGroupProvider);
|
|
||||||
return switch (activeProxies) {
|
|
||||||
AsyncData(value: final activeGroup?) =>
|
|
||||||
AsyncData((proxy: activeGroup, ipInfo: ipInfo)),
|
|
||||||
AsyncError(:final error, :final stackTrace) =>
|
|
||||||
AsyncError(error, stackTrace),
|
|
||||||
_ => const AsyncLoading(),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
final _urlTestDebouncer = CallbackDebouncer(const Duration(seconds: 1));
|
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
||||||
|
if (!serviceRunning) {
|
||||||
Future<void> refreshIpInfo() async {
|
throw const ServiceNotRunning();
|
||||||
if (state case AsyncData(:final value) when !value.ipInfo.isLoading) {
|
|
||||||
ref.invalidate(proxyIpInfoProvider);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
yield* ref
|
||||||
|
.watch(proxyRepositoryProvider)
|
||||||
|
.watchActiveProxies()
|
||||||
|
.map((event) => event.getOrElse((l) => throw l))
|
||||||
|
.map((event) => event.firstOrNull!.items.first);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final _urlTestThrottler = Throttler(const Duration(seconds: 2));
|
||||||
|
|
||||||
Future<void> urlTest(String groupTag) async {
|
Future<void> urlTest(String groupTag) async {
|
||||||
_urlTestDebouncer(
|
_urlTestThrottler(
|
||||||
() async {
|
() async {
|
||||||
loggy.debug("testing group: [$groupTag]");
|
loggy.debug("testing group: [$groupTag]");
|
||||||
if (state case AsyncData()) {
|
if (state case AsyncData()) {
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ class ActiveProxySideBarCard extends HookConsumerWidget {
|
|||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
final t = ref.watch(translationsProvider);
|
final t = ref.watch(translationsProvider);
|
||||||
final asyncState = ref.watch(activeProxyNotifierProvider);
|
final activeProxy = ref.watch(activeProxyNotifierProvider);
|
||||||
|
final ipInfo = ref.watch(ipInfoNotifierProvider);
|
||||||
|
|
||||||
Widget propText(String txt) {
|
Widget propText(String txt) {
|
||||||
return Text(
|
return Text(
|
||||||
@@ -50,13 +51,13 @@ class ActiveProxySideBarCard extends HookConsumerWidget {
|
|||||||
children: [
|
children: [
|
||||||
Text(t.home.stats.connection),
|
Text(t.home.stats.connection),
|
||||||
const Gap(4),
|
const Gap(4),
|
||||||
switch (asyncState) {
|
switch (activeProxy) {
|
||||||
AsyncData(:final value) => buildProp(
|
AsyncData(value: final proxy) => buildProp(
|
||||||
const Icon(FluentIcons.arrow_routing_20_regular),
|
const Icon(FluentIcons.arrow_routing_20_regular),
|
||||||
propText(
|
propText(
|
||||||
value.proxy.selectedName.isNotNullOrBlank
|
proxy.selectedName.isNotNullOrBlank
|
||||||
? value.proxy.selectedName!
|
? proxy.selectedName!
|
||||||
: value.proxy.name,
|
: proxy.name,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
_ => buildProp(
|
_ => buildProp(
|
||||||
@@ -65,43 +66,29 @@ class ActiveProxySideBarCard extends HookConsumerWidget {
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
const Gap(4),
|
const Gap(4),
|
||||||
() {
|
switch (ipInfo) {
|
||||||
if (asyncState case AsyncData(:final value)) {
|
AsyncData(value: final info) => buildProp(
|
||||||
switch (value.ipInfo) {
|
IPCountryFlag(
|
||||||
case AsyncData(value: final ipInfo?):
|
countryCode: info.countryCode,
|
||||||
return buildProp(
|
size: 16,
|
||||||
IPCountryFlag(
|
),
|
||||||
countryCode: ipInfo.countryCode, size: 16),
|
IPText(
|
||||||
IPText(
|
ip: info.ip,
|
||||||
ip: ipInfo.ip,
|
onLongPress: () async {
|
||||||
onLongPress: () async {
|
ref.read(ipInfoNotifierProvider.notifier).refresh();
|
||||||
ref
|
},
|
||||||
.read(
|
constrained: true,
|
||||||
activeProxyNotifierProvider.notifier,
|
),
|
||||||
)
|
),
|
||||||
.refreshIpInfo();
|
AsyncLoading() => buildProp(
|
||||||
},
|
const Icon(FluentIcons.question_circle_20_regular),
|
||||||
constrained: true,
|
const ShimmerSkeleton(widthFactor: .85, height: 14),
|
||||||
),
|
),
|
||||||
);
|
_ => buildProp(
|
||||||
case AsyncError():
|
const Icon(FluentIcons.error_circle_20_regular),
|
||||||
return buildProp(
|
propText(t.general.unknown),
|
||||||
const Icon(FluentIcons.error_circle_20_regular),
|
),
|
||||||
propText(t.general.unknown),
|
},
|
||||||
);
|
|
||||||
case AsyncLoading():
|
|
||||||
return buildProp(
|
|
||||||
const Icon(FluentIcons.question_circle_20_regular),
|
|
||||||
const ShimmerSkeleton(widthFactor: .85, height: 14),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return buildProp(
|
|
||||||
const Icon(FluentIcons.question_circle_20_regular),
|
|
||||||
propText(t.general.unknown),
|
|
||||||
);
|
|
||||||
}(),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in New Issue
Block a user