Add connection info footer
This commit is contained in:
149
lib/features/proxy/active/active_proxy_footer.dart
Normal file
149
lib/features/proxy/active/active_proxy_footer.dart
Normal file
@@ -0,0 +1,149 @@
|
||||
import 'package:circle_flags/circle_flags.dart';
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:gap/gap.dart';
|
||||
import 'package:hiddify/core/localization/translations.dart';
|
||||
import 'package:hiddify/core/widget/animated_visibility.dart';
|
||||
import 'package:hiddify/core/widget/skeleton_widget.dart';
|
||||
import 'package:hiddify/features/proxy/active/active_proxy_notifier.dart';
|
||||
import 'package:hiddify/features/stats/notifier/stats_notifier.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
class ActiveProxyFooter extends HookConsumerWidget {
|
||||
const ActiveProxyFooter({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final t = ref.watch(translationsProvider);
|
||||
final asyncState = ref.watch(activeProxyNotifierProvider);
|
||||
final stats = ref.watch(statsNotifierProvider).value;
|
||||
|
||||
return AnimatedVisibility(
|
||||
axis: Axis.vertical,
|
||||
visible: asyncState is AsyncData,
|
||||
child: switch (asyncState) {
|
||||
AsyncData(value: final info) => Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Flexible(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_InfoProp(
|
||||
icon: FluentIcons.arrow_routing_24_regular,
|
||||
text: info.proxy.selectedName.isNotNullOrBlank
|
||||
? info.proxy.selectedName!
|
||||
: info.proxy.name,
|
||||
),
|
||||
const Gap(8),
|
||||
switch (info.ipInfo) {
|
||||
AsyncData(value: final ip?) => _InfoProp.flag(
|
||||
countryCode: ip.countryCode,
|
||||
text: ip.ip,
|
||||
),
|
||||
AsyncError() => _InfoProp(
|
||||
icon: FluentIcons.error_circle_20_regular,
|
||||
text: t.general.unknown,
|
||||
),
|
||||
_ => _InfoProp.loading(
|
||||
icon: FluentIcons.question_circle_24_regular,
|
||||
),
|
||||
},
|
||||
],
|
||||
),
|
||||
),
|
||||
Directionality(
|
||||
textDirection: TextDirection.values[
|
||||
(Directionality.of(context).index + 1) %
|
||||
TextDirection.values.length],
|
||||
child: Flexible(
|
||||
child: Column(
|
||||
children: [
|
||||
_InfoProp(
|
||||
icon: FluentIcons
|
||||
.arrow_bidirectional_up_down_24_regular,
|
||||
text: (stats?.downlinkTotal ?? 0).size(),
|
||||
),
|
||||
const Gap(8),
|
||||
_InfoProp(
|
||||
icon: FluentIcons.arrow_download_24_regular,
|
||||
text: (stats?.downlink ?? 0).speed(),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
_ => const SizedBox(),
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _InfoProp extends StatelessWidget {
|
||||
_InfoProp({
|
||||
required IconData icon,
|
||||
required String text,
|
||||
}) : icon = Icon(icon),
|
||||
child = Text(
|
||||
text,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
isLoading = false;
|
||||
|
||||
_InfoProp.flag({
|
||||
required String countryCode,
|
||||
required String text,
|
||||
}) : icon = Container(
|
||||
width: 24,
|
||||
height: 24,
|
||||
padding: const EdgeInsets.all(2),
|
||||
child: CircleFlag(countryCode),
|
||||
),
|
||||
child = Text(
|
||||
text,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
isLoading = false;
|
||||
|
||||
_InfoProp.loading({
|
||||
required IconData icon,
|
||||
}) : icon = Icon(icon),
|
||||
child = const SizedBox(),
|
||||
isLoading = true;
|
||||
|
||||
final Widget icon;
|
||||
final Widget child;
|
||||
final bool isLoading;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Row(
|
||||
children: [
|
||||
icon,
|
||||
const Gap(8),
|
||||
if (isLoading)
|
||||
Flexible(
|
||||
child: const Skeleton(height: 16, widthFactor: 1)
|
||||
.animate(
|
||||
onPlay: (controller) => controller.loop(),
|
||||
)
|
||||
.shimmer(
|
||||
duration: 1000.ms,
|
||||
angle: 45,
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
)
|
||||
else
|
||||
Flexible(child: child),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
75
lib/features/proxy/active/active_proxy_notifier.dart
Normal file
75
lib/features/proxy/active/active_proxy_notifier.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
import 'package:dio/dio.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/model/ip_info_entity.dart';
|
||||
import 'package:hiddify/features/proxy/model/proxy_entity.dart';
|
||||
import 'package:hiddify/features/proxy/model/proxy_failure.dart';
|
||||
import 'package:hiddify/utils/riverpod_utils.dart';
|
||||
import 'package:hiddify/utils/utils.dart';
|
||||
import 'package:loggy/loggy.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'active_proxy_notifier.g.dart';
|
||||
|
||||
typedef ActiveProxyInfo = ({
|
||||
ProxyItemEntity proxy,
|
||||
AsyncValue<IpInfo?> ipInfo,
|
||||
});
|
||||
|
||||
@riverpod
|
||||
Stream<ProxyItemEntity?> activeProxyGroup(ActiveProxyGroupRef ref) async* {
|
||||
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
||||
if (!serviceRunning) {
|
||||
throw const ServiceNotRunning();
|
||||
}
|
||||
yield* ref
|
||||
.watch(proxyRepositoryProvider)
|
||||
.watchActiveProxies()
|
||||
.map((event) => event.getOrElse((l) => throw l))
|
||||
.map((event) => event.firstOrNull?.items.firstOrNull);
|
||||
}
|
||||
|
||||
@riverpod
|
||||
Future<IpInfo?> proxyIpInfo(ProxyIpInfoRef ref) async {
|
||||
final serviceRunning = await ref.watch(serviceRunningProvider.future);
|
||||
if (!serviceRunning) {
|
||||
return null;
|
||||
}
|
||||
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
|
||||
class ActiveProxyNotifier extends _$ActiveProxyNotifier with AppLogger {
|
||||
@override
|
||||
AsyncValue<ActiveProxyInfo> build() {
|
||||
ref.disposeDelay(const Duration(seconds: 20));
|
||||
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(),
|
||||
};
|
||||
}
|
||||
|
||||
Future<void> refreshIpInfo() async {
|
||||
if (state case AsyncData(:final value) when !value.ipInfo.isLoading) {
|
||||
ref.invalidate(proxyIpInfoProvider);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user