selfprivacy.org.app/lib/logic/cubit/hetzner_metrics/hetzner_metrics_cubit.dart

51 lines
1.2 KiB
Dart
Raw Normal View History

2021-04-10 03:04:23 +00:00
import 'dart:async';
2022-05-24 18:55:39 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2021-04-10 03:04:23 +00:00
import 'package:equatable/equatable.dart';
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
import 'package:selfprivacy/logic/models/hetzner_metrics.dart';
2022-06-05 19:36:32 +00:00
import 'package:selfprivacy/logic/cubit/hetzner_metrics/hetzner_metrics_repository.dart';
2021-04-10 03:04:23 +00:00
part 'hetzner_metrics_state.dart';
class HetznerMetricsCubit extends Cubit<HetznerMetricsState> {
2022-05-24 18:55:39 +00:00
HetznerMetricsCubit() : super(const HetznerMetricsLoading(Period.day));
2021-04-10 03:04:23 +00:00
2022-06-05 19:36:32 +00:00
final HetznerMetricsRepository repository = HetznerMetricsRepository();
2021-04-10 03:04:23 +00:00
Timer? timer;
2022-05-24 18:55:39 +00:00
@override
2022-06-05 19:36:32 +00:00
Future<void> close() {
2021-04-10 03:04:23 +00:00
closeTimer();
return super.close();
}
void closeTimer() {
if (timer != null && timer!.isActive) {
timer!.cancel();
}
}
2022-06-05 19:36:32 +00:00
void changePeriod(final Period period) async {
2021-04-10 03:04:23 +00:00
closeTimer();
emit(HetznerMetricsLoading(period));
load(period);
}
void restart() async {
load(state.period);
}
2022-06-05 19:36:32 +00:00
void load(final Period period) async {
final HetznerMetricsLoaded newState = await repository.getMetrics(period);
2021-04-10 03:04:23 +00:00
timer = Timer(
Duration(seconds: newState.stepInSeconds.toInt()),
() => load(newState.period),
);
emit(newState);
}
}