mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-11 18:39:45 +00:00
refactor(job): Implement polymorphic predicate for job accessibility
Now every job type can impement canAddTo function to make JobsCubit know whether it can be applied or not
This commit is contained in:
parent
db2f5c1342
commit
7bad11967a
|
@ -24,10 +24,13 @@ class JobsCubit extends Cubit<JobsState> {
|
|||
final ServicesCubit servicesCubit;
|
||||
|
||||
void addJob(final ClientJob job) {
|
||||
_updateJobsState([
|
||||
...currentJobList,
|
||||
...[job]
|
||||
]);
|
||||
final jobs = currentJobList;
|
||||
if (job.canAddTo(jobs)) {
|
||||
_updateJobsState([
|
||||
...jobs,
|
||||
...[job],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
void removeJob(final String id) {
|
||||
|
@ -35,25 +38,6 @@ class JobsCubit extends Cubit<JobsState> {
|
|||
emit(newState);
|
||||
}
|
||||
|
||||
void createOrRemoveServiceToggleJob(final ToggleJob job) {
|
||||
final List<ClientJob> newJobsList = <ClientJob>[];
|
||||
if (state is JobsStateWithJobs) {
|
||||
newJobsList.addAll((state as JobsStateWithJobs).clientJobList);
|
||||
}
|
||||
final bool needToRemoveJob = newJobsList
|
||||
.any((final el) => el is ServiceToggleJob && el.id == job.id);
|
||||
if (needToRemoveJob) {
|
||||
final ClientJob removingJob = newJobsList.firstWhere(
|
||||
(final el) => el is ServiceToggleJob && el.id == job.id,
|
||||
);
|
||||
removeJob(removingJob.id);
|
||||
} else {
|
||||
newJobsList.add(job);
|
||||
getIt<NavigationService>().showSnackBar('jobs.job_added'.tr());
|
||||
emit(JobsStateWithJobs(newJobsList));
|
||||
}
|
||||
}
|
||||
|
||||
List<ClientJob> get currentJobList {
|
||||
final List<ClientJob> jobs = <ClientJob>[];
|
||||
if (state is JobsStateWithJobs) {
|
||||
|
@ -68,17 +52,6 @@ class JobsCubit extends Cubit<JobsState> {
|
|||
emit(JobsStateWithJobs(newJobs));
|
||||
}
|
||||
|
||||
void addUniqueJob<J extends ClientJob>(final J job) {
|
||||
final List<ClientJob> jobs = currentJobList;
|
||||
final bool exists = jobs.any((final el) => el is J);
|
||||
if (!exists) {
|
||||
_updateJobsState([
|
||||
...jobs,
|
||||
...[job]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> rebootServer() async {
|
||||
emit(JobsStateLoading());
|
||||
final bool isSuccessful = await api.reboot();
|
||||
|
|
|
@ -17,6 +17,7 @@ abstract class ClientJob extends Equatable {
|
|||
final String title;
|
||||
final String id;
|
||||
|
||||
bool canAddTo(final List<ClientJob> jobs) => true;
|
||||
void execute(final JobsCubit cubit);
|
||||
|
||||
@override
|
||||
|
@ -25,10 +26,14 @@ abstract class ClientJob extends Equatable {
|
|||
|
||||
class RebuildServerJob extends ClientJob {
|
||||
RebuildServerJob({
|
||||
required final super.title,
|
||||
final super.id,
|
||||
required super.title,
|
||||
super.id,
|
||||
});
|
||||
|
||||
@override
|
||||
bool canAddTo(final List<ClientJob> jobs) =>
|
||||
super.canAddTo(jobs) && !jobs.any((final job) => job is RebuildServerJob);
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.upgradeServer();
|
||||
|
@ -74,6 +79,12 @@ class DeleteUserJob extends ClientJob {
|
|||
|
||||
final User user;
|
||||
|
||||
@override
|
||||
bool canAddTo(final List<ClientJob> jobs) =>
|
||||
super.canAddTo(jobs) &&
|
||||
!jobs.any(
|
||||
(final job) => job is DeleteUserJob && job.user.login == user.login);
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.usersCubit.deleteUser(user);
|
||||
|
@ -83,33 +94,31 @@ class DeleteUserJob extends ClientJob {
|
|||
List<Object> get props => [id, title, user];
|
||||
}
|
||||
|
||||
abstract class ToggleJob extends ClientJob {
|
||||
ToggleJob({
|
||||
required final this.service,
|
||||
required final super.title,
|
||||
});
|
||||
|
||||
final Service service;
|
||||
|
||||
@override
|
||||
List<Object> get props => [...super.props, service];
|
||||
}
|
||||
|
||||
class ServiceToggleJob extends ToggleJob {
|
||||
class ServiceToggleJob extends ClientJob {
|
||||
ServiceToggleJob({
|
||||
required super.service,
|
||||
required this.service,
|
||||
required this.needToTurnOn,
|
||||
}) : super(
|
||||
title:
|
||||
'${needToTurnOn ? "jobs.service_turn_on".tr() : "jobs.service_turn_off".tr()} ${service.displayName}',
|
||||
);
|
||||
|
||||
final bool needToTurnOn;
|
||||
final Service service;
|
||||
|
||||
@override
|
||||
bool canAddTo(final List<ClientJob> jobs) =>
|
||||
super.canAddTo(jobs) &&
|
||||
!jobs.any((final job) =>
|
||||
job is ServiceToggleJob && job.service.id == service.id);
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.api.switchService(service.id, needToTurnOn);
|
||||
}
|
||||
|
||||
final bool needToTurnOn;
|
||||
@override
|
||||
List<Object> get props => [...super.props, service];
|
||||
}
|
||||
|
||||
class CreateSSHKeyJob extends ClientJob {
|
||||
|
@ -139,6 +148,14 @@ class DeleteSSHKeyJob extends ClientJob {
|
|||
final User user;
|
||||
final String publicKey;
|
||||
|
||||
@override
|
||||
bool canAddTo(final List<ClientJob> jobs) =>
|
||||
super.canAddTo(jobs) &&
|
||||
!jobs.any((final job) =>
|
||||
job is DeleteSSHKeyJob &&
|
||||
job.publicKey == publicKey &&
|
||||
job.user.login == user.login);
|
||||
|
||||
@override
|
||||
void execute(final JobsCubit cubit) async {
|
||||
await cubit.usersCubit.deleteSshKey(user, publicKey);
|
||||
|
|
|
@ -29,7 +29,7 @@ class _ServerSettingsState extends State<_ServerSettings> {
|
|||
SwitchListTile(
|
||||
value: allowAutoUpgrade ?? false,
|
||||
onChanged: (final switched) {
|
||||
context.read<JobsCubit>().addUniqueJob(
|
||||
context.read<JobsCubit>().addJob(
|
||||
RebuildServerJob(title: 'jobs.upgrade_server'.tr()),
|
||||
);
|
||||
context
|
||||
|
@ -54,7 +54,7 @@ class _ServerSettingsState extends State<_ServerSettings> {
|
|||
SwitchListTile(
|
||||
value: rebootAfterUpgrade ?? false,
|
||||
onChanged: (final switched) {
|
||||
context.read<JobsCubit>().addUniqueJob(
|
||||
context.read<JobsCubit>().addJob(
|
||||
RebuildServerJob(title: 'jobs.upgrade_server'.tr()),
|
||||
);
|
||||
context
|
||||
|
@ -82,7 +82,7 @@ class _ServerSettingsState extends State<_ServerSettings> {
|
|||
serverDetailsState.serverTimezone.toString(),
|
||||
),
|
||||
onTap: () {
|
||||
context.read<JobsCubit>().addUniqueJob(
|
||||
context.read<JobsCubit>().addJob(
|
||||
RebuildServerJob(title: 'jobs.upgrade_server'.tr()),
|
||||
);
|
||||
Navigator.of(context).push(
|
||||
|
|
|
@ -89,7 +89,7 @@ class _ServicePageState extends State<ServicePage> {
|
|||
ListTile(
|
||||
iconColor: Theme.of(context).colorScheme.onBackground,
|
||||
onTap: () => {
|
||||
context.read<JobsCubit>().createOrRemoveServiceToggleJob(
|
||||
context.read<JobsCubit>().addJob(
|
||||
ServiceToggleJob(
|
||||
service: service,
|
||||
needToTurnOn: serviceDisabled,
|
||||
|
|
|
@ -157,8 +157,7 @@ class _Card extends StatelessWidget {
|
|||
|
||||
return BrandSwitch(
|
||||
value: isActive,
|
||||
onChanged: (final value) =>
|
||||
jobsCubit.createOrRemoveServiceToggleJob(
|
||||
onChanged: (final value) => jobsCubit.addJob(
|
||||
ServiceToggleJob(
|
||||
service: service,
|
||||
needToTurnOn: value,
|
||||
|
|
Loading…
Reference in a new issue