Files
umbrix/lib/features/profile/add/add_profile_modal.dart

342 lines
14 KiB
Dart
Raw Normal View History

import 'package:combine/combine.dart';
2024-02-15 15:23:02 +03:30
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
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';
2023-12-01 12:56:24 +03:30
import 'package:hiddify/core/localization/translations.dart';
2024-05-31 21:14:37 +02:00
import 'package:hiddify/core/notification/in_app_notification_controller.dart';
import 'package:hiddify/core/preferences/preferences_provider.dart';
2023-07-26 14:17:11 +03:30
import 'package:hiddify/core/router/router.dart';
import 'package:hiddify/features/common/qr_code_scanner_screen.dart';
import 'package:hiddify/features/config_option/data/config_option_repository.dart';
import 'package:hiddify/features/config_option/notifier/warp_option_notifier.dart';
2024-07-04 21:06:08 +02:00
import 'package:hiddify/features/config_option/overview/warp_options_widgets.dart';
2023-11-26 21:20:58 +03:30
import 'package:hiddify/features/profile/notifier/profile_notifier.dart';
2023-07-26 14:17:11 +03:30
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,
});
static const warpConsentGiven = "warp_consent_given";
2023-07-26 14:17:11 +03:30
final String? url;
final ScrollController? scrollController;
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
2023-11-26 21:20:58 +03:30
final addProfileState = ref.watch(addProfileProvider);
2023-07-26 14:17:11 +03:30
2023-11-26 21:20:58 +03:30
ref.listen(
addProfileProvider,
(previous, next) {
if (next case AsyncData(value: final _?)) {
WidgetsBinding.instance.addPostFrameCallback(
(_) {
2024-08-05 02:07:07 +02:00
if (context.mounted && context.canPop()) context.pop();
2023-11-26 21:20:58 +03:30
},
);
2023-10-02 18:51:14 +03:30
}
2023-07-26 14:17:11 +03:30
},
);
useMemoized(() async {
await Future.delayed(const Duration(milliseconds: 200));
if (url != null && context.mounted) {
2023-11-26 21:20:58 +03:30
if (addProfileState.isLoading) return;
ref.read(addProfileProvider.notifier).add(url!);
2023-07-26 14:17:11 +03:30
}
});
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));
2023-07-26 14:17:11 +03:30
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,
),
2024-01-04 21:35:04 +03:30
const Gap(8),
TextButton(
onPressed: () {
ref.invalidate(addProfileProvider);
},
child: Text(
MaterialLocalizations.of(context).cancelButtonLabel,
),
),
2023-07-26 14:17:11 +03:30
],
),
),
),
secondChild: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: buttonsPadding),
2023-07-26 14:17:11 +03:30
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,
2024-02-15 15:23:02 +03:30
icon: FluentIcons.clipboard_paste_24_regular,
2023-07-26 14:17:11 +03:30
size: buttonWidth,
onTap: () async {
final captureResult = await Clipboard.getData(Clipboard.kTextPlain).then((value) => value?.text ?? '');
2023-11-26 21:20:58 +03:30
if (addProfileState.isLoading) return;
ref.read(addProfileProvider.notifier).add(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,
2024-02-15 15:23:02 +03:30
icon: FluentIcons.qr_code_24_regular,
2023-07-26 14:17:11 +03:30
size: buttonWidth,
onTap: () async {
final cr = await QRCodeScannerScreen().open(context);
if (cr == null) return;
2023-11-26 21:20:58 +03:30
if (addProfileState.isLoading) return;
ref.read(addProfileProvider.notifier).add(cr);
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,
2024-02-15 15:23:02 +03:30
icon: FluentIcons.add_24_regular,
2023-07-26 14:17:11 +03:30
size: buttonWidth,
onTap: () async {
context.pop();
await const NewProfileRoute().push(context);
},
),
],
),
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: buttonsPadding,
vertical: 16,
),
child: Column(
children: [
Semantics(
button: true,
child: SizedBox(
height: 36,
child: Material(
key: const ValueKey("add_warp_button"),
elevation: 8,
color: theme.colorScheme.surface,
surfaceTintColor: theme.colorScheme.surfaceTint,
shadowColor: Colors.transparent,
borderRadius: BorderRadius.circular(8),
clipBehavior: Clip.antiAlias,
child: InkWell(
onTap: () async {
await addProfileModal(context, ref);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
FluentIcons.add_24_regular,
2023-09-02 21:09:22 +03:30
color: theme.colorScheme.primary,
),
const SizedBox(width: 8),
Text(
t.profile.add.addWarp,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.primary,
),
),
],
),
2023-09-02 21:09:22 +03:30
),
2023-07-26 14:17:11 +03:30
),
),
),
if (!PlatformUtils.isDesktop) const SizedBox(height: 16), // Spacing between the buttons
if (!PlatformUtils.isDesktop)
Semantics(
button: true,
child: SizedBox(
height: 36,
child: Material(
key: const ValueKey("add_manually_button"),
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(
FluentIcons.add_24_regular,
color: theme.colorScheme.primary,
),
const SizedBox(width: 8),
Text(
t.profile.add.manually,
style: theme.textTheme.labelLarge?.copyWith(
color: theme.colorScheme.primary,
),
),
],
),
),
),
),
),
],
2023-07-26 14:17:11 +03:30
),
),
2023-07-26 14:17:11 +03:30
const Gap(24),
],
),
crossFadeState: addProfileState.isLoading ? CrossFadeState.showFirst : CrossFadeState.showSecond,
2023-07-26 14:17:11 +03:30
duration: const Duration(milliseconds: 250),
);
},
),
),
);
}
2024-05-31 21:14:37 +02:00
Future<void> addProfileModal(BuildContext context, WidgetRef ref) async {
2024-05-31 21:14:37 +02:00
final _prefs = ref.read(sharedPreferencesProvider).requireValue;
final _warp = ref.read(warpOptionNotifierProvider.notifier);
final _profile = ref.read(addProfileProvider.notifier);
2024-07-14 15:35:36 +02:00
final consent = (_prefs.getBool(warpConsentGiven) ?? false);
2024-07-26 13:34:49 +02:00
final region = ref.read(ConfigOptions.region.notifier).raw();
2024-05-31 21:14:37 +02:00
context.pop();
final t = ref.read(translationsProvider);
final notification = ref.read(inAppNotificationControllerProvider);
2024-05-31 21:14:37 +02:00
if (!consent) {
final agreed = await showDialog<bool>(
context: context,
builder: (context) => const WarpLicenseAgreementModal(),
);
2024-08-05 16:25:21 +02:00
if (agreed != true) return;
}
2024-08-05 16:25:21 +02:00
await _prefs.setBool(warpConsentGiven, true);
var toast = notification.showInfoToast(t.profile.add.addingWarpMsg, duration: const Duration(milliseconds: 100));
toast?.pause();
await _warp.generateWarpConfig();
toast?.start();
2024-05-31 21:14:37 +02:00
2024-07-14 15:35:36 +02:00
// final accountId = _prefs.getString("warp2-account-id");
// final accessToken = _prefs.getString("warp2-access-token");
// final hasWarp2Config = accountId != null && accessToken != null;
2024-05-31 21:14:37 +02:00
2024-07-14 15:35:36 +02:00
// if (!hasWarp2Config || true) {
2024-08-05 16:25:21 +02:00
toast = notification.showInfoToast(t.profile.add.addingWarpMsg, duration: const Duration(milliseconds: 100));
2024-07-14 15:35:36 +02:00
toast?.pause();
await _warp.generateWarp2Config();
toast?.start();
// }
2024-07-26 13:34:49 +02:00
if (region == "cn") {
await _profile.add("#profile-title: Hiddify WARP\nwarp://p1@auto#National&&detour=warp://p2@auto#WoW"); //
} else {
await _profile.add("https://raw.githubusercontent.com/hiddify/hiddify-next/main/test.configs/warp"); //
}
2024-05-31 21:14:37 +02:00
}
2023-07-26 14:17:11 +03:30
}
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),
2024-01-12 22:34:37 +03:30
textAlign: TextAlign.center,
2023-09-02 21:09:22 +03:30
),
),
],
),
2023-07-26 14:17:11 +03:30
),
),
),
);
}
}