Refactor profile addition flow

This commit is contained in:
problematicconsumer
2023-07-26 14:17:11 +03:30
parent cad4e47ee5
commit d741b7a427
13 changed files with 340 additions and 223 deletions

View File

@@ -9,6 +9,7 @@ import 'package:hiddify/domain/profiles/profiles.dart';
import 'package:hiddify/services/files_editor_service.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:meta/meta.dart';
import 'package:uuid/uuid.dart';
class ProfilesRepositoryImpl
with ExceptionHandler, InfraLogger
@@ -55,16 +56,45 @@ class ProfilesRepositoryImpl
.handleExceptions(ProfileUnexpectedFailure.new);
}
@override
TaskEither<ProfileFailure, Unit> addByUrl(
String url, {
bool markAsActive = false,
}) {
return exceptionHandler(
() async {
final profileId = const Uuid().v4();
return fetch(url, profileId)
.flatMap(
(profile) => TaskEither(
() async {
await profilesDao.create(
profile.copyWith(
id: profileId,
active: markAsActive,
),
);
return right(unit);
},
),
)
.run();
},
ProfileUnexpectedFailure.new,
);
}
@override
TaskEither<ProfileFailure, Unit> add(Profile baseProfile) {
return exceptionHandler(
() async {
return fetch(baseProfile.url, baseProfile.id)
.flatMap(
(subInfo) => TaskEither(() async {
(remoteProfile) => TaskEither(() async {
await profilesDao.create(
baseProfile.copyWith(
subInfo: subInfo,
subInfo: remoteProfile.subInfo,
extra: remoteProfile.extra,
lastUpdate: DateTime.now(),
),
);
@@ -83,10 +113,11 @@ class ProfilesRepositoryImpl
() async {
return fetch(baseProfile.url, baseProfile.id)
.flatMap(
(subInfo) => TaskEither(() async {
(remoteProfile) => TaskEither(() async {
await profilesDao.edit(
baseProfile.copyWith(
subInfo: subInfo,
subInfo: remoteProfile.subInfo,
extra: remoteProfile.extra,
lastUpdate: DateTime.now(),
),
);
@@ -123,7 +154,7 @@ class ProfilesRepositoryImpl
}
@visibleForTesting
TaskEither<ProfileFailure, SubscriptionInfo?> fetch(
TaskEither<ProfileFailure, Profile> fetch(
String url,
String fileName,
) {
@@ -143,12 +174,8 @@ class ProfilesRepositoryImpl
await File(path).delete();
return left(const ProfileFailure.invalidConfig());
}
final subInfoString =
response.headers.map['subscription-userinfo']?.single;
final subInfo = subInfoString != null
? SubscriptionInfo.fromResponseHeader(subInfoString)
: null;
return right(subInfo);
final profile = Profile.fromResponse(url, response.headers.map);
return right(profile);
},
);
}