Files
umbrix/lib/features/profile/details/profile_details_page.dart

356 lines
15 KiB
Dart
Raw Normal View History

2024-02-15 15:23:02 +03:30
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
2023-07-06 17:18:41 +03:30
import 'package:flutter/material.dart';
2023-09-28 14:03:45 +03:30
import 'package:fpdart/fpdart.dart';
2024-02-18 14:34:54 +03:30
import 'package:gap/gap.dart';
2023-07-06 17:18:41 +03:30
import 'package:go_router/go_router.dart';
2023-12-01 12:56:24 +03:30
import 'package:hiddify/core/localization/translations.dart';
import 'package:hiddify/core/model/failures.dart';
2024-02-15 15:23:02 +03:30
import 'package:hiddify/core/widget/adaptive_icon.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/features/common/confirmation_dialogs.dart';
2023-11-26 21:20:58 +03:30
import 'package:hiddify/features/profile/details/profile_details_notifier.dart';
import 'package:hiddify/features/profile/model/profile_entity.dart';
2023-09-28 14:03:45 +03:30
import 'package:hiddify/features/settings/widgets/widgets.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/utils/utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
2023-09-28 14:03:45 +03:30
import 'package:humanizer/humanizer.dart';
2023-07-06 17:18:41 +03:30
2023-11-26 21:20:58 +03:30
class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
const ProfileDetailsPage(this.id, {super.key});
2023-07-06 17:18:41 +03:30
final String id;
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
2023-11-26 21:20:58 +03:30
final provider = profileDetailsNotifierProvider(id);
2023-09-28 14:03:45 +03:30
final notifier = ref.watch(provider.notifier);
2023-07-06 17:18:41 +03:30
ref.listen(
2023-11-26 21:20:58 +03:30
provider.selectAsync((data) => data.save),
(_, next) async {
switch (await next) {
case AsyncData():
CustomToast.success(t.profile.save.successMsg).show(context);
WidgetsBinding.instance.addPostFrameCallback(
(_) {
if (context.mounted) context.pop();
},
);
case AsyncError(:final error):
final String action;
if (ref.read(provider) case AsyncData(value: final data)
when data.isEditing) {
action = t.profile.save.failureMsg;
} else {
action = t.profile.add.failureMsg;
}
CustomAlertDialog.fromErr(t.presentError(error, action: action))
.show(context);
2023-07-06 17:18:41 +03:30
}
},
);
2023-09-28 14:03:45 +03:30
ref.listen(
2023-11-26 21:20:58 +03:30
provider.selectAsync((data) => data.update),
(_, next) async {
switch (await next) {
case AsyncData():
CustomToast.success(t.profile.update.successMsg).show(context);
case AsyncError(:final error):
CustomAlertDialog.fromErr(t.presentError(error)).show(context);
2023-09-28 14:03:45 +03:30
}
},
);
2023-07-06 17:18:41 +03:30
ref.listen(
2023-11-26 21:20:58 +03:30
provider.selectAsync((data) => data.delete),
(_, next) async {
switch (await next) {
case AsyncData():
CustomToast.success(t.profile.delete.successMsg).show(context);
WidgetsBinding.instance.addPostFrameCallback(
(_) {
if (context.mounted) context.pop();
},
);
case AsyncError(:final error):
CustomToast.error(t.presentShortError(error)).show(context);
2023-07-06 17:18:41 +03:30
}
},
);
2023-09-28 14:03:45 +03:30
switch (ref.watch(provider)) {
2023-07-06 17:18:41 +03:30
case AsyncData(value: final state):
2023-09-28 14:03:45 +03:30
final showLoadingOverlay = state.isBusy ||
state.save is MutationSuccess ||
state.delete is MutationSuccess;
2023-07-06 17:18:41 +03:30
return Stack(
children: [
Scaffold(
2024-01-12 22:45:52 +03:30
body: SafeArea(
child: CustomScrollView(
slivers: [
SliverAppBar(
title: Text(t.profile.detailsPageTitle),
pinned: true,
actions: [
if (state.isEditing)
PopupMenuButton(
2024-02-15 15:23:02 +03:30
icon: Icon(AdaptiveIcon(context).more),
2024-01-12 22:45:52 +03:30
itemBuilder: (context) {
return [
if (state.profile case RemoteProfileEntity())
PopupMenuItem(
child: Text(t.profile.update.buttonTxt),
onTap: () async {
await notifier.updateProfile();
},
),
2023-10-02 18:51:14 +03:30
PopupMenuItem(
2024-01-12 22:45:52 +03:30
child: Text(t.profile.delete.buttonTxt),
2023-10-02 18:51:14 +03:30
onTap: () async {
2024-01-12 22:45:52 +03:30
final deleteConfirmed =
await showConfirmationDialog(
context,
title: t.profile.delete.buttonTxt,
message: t.profile.delete.confirmationMsg,
2024-03-04 15:58:56 +03:30
icon: FluentIcons.delete_24_regular,
2024-01-12 22:45:52 +03:30
);
if (deleteConfirmed) {
await notifier.delete();
}
2023-10-02 18:51:14 +03:30
},
),
2024-01-12 22:45:52 +03:30
];
},
2023-09-28 14:03:45 +03:30
),
2024-01-12 22:45:52 +03:30
],
),
Form(
autovalidateMode: state.showErrorMessages
? AutovalidateMode.always
: AutovalidateMode.disabled,
child: SliverList.list(
children: [
2023-10-02 18:51:14 +03:30
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
child: CustomTextFormField(
2024-01-12 22:45:52 +03:30
initialValue: state.profile.name,
2023-10-02 18:51:14 +03:30
onChanged: (value) =>
2024-01-12 22:45:52 +03:30
notifier.setField(name: value),
validator: (value) => (value?.isEmpty ?? true)
? t.profile.detailsForm.emptyNameMsg
: null,
label: t.profile.detailsForm.nameLabel,
hint: t.profile.detailsForm.nameHint,
2023-10-02 18:51:14 +03:30
),
2023-09-28 14:03:45 +03:30
),
2024-01-12 22:45:52 +03:30
if (state.profile
case RemoteProfileEntity(
:final url,
:final options
)) ...[
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
child: CustomTextFormField(
initialValue: url,
onChanged: (value) =>
notifier.setField(url: value),
validator: (value) =>
(value != null && !isUrl(value))
? t.profile.detailsForm.invalidUrlMsg
: null,
label: t.profile.detailsForm.urlLabel,
hint: t.profile.detailsForm.urlHint,
),
),
ListTile(
title: Text(t.profile.detailsForm.updateInterval),
subtitle: Text(
options?.updateInterval.toApproximateTime(
isRelativeToNow: false,
) ??
t.general.toggle.disabled,
),
2024-02-15 15:23:02 +03:30
leading:
const Icon(FluentIcons.arrow_sync_24_regular),
2024-01-12 22:45:52 +03:30
onTap: () async {
final intervalInHours =
await SettingsInputDialog(
title: t.profile.detailsForm
.updateIntervalDialogTitle,
initialValue: options?.updateInterval.inHours,
optionalAction: (
t.general.state.disable,
() => notifier.setField(
2024-02-15 15:23:02 +03:30
updateInterval: none(),
),
2024-01-12 22:45:52 +03:30
),
validator: isPort,
mapTo: int.tryParse,
digitsOnly: true,
).show(context);
if (intervalInHours == null) return;
notifier.setField(
updateInterval: optionOf(intervalInHours),
);
},
),
],
2024-02-18 14:34:54 +03:30
if (state.isEditing) ...[
2024-01-12 22:45:52 +03:30
ListTile(
title: Text(t.profile.detailsForm.lastUpdate),
2024-02-18 14:34:54 +03:30
leading:
const Icon(FluentIcons.history_24_regular),
2024-01-12 22:45:52 +03:30
subtitle: Text(state.profile.lastUpdate.format()),
dense: true,
2023-10-02 18:51:14 +03:30
),
2024-02-18 14:34:54 +03:30
],
if (state.profile
case RemoteProfileEntity(:final subInfo?)) ...[
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 18,
vertical: 8,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text.rich(
style:
Theme.of(context).textTheme.bodySmall,
TextSpan(
children: [
_buildSubProp(
FluentIcons.arrow_upload_16_regular,
subInfo.upload.size(),
t.profile.subscription.upload,
),
const TextSpan(text: " "),
_buildSubProp(
FluentIcons.arrow_download_16_regular,
subInfo.download.size(),
t.profile.subscription.download,
),
const TextSpan(text: " "),
_buildSubProp(
FluentIcons
.arrow_bidirectional_up_down_16_regular,
subInfo.total.size(),
t.profile.subscription.total,
),
],
),
),
const Gap(12),
Text.rich(
style:
Theme.of(context).textTheme.bodySmall,
TextSpan(
children: [
_buildSubProp(
FluentIcons.clock_dismiss_20_regular,
subInfo.expire.format(),
t.profile.subscription.expireDate,
),
],
),
),
],
),
),
],
2023-10-02 18:51:14 +03:30
],
2023-07-06 17:18:41 +03:30
),
2024-01-12 22:45:52 +03:30
),
SliverFillRemaining(
hasScrollBody: false,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 16,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
OverflowBar(
spacing: 12,
overflowAlignment: OverflowBarAlignment.end,
children: [
OutlinedButton(
onPressed: context.pop,
child: Text(
MaterialLocalizations.of(context)
.cancelButtonLabel,
),
2023-09-28 14:03:45 +03:30
),
2024-01-12 22:45:52 +03:30
FilledButton(
onPressed: notifier.save,
child: Text(t.profile.save.buttonText),
),
],
),
],
),
2023-07-06 17:18:41 +03:30
),
),
2024-01-12 22:45:52 +03:30
],
),
2023-07-06 17:18:41 +03:30
),
),
2023-09-28 14:03:45 +03:30
if (showLoadingOverlay)
2023-07-06 17:18:41 +03:30
Positioned.fill(
child: Container(
color: Colors.black54,
padding: const EdgeInsets.symmetric(horizontal: 36),
child: const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
LinearProgressIndicator(
backgroundColor: Colors.transparent,
),
],
),
),
),
],
);
2023-09-28 14:03:45 +03:30
case AsyncError(:final error):
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
title: Text(t.profile.detailsPageTitle),
pinned: true,
),
2023-10-04 18:06:48 +03:30
SliverErrorBodyPlaceholder(t.presentShortError(error)),
2023-09-28 14:03:45 +03:30
],
),
);
2023-07-06 17:18:41 +03:30
default:
return const Scaffold();
}
}
2024-02-18 14:34:54 +03:30
InlineSpan _buildSubProp(IconData icon, String text, String semanticLabel) {
return TextSpan(
children: [
WidgetSpan(child: Icon(icon, size: 16, semanticLabel: semanticLabel)),
const TextSpan(text: " "),
TextSpan(text: text),
],
);
}
2023-07-06 17:18:41 +03:30
}