new: add json editor and editing configs <3

This commit is contained in:
hiddify-com
2024-07-30 08:07:46 +02:00
parent 503e48a830
commit 531ddbc2e2
10 changed files with 1524 additions and 136 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart';
import 'package:fpdart/fpdart.dart';
@@ -7,12 +9,17 @@ import 'package:hiddify/core/localization/translations.dart';
import 'package:hiddify/core/model/failures.dart';
import 'package:hiddify/core/widget/adaptive_icon.dart';
import 'package:hiddify/features/common/confirmation_dialogs.dart';
import 'package:hiddify/features/profile/details/json_editor.dart';
import 'package:hiddify/features/profile/details/profile_details_notifier.dart';
import 'package:hiddify/features/profile/model/profile_entity.dart';
import 'package:hiddify/features/settings/widgets/widgets.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:humanizer/humanizer.dart';
// import 'package:lucy_editor/lucy_editor.dart';
// import 'package:re_highlight/languages/json.dart';
// import 'package:re_highlight/styles/atom-one-light.dart';
// import 'package:json_editor_flutter/json_editor_flutter.dart';
class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
const ProfileDetailsPage(this.id, {super.key});
@@ -39,14 +46,12 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
);
case AsyncError(:final error):
final String action;
if (ref.read(provider) case AsyncData(value: final data)
when data.isEditing) {
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);
CustomAlertDialog.fromErr(t.presentError(error, action: action)).show(context);
}
},
);
@@ -82,9 +87,7 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
switch (ref.watch(provider)) {
case AsyncData(value: final state):
final showLoadingOverlay = state.isBusy ||
state.save is MutationSuccess ||
state.delete is MutationSuccess;
final showLoadingOverlay = state.isBusy || state.save is MutationSuccess || state.delete is MutationSuccess;
return Stack(
children: [
@@ -96,6 +99,16 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
title: Text(t.profile.detailsPageTitle),
pinned: true,
actions: [
// MenuItemButton(
// onPressed: context.pop,
// child: Text(
// MaterialLocalizations.of(context).cancelButtonLabel,
// ),
// ),
MenuItemButton(
onPressed: notifier.save,
child: Text(t.profile.save.buttonText),
),
if (state.isEditing)
PopupMenuButton(
icon: Icon(AdaptiveIcon(context).more),
@@ -111,8 +124,7 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
PopupMenuItem(
child: Text(t.profile.delete.buttonTxt),
onTap: () async {
final deleteConfirmed =
await showConfirmationDialog(
final deleteConfirmed = await showConfirmationDialog(
context,
title: t.profile.delete.buttonTxt,
message: t.profile.delete.confirmationMsg,
@@ -129,9 +141,7 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
],
),
Form(
autovalidateMode: state.showErrorMessages
? AutovalidateMode.always
: AutovalidateMode.disabled,
autovalidateMode: state.showErrorMessages ? AutovalidateMode.always : AutovalidateMode.disabled,
child: SliverList.list(
children: [
Padding(
@@ -141,20 +151,13 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
),
child: CustomTextFormField(
initialValue: state.profile.name,
onChanged: (value) =>
notifier.setField(name: value),
validator: (value) => (value?.isEmpty ?? true)
? t.profile.detailsForm.emptyNameMsg
: null,
onChanged: (value) => notifier.setField(name: value),
validator: (value) => (value?.isEmpty ?? true) ? t.profile.detailsForm.emptyNameMsg : null,
label: t.profile.detailsForm.nameLabel,
hint: t.profile.detailsForm.nameHint,
),
),
if (state.profile
case RemoteProfileEntity(
:final url,
:final options
)) ...[
if (state.profile case RemoteProfileEntity(:final url, :final options)) ...[
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
@@ -162,12 +165,8 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
),
child: CustomTextFormField(
initialValue: url,
onChanged: (value) =>
notifier.setField(url: value),
validator: (value) =>
(value != null && !isUrl(value))
? t.profile.detailsForm.invalidUrlMsg
: null,
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,
),
@@ -180,13 +179,10 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
) ??
t.general.toggle.disabled,
),
leading:
const Icon(FluentIcons.arrow_sync_24_regular),
leading: const Icon(FluentIcons.arrow_sync_24_regular),
onTap: () async {
final intervalInHours =
await SettingsInputDialog(
title: t.profile.detailsForm
.updateIntervalDialogTitle,
final intervalInHours = await SettingsInputDialog(
title: t.profile.detailsForm.updateIntervalDialogTitle,
initialValue: options?.updateInterval.inHours,
optionalAction: (
t.general.state.disable,
@@ -205,17 +201,28 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
},
),
],
// Padding(
// padding: const EdgeInsets.symmetric(
// horizontal: 16,
// vertical: 8,
// ),
// child: CustomTextFormField(
// initialValue: state.configContent,
// // onChanged: (value) => notifier.setField(name: value),
// maxLines: 7,
// label: t.profile.detailsForm.configContentLabel,
// hint: t.profile.detailsForm.configContentHint,
// ),
// ),
if (state.isEditing) ...[
ListTile(
title: Text(t.profile.detailsForm.lastUpdate),
leading:
const Icon(FluentIcons.history_24_regular),
leading: const Icon(FluentIcons.history_24_regular),
subtitle: Text(state.profile.lastUpdate.format()),
dense: true,
),
],
if (state.profile
case RemoteProfileEntity(:final subInfo?)) ...[
if (state.profile case RemoteProfileEntity(:final subInfo?)) ...[
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 18,
@@ -225,8 +232,7 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text.rich(
style:
Theme.of(context).textTheme.bodySmall,
style: Theme.of(context).textTheme.bodySmall,
TextSpan(
children: [
_buildSubProp(
@@ -242,8 +248,7 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
),
const TextSpan(text: " "),
_buildSubProp(
FluentIcons
.arrow_bidirectional_up_down_16_regular,
FluentIcons.arrow_bidirectional_up_down_16_regular,
subInfo.total.size(),
t.profile.subscription.total,
),
@@ -252,8 +257,7 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
),
const Gap(12),
Text.rich(
style:
Theme.of(context).textTheme.bodySmall,
style: Theme.of(context).textTheme.bodySmall,
TextSpan(
children: [
_buildSubProp(
@@ -268,39 +272,23 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
),
),
],
],
),
),
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,
),
),
FilledButton(
onPressed: notifier.save,
child: Text(t.profile.save.buttonText),
),
],
if (state.isEditing) ...[
SizedBox(
height: MediaQuery.of(context).size.height * 0.8,
child: JsonEditor(
expandedObjects: const ["outbounds"],
onChanged: (value) {
if (value == null) return;
const encoder = const JsonEncoder.withIndent(' ');
notifier.setField(configContent: encoder.convert(value));
},
enableHorizontalScroll: true,
json: state.configContent,
),
),
],
),
],
),
),
],