new: add json editor and editing configs <3
This commit is contained in:
1330
lib/features/profile/details/json_editor.dart
Normal file
1330
lib/features/profile/details/json_editor.dart
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,5 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dartx/dartx.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:hiddify/features/profile/data/profile_data_providers.dart';
|
||||
@@ -36,22 +38,53 @@ class ProfileDetailsNotifier extends _$ProfileDetailsNotifier with AppLogger {
|
||||
loggy.warning('failed to load profile', err);
|
||||
throw err;
|
||||
},
|
||||
(profile) {
|
||||
(profile) async {
|
||||
if (profile == null) {
|
||||
loggy.warning('profile with id: [$id] does not exist');
|
||||
throw const ProfileNotFoundFailure();
|
||||
}
|
||||
|
||||
_originalProfile = profile;
|
||||
return ProfileDetailsState(profile: profile, isEditing: true);
|
||||
final result = await _profilesRepo.generateConfig(id).run();
|
||||
|
||||
var configContent = result.fold(
|
||||
(failure) => throw Exception('Failed to generate config: $failure'),
|
||||
(config) => config,
|
||||
);
|
||||
if (configContent.isNotEmpty) {
|
||||
try {
|
||||
final jsonObject = jsonDecode(configContent);
|
||||
List<Map<String, dynamic>> res = [];
|
||||
if (jsonObject is Map<String, dynamic> && jsonObject['outbounds'] is List) {
|
||||
for (var outbound in jsonObject['outbounds'] as List<dynamic>) {
|
||||
if (outbound is Map<String, dynamic> && outbound['type'] != null && !['selector', 'urltest', 'dns', 'block'].contains(outbound['type']) && !['direct', 'bypass', 'direct-fragment'].contains(outbound['tag'])) {
|
||||
res.add(outbound);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// print('No outbounds found in the config');
|
||||
}
|
||||
configContent = '{"outbounds": ${json.encode(res)}}';
|
||||
} catch (e) {
|
||||
// print('Error parsing JSON: $e');
|
||||
}
|
||||
} else {
|
||||
// print('Config content is null or empty');
|
||||
}
|
||||
return ProfileDetailsState(profile: profile, isEditing: true, configContent: configContent);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
ProfileRepository get _profilesRepo =>
|
||||
ref.read(profileRepositoryProvider).requireValue;
|
||||
ProfileRepository get _profilesRepo => ref.read(profileRepositoryProvider).requireValue;
|
||||
ProfileEntity? _originalProfile;
|
||||
|
||||
void setField({String? name, String? url, Option<int>? updateInterval}) {
|
||||
void setField({
|
||||
String? name,
|
||||
String? url,
|
||||
Option<int>? updateInterval,
|
||||
String? configContent,
|
||||
}) {
|
||||
if (state case AsyncData(:final value)) {
|
||||
state = AsyncData(
|
||||
value.copyWith(
|
||||
@@ -70,6 +103,8 @@ class ProfileDetailsNotifier extends _$ProfileDetailsNotifier with AppLogger {
|
||||
),
|
||||
local: (lp) => lp.copyWith(name: name ?? lp.name),
|
||||
),
|
||||
configContentChanged: value.configContentChanged || value.configContent != configContent,
|
||||
configContent: configContent ?? value.configContent,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -91,15 +126,28 @@ class ProfileDetailsNotifier extends _$ProfileDetailsNotifier with AppLogger {
|
||||
if (profile.name.isBlank || profile.url.isBlank) {
|
||||
loggy.debug('save: invalid arguments');
|
||||
} else if (value.isEditing) {
|
||||
if (_originalProfile case RemoteProfileEntity(:final url)
|
||||
when url == profile.url) {
|
||||
if (_originalProfile case RemoteProfileEntity(:final url) when url == profile.url) {
|
||||
loggy.debug('editing profile');
|
||||
failureOrSuccess = await _profilesRepo.patch(profile).run();
|
||||
if (failureOrSuccess.isRight()) {
|
||||
failureOrSuccess = await _profilesRepo
|
||||
.updateContent(
|
||||
profile.id,
|
||||
value.configContent,
|
||||
)
|
||||
.run();
|
||||
}
|
||||
} else {
|
||||
loggy.debug('updating profile');
|
||||
failureOrSuccess = await _profilesRepo
|
||||
.updateSubscription(profile, patchBaseProfile: true)
|
||||
.run();
|
||||
failureOrSuccess = await _profilesRepo.updateSubscription(profile, patchBaseProfile: true).run();
|
||||
if (failureOrSuccess.isRight()) {
|
||||
failureOrSuccess = await _profilesRepo
|
||||
.updateContent(
|
||||
profile.id,
|
||||
value.configContent,
|
||||
)
|
||||
.run();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
loggy.debug('adding profile, url: [${profile.url}]');
|
||||
@@ -138,10 +186,7 @@ class ProfileDetailsNotifier extends _$ProfileDetailsNotifier with AppLogger {
|
||||
final profile = value.profile;
|
||||
state = AsyncData(value.copyWith(update: const AsyncLoading()));
|
||||
|
||||
final failureOrUpdatedProfile = await _profilesRepo
|
||||
.updateSubscription(profile as RemoteProfileEntity)
|
||||
.flatMap((_) => _profilesRepo.getById(id))
|
||||
.run();
|
||||
final failureOrUpdatedProfile = await _profilesRepo.updateSubscription(profile as RemoteProfileEntity).flatMap((_) => _profilesRepo.getById(id)).run();
|
||||
|
||||
state = AsyncData(
|
||||
value.copyWith(
|
||||
@@ -167,10 +212,7 @@ class ProfileDetailsNotifier extends _$ProfileDetailsNotifier with AppLogger {
|
||||
state = AsyncData(
|
||||
value.copyWith(
|
||||
delete: await AsyncValue.guard(() async {
|
||||
await _profilesRepo
|
||||
.deleteById(profile.id)
|
||||
.getOrElse((l) => throw l)
|
||||
.run();
|
||||
await _profilesRepo.deleteById(profile.id).getOrElse((l) => throw l).run();
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -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,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -15,8 +15,9 @@ class ProfileDetailsState with _$ProfileDetailsState {
|
||||
AsyncValue<void>? save,
|
||||
AsyncValue<void>? update,
|
||||
AsyncValue<void>? delete,
|
||||
@Default("") String configContent,
|
||||
@Default(false) bool configContentChanged,
|
||||
}) = _ProfileDetailsState;
|
||||
|
||||
bool get isBusy =>
|
||||
save is AsyncLoading || delete is AsyncLoading || update is AsyncLoading;
|
||||
bool get isBusy => save is AsyncLoading || delete is AsyncLoading || update is AsyncLoading;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user