2022-08-30 03:09:09 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-10-28 08:17:08 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-09-18 14:29:00 +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';
|
2022-08-30 03:09:09 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/server_job.dart';
|
|
|
|
|
|
|
|
export 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
part 'server_jobs_state.dart';
|
|
|
|
|
|
|
|
class ServerJobsCubit
|
|
|
|
extends ServerInstallationDependendCubit<ServerJobsState> {
|
|
|
|
ServerJobsCubit(final ServerInstallationCubit serverInstallationCubit)
|
|
|
|
: super(
|
|
|
|
serverInstallationCubit,
|
2022-09-18 20:12:09 +00:00
|
|
|
ServerJobsState(),
|
2022-08-30 03:09:09 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
Timer? timer;
|
|
|
|
final ServerApi api = ServerApi();
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() async {
|
|
|
|
emit(
|
2022-09-18 20:12:09 +00:00
|
|
|
ServerJobsState(),
|
2022-08-30 03:09:09 +00:00
|
|
|
);
|
|
|
|
if (timer != null && timer!.isActive) {
|
|
|
|
timer!.cancel();
|
|
|
|
timer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void load() async {
|
|
|
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
|
|
|
final List<ServerJob> jobs = await api.getServerJobs();
|
|
|
|
emit(
|
|
|
|
ServerJobsState(
|
|
|
|
serverJobList: jobs,
|
|
|
|
),
|
|
|
|
);
|
2022-09-18 20:12:09 +00:00
|
|
|
timer = Timer(const Duration(seconds: 5), () => reload(useTimer: true));
|
2022-08-30 03:09:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 14:05:41 +00:00
|
|
|
Future<void> migrateToBinds(final Map<String, String> serviceToDisk) async {
|
2022-09-18 14:29:00 +00:00
|
|
|
final result = await api.migrateToBinds(serviceToDisk);
|
2022-10-28 08:17:08 +00:00
|
|
|
if (result.data == null) {
|
2022-09-18 14:29:00 +00:00
|
|
|
getIt<NavigationService>().showSnackBar(result.message!);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-18 14:05:41 +00:00
|
|
|
emit(
|
|
|
|
ServerJobsState(
|
2022-10-28 08:17:08 +00:00
|
|
|
migrationJobUid: result.data,
|
2022-09-18 14:05:41 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerJob? getServerJobByUid(final String uid) {
|
|
|
|
ServerJob? job;
|
|
|
|
|
|
|
|
try {
|
|
|
|
job = state.serverJobList.firstWhere(
|
|
|
|
(final ServerJob job) => job.uid == uid,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return job;
|
|
|
|
}
|
|
|
|
|
2022-09-18 20:12:09 +00:00
|
|
|
Future<void> removeServerJob(final String uid) async {
|
|
|
|
final result = await api.removeApiJob(uid);
|
|
|
|
if (!result.success) {
|
2022-10-28 08:17:08 +00:00
|
|
|
getIt<NavigationService>().showSnackBar('jobs.generic_error'.tr());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.data) {
|
|
|
|
getIt<NavigationService>()
|
|
|
|
.showSnackBar(result.message ?? 'jobs.generic_error'.tr());
|
2022-09-18 20:12:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit(
|
|
|
|
ServerJobsState(
|
2022-09-19 00:42:00 +00:00
|
|
|
serverJobList: [
|
|
|
|
for (final ServerJob job in state.serverJobList)
|
|
|
|
if (job.uid != uid) job
|
|
|
|
],
|
2022-09-18 20:12:09 +00:00
|
|
|
),
|
|
|
|
);
|
2022-09-19 00:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> removeAllFinishedJobs() async {
|
|
|
|
final List<ServerJob> finishedJobs = state.serverJobList
|
|
|
|
.where(
|
|
|
|
(final ServerJob job) =>
|
|
|
|
job.status == JobStatusEnum.finished ||
|
|
|
|
job.status == JobStatusEnum.error,
|
|
|
|
)
|
|
|
|
.toList();
|
|
|
|
for (final ServerJob job in finishedJobs) {
|
|
|
|
await removeServerJob(job.uid);
|
|
|
|
}
|
2022-09-18 20:12:09 +00:00
|
|
|
}
|
|
|
|
|
2022-08-30 03:09:09 +00:00
|
|
|
Future<void> reload({final bool useTimer = false}) async {
|
|
|
|
final List<ServerJob> jobs = await api.getServerJobs();
|
|
|
|
emit(
|
|
|
|
ServerJobsState(
|
|
|
|
serverJobList: jobs,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (useTimer) {
|
2022-09-18 20:12:09 +00:00
|
|
|
timer = Timer(const Duration(seconds: 5), () => reload(useTimer: true));
|
2022-08-30 03:09:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|