selfprivacy.org.app/lib/logic/cubit/services/services_cubit.dart

84 lines
2.4 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:easy_localization/easy_localization.dart';
import 'package:selfprivacy/config/get_it_config.dart';
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
import 'package:selfprivacy/logic/models/service.dart';
2021-08-29 12:50:24 +03:00
part 'services_state.dart';
class ServicesCubit extends ServerInstallationDependendCubit<ServicesState> {
2022-06-05 22:36:32 +03:00
ServicesCubit(final ServerInstallationCubit serverInstallationCubit)
: super(serverInstallationCubit, const ServicesState.empty());
2022-06-05 22:36:32 +03:00
final ServerApi api = ServerApi();
Timer? timer;
2022-05-24 21:55:39 +03:00
@override
Future<void> load() async {
if (serverInstallationCubit.state is ServerInstallationFinished) {
final List<Service> services = await api.getAllServices();
2021-09-29 21:28:47 +03:00
emit(
ServicesState(
services: services,
2022-09-19 03:21:08 +03:00
lockedServices: const [],
2021-09-29 21:28:47 +03: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 03:21:08 +03:00
state.copyWith(
services: services,
),
);
if (useTimer) {
timer = Timer(const Duration(seconds: 60), () => reload(useTimer: true));
2021-09-29 21:28:47 +03:00
}
2021-08-29 16:54:28 +03:00
}
Future<void> restart(final String serviceId) async {
2022-09-19 03:21:08 +03:00
emit(state.copyWith(lockedServices: [...state.lockedServices, serviceId]));
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 03:21:08 +03:00
await Future.delayed(const Duration(seconds: 2));
reload();
await Future.delayed(const Duration(seconds: 10));
emit(
state.copyWith(
lockedServices: state.lockedServices
.where((final element) => element != serviceId)
.toList(),
),
);
reload();
}
Future<void> moveService(
final String serviceId,
final String destination,
) async {
await api.moveService(serviceId, destination);
}
@override
void clear() async {
emit(const ServicesState.empty());
if (timer != null && timer!.isActive) {
timer!.cancel();
timer = null;
}
2021-08-29 16:54:28 +03:00
}
2021-08-29 12:50:24 +03:00
}