2023-07-06 17:18:41 +03:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
|
|
|
|
import 'package:gap/gap.dart';
|
2023-12-01 12:56:24 +03:30
|
|
|
import 'package:hiddify/core/localization/translations.dart';
|
|
|
|
|
import 'package:hiddify/core/model/failures.dart';
|
|
|
|
|
import 'package:hiddify/core/theme/theme_extensions.dart';
|
2024-03-08 15:07:45 +03:30
|
|
|
import 'package:hiddify/core/widget/animated_text.dart';
|
2024-03-02 22:53:14 +03:30
|
|
|
import 'package:hiddify/features/config_option/data/config_option_repository.dart';
|
2024-03-07 19:17:05 +03:30
|
|
|
import 'package:hiddify/features/config_option/notifier/config_option_notifier.dart';
|
2023-12-01 12:56:24 +03:30
|
|
|
import 'package:hiddify/features/connection/model/connection_status.dart';
|
|
|
|
|
import 'package:hiddify/features/connection/notifier/connection_notifier.dart';
|
2023-12-31 10:28:52 +03:30
|
|
|
import 'package:hiddify/features/connection/widget/experimental_feature_notice.dart';
|
2024-03-07 19:17:05 +03:30
|
|
|
import 'package:hiddify/features/profile/notifier/active_profile_notifier.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/gen/assets.gen.dart';
|
2023-08-19 22:27:23 +03:30
|
|
|
import 'package:hiddify/utils/alerts.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
|
|
|
|
|
|
// TODO: rewrite
|
|
|
|
|
class ConnectionButton extends HookConsumerWidget {
|
|
|
|
|
const ConnectionButton({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
|
final t = ref.watch(translationsProvider);
|
2023-12-01 12:56:24 +03:30
|
|
|
final connectionStatus = ref.watch(connectionNotifierProvider);
|
2024-03-07 19:17:05 +03:30
|
|
|
final requiresReconnect =
|
|
|
|
|
ref.watch(configOptionNotifierProvider).valueOrNull;
|
2024-03-17 03:53:11 +03:30
|
|
|
final today = DateTime.now();
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-08-19 22:27:23 +03:30
|
|
|
ref.listen(
|
2023-12-01 12:56:24 +03:30
|
|
|
connectionNotifierProvider,
|
2023-08-19 22:27:23 +03:30
|
|
|
(_, next) {
|
|
|
|
|
if (next case AsyncError(:final error)) {
|
2023-08-26 17:01:51 +03:30
|
|
|
CustomAlertDialog.fromErr(t.presentError(error)).show(context);
|
2023-08-19 22:27:23 +03:30
|
|
|
}
|
|
|
|
|
if (next
|
|
|
|
|
case AsyncData(value: Disconnected(:final connectionFailure?))) {
|
2023-08-26 17:01:51 +03:30
|
|
|
CustomAlertDialog.fromErr(t.presentError(connectionFailure))
|
|
|
|
|
.show(context);
|
2023-08-19 22:27:23 +03:30
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
2023-09-06 12:56:30 +03:30
|
|
|
final buttonTheme = Theme.of(context).extension<ConnectionButtonTheme>()!;
|
|
|
|
|
|
2024-03-07 19:17:05 +03:30
|
|
|
Future<bool> showExperimentalNotice() async {
|
|
|
|
|
final hasExperimental = ref.read(ConfigOptions.hasExperimentalFeatures);
|
|
|
|
|
final canShowNotice = !ref.read(disableExperimentalFeatureNoticeProvider);
|
|
|
|
|
if (hasExperimental && canShowNotice && context.mounted) {
|
|
|
|
|
return await const ExperimentalFeatureNoticeDialog().show(context) ??
|
2024-03-08 17:15:17 +03:30
|
|
|
false;
|
2024-03-07 19:17:05 +03:30
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-12-31 10:28:52 +03:30
|
|
|
|
2024-03-07 19:17:05 +03:30
|
|
|
return _ConnectionButton(
|
|
|
|
|
onTap: switch (connectionStatus) {
|
|
|
|
|
AsyncData(value: Disconnected()) || AsyncError() => () async {
|
|
|
|
|
if (await showExperimentalNotice()) {
|
|
|
|
|
return await ref
|
2023-12-31 10:28:52 +03:30
|
|
|
.read(connectionNotifierProvider.notifier)
|
|
|
|
|
.toggleConnection();
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-03-07 19:17:05 +03:30
|
|
|
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)
|
|
|
|
|
.toggleConnection();
|
|
|
|
|
},
|
|
|
|
|
_ => () {},
|
|
|
|
|
},
|
|
|
|
|
enabled: switch (connectionStatus) {
|
|
|
|
|
AsyncData(value: Connected()) ||
|
|
|
|
|
AsyncData(value: Disconnected()) ||
|
|
|
|
|
AsyncError() =>
|
|
|
|
|
true,
|
|
|
|
|
_ => false,
|
|
|
|
|
},
|
|
|
|
|
label: switch (connectionStatus) {
|
|
|
|
|
AsyncData(value: Connected()) when requiresReconnect == true =>
|
2024-03-08 17:24:43 +03:30
|
|
|
t.connection.reconnect,
|
2024-03-07 19:17:05 +03:30
|
|
|
AsyncData(value: final status) => status.present(t),
|
|
|
|
|
_ => "",
|
|
|
|
|
},
|
|
|
|
|
buttonColor: switch (connectionStatus) {
|
|
|
|
|
AsyncData(value: Connected()) when requiresReconnect == true =>
|
|
|
|
|
Colors.teal,
|
|
|
|
|
AsyncData(value: Connected()) => buttonTheme.connectedColor!,
|
|
|
|
|
AsyncData(value: _) => buttonTheme.idleColor!,
|
|
|
|
|
_ => Colors.red,
|
|
|
|
|
},
|
2024-03-17 14:45:15 +01:00
|
|
|
image: switch (connectionStatus) {
|
2024-03-17 01:54:05 +03:30
|
|
|
AsyncData(value: Connected()) when requiresReconnect == true =>
|
2024-03-17 14:45:15 +01:00
|
|
|
Assets.images.disconnectNorouz,
|
2024-03-17 01:54:05 +03:30
|
|
|
AsyncData(value: Connected()) => Assets.images.connectNorouz,
|
|
|
|
|
AsyncData(value: _) => Assets.images.disconnectNorouz,
|
|
|
|
|
_ => Assets.images.disconnectNorouz,
|
2024-03-17 14:45:15 +01:00
|
|
|
AsyncData(value: Disconnected()) ||
|
|
|
|
|
AsyncError() =>
|
|
|
|
|
Assets.images.disconnectNorouz,
|
|
|
|
|
AsyncData(value: Connected()) => Assets.images.connectNorouz,
|
|
|
|
|
_ => Assets.images.disconnectNorouz,
|
2024-03-17 03:53:11 +03:30
|
|
|
},
|
2024-03-17 14:45:15 +01:00
|
|
|
useImage: today.day >= 19 && today.day <= 23 && today.month == 3,
|
2024-03-07 19:17:05 +03:30
|
|
|
);
|
2023-08-19 22:27:23 +03:30
|
|
|
}
|
|
|
|
|
}
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-08-19 22:27:23 +03:30
|
|
|
class _ConnectionButton extends StatelessWidget {
|
|
|
|
|
const _ConnectionButton({
|
|
|
|
|
required this.onTap,
|
|
|
|
|
required this.enabled,
|
|
|
|
|
required this.label,
|
|
|
|
|
required this.buttonColor,
|
2024-03-17 01:54:05 +03:30
|
|
|
required this.image,
|
2024-03-17 03:53:11 +03:30
|
|
|
required this.useImage,
|
2023-08-19 22:27:23 +03:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final VoidCallback onTap;
|
|
|
|
|
final bool enabled;
|
|
|
|
|
final String label;
|
|
|
|
|
final Color buttonColor;
|
2024-03-17 01:54:05 +03:30
|
|
|
final AssetGenImage image;
|
2024-03-17 03:53:11 +03:30
|
|
|
final bool useImage;
|
2023-08-19 22:27:23 +03:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2023-07-06 17:18:41 +03:30
|
|
|
return Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
2023-09-02 21:09:22 +03:30
|
|
|
Semantics(
|
|
|
|
|
button: true,
|
|
|
|
|
enabled: enabled,
|
|
|
|
|
label: label,
|
|
|
|
|
child: Container(
|
|
|
|
|
clipBehavior: Clip.antiAlias,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
|
boxShadow: [
|
|
|
|
|
BoxShadow(
|
|
|
|
|
blurRadius: 16,
|
|
|
|
|
color: buttonColor.withOpacity(0.5),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
width: 148,
|
|
|
|
|
height: 148,
|
|
|
|
|
child: Material(
|
2023-09-20 12:42:06 +03:30
|
|
|
key: const ValueKey("home_connection_button"),
|
2023-09-02 21:09:22 +03:30
|
|
|
shape: const CircleBorder(),
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
child: InkWell(
|
|
|
|
|
onTap: onTap,
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.all(36),
|
2024-03-07 19:17:05 +03:30
|
|
|
child: TweenAnimationBuilder(
|
|
|
|
|
tween: ColorTween(end: buttonColor),
|
|
|
|
|
duration: const Duration(milliseconds: 250),
|
|
|
|
|
builder: (context, value, child) {
|
2024-03-17 14:45:15 +01:00
|
|
|
if (useImage) {
|
|
|
|
|
return image.image(filterQuality: FilterQuality.medium);
|
|
|
|
|
} else {
|
2024-03-17 03:53:11 +03:30
|
|
|
return Assets.images.logo.svg(
|
|
|
|
|
colorFilter: ColorFilter.mode(
|
|
|
|
|
value!,
|
|
|
|
|
BlendMode.srcIn,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-03-07 19:17:05 +03:30
|
|
|
},
|
2023-07-06 17:18:41 +03:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2023-09-02 21:09:22 +03:30
|
|
|
).animate(target: enabled ? 0 : 1).blurXY(end: 1),
|
|
|
|
|
)
|
|
|
|
|
.animate(target: enabled ? 0 : 1)
|
|
|
|
|
.scaleXY(end: .88, curve: Curves.easeIn),
|
|
|
|
|
),
|
2023-07-06 17:18:41 +03:30
|
|
|
const Gap(16),
|
2024-02-13 18:49:58 +03:30
|
|
|
ExcludeSemantics(
|
2024-03-08 15:07:45 +03:30
|
|
|
child: AnimatedText(
|
|
|
|
|
label,
|
|
|
|
|
style: Theme.of(context).textTheme.titleMedium,
|
2024-02-13 18:49:58 +03:30
|
|
|
),
|
2023-07-06 17:18:41 +03:30
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|