2022-08-24 05:35:49 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
import 'package:collection/collection.dart';
|
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';
|
2021-08-29 15:02:51 +00:00
|
|
|
import 'package:selfprivacy/logic/models/job.dart';
|
2024-02-20 16:33:24 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/server_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> {
|
2024-02-20 16:33:24 +00:00
|
|
|
JobsCubit() : super(JobsStateEmpty()) {
|
|
|
|
final apiConnectionRepository = getIt<ApiConnectionRepository>();
|
|
|
|
_apiDataSubscription = apiConnectionRepository.dataStream.listen(
|
|
|
|
(final ApiData apiData) {
|
|
|
|
if (apiData.serverJobs.data != null &&
|
|
|
|
apiData.serverJobs.data!.isNotEmpty) {
|
|
|
|
_handleServerJobs(apiData.serverJobs.data!);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamSubscription? _apiDataSubscription;
|
2021-05-25 21:53:54 +00:00
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
void _handleServerJobs(final List<ServerJob> jobs) {
|
|
|
|
if (state is! JobsStateLoading) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (state.rebuildJobUid == null) {
|
|
|
|
return;
|
2022-10-07 17:50:18 +00:00
|
|
|
}
|
2024-02-20 16:33:24 +00:00
|
|
|
// Find a job with the uid of the rebuild job
|
|
|
|
final ServerJob? rebuildJob = jobs.firstWhereOrNull(
|
|
|
|
(final job) => job.uid == state.rebuildJobUid,
|
|
|
|
);
|
|
|
|
if (rebuildJob == null ||
|
|
|
|
rebuildJob.status == JobStatusEnum.error ||
|
|
|
|
rebuildJob.status == JobStatusEnum.finished) {
|
|
|
|
emit((state as JobsStateLoading).finished());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addJob(final ClientJob job) async {
|
|
|
|
emit(state.addJob(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);
|
|
|
|
}
|
|
|
|
|
2021-12-06 18:31:19 +00:00
|
|
|
Future<void> rebootServer() async {
|
2024-02-20 16:33:24 +00:00
|
|
|
if (state is JobsStateEmpty) {
|
|
|
|
emit(
|
|
|
|
JobsStateLoading(
|
|
|
|
[RebootServerJob(status: JobStatusEnum.running)],
|
|
|
|
null,
|
|
|
|
const [],
|
|
|
|
),
|
|
|
|
);
|
2024-02-20 17:04:39 +00:00
|
|
|
final rebootResult = await getIt<ApiConnectionRepository>().api.reboot();
|
2024-02-20 16:33:24 +00:00
|
|
|
if (rebootResult.success && rebootResult.data != null) {
|
|
|
|
emit(
|
|
|
|
JobsStateFinished(
|
|
|
|
[
|
|
|
|
RebootServerJob(
|
|
|
|
status: JobStatusEnum.finished,
|
|
|
|
message: rebootResult.message,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
null,
|
|
|
|
const [],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
emit(
|
|
|
|
JobsStateFinished(
|
|
|
|
[RebootServerJob(status: JobStatusEnum.error)],
|
|
|
|
null,
|
|
|
|
const [],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2021-12-06 18:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> upgradeServer() async {
|
2024-02-20 16:33:24 +00:00
|
|
|
if (state is JobsStateEmpty) {
|
|
|
|
emit(
|
|
|
|
JobsStateLoading(
|
|
|
|
[UpgradeServerJob(status: JobStatusEnum.running)],
|
|
|
|
null,
|
|
|
|
const [],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
final result = await getIt<ApiConnectionRepository>().api.upgrade();
|
|
|
|
if (result.success && result.data != null) {
|
|
|
|
emit(
|
|
|
|
JobsStateLoading(
|
|
|
|
[UpgradeServerJob(status: JobStatusEnum.finished)],
|
|
|
|
result.data!.uid,
|
|
|
|
const [],
|
|
|
|
),
|
|
|
|
);
|
2021-12-06 18:31:19 +00:00
|
|
|
} else {
|
2024-02-20 16:33:24 +00:00
|
|
|
emit(
|
|
|
|
JobsStateFinished(
|
|
|
|
[UpgradeServerJob(status: JobStatusEnum.error)],
|
|
|
|
null,
|
|
|
|
const [],
|
|
|
|
),
|
|
|
|
);
|
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;
|
2024-02-20 16:33:24 +00:00
|
|
|
emit(JobsStateLoading(jobs, null, const []));
|
|
|
|
|
|
|
|
await Future<void>.delayed(Duration.zero);
|
|
|
|
|
|
|
|
final rebuildRequired = jobs.any((final job) => job.requiresRebuild);
|
2022-10-06 19:15:25 +00:00
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
for (final ClientJob job in jobs) {
|
2024-02-20 16:33:24 +00:00
|
|
|
emit(
|
|
|
|
(state as JobsStateLoading)
|
|
|
|
.updateJobStatus(job.id, JobStatusEnum.running),
|
|
|
|
);
|
2024-02-20 17:04:39 +00:00
|
|
|
final (result, message) = await job.execute();
|
2024-02-20 16:33:24 +00:00
|
|
|
if (result) {
|
|
|
|
emit(
|
|
|
|
(state as JobsStateLoading).updateJobStatus(
|
|
|
|
job.id,
|
|
|
|
JobStatusEnum.finished,
|
|
|
|
message: message,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
emit(
|
|
|
|
(state as JobsStateLoading)
|
|
|
|
.updateJobStatus(job.id, JobStatusEnum.error, message: message),
|
|
|
|
);
|
|
|
|
}
|
2021-06-08 18:52:44 +00:00
|
|
|
}
|
2021-07-29 05:24:42 +00:00
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
if (!rebuildRequired) {
|
|
|
|
emit((state as JobsStateLoading).finished());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final rebuildResult = await getIt<ApiConnectionRepository>().api.apply();
|
|
|
|
if (rebuildResult.success) {
|
|
|
|
if (rebuildResult.data != null) {
|
|
|
|
emit(
|
|
|
|
(state as JobsStateLoading)
|
|
|
|
.copyWith(rebuildJobUid: rebuildResult.data!.uid),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
emit((state as JobsStateLoading).finished());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
emit((state as JobsStateLoading).finished());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-06 19:15:25 +00:00
|
|
|
|
2024-02-20 16:33:24 +00:00
|
|
|
Future<void> acknowledgeFinished() async {
|
|
|
|
if (state is! JobsStateFinished) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final rebuildJobUid = state.rebuildJobUid;
|
|
|
|
if ((state as JobsStateFinished).postponedJobs.isNotEmpty) {
|
|
|
|
emit(JobsStateWithJobs((state as JobsStateFinished).postponedJobs));
|
|
|
|
} else {
|
2022-08-30 03:09:09 +00:00
|
|
|
emit(JobsStateEmpty());
|
2021-06-08 18:52:44 +00:00
|
|
|
}
|
2024-02-20 16:33:24 +00:00
|
|
|
if (rebuildJobUid != null) {
|
|
|
|
await getIt<ApiConnectionRepository>().removeServerJob(rebuildJobUid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onChange(final Change<JobsState> change) {
|
|
|
|
super.onChange(change);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> close() {
|
|
|
|
_apiDataSubscription?.cancel();
|
|
|
|
return super.close();
|
2021-05-25 21:53:54 +00:00
|
|
|
}
|
|
|
|
}
|