Skip to content

Commit

Permalink
connection button by proxy status indicator, change connected to conn…
Browse files Browse the repository at this point in the history
…ecting when timeout
  • Loading branch information
hiddify-com committed Aug 4, 2024
1 parent 7a697ac commit 92bf0fa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 51 deletions.
52 changes: 19 additions & 33 deletions lib/features/home/widget/connection_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:hiddify/features/connection/model/connection_status.dart';
import 'package:hiddify/features/connection/notifier/connection_notifier.dart';
import 'package:hiddify/features/connection/widget/experimental_feature_notice.dart';
import 'package:hiddify/features/profile/notifier/active_profile_notifier.dart';
import 'package:hiddify/features/proxy/active/active_proxy_notifier.dart';
import 'package:hiddify/gen/assets.gen.dart';
import 'package:hiddify/utils/alerts.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
Expand All @@ -23,8 +24,10 @@ class ConnectionButton extends HookConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
final connectionStatus = ref.watch(connectionNotifierProvider);
final requiresReconnect =
ref.watch(configOptionNotifierProvider).valueOrNull;
final activeProxy = ref.watch(activeProxyNotifierProvider);
final delay = activeProxy.valueOrNull?.urlTestDelay ?? 0;

final requiresReconnect = ref.watch(configOptionNotifierProvider).valueOrNull;
final today = DateTime.now();

ref.listen(
Expand All @@ -33,10 +36,8 @@ class ConnectionButton extends HookConsumerWidget {
if (next case AsyncError(:final error)) {
CustomAlertDialog.fromErr(t.presentError(error)).show(context);
}
if (next
case AsyncData(value: Disconnected(:final connectionFailure?))) {
CustomAlertDialog.fromErr(t.presentError(connectionFailure))
.show(context);
if (next case AsyncData(value: Disconnected(:final connectionFailure?))) {
CustomAlertDialog.fromErr(t.presentError(connectionFailure)).show(context);
}
},
);
Expand All @@ -47,8 +48,7 @@ class ConnectionButton extends HookConsumerWidget {
final hasExperimental = ref.read(ConfigOptions.hasExperimentalFeatures);
final canShowNotice = !ref.read(disableExperimentalFeatureNoticeProvider);
if (hasExperimental && canShowNotice && context.mounted) {
return await const ExperimentalFeatureNoticeDialog().show(context) ??
false;
return await const ExperimentalFeatureNoticeDialog().show(context) ?? false;
}
return true;
}
Expand All @@ -57,52 +57,40 @@ class ConnectionButton extends HookConsumerWidget {
onTap: switch (connectionStatus) {
AsyncData(value: Disconnected()) || AsyncError() => () async {
if (await showExperimentalNotice()) {
return await ref
.read(connectionNotifierProvider.notifier)
.toggleConnection();
return await ref.read(connectionNotifierProvider.notifier).toggleConnection();
}
},
AsyncData(value: Connected()) => () async {
if (requiresReconnect == true && await showExperimentalNotice()) {
return await ref
.read(connectionNotifierProvider.notifier)
.reconnect(await ref.read(activeProfileProvider.future));
return await ref.read(connectionNotifierProvider.notifier).reconnect(await ref.read(activeProfileProvider.future));
}
return await ref
.read(connectionNotifierProvider.notifier)
.toggleConnection();
return await ref.read(connectionNotifierProvider.notifier).toggleConnection();
},
_ => () {},
},
enabled: switch (connectionStatus) {
AsyncData(value: Connected()) ||
AsyncData(value: Disconnected()) ||
AsyncError() =>
true,
AsyncData(value: Connected()) || AsyncData(value: Disconnected()) || AsyncError() => true,
_ => false,
},
label: switch (connectionStatus) {
AsyncData(value: Connected()) when requiresReconnect == true =>
t.connection.reconnect,
AsyncData(value: Connected()) when requiresReconnect == true => t.connection.reconnect,
AsyncData(value: Connected()) when delay <= 0 || delay >= 65000 => t.connection.connecting,
AsyncData(value: final status) => status.present(t),
_ => "",
},
buttonColor: switch (connectionStatus) {
AsyncData(value: Connected()) when requiresReconnect == true =>
Colors.teal,
AsyncData(value: Connected()) when requiresReconnect == true => Colors.teal,
AsyncData(value: Connected()) when delay <= 0 || delay >= 65000 => Color.fromARGB(255, 185, 176, 103),
AsyncData(value: Connected()) => buttonTheme.connectedColor!,
AsyncData(value: _) => buttonTheme.idleColor!,
_ => Colors.red,
},
image: switch (connectionStatus) {
AsyncData(value: Connected()) when requiresReconnect == true =>
Assets.images.disconnectNorouz,
AsyncData(value: Connected()) when requiresReconnect == true => Assets.images.disconnectNorouz,
AsyncData(value: Connected()) => Assets.images.connectNorouz,
AsyncData(value: _) => Assets.images.disconnectNorouz,
_ => Assets.images.disconnectNorouz,
AsyncData(value: Disconnected()) ||
AsyncError() =>
Assets.images.disconnectNorouz,
AsyncData(value: Disconnected()) || AsyncError() => Assets.images.disconnectNorouz,
AsyncData(value: Connected()) => Assets.images.connectNorouz,
_ => Assets.images.disconnectNorouz,
},
Expand Down Expand Up @@ -177,9 +165,7 @@ class _ConnectionButton extends StatelessWidget {
),
),
).animate(target: enabled ? 0 : 1).blurXY(end: 1),
)
.animate(target: enabled ? 0 : 1)
.scaleXY(end: .88, curve: Curves.easeIn),
).animate(target: enabled ? 0 : 1).scaleXY(end: .88, curve: Curves.easeIn),
),
const Gap(16),
ExcludeSemantics(
Expand Down
10 changes: 4 additions & 6 deletions lib/features/home/widget/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:hiddify/features/profile/notifier/active_profile_notifier.dart';
import 'package:hiddify/features/profile/widget/profile_tile.dart';
import 'package:hiddify/features/proxy/active/active_proxy_delay_indicator.dart';
import 'package:hiddify/features/proxy/active/active_proxy_footer.dart';
import 'package:hiddify/features/proxy/active/active_proxy_notifier.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:sliver_tools/sliver_tools.dart';
Expand Down Expand Up @@ -76,20 +77,17 @@ class HomePage extends HookConsumerWidget {
],
),
),
if (MediaQuery.sizeOf(context).width < 840)
const ActiveProxyFooter(),
if (MediaQuery.sizeOf(context).width < 840) const ActiveProxyFooter(),
],
),
),
],
),
AsyncData() => switch (hasAnyProfile) {
AsyncData(value: true) =>
const EmptyActiveProfileHomeBody(),
AsyncData(value: true) => const EmptyActiveProfileHomeBody(),
_ => const EmptyProfilesHomeBody(),
},
AsyncError(:final error) =>
SliverErrorBodyPlaceholder(t.presentShortError(error)),
AsyncError(:final error) => SliverErrorBodyPlaceholder(t.presentShortError(error)),
_ => const SliverToBoxAdapter(),
},
],
Expand Down
20 changes: 10 additions & 10 deletions lib/features/proxy/active/active_proxy_delay_indicator.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import 'dart:io';

import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.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/shimmer_skeleton.dart';
import 'package:hiddify/features/connection/model/connection_status.dart';
import 'package:hiddify/features/proxy/active/active_proxy_notifier.dart';
import 'package:hiddify/features/system_tray/notifier/system_tray_notifier.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:tray_manager/tray_manager.dart';

class ActiveProxyDelayIndicator extends HookConsumerWidget {
const ActiveProxyDelayIndicator({super.key});
Expand All @@ -24,27 +29,23 @@ class ActiveProxyDelayIndicator extends HookConsumerWidget {
case AsyncData(value: final proxy):
final delay = proxy.urlTestDelay;
final timeout = delay > 65000;

return Center(
child: InkWell(
onTap: () async {
await ref
.read(activeProxyNotifierProvider.notifier)
.urlTest(proxy.tag);
await ref.read(activeProxyNotifierProvider.notifier).urlTest(proxy.tag);
},
borderRadius: BorderRadius.circular(24),
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
const Icon(FluentIcons.wifi_1_24_regular),
const Gap(8),
if (delay > 0)
Text.rich(
semanticsLabel: timeout
? t.proxies.delaySemantics.timeout
: t.proxies.delaySemantics.result(delay: delay),
semanticsLabel: timeout ? t.proxies.delaySemantics.timeout : t.proxies.delaySemantics.result(delay: delay),
TextSpan(
children: [
if (timeout)
Expand All @@ -58,8 +59,7 @@ class ActiveProxyDelayIndicator extends HookConsumerWidget {
else ...[
TextSpan(
text: delay.toString(),
style: theme.textTheme.titleMedium
?.copyWith(fontWeight: FontWeight.bold),
style: theme.textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold),
),
const TextSpan(text: " ms"),
],
Expand Down
4 changes: 2 additions & 2 deletions lib/features/proxy/active/active_proxy_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ class IpInfoNotifier extends _$IpInfoNotifier with AppLogger {
}
}

@riverpod
@Riverpod(keepAlive: true)
class ActiveProxyNotifier extends _$ActiveProxyNotifier with AppLogger {
@override
Stream<ProxyItemEntity> build() async* {
ref.disposeDelay(const Duration(seconds: 20));
// ref.disposeDelay(const Duration(seconds: 20));

final serviceRunning = await ref.watch(serviceRunningProvider.future);
if (!serviceRunning) {
Expand Down

0 comments on commit 92bf0fa

Please sign in to comment.