Refactor profiles

This commit is contained in:
problematicconsumer
2023-11-26 21:20:58 +03:30
parent e2f5f51176
commit 829d58a1a2
49 changed files with 1206 additions and 1024 deletions

View File

@@ -0,0 +1,178 @@
import 'package:dartx/dartx.dart';
import 'package:fpdart/fpdart.dart';
import 'package:hiddify/features/profile/data/profile_data_providers.dart';
import 'package:hiddify/features/profile/data/profile_repository.dart';
import 'package:hiddify/features/profile/details/profile_details_state.dart';
import 'package:hiddify/features/profile/model/profile_entity.dart';
import 'package:hiddify/features/profile/model/profile_failure.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:uuid/uuid.dart';
part 'profile_details_notifier.g.dart';
@riverpod
class ProfileDetailsNotifier extends _$ProfileDetailsNotifier with AppLogger {
@override
Future<ProfileDetailsState> build(
String id, {
String? url,
String? profileName,
}) async {
if (id == 'new') {
return ProfileDetailsState(
profile: RemoteProfileEntity(
id: const Uuid().v4(),
active: true,
name: profileName ?? "",
url: url ?? "",
lastUpdate: DateTime.now(),
),
);
}
final failureOrProfile = await _profilesRepo.getById(id).run();
return failureOrProfile.match(
(err) {
loggy.warning('failed to load profile', err);
throw err;
},
(profile) {
if (profile == null) {
loggy.warning('profile with id: [$id] does not exist');
throw const ProfileNotFoundFailure();
}
_originalProfile = profile;
return ProfileDetailsState(profile: profile, isEditing: true);
},
);
}
ProfileRepository get _profilesRepo =>
ref.read(profileRepositoryProvider).requireValue;
ProfileEntity? _originalProfile;
void setField({String? name, String? url, Option<int>? updateInterval}) {
if (state case AsyncData(:final value)) {
state = AsyncData(
value.copyWith(
profile: value.profile.map(
remote: (rp) => rp.copyWith(
name: name ?? rp.name,
url: url ?? rp.url,
options: updateInterval == null
? rp.options
: updateInterval.fold(
() => null,
(t) => ProfileOptions(
updateInterval: Duration(hours: t),
),
),
),
local: (lp) => lp.copyWith(name: name ?? lp.name),
),
),
);
}
}
Future<void> save() async {
if (state case AsyncData(:final value)) {
if (value.save case AsyncLoading()) return;
final profile = value.profile;
Either<ProfileFailure, Unit>? failureOrSuccess;
state = AsyncData(value.copyWith(save: const AsyncLoading()));
switch (profile) {
case RemoteProfileEntity():
loggy.debug(
'saving profile, url: [${profile.url}], name: [${profile.name}]',
);
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) {
loggy.debug('editing profile');
failureOrSuccess = await _profilesRepo.patch(profile).run();
} else {
loggy.debug('updating profile');
failureOrSuccess =
await _profilesRepo.updateSubscription(profile).run();
}
} else {
loggy.debug('adding profile, url: [${profile.url}]');
failureOrSuccess = await _profilesRepo.add(profile).run();
}
case LocalProfileEntity() when value.isEditing:
loggy.debug('editing profile');
failureOrSuccess = await _profilesRepo.patch(profile).run();
default:
loggy.warning("local profile can't be added manually");
}
state = AsyncData(
value.copyWith(
save: failureOrSuccess?.fold(
(l) => AsyncError(l, StackTrace.current),
(_) => const AsyncData(null),
) ??
value.save,
showErrorMessages: true,
),
);
}
}
Future<void> updateProfile() async {
if (state case AsyncData(:final value)) {
if (value.update?.isLoading ?? false || !value.isEditing) return;
if (value.profile case LocalProfileEntity()) {
loggy.warning("local profile can't be updated");
return;
}
final profile = value.profile;
state = AsyncData(value.copyWith(update: const AsyncLoading()));
final failureOrUpdatedProfile = await _profilesRepo
.updateSubscription(profile as RemoteProfileEntity)
.flatMap((_) => _profilesRepo.getById(id))
.run();
state = AsyncData(
value.copyWith(
update: failureOrUpdatedProfile.match(
(l) => AsyncError(l, StackTrace.current),
(_) => const AsyncData(null),
),
profile: failureOrUpdatedProfile.match(
(_) => profile,
(updatedProfile) => updatedProfile ?? profile,
),
),
);
}
}
Future<void> delete() async {
if (state case AsyncData(:final value)) {
if (value.delete case AsyncLoading()) return;
final profile = value.profile;
state = AsyncData(value.copyWith(delete: const AsyncLoading()));
state = AsyncData(
value.copyWith(
delete: await AsyncValue.guard(() async {
await _profilesRepo
.deleteById(profile.id)
.getOrElse((l) => throw l)
.run();
}),
),
);
}
}
}

View File

@@ -0,0 +1,278 @@
import 'package:flutter/material.dart';
import 'package:fpdart/fpdart.dart';
import 'package:go_router/go_router.dart';
import 'package:hiddify/core/core_providers.dart';
import 'package:hiddify/domain/failures.dart';
import 'package:hiddify/features/common/confirmation_dialogs.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';
class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
const ProfileDetailsPage(this.id, {super.key});
final String id;
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
final provider = profileDetailsNotifierProvider(id);
final notifier = ref.watch(provider.notifier);
ref.listen(
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);
}
},
);
ref.listen(
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);
}
},
);
ref.listen(
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);
}
},
);
switch (ref.watch(provider)) {
case AsyncData(value: final state):
final showLoadingOverlay = state.isBusy ||
state.save is MutationSuccess ||
state.delete is MutationSuccess;
return Stack(
children: [
Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
title: Text(t.profile.detailsPageTitle),
pinned: true,
actions: [
if (state.isEditing)
PopupMenuButton(
itemBuilder: (context) {
return [
if (state.profile case RemoteProfileEntity())
PopupMenuItem(
child: Text(t.profile.update.buttonTxt),
onTap: () async {
await notifier.updateProfile();
},
),
PopupMenuItem(
child: Text(t.profile.delete.buttonTxt),
onTap: () async {
final deleteConfirmed =
await showConfirmationDialog(
context,
title: t.profile.delete.buttonTxt,
message: t.profile.delete.confirmationMsg,
);
if (deleteConfirmed) {
await notifier.delete();
}
},
),
];
},
),
],
),
Form(
autovalidateMode: state.showErrorMessages
? AutovalidateMode.always
: AutovalidateMode.disabled,
child: SliverList.list(
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 8,
),
child: CustomTextFormField(
initialValue: state.profile.name,
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
)) ...[
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,
),
leading: const Icon(Icons.update),
onTap: () async {
final intervalInHours = await SettingsInputDialog(
title: t.profile.detailsForm
.updateIntervalDialogTitle,
initialValue: options?.updateInterval.inHours,
optionalAction: (
t.general.state.disable,
() =>
notifier.setField(updateInterval: none()),
),
validator: isPort,
mapTo: int.tryParse,
digitsOnly: true,
).show(context);
if (intervalInHours == null) return;
notifier.setField(
updateInterval: optionOf(intervalInHours),
);
},
),
],
if (state.isEditing)
ListTile(
title: Text(t.profile.detailsForm.lastUpdate),
subtitle: Text(state.profile.lastUpdate.format()),
dense: true,
),
],
),
),
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 (showLoadingOverlay)
Positioned.fill(
child: Container(
color: Colors.black54,
padding: const EdgeInsets.symmetric(horizontal: 36),
child: const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
LinearProgressIndicator(
backgroundColor: Colors.transparent,
),
],
),
),
),
],
);
case AsyncError(:final error):
return Scaffold(
body: CustomScrollView(
slivers: [
SliverAppBar(
title: Text(t.profile.detailsPageTitle),
pinned: true,
),
SliverErrorBodyPlaceholder(t.presentShortError(error)),
],
),
);
default:
return const Scaffold();
}
}
}

View File

@@ -0,0 +1,22 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hiddify/features/profile/model/profile_entity.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
part 'profile_details_state.freezed.dart';
@freezed
class ProfileDetailsState with _$ProfileDetailsState {
const ProfileDetailsState._();
const factory ProfileDetailsState({
required ProfileEntity profile,
@Default(false) bool isEditing,
@Default(false) bool showErrorMessages,
AsyncValue<void>? save,
AsyncValue<void>? update,
AsyncValue<void>? delete,
}) = _ProfileDetailsState;
bool get isBusy =>
save is AsyncLoading || delete is AsyncLoading || update is AsyncLoading;
}