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

180 lines
5.7 KiB
Dart
Raw Normal View History

2023-07-06 17:18:41 +03:30
import 'package:dartx/dartx.dart';
import 'package:fpdart/fpdart.dart';
2023-11-26 21:20:58 +03:30
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';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/utils/utils.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:uuid/uuid.dart';
2023-11-26 21:20:58 +03:30
part 'profile_details_notifier.g.dart';
2023-07-06 17:18:41 +03:30
@riverpod
2023-11-26 21:20:58 +03:30
class ProfileDetailsNotifier extends _$ProfileDetailsNotifier with AppLogger {
2023-07-06 17:18:41 +03:30
@override
2023-11-26 21:20:58 +03:30
Future<ProfileDetailsState> build(
2023-07-06 17:18:41 +03:30
String id, {
String? url,
2023-09-02 22:33:29 +03:30
String? profileName,
2023-07-06 17:18:41 +03:30
}) async {
if (id == 'new') {
2023-11-26 21:20:58 +03:30
return ProfileDetailsState(
profile: RemoteProfileEntity(
2023-07-06 17:18:41 +03:30
id: const Uuid().v4(),
active: true,
2023-09-02 22:33:29 +03:30
name: profileName ?? "",
2023-07-06 17:18:41 +03:30
url: url ?? "",
lastUpdate: DateTime.now(),
),
);
}
2023-11-26 21:20:58 +03:30
final failureOrProfile = await _profilesRepo.getById(id).run();
2023-07-06 17:18:41 +03:30
return failureOrProfile.match(
2023-10-03 21:12:14 +03:30
(err) {
loggy.warning('failed to load profile', err);
throw err;
2023-07-06 17:18:41 +03:30
},
(profile) {
if (profile == null) {
loggy.warning('profile with id: [$id] does not exist');
throw const ProfileNotFoundFailure();
}
2023-09-28 14:03:45 +03:30
_originalProfile = profile;
2023-11-26 21:20:58 +03:30
return ProfileDetailsState(profile: profile, isEditing: true);
2023-07-06 17:18:41 +03:30
},
);
}
2023-11-26 21:20:58 +03:30
ProfileRepository get _profilesRepo =>
ref.read(profileRepositoryProvider).requireValue;
ProfileEntity? _originalProfile;
2023-07-06 17:18:41 +03:30
2023-09-28 14:03:45 +03:30
void setField({String? name, String? url, Option<int>? updateInterval}) {
2023-07-06 17:18:41 +03:30
if (state case AsyncData(:final value)) {
state = AsyncData(
value.copyWith(
2023-10-02 18:51:14 +03:30
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),
2023-07-06 17:18:41 +03:30
),
),
2023-09-28 14:03:45 +03:30
);
2023-07-06 17:18:41 +03:30
}
}
Future<void> save() async {
if (state case AsyncData(:final value)) {
2023-11-26 21:20:58 +03:30
if (value.save case AsyncLoading()) return;
2023-07-06 17:18:41 +03:30
final profile = value.profile;
Either<ProfileFailure, Unit>? failureOrSuccess;
2023-11-26 21:20:58 +03:30
state = AsyncData(value.copyWith(save: const AsyncLoading()));
2023-10-02 18:51:14 +03:30
switch (profile) {
2023-11-26 21:20:58 +03:30
case RemoteProfileEntity():
2023-10-02 18:51:14 +03:30
loggy.debug(
'saving profile, url: [${profile.url}], name: [${profile.name}]',
);
if (profile.name.isBlank || profile.url.isBlank) {
2023-11-26 21:20:58 +03:30
loggy.debug('save: invalid arguments');
2023-10-02 18:51:14 +03:30
} else if (value.isEditing) {
2023-11-26 21:20:58 +03:30
if (_originalProfile case RemoteProfileEntity(:final url)
2023-10-02 18:51:14 +03:30
when url == profile.url) {
loggy.debug('editing profile');
2023-11-26 21:20:58 +03:30
failureOrSuccess = await _profilesRepo.patch(profile).run();
2023-10-02 18:51:14 +03:30
} else {
loggy.debug('updating profile');
2024-01-01 17:46:29 +03:30
failureOrSuccess = await _profilesRepo
.updateSubscription(profile, patchBaseProfile: true)
.run();
2023-10-02 18:51:14 +03:30
}
} else {
loggy.debug('adding profile, url: [${profile.url}]');
failureOrSuccess = await _profilesRepo.add(profile).run();
}
2023-11-26 21:20:58 +03:30
case LocalProfileEntity() when value.isEditing:
2023-09-28 14:03:45 +03:30
loggy.debug('editing profile');
2023-11-26 21:20:58 +03:30
failureOrSuccess = await _profilesRepo.patch(profile).run();
2023-10-02 18:51:14 +03:30
default:
loggy.warning("local profile can't be added manually");
2023-07-06 17:18:41 +03:30
}
2023-11-26 21:20:58 +03:30
2023-07-06 17:18:41 +03:30
state = AsyncData(
value.copyWith(
save: failureOrSuccess?.fold(
2023-11-26 21:20:58 +03:30
(l) => AsyncError(l, StackTrace.current),
(_) => const AsyncData(null),
2023-07-06 17:18:41 +03:30
) ??
value.save,
showErrorMessages: true,
),
2023-09-28 14:03:45 +03:30
);
}
}
Future<void> updateProfile() async {
if (state case AsyncData(:final value)) {
2023-11-26 21:20:58 +03:30
if (value.update?.isLoading ?? false || !value.isEditing) return;
if (value.profile case LocalProfileEntity()) {
2023-10-02 18:51:14 +03:30
loggy.warning("local profile can't be updated");
return;
}
2023-11-26 21:20:58 +03:30
2023-09-28 14:03:45 +03:30
final profile = value.profile;
2023-11-26 21:20:58 +03:30
state = AsyncData(value.copyWith(update: const AsyncLoading()));
2023-09-28 14:03:45 +03:30
final failureOrUpdatedProfile = await _profilesRepo
2023-11-26 21:20:58 +03:30
.updateSubscription(profile as RemoteProfileEntity)
.flatMap((_) => _profilesRepo.getById(id))
2023-09-28 14:03:45 +03:30
.run();
2023-11-26 21:20:58 +03:30
2023-09-28 14:03:45 +03:30
state = AsyncData(
value.copyWith(
update: failureOrUpdatedProfile.match(
2023-11-26 21:20:58 +03:30
(l) => AsyncError(l, StackTrace.current),
(_) => const AsyncData(null),
2023-09-28 14:03:45 +03:30
),
profile: failureOrUpdatedProfile.match(
(_) => profile,
(updatedProfile) => updatedProfile ?? profile,
),
),
);
2023-07-06 17:18:41 +03:30
}
}
Future<void> delete() async {
if (state case AsyncData(:final value)) {
2023-11-26 21:20:58 +03:30
if (value.delete case AsyncLoading()) return;
2023-07-06 17:18:41 +03:30
final profile = value.profile;
2023-11-26 21:20:58 +03:30
state = AsyncData(value.copyWith(delete: const AsyncLoading()));
2023-07-06 17:18:41 +03:30
state = AsyncData(
value.copyWith(
2023-11-26 21:20:58 +03:30
delete: await AsyncValue.guard(() async {
await _profilesRepo
.deleteById(profile.id)
.getOrElse((l) => throw l)
.run();
}),
2023-07-06 17:18:41 +03:30
),
2023-09-28 14:03:45 +03:30
);
2023-07-06 17:18:41 +03:30
}
}
}