2022-09-16 11:28:17 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2022-11-16 00:24:40 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server_api.dart';
|
2022-08-24 23:45:02 +00:00
|
|
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
2024-01-26 14:46:09 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/server_connection_dependent/server_connection_dependent_cubit.dart';
|
2022-09-16 11:28:17 +00:00
|
|
|
import 'package:selfprivacy/logic/cubit/provider_volumes/provider_volume_cubit.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/disk_status.dart';
|
2022-08-24 23:45:02 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/server_disk_volume.dart';
|
|
|
|
|
|
|
|
part 'server_volume_state.dart';
|
|
|
|
|
|
|
|
class ApiServerVolumeCubit
|
2024-01-26 14:46:09 +00:00
|
|
|
extends ServerConnectionDependentCubit<ApiServerVolumeState> {
|
2022-09-16 11:28:17 +00:00
|
|
|
ApiServerVolumeCubit(
|
2022-09-16 13:36:51 +00:00
|
|
|
final ServerInstallationCubit serverInstallationCubit,
|
|
|
|
this.providerVolumeCubit,
|
2024-01-26 14:46:09 +00:00
|
|
|
) : super(ApiServerVolumeState.initial()) {
|
|
|
|
// TODO: Remove this connection between cubits
|
2022-09-16 11:28:17 +00:00
|
|
|
_providerVolumeSubscription =
|
|
|
|
providerVolumeCubit.stream.listen(checkProviderVolumes);
|
|
|
|
}
|
2022-08-24 23:45:02 +00:00
|
|
|
|
|
|
|
final ServerApi serverApi = ServerApi();
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> load() async {
|
2024-01-26 14:46:09 +00:00
|
|
|
unawaited(reload());
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 11:28:17 +00:00
|
|
|
late StreamSubscription<ApiProviderVolumeState> _providerVolumeSubscription;
|
|
|
|
final ApiProviderVolumeCubit providerVolumeCubit;
|
|
|
|
|
|
|
|
void checkProviderVolumes(final ApiProviderVolumeState state) {
|
|
|
|
emit(
|
|
|
|
ApiServerVolumeState(
|
|
|
|
this.state._volumes,
|
|
|
|
this.state.status,
|
|
|
|
DiskStatus.fromVolumes(this.state._volumes, state.volumes),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-18 22:11:26 +00:00
|
|
|
Future<void> reload() async {
|
2022-09-02 05:59:46 +00:00
|
|
|
final volumes = await serverApi.getServerDiskVolumes();
|
|
|
|
var status = LoadingStatus.error;
|
|
|
|
|
2022-08-24 23:45:02 +00:00
|
|
|
if (volumes.isNotEmpty) {
|
2022-09-02 05:59:46 +00:00
|
|
|
status = LoadingStatus.success;
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
2022-09-02 05:59:46 +00:00
|
|
|
|
2022-09-16 11:28:17 +00:00
|
|
|
emit(
|
|
|
|
ApiServerVolumeState(
|
|
|
|
volumes,
|
|
|
|
status,
|
|
|
|
DiskStatus.fromVolumes(
|
|
|
|
volumes,
|
|
|
|
providerVolumeCubit.state.volumes,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() {
|
2022-09-16 11:28:17 +00:00
|
|
|
emit(ApiServerVolumeState.initial());
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> close() {
|
|
|
|
_providerVolumeSubscription.cancel();
|
|
|
|
return super.close();
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
}
|