2022-08-24 05:35:49 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-03-03 17:38:30 +00:00
|
|
|
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';
|
2022-11-16 00:24:40 +00:00
|
|
|
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';
|
2022-03-03 17:38:30 +00:00
|
|
|
|
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
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
void addJob(final ClientJob job) {
|
2022-10-07 17:50:18 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:36:17 +00:00
|
|
|
List<ClientJob> get currentJobList {
|
|
|
|
final List<ClientJob> jobs = <ClientJob>[];
|
2021-09-02 19:32:07 +00:00
|
|
|
if (state is JobsStateWithJobs) {
|
2022-10-07 16:36:17 +00:00
|
|
|
jobs.addAll((state as JobsStateWithJobs).clientJobList);
|
2021-09-02 19:32:07 +00:00
|
|
|
}
|
2022-10-07 16:36:17 +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) {
|
2022-10-03 23:32:35 +00:00
|
|
|
getIt<NavigationService>().showSnackBar('jobs.reboot_success'.tr());
|
2021-12-06 18:31:19 +00:00
|
|
|
} else {
|
2022-10-03 23:32:35 +00:00
|
|
|
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) {
|
2022-10-03 23:32:35 +00:00
|
|
|
getIt<NavigationService>().showSnackBar('jobs.config_pull_failed'.tr());
|
2021-12-06 18:31:19 +00:00
|
|
|
} else {
|
2022-10-03 23:32:35 +00:00
|
|
|
getIt<NavigationService>().showSnackBar('jobs.upgrade_success'.tr());
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-10-03 23:32:35 +00:00
|
|
|
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) {
|
2022-08-24 05:35:49 +00:00
|
|
|
final List<ClientJob> jobs = (state as JobsStateWithJobs).clientJobList;
|
2022-08-30 03:09:09 +00:00
|
|
|
emit(JobsStateLoading());
|
2022-10-06 19:15:25 +00:00
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
for (final ClientJob job in jobs) {
|
2022-10-06 19:15:25 +00:00
|
|
|
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();
|
2022-10-06 19:45:25 +00:00
|
|
|
await servicesCubit.load();
|
2022-10-06 19:15:25 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|