2022-09-18 16:26:55 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-08-24 23:45:02 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2022-08-26 19:28:01 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
|
2022-08-24 23:45:02 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/api_factory_creator.dart';
|
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/server_providers/server_provider_factory.dart';
|
|
|
|
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';
|
2022-09-16 11:28:17 +00:00
|
|
|
import 'package:selfprivacy/logic/models/disk_status.dart';
|
2022-08-24 23:45:02 +00:00
|
|
|
|
|
|
|
part 'provider_volume_state.dart';
|
|
|
|
|
|
|
|
class ApiProviderVolumeCubit
|
|
|
|
extends ServerInstallationDependendCubit<ApiProviderVolumeState> {
|
|
|
|
ApiProviderVolumeCubit(final ServerInstallationCubit serverInstallationCubit)
|
2022-08-29 16:37:31 +00:00
|
|
|
: super(serverInstallationCubit, const ApiProviderVolumeState.initial());
|
2022-08-24 23:45:02 +00:00
|
|
|
|
|
|
|
VolumeProviderApiFactory? providerApi;
|
|
|
|
|
2022-09-18 22:11:26 +00:00
|
|
|
final ServerApi serverApi = ServerApi();
|
|
|
|
|
2022-08-24 23:45:02 +00:00
|
|
|
@override
|
|
|
|
Future<void> load() async {
|
|
|
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
2022-08-29 16:37:31 +00:00
|
|
|
final serverDetails = getIt<ApiConfigModel>().serverDetails;
|
|
|
|
providerApi = serverDetails == null
|
|
|
|
? null
|
|
|
|
: VolumeApiFactoryCreator.createVolumeProviderApiFactory(
|
|
|
|
getIt<ApiConfigModel>().serverDetails!.provider,
|
|
|
|
);
|
2022-08-24 23:45:02 +00:00
|
|
|
_refetch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<double?> getPricePerGb() async =>
|
|
|
|
providerApi!.getVolumeProvider().getPricePerGb();
|
|
|
|
|
|
|
|
Future<void> refresh() async {
|
2022-09-18 22:11:26 +00:00
|
|
|
emit(const ApiProviderVolumeState([], LoadingStatus.refreshing, false));
|
2022-08-24 23:45:02 +00:00
|
|
|
_refetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _refetch() async {
|
|
|
|
if (providerApi == null) {
|
2022-09-18 22:11:26 +00:00
|
|
|
return emit(const ApiProviderVolumeState([], LoadingStatus.error, false));
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final List<ServerVolume> volumes =
|
|
|
|
await providerApi!.getVolumeProvider().getVolumes();
|
|
|
|
|
|
|
|
if (volumes.isEmpty) {
|
2022-09-18 22:11:26 +00:00
|
|
|
return emit(const ApiProviderVolumeState([], LoadingStatus.error, false));
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-18 22:11:26 +00:00
|
|
|
emit(ApiProviderVolumeState(volumes, LoadingStatus.success, false));
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 02:34:25 +00:00
|
|
|
Future<void> attachVolume(final DiskVolume volume) async {
|
2022-08-24 23:45:02 +00:00
|
|
|
final ServerHostingDetails server = getIt<ApiConfigModel>().serverDetails!;
|
2022-08-26 02:34:25 +00:00
|
|
|
await providerApi!
|
|
|
|
.getVolumeProvider()
|
|
|
|
.attachVolume(volume.providerVolume!.id, server.id);
|
2022-08-24 23:45:02 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
2022-08-26 02:34:25 +00:00
|
|
|
Future<void> detachVolume(final DiskVolume volume) async {
|
|
|
|
await providerApi!
|
|
|
|
.getVolumeProvider()
|
|
|
|
.detachVolume(volume.providerVolume!.id);
|
2022-08-24 23:45:02 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> resizeVolume(
|
|
|
|
final DiskVolume volume,
|
|
|
|
final int newSizeGb,
|
2022-09-18 22:11:26 +00:00
|
|
|
final Function() callback,
|
2022-08-24 23:45:02 +00:00
|
|
|
) async {
|
2022-09-18 22:11:26 +00:00
|
|
|
getIt<NavigationService>().showSnackBar(
|
|
|
|
'Starting resize',
|
|
|
|
);
|
|
|
|
emit(state.copyWith(isResizing: true));
|
2022-08-24 23:45:02 +00:00
|
|
|
final bool resized = await providerApi!.getVolumeProvider().resizeVolume(
|
|
|
|
volume.providerVolume!.id,
|
|
|
|
newSizeGb,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!resized) {
|
2022-09-18 16:26:55 +00:00
|
|
|
getIt<NavigationService>().showSnackBar(
|
|
|
|
'providers.storage.extending_volume_error'.tr(),
|
|
|
|
);
|
2022-09-18 22:11:26 +00:00
|
|
|
emit(state.copyWith(isResizing: false));
|
2022-08-24 23:45:02 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-18 22:11:26 +00:00
|
|
|
getIt<NavigationService>().showSnackBar(
|
|
|
|
'Hetzner resized, waiting 10 seconds',
|
|
|
|
);
|
2022-09-18 16:35:16 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 10));
|
|
|
|
|
2022-08-24 23:45:02 +00:00
|
|
|
await ServerApi().resizeVolume(volume.name);
|
2022-09-18 22:11:26 +00:00
|
|
|
getIt<NavigationService>().showSnackBar(
|
|
|
|
'Server api resized, waiting 20 seconds',
|
|
|
|
);
|
|
|
|
|
|
|
|
await Future.delayed(const Duration(seconds: 20));
|
|
|
|
getIt<NavigationService>().showSnackBar(
|
|
|
|
'Restarting server',
|
|
|
|
);
|
|
|
|
|
|
|
|
await refresh();
|
|
|
|
emit(state.copyWith(isResizing: false));
|
|
|
|
await callback();
|
|
|
|
await serverApi.reboot();
|
2022-08-24 23:45:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> createVolume() async {
|
|
|
|
final ServerVolume? volume =
|
|
|
|
await providerApi!.getVolumeProvider().createVolume();
|
2022-08-26 02:34:25 +00:00
|
|
|
|
2022-09-06 10:27:18 +00:00
|
|
|
final diskVolume = DiskVolume(providerVolume: volume);
|
2022-08-26 02:34:25 +00:00
|
|
|
await attachVolume(diskVolume);
|
2022-08-24 23:45:02 +00:00
|
|
|
|
|
|
|
await Future.delayed(const Duration(seconds: 10));
|
|
|
|
|
2022-08-26 02:34:25 +00:00
|
|
|
await ServerApi().mountVolume(volume!.name);
|
2022-08-24 23:45:02 +00:00
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteVolume(final DiskVolume volume) async {
|
|
|
|
await providerApi!
|
|
|
|
.getVolumeProvider()
|
|
|
|
.deleteVolume(volume.providerVolume!.id);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() {
|
|
|
|
emit(const ApiProviderVolumeState.initial());
|
|
|
|
}
|
|
|
|
}
|