Add local profile

This commit is contained in:
problematicconsumer
2023-10-02 18:51:14 +03:30
parent a7e157c036
commit d50541f7a3
26 changed files with 1118 additions and 260 deletions

View File

@@ -49,21 +49,39 @@ class ProfilesNotifier extends _$ProfilesNotifier with AppLogger {
}).run();
}
Future<Unit> addProfile(String url) async {
Future<Unit> addProfile(String rawInput) async {
final activeProfile = await ref.read(activeProfileProvider.future);
final markAsActive =
activeProfile == null || ref.read(markNewProfileActiveProvider);
loggy.debug("adding profile, url: [$url]");
return ref
.read(profilesRepositoryProvider)
.addByUrl(url, markAsActive: markAsActive)
.getOrElse((l) {
loggy.warning("failed to add profile: $l");
throw l;
}).run();
if (LinkParser.parse(rawInput) case (final link)?) {
loggy.debug("adding profile, url: [${link.url}]");
return ref
.read(profilesRepositoryProvider)
.addByUrl(link.url, markAsActive: markAsActive)
.getOrElse((l) {
loggy.warning("failed to add profile: $l");
throw l;
}).run();
} else if (LinkParser.protocol(rawInput) case (final parsed)?) {
loggy.debug("adding profile, content");
return ref
.read(profilesRepositoryProvider)
.addByContent(
parsed.content,
name: parsed.name,
markAsActive: markAsActive,
)
.getOrElse((l) {
loggy.warning("failed to add profile: $l");
throw l;
}).run();
} else {
loggy.debug("invalid content");
throw const ProfileInvalidUrlFailure();
}
}
Future<Unit?> updateProfile(Profile profile) async {
Future<Unit?> updateProfile(RemoteProfile profile) async {
loggy.debug("updating profile");
return ref
.read(profilesRepositoryProvider)

View File

@@ -50,20 +50,22 @@ class ProfilesUpdateNotifier extends _$ProfilesUpdateNotifier with AppLogger {
await ref.read(profilesRepositoryProvider).watchAll().first;
if (failureOrProfiles case Right(value: final profiles)) {
for (final profile in profiles) {
loggy.debug("checking profile: [${profile.name}]");
final updateInterval = profile.options?.updateInterval;
if (updateInterval != null &&
updateInterval <=
DateTime.now().difference(profile.lastUpdate)) {
final failureOrSuccess = await ref
.read(profilesRepositoryProvider)
.update(profile)
.run();
state = AsyncData(
(name: profile.name, failureOrSuccess: failureOrSuccess),
);
} else {
loggy.debug("skipping profile: [${profile.name}]");
if (profile case RemoteProfile()) {
loggy.debug("checking profile: [${profile.name}]");
final updateInterval = profile.options?.updateInterval;
if (updateInterval != null &&
updateInterval <=
DateTime.now().difference(profile.lastUpdate)) {
final failureOrSuccess = await ref
.read(profilesRepositoryProvider)
.update(profile)
.run();
state = AsyncData(
(name: profile.name, failureOrSuccess: failureOrSuccess),
);
} else {
loggy.debug("skipping profile: [${profile.name}]");
}
}
}
}