2023-07-26 14:17:11 +03:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
|
|
|
import 'package:gap/gap.dart';
|
|
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
|
import 'package:hiddify/core/core_providers.dart';
|
|
|
|
|
import 'package:hiddify/core/router/router.dart';
|
|
|
|
|
import 'package:hiddify/domain/failures.dart';
|
2023-10-02 18:51:14 +03:30
|
|
|
import 'package:hiddify/domain/profiles/profiles.dart';
|
2023-07-26 14:17:11 +03:30
|
|
|
import 'package:hiddify/features/common/qr_code_scanner_screen.dart';
|
|
|
|
|
import 'package:hiddify/features/profiles/notifier/notifier.dart';
|
|
|
|
|
import 'package:hiddify/utils/utils.dart';
|
|
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
|
|
|
|
|
|
|
|
class AddProfileModal extends HookConsumerWidget {
|
|
|
|
|
const AddProfileModal({
|
|
|
|
|
super.key,
|
|
|
|
|
this.url,
|
|
|
|
|
this.scrollController,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final String? url;
|
|
|
|
|
final ScrollController? scrollController;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
|
|
|
final t = ref.watch(translationsProvider);
|
|
|
|
|
|
|
|
|
|
final mutationTriggered = useState(false);
|
|
|
|
|
final addProfileMutation = useMutation(
|
|
|
|
|
initialOnFailure: (err) {
|
|
|
|
|
mutationTriggered.value = false;
|
2023-10-02 18:51:14 +03:30
|
|
|
if (err case ProfileInvalidUrlFailure()) {
|
|
|
|
|
CustomToast.error(
|
|
|
|
|
t.profile.add.invalidUrlMsg,
|
|
|
|
|
).show(context);
|
|
|
|
|
} else {
|
|
|
|
|
CustomAlertDialog.fromErr(t.presentError(err)).show(context);
|
|
|
|
|
}
|
2023-07-26 14:17:11 +03:30
|
|
|
},
|
|
|
|
|
initialOnSuccess: () {
|
2023-09-07 01:56:59 +03:30
|
|
|
CustomToast.success(t.profile.save.successMsg).show(context);
|
2023-07-26 14:17:11 +03:30
|
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
|
|
|
(_) {
|
|
|
|
|
if (context.mounted) context.pop();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final showProgressIndicator =
|
|
|
|
|
addProfileMutation.state.isInProgress || mutationTriggered.value;
|
|
|
|
|
|
|
|
|
|
useMemoized(() async {
|
|
|
|
|
await Future.delayed(const Duration(milliseconds: 200));
|
|
|
|
|
if (url != null && context.mounted) {
|
|
|
|
|
addProfileMutation.setFuture(
|
|
|
|
|
ref.read(profilesNotifierProvider.notifier).addProfile(url!),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
const buttonsPadding = 24.0;
|
|
|
|
|
const buttonsGap = 16.0;
|
|
|
|
|
|
|
|
|
|
return SingleChildScrollView(
|
|
|
|
|
controller: scrollController,
|
|
|
|
|
child: AnimatedSize(
|
|
|
|
|
duration: const Duration(milliseconds: 250),
|
|
|
|
|
child: LayoutBuilder(
|
|
|
|
|
builder: (context, constraints) {
|
|
|
|
|
// temporary solution, aspect ratio widget relies on height and in a row there no height!
|
|
|
|
|
final buttonWidth =
|
|
|
|
|
constraints.maxWidth / 2 - (buttonsPadding + (buttonsGap / 2));
|
|
|
|
|
|
|
|
|
|
return AnimatedCrossFade(
|
|
|
|
|
firstChild: SizedBox(
|
|
|
|
|
height: buttonWidth.clamp(0, 168),
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 64),
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
2023-09-07 01:56:59 +03:30
|
|
|
t.profile.add.addingProfileMsg,
|
2023-07-26 14:17:11 +03:30
|
|
|
style: theme.textTheme.bodySmall,
|
|
|
|
|
),
|
|
|
|
|
const Gap(8),
|
|
|
|
|
const LinearProgressIndicator(
|
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
secondChild: Column(
|
|
|
|
|
children: [
|
|
|
|
|
Padding(
|
|
|
|
|
padding:
|
|
|
|
|
const EdgeInsets.symmetric(horizontal: buttonsPadding),
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
_Button(
|
2023-09-20 12:42:06 +03:30
|
|
|
key: const ValueKey("add_from_clipboard_button"),
|
2023-09-07 01:56:59 +03:30
|
|
|
label: t.profile.add.fromClipboard,
|
2023-07-26 14:17:11 +03:30
|
|
|
icon: Icons.content_paste,
|
|
|
|
|
size: buttonWidth,
|
|
|
|
|
onTap: () async {
|
|
|
|
|
final captureResult =
|
2023-10-02 18:51:14 +03:30
|
|
|
await Clipboard.getData(Clipboard.kTextPlain)
|
|
|
|
|
.then((value) => value?.text ?? '');
|
|
|
|
|
if (addProfileMutation.state.isInProgress) return;
|
|
|
|
|
mutationTriggered.value = true;
|
|
|
|
|
addProfileMutation.setFuture(
|
|
|
|
|
ref
|
|
|
|
|
.read(profilesNotifierProvider.notifier)
|
|
|
|
|
.addProfile(captureResult),
|
|
|
|
|
);
|
2023-07-26 14:17:11 +03:30
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
const Gap(buttonsGap),
|
|
|
|
|
if (!PlatformUtils.isDesktop)
|
|
|
|
|
_Button(
|
2023-09-20 12:42:06 +03:30
|
|
|
key: const ValueKey("add_by_qr_code_button"),
|
2023-07-26 14:17:11 +03:30
|
|
|
label: t.profile.add.scanQr,
|
|
|
|
|
icon: Icons.qr_code_scanner,
|
|
|
|
|
size: buttonWidth,
|
|
|
|
|
onTap: () async {
|
|
|
|
|
final captureResult =
|
|
|
|
|
await const QRCodeScannerScreen()
|
|
|
|
|
.open(context);
|
|
|
|
|
if (captureResult == null) return;
|
2023-10-02 18:51:14 +03:30
|
|
|
if (addProfileMutation.state.isInProgress) {
|
|
|
|
|
return;
|
2023-07-26 14:17:11 +03:30
|
|
|
}
|
2023-10-02 18:51:14 +03:30
|
|
|
mutationTriggered.value = true;
|
|
|
|
|
addProfileMutation.setFuture(
|
|
|
|
|
ref
|
|
|
|
|
.read(profilesNotifierProvider.notifier)
|
|
|
|
|
.addProfile(captureResult),
|
|
|
|
|
);
|
2023-07-26 14:17:11 +03:30
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
else
|
|
|
|
|
_Button(
|
2023-09-20 12:42:06 +03:30
|
|
|
key: const ValueKey("add_manually_button"),
|
2023-09-07 01:56:59 +03:30
|
|
|
label: t.profile.add.manually,
|
2023-07-26 14:17:11 +03:30
|
|
|
icon: Icons.add,
|
|
|
|
|
size: buttonWidth,
|
|
|
|
|
onTap: () async {
|
|
|
|
|
context.pop();
|
|
|
|
|
await const NewProfileRoute().push(context);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
if (!PlatformUtils.isDesktop)
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: buttonsPadding,
|
|
|
|
|
vertical: 16,
|
|
|
|
|
),
|
2023-09-02 21:09:22 +03:30
|
|
|
child: Semantics(
|
|
|
|
|
button: true,
|
|
|
|
|
child: SizedBox(
|
|
|
|
|
height: 36,
|
|
|
|
|
child: Material(
|
2023-09-20 12:42:06 +03:30
|
|
|
key: const ValueKey("add_manually_button"),
|
2023-09-02 21:09:22 +03:30
|
|
|
elevation: 8,
|
|
|
|
|
color: theme.colorScheme.surface,
|
|
|
|
|
surfaceTintColor: theme.colorScheme.surfaceTint,
|
|
|
|
|
shadowColor: Colors.transparent,
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
clipBehavior: Clip.antiAlias,
|
|
|
|
|
child: InkWell(
|
|
|
|
|
onTap: () async {
|
|
|
|
|
context.pop();
|
|
|
|
|
await const NewProfileRoute().push(context);
|
|
|
|
|
},
|
|
|
|
|
child: Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
Icon(
|
|
|
|
|
Icons.add,
|
2023-07-26 14:17:11 +03:30
|
|
|
color: theme.colorScheme.primary,
|
|
|
|
|
),
|
2023-09-02 21:09:22 +03:30
|
|
|
const Gap(8),
|
|
|
|
|
Text(
|
2023-09-07 01:56:59 +03:30
|
|
|
t.profile.add.manually,
|
2023-09-02 21:09:22 +03:30
|
|
|
style: theme.textTheme.labelLarge?.copyWith(
|
|
|
|
|
color: theme.colorScheme.primary,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2023-07-26 14:17:11 +03:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const Gap(24),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
crossFadeState: showProgressIndicator
|
|
|
|
|
? CrossFadeState.showFirst
|
|
|
|
|
: CrossFadeState.showSecond,
|
|
|
|
|
duration: const Duration(milliseconds: 250),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _Button extends StatelessWidget {
|
|
|
|
|
const _Button({
|
2023-09-20 12:42:06 +03:30
|
|
|
super.key,
|
2023-07-26 14:17:11 +03:30
|
|
|
required this.label,
|
|
|
|
|
required this.icon,
|
|
|
|
|
required this.size,
|
|
|
|
|
required this.onTap,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final String label;
|
|
|
|
|
final IconData icon;
|
|
|
|
|
final double size;
|
|
|
|
|
final VoidCallback onTap;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final theme = Theme.of(context);
|
|
|
|
|
final color = theme.colorScheme.primary;
|
|
|
|
|
|
2023-09-02 21:09:22 +03:30
|
|
|
return Semantics(
|
|
|
|
|
button: true,
|
|
|
|
|
child: SizedBox(
|
|
|
|
|
width: size,
|
|
|
|
|
height: size,
|
|
|
|
|
child: Material(
|
|
|
|
|
elevation: 8,
|
|
|
|
|
color: theme.colorScheme.surface,
|
|
|
|
|
surfaceTintColor: theme.colorScheme.surfaceTint,
|
|
|
|
|
shadowColor: Colors.transparent,
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
clipBehavior: Clip.antiAlias,
|
|
|
|
|
child: InkWell(
|
|
|
|
|
onTap: onTap,
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
Icon(
|
|
|
|
|
icon,
|
|
|
|
|
size: size / 3,
|
|
|
|
|
color: color,
|
2023-07-26 14:17:11 +03:30
|
|
|
),
|
2023-09-02 21:09:22 +03:30
|
|
|
const Gap(16),
|
|
|
|
|
Flexible(
|
|
|
|
|
child: Text(
|
|
|
|
|
label,
|
|
|
|
|
style: theme.textTheme.labelLarge?.copyWith(color: color),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2023-07-26 14:17:11 +03:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|