selfprivacy.org.app/lib/logic/cubit/client_jobs/client_jobs_cubit.dart

99 lines
2.8 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
import 'package:equatable/equatable.dart';
2021-05-25 21:53:54 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
2021-08-29 13:54:28 +00:00
import 'package:selfprivacy/logic/cubit/services/services_cubit.dart';
2021-07-29 05:24:42 +00:00
import 'package:selfprivacy/logic/cubit/users/users_cubit.dart';
2021-08-29 15:02:51 +00:00
import 'package:selfprivacy/logic/models/job.dart';
2021-05-25 21:53:54 +00:00
export 'package:provider/provider.dart';
2022-08-30 03:09:09 +00:00
part 'client_jobs_state.dart';
2021-05-25 21:53:54 +00:00
class JobsCubit extends Cubit<JobsState> {
2021-08-29 13:54:28 +00:00
JobsCubit({
required this.usersCubit,
required this.servicesCubit,
2022-08-30 03:09:09 +00:00
}) : super(JobsStateEmpty());
2021-05-25 21:53:54 +00:00
2022-06-05 19:36:32 +00:00
final ServerApi api = ServerApi();
2021-07-29 05:24:42 +00:00
final UsersCubit usersCubit;
2021-08-29 13:54:28 +00:00
final ServicesCubit servicesCubit;
2021-05-25 21:53:54 +00:00
void addJob(final ClientJob job) {
final jobs = currentJobList;
if (job.canAddTo(jobs)) {
_updateJobsState([
...jobs,
...[job],
]);
}
2021-05-25 21:53:54 +00:00
}
2022-06-05 19:36:32 +00:00
void removeJob(final String id) {
final JobsState newState = (state as JobsStateWithJobs).removeById(id);
2021-05-25 21:53:54 +00:00
emit(newState);
}
List<ClientJob> get currentJobList {
final List<ClientJob> jobs = <ClientJob>[];
2021-09-02 19:32:07 +00:00
if (state is JobsStateWithJobs) {
jobs.addAll((state as JobsStateWithJobs).clientJobList);
2021-09-02 19:32:07 +00:00
}
return jobs;
}
void _updateJobsState(final List<ClientJob> newJobs) {
getIt<NavigationService>().showSnackBar('jobs.job_added'.tr());
emit(JobsStateWithJobs(newJobs));
}
2021-12-06 18:31:19 +00:00
Future<void> rebootServer() async {
2022-08-30 03:09:09 +00:00
emit(JobsStateLoading());
2022-06-05 19:36:32 +00:00
final bool isSuccessful = await api.reboot();
2021-12-06 18:31:19 +00:00
if (isSuccessful) {
getIt<NavigationService>().showSnackBar('jobs.reboot_success'.tr());
2021-12-06 18:31:19 +00:00
} else {
getIt<NavigationService>().showSnackBar('jobs.reboot_failed'.tr());
2021-12-06 18:31:19 +00:00
}
2022-08-30 03:09:09 +00:00
emit(JobsStateEmpty());
2021-12-06 18:31:19 +00:00
}
Future<void> upgradeServer() async {
2022-08-30 03:09:09 +00:00
emit(JobsStateLoading());
2022-06-05 19:36:32 +00:00
final bool isPullSuccessful = await api.pullConfigurationUpdate();
final bool isSuccessful = await api.upgrade();
2021-12-06 18:31:19 +00:00
if (isSuccessful) {
if (!isPullSuccessful) {
getIt<NavigationService>().showSnackBar('jobs.config_pull_failed'.tr());
2021-12-06 18:31:19 +00:00
} else {
getIt<NavigationService>().showSnackBar('jobs.upgrade_success'.tr());
2021-12-06 18:31:19 +00:00
}
} else {
getIt<NavigationService>().showSnackBar('jobs.upgrade_failed'.tr());
2021-12-06 18:31:19 +00:00
}
2022-08-30 03:09:09 +00:00
emit(JobsStateEmpty());
2021-12-06 18:31:19 +00:00
}
2021-06-08 18:52:44 +00:00
Future<void> applyAll() async {
2021-07-29 05:24:42 +00:00
if (state is JobsStateWithJobs) {
final List<ClientJob> jobs = (state as JobsStateWithJobs).clientJobList;
2022-08-30 03:09:09 +00:00
emit(JobsStateLoading());
for (final ClientJob job in jobs) {
job.execute(this);
2021-06-08 18:52:44 +00:00
}
2021-07-29 05:24:42 +00:00
2021-12-06 18:31:19 +00:00
await api.pullConfigurationUpdate();
2021-07-29 05:24:42 +00:00
await api.apply();
await servicesCubit.load();
2022-08-30 03:09:09 +00:00
emit(JobsStateEmpty());
2021-06-08 18:52:44 +00:00
}
2021-05-25 21:53:54 +00:00
}
}