2023-04-12 05:42:33 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
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-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-11-04 12:18:35 +00:00
|
|
|
import 'package:selfprivacy/logic/models/disk_size.dart';
|
2022-08-24 23:45:02 +00:00
|
|
|
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-10-12 01:42:45 +00:00
|
|
|
import 'package:selfprivacy/logic/models/price.dart';
|
2023-04-24 15:09:23 +00:00
|
|
|
import 'package:selfprivacy/logic/providers/providers_controller.dart';
|
2022-08-24 23:45:02 +00:00
|
|
|
|
|
|
|
part 'provider_volume_state.dart';
|
|
|
|
|
|
|
|
class ApiProviderVolumeCubit
|
2024-01-26 14:46:09 +00:00
|
|
|
extends ServerConnectionDependentCubit<ApiProviderVolumeState> {
|
2022-08-24 23:45:02 +00:00
|
|
|
ApiProviderVolumeCubit(final ServerInstallationCubit serverInstallationCubit)
|
2024-01-26 14:46:09 +00:00
|
|
|
: super(const ApiProviderVolumeState.initial());
|
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 {
|
2024-01-26 14:46:09 +00:00
|
|
|
unawaited(_refetch());
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 10:51:47 +00:00
|
|
|
Future<Price?> getPricePerGb() async {
|
|
|
|
Price? price;
|
|
|
|
final pricingResult =
|
|
|
|
await ProvidersController.currentServerProvider!.getAdditionalPricing();
|
|
|
|
if (pricingResult.data == null || !pricingResult.success) {
|
|
|
|
getIt<NavigationService>().showSnackBar('server.pricing_error'.tr());
|
|
|
|
return price;
|
|
|
|
}
|
|
|
|
price = pricingResult.data!.perVolumeGb;
|
|
|
|
return price;
|
|
|
|
}
|
2022-08-24 23:45:02 +00:00
|
|
|
|
|
|
|
Future<void> refresh() async {
|
2022-09-18 22:11:26 +00:00
|
|
|
emit(const ApiProviderVolumeState([], LoadingStatus.refreshing, false));
|
2023-04-12 05:42:33 +00:00
|
|
|
unawaited(_refetch());
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _refetch() async {
|
2023-04-24 16:45:16 +00:00
|
|
|
if (ProvidersController.currentServerProvider == null) {
|
2022-09-18 22:11:26 +00:00
|
|
|
return emit(const ApiProviderVolumeState([], LoadingStatus.error, false));
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 15:09:23 +00:00
|
|
|
final volumesResult =
|
|
|
|
await ProvidersController.currentServerProvider!.getVolumes();
|
2022-08-24 23:45:02 +00:00
|
|
|
|
2023-04-24 15:09:23 +00:00
|
|
|
if (!volumesResult.success || volumesResult.data.isEmpty) {
|
2022-09-18 22:11:26 +00:00
|
|
|
return emit(const ApiProviderVolumeState([], LoadingStatus.error, false));
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
2023-04-24 15:09:23 +00:00
|
|
|
emit(
|
|
|
|
ApiProviderVolumeState(
|
|
|
|
volumesResult.data,
|
|
|
|
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!;
|
2023-04-24 15:09:23 +00:00
|
|
|
await ProvidersController.currentServerProvider!
|
2022-10-17 23:58:29 +00:00
|
|
|
.attachVolume(volume.providerVolume!, server.id);
|
2023-04-12 05:42:33 +00:00
|
|
|
unawaited(refresh());
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-26 02:34:25 +00:00
|
|
|
Future<void> detachVolume(final DiskVolume volume) async {
|
2023-04-24 15:09:23 +00:00
|
|
|
await ProvidersController.currentServerProvider!
|
2022-11-18 03:07:42 +00:00
|
|
|
.detachVolume(volume.providerVolume!);
|
2023-04-12 05:42:33 +00:00
|
|
|
unawaited(refresh());
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> resizeVolume(
|
|
|
|
final DiskVolume volume,
|
2022-11-04 12:18:35 +00:00
|
|
|
final DiskSize newSize,
|
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));
|
2023-04-24 15:09:23 +00:00
|
|
|
final resizedResult =
|
|
|
|
await ProvidersController.currentServerProvider!.resizeVolume(
|
|
|
|
volume.providerVolume!,
|
|
|
|
newSize,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!resizedResult.success || !resizedResult.data) {
|
2022-09-18 16:26:55 +00:00
|
|
|
getIt<NavigationService>().showSnackBar(
|
2022-10-03 23:32:35 +00:00
|
|
|
'storage.extending_volume_error'.tr(),
|
2022-09-18 16:26:55 +00:00
|
|
|
);
|
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(
|
2022-11-12 17:29:06 +00:00
|
|
|
'Provider volume resized, waiting 10 seconds',
|
2022-09-18 22:11:26 +00:00
|
|
|
);
|
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(
|
2022-11-12 17:29:06 +00:00
|
|
|
'Server volume resized, waiting 20 seconds',
|
2022-09-18 22:11:26 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-08-06 23:28:02 +00:00
|
|
|
Future<void> createVolume(final DiskSize size) async {
|
|
|
|
final ServerVolume? volume = (await ProvidersController
|
|
|
|
.currentServerProvider!
|
|
|
|
.createVolume(size.gibibyte.toInt()))
|
|
|
|
.data;
|
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);
|
2023-04-12 05:42:33 +00:00
|
|
|
unawaited(refresh());
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteVolume(final DiskVolume volume) async {
|
2023-04-24 15:09:23 +00:00
|
|
|
await ProvidersController.currentServerProvider!
|
2022-11-18 03:07:42 +00:00
|
|
|
.deleteVolume(volume.providerVolume!);
|
2023-04-12 05:42:33 +00:00
|
|
|
unawaited(refresh());
|
2022-08-24 23:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void clear() {
|
|
|
|
emit(const ApiProviderVolumeState.initial());
|
|
|
|
}
|
|
|
|
}
|