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

49 lines
1.4 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
2021-08-29 13:54:28 +00:00
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
import 'package:selfprivacy/logic/models/service.dart';
2021-08-29 09:50:24 +00:00
part 'services_state.dart';
class ServicesCubit extends ServerInstallationDependendCubit<ServicesState> {
2022-06-05 19:36:32 +00:00
ServicesCubit(final ServerInstallationCubit serverInstallationCubit)
: super(serverInstallationCubit, const ServicesState.empty());
2022-06-05 19:36:32 +00:00
final ServerApi api = ServerApi();
Timer? timer;
2022-05-24 18:55:39 +00:00
@override
Future<void> load() async {
if (serverInstallationCubit.state is ServerInstallationFinished) {
final List<Service> services = await api.getAllServices();
2021-09-29 18:28:47 +00:00
emit(
ServicesState(
services: services,
2021-09-29 18:28:47 +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(
ServicesState(
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
}
@override
void clear() async {
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
}