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}]");
}
}
}
}

View File

@@ -6,6 +6,7 @@ import 'package:go_router/go_router.dart';
import 'package:hiddify/core/core_providers.dart';
import 'package:hiddify/core/router/router.dart';
import 'package:hiddify/domain/failures.dart';
import 'package:hiddify/domain/profiles/profiles.dart';
import 'package:hiddify/features/common/qr_code_scanner_screen.dart';
import 'package:hiddify/features/profiles/notifier/notifier.dart';
import 'package:hiddify/utils/utils.dart';
@@ -29,8 +30,13 @@ class AddProfileModal extends HookConsumerWidget {
final addProfileMutation = useMutation(
initialOnFailure: (err) {
mutationTriggered.value = false;
// CustomToast.error(t.presentError(err)).show(context);
CustomAlertDialog.fromErr(t.presentError(err)).show(context);
if (err case ProfileInvalidUrlFailure()) {
CustomToast.error(
t.profile.add.invalidUrlMsg,
).show(context);
} else {
CustomAlertDialog.fromErr(t.presentError(err)).show(context);
}
},
initialOnSuccess: () {
CustomToast.success(t.profile.save.successMsg).show(context);
@@ -102,24 +108,15 @@ class AddProfileModal extends HookConsumerWidget {
size: buttonWidth,
onTap: () async {
final captureResult =
await Clipboard.getData(Clipboard.kTextPlain);
final link =
LinkParser.parse(captureResult?.text ?? '');
if (link != null && context.mounted) {
if (addProfileMutation.state.isInProgress) return;
mutationTriggered.value = true;
addProfileMutation.setFuture(
ref
.read(profilesNotifierProvider.notifier)
.addProfile(link.url),
);
} else {
if (context.mounted) {
CustomToast.error(
t.profile.add.invalidUrlMsg,
).show(context);
}
}
await Clipboard.getData(Clipboard.kTextPlain)
.then((value) => value?.text ?? '');
if (addProfileMutation.state.isInProgress) return;
mutationTriggered.value = true;
addProfileMutation.setFuture(
ref
.read(profilesNotifierProvider.notifier)
.addProfile(captureResult),
);
},
),
const Gap(buttonsGap),
@@ -134,24 +131,15 @@ class AddProfileModal extends HookConsumerWidget {
await const QRCodeScannerScreen()
.open(context);
if (captureResult == null) return;
final link = LinkParser.simple(captureResult);
if (link != null && context.mounted) {
if (addProfileMutation.state.isInProgress) {
return;
}
mutationTriggered.value = true;
addProfileMutation.setFuture(
ref
.read(profilesNotifierProvider.notifier)
.addProfile(link.url),
);
} else {
if (context.mounted) {
CustomToast.error(
t.profile.add.invalidUrlMsg,
).show(context);
}
if (addProfileMutation.state.isInProgress) {
return;
}
mutationTriggered.value = true;
addProfileMutation.setFuture(
ref
.read(profilesNotifierProvider.notifier)
.addProfile(captureResult),
);
},
)
else