2022-06-27 07:07:11 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2022-07-12 12:54:16 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/api_factory_creator.dart';
|
2022-07-14 13:34:08 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_provider_factory.dart';
|
2022-06-27 07:07:11 +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/hive/server_details.dart';
|
|
|
|
|
|
|
|
part 'volumes_state.dart';
|
|
|
|
|
|
|
|
class ApiVolumesCubit
|
|
|
|
extends ServerInstallationDependendCubit<ApiVolumesState> {
|
|
|
|
ApiVolumesCubit(final ServerInstallationCubit serverInstallationCubit)
|
|
|
|
: super(serverInstallationCubit, const ApiVolumesState.initial());
|
|
|
|
|
2022-07-12 12:54:16 +00:00
|
|
|
final VolumeProviderApiFactory providerApi =
|
|
|
|
VolumeApiFactoryCreator.createVolumeProviderApiFactory(
|
|
|
|
getIt<ApiConfigModel>().serverDetails!.provider,
|
|
|
|
);
|
2022-06-27 07:07:11 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void load() async {
|
|
|
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
|
|
|
_refetch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void refresh() async {
|
|
|
|
emit(const ApiVolumesState([], LoadingStatus.refreshing));
|
|
|
|
_refetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _refetch() async {
|
2022-07-12 12:54:16 +00:00
|
|
|
final List<ServerVolume> volumes =
|
|
|
|
await providerApi.getVolumeProvider().getVolumes();
|
2022-06-27 07:07:11 +00:00
|
|
|
if (volumes.isNotEmpty) {
|
|
|
|
emit(ApiVolumesState(volumes, LoadingStatus.success));
|
|
|
|
} else {
|
|
|
|
emit(const ApiVolumesState([], LoadingStatus.error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void attachVolume(final ServerVolume volume) async {
|
|
|
|
final ServerHostingDetails server = getIt<ApiConfigModel>().serverDetails!;
|
2022-07-12 12:54:16 +00:00
|
|
|
await providerApi.getVolumeProvider().attachVolume(volume.id, server.id);
|
2022-06-27 07:07:11 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void detachVolume(final ServerVolume volume) async {
|
2022-07-12 12:54:16 +00:00
|
|
|
await providerApi.getVolumeProvider().detachVolume(volume.id);
|
2022-06-27 07:07:11 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2022-07-12 12:54:16 +00:00
|
|
|
void resizeVolume(final ServerVolume volume, final int newSizeGb) async {
|
|
|
|
//if (volume.sizeByte < newSizeGb) {
|
|
|
|
await providerApi.getVolumeProvider().resizeVolume(volume.id, newSizeGb);
|
|
|
|
refresh();
|
|
|
|
//}
|
2022-06-27 07:07:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void createVolume() async {
|
2022-07-12 12:54:16 +00:00
|
|
|
await providerApi.getVolumeProvider().createVolume();
|
2022-06-27 07:07:11 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
void deleteVolume(final ServerVolume volume) async {
|
2022-07-12 12:54:16 +00:00
|
|
|
await providerApi.getVolumeProvider().deleteVolume(volume.id);
|
2022-06-27 07:07:11 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() {
|
|
|
|
emit(const ApiVolumesState.initial());
|
|
|
|
}
|
|
|
|
}
|