2022-08-29 20:35:06 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-10-28 08:17:08 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
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-09-29 13:08:19 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
2022-08-29 20:35:06 +00:00
|
|
|
import 'package:selfprivacy/logic/models/service.dart';
|
2021-08-29 09:50:24 +00:00
|
|
|
|
|
|
|
part 'services_state.dart';
|
|
|
|
|
2022-05-17 13:31:34 +00:00
|
|
|
class ServicesCubit extends ServerInstallationDependendCubit<ServicesState> {
|
2022-06-05 19:36:32 +00:00
|
|
|
ServicesCubit(final ServerInstallationCubit serverInstallationCubit)
|
2022-08-29 20:35:06 +00:00
|
|
|
: super(serverInstallationCubit, const ServicesState.empty());
|
2022-06-05 19:36:32 +00:00
|
|
|
final ServerApi api = ServerApi();
|
2022-08-29 20:35:06 +00:00
|
|
|
Timer? timer;
|
2022-05-24 18:55:39 +00:00
|
|
|
@override
|
2021-09-29 13:08:19 +00:00
|
|
|
Future<void> load() async {
|
2022-05-17 20:08:28 +00:00
|
|
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
2022-08-29 20:35:06 +00:00
|
|
|
final List<Service> services = await api.getAllServices();
|
2021-09-29 18:28:47 +00:00
|
|
|
emit(
|
|
|
|
ServicesState(
|
2022-08-29 20:35:06 +00:00
|
|
|
services: services,
|
2022-09-19 00:21:08 +00:00
|
|
|
lockedServices: const [],
|
2021-09-29 18:28:47 +00:00
|
|
|
),
|
|
|
|
);
|
2022-08-29 20:35:06 +00:00
|
|
|
timer = Timer(const Duration(seconds: 10), () => reload(useTimer: true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> reload({final bool useTimer = false}) async {
|
|
|
|
final List<Service> services = await api.getAllServices();
|
|
|
|
emit(
|
2022-09-19 00:21:08 +00:00
|
|
|
state.copyWith(
|
2022-08-29 20:35:06 +00:00
|
|
|
services: services,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (useTimer) {
|
|
|
|
timer = Timer(const Duration(seconds: 60), () => reload(useTimer: true));
|
2021-09-29 18:28:47 +00:00
|
|
|
}
|
2021-08-29 13:54:28 +00:00
|
|
|
}
|
|
|
|
|
2022-09-06 10:25:28 +00:00
|
|
|
Future<void> restart(final String serviceId) async {
|
2022-09-19 00:21:08 +00:00
|
|
|
emit(state.copyWith(lockedServices: [...state.lockedServices, serviceId]));
|
2022-10-28 08:17:08 +00:00
|
|
|
final result = await api.restartService(serviceId);
|
|
|
|
if (!result.success) {
|
|
|
|
getIt<NavigationService>().showSnackBar('jobs.generic_error'.tr());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!result.data) {
|
|
|
|
getIt<NavigationService>()
|
|
|
|
.showSnackBar(result.message ?? 'jobs.generic_error'.tr());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-19 00:21:08 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 2));
|
2023-04-12 05:42:33 +00:00
|
|
|
unawaited(reload());
|
2022-09-19 00:21:08 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 10));
|
|
|
|
emit(
|
|
|
|
state.copyWith(
|
|
|
|
lockedServices: state.lockedServices
|
|
|
|
.where((final element) => element != serviceId)
|
|
|
|
.toList(),
|
|
|
|
),
|
|
|
|
);
|
2023-04-12 05:42:33 +00:00
|
|
|
unawaited(reload());
|
2022-09-19 00:21:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> moveService(
|
|
|
|
final String serviceId,
|
|
|
|
final String destination,
|
|
|
|
) async {
|
|
|
|
await api.moveService(serviceId, destination);
|
2022-09-06 10:25:28 +00:00
|
|
|
}
|
|
|
|
|
2021-09-29 13:08:19 +00:00
|
|
|
@override
|
|
|
|
void clear() async {
|
2022-08-29 20:35:06 +00:00
|
|
|
emit(const ServicesState.empty());
|
|
|
|
if (timer != null && timer!.isActive) {
|
|
|
|
timer!.cancel();
|
|
|
|
timer = null;
|
|
|
|
}
|
2021-08-29 13:54:28 +00:00
|
|
|
}
|
2021-08-29 09:50:24 +00:00
|
|
|
}
|