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';
|
|
|
|
|
import 'package:hiddify/core/core_providers.dart';
|
|
|
|
|
import 'package:hiddify/core/theme/theme.dart';
|
2023-08-19 22:27:23 +03:30
|
|
|
import 'package:hiddify/domain/connectivity/connectivity.dart';
|
|
|
|
|
import 'package:hiddify/domain/failures.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
import 'package:hiddify/features/common/connectivity/connectivity_controller.dart';
|
|
|
|
|
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';
|
2023-07-27 23:46:21 +03:30
|
|
|
import 'package:recase/recase.dart';
|
2023-07-06 17:18:41 +03:30
|
|
|
|
|
|
|
|
// TODO: rewrite
|
|
|
|
|
class ConnectionButton extends HookConsumerWidget {
|
|
|
|
|
const ConnectionButton({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
|
final t = ref.watch(translationsProvider);
|
|
|
|
|
final connectionStatus = ref.watch(connectivityControllerProvider);
|
|
|
|
|
|
2023-08-19 22:27:23 +03:30
|
|
|
ref.listen(
|
|
|
|
|
connectivityControllerProvider,
|
|
|
|
|
(_, 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
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
switch (connectionStatus) {
|
|
|
|
|
case AsyncData(value: final status):
|
|
|
|
|
final Color connectionLogoColor = status.isConnected
|
|
|
|
|
? ConnectionButtonColor.connected
|
|
|
|
|
: ConnectionButtonColor.disconnected;
|
2023-07-06 17:18:41 +03:30
|
|
|
|
2023-08-19 22:27:23 +03:30
|
|
|
return _ConnectionButton(
|
|
|
|
|
onTap: () => ref
|
|
|
|
|
.read(connectivityControllerProvider.notifier)
|
|
|
|
|
.toggleConnection(),
|
|
|
|
|
enabled: !status.isSwitching,
|
|
|
|
|
label: status.present(t),
|
|
|
|
|
buttonColor: connectionLogoColor,
|
|
|
|
|
);
|
|
|
|
|
case AsyncError():
|
|
|
|
|
return _ConnectionButton(
|
|
|
|
|
onTap: () => ref
|
|
|
|
|
.read(connectivityControllerProvider.notifier)
|
|
|
|
|
.toggleConnection(),
|
|
|
|
|
enabled: true,
|
|
|
|
|
label: const Disconnected().present(t),
|
|
|
|
|
buttonColor: ConnectionButtonColor.disconnected,
|
|
|
|
|
);
|
|
|
|
|
default:
|
|
|
|
|
// HACK
|
|
|
|
|
return _ConnectionButton(
|
|
|
|
|
onTap: () {},
|
|
|
|
|
enabled: false,
|
|
|
|
|
label: "",
|
|
|
|
|
buttonColor: Colors.red,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
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,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final VoidCallback onTap;
|
|
|
|
|
final bool enabled;
|
|
|
|
|
final String label;
|
|
|
|
|
final Color buttonColor;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2023-07-06 17:18:41 +03:30
|
|
|
return Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
clipBehavior: Clip.antiAlias,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
|
boxShadow: [
|
|
|
|
|
BoxShadow(
|
|
|
|
|
blurRadius: 16,
|
2023-08-19 22:27:23 +03:30
|
|
|
color: buttonColor.withOpacity(0.5),
|
2023-07-06 17:18:41 +03:30
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
width: 148,
|
|
|
|
|
height: 148,
|
|
|
|
|
child: Material(
|
|
|
|
|
shape: const CircleBorder(),
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
child: InkWell(
|
2023-08-19 22:27:23 +03:30
|
|
|
onTap: onTap,
|
2023-07-06 17:18:41 +03:30
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.all(36),
|
|
|
|
|
child: Assets.images.logo.svg(
|
|
|
|
|
colorFilter: ColorFilter.mode(
|
2023-08-19 22:27:23 +03:30
|
|
|
buttonColor,
|
2023-07-06 17:18:41 +03:30
|
|
|
BlendMode.srcIn,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2023-08-19 22:27:23 +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),
|
|
|
|
|
Text(
|
2023-08-19 22:27:23 +03:30
|
|
|
label.sentenceCase,
|
2023-07-06 17:18:41 +03:30
|
|
|
style: Theme.of(context).textTheme.bodyLarge,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|