From 6f5ffa0f8033061bef8b16acfb88cca9640d371b Mon Sep 17 00:00:00 2001 From: Inex Code Date: Tue, 6 Sep 2022 13:27:18 +0300 Subject: [PATCH] Make DiskSize a constant constructor and fix slider on Volume resize screen --- .../provider_volume_cubit.dart | 3 +- lib/logic/models/disk_size.dart | 4 +- lib/logic/models/service.dart | 2 +- lib/ui/pages/server_storage/disk_status.dart | 81 ++++++++++++++----- .../server_storage/extending_volume.dart | 16 +++- 5 files changed, 76 insertions(+), 30 deletions(-) diff --git a/lib/logic/cubit/provider_volumes/provider_volume_cubit.dart b/lib/logic/cubit/provider_volumes/provider_volume_cubit.dart index 84d8aee1..a4df4469 100644 --- a/lib/logic/cubit/provider_volumes/provider_volume_cubit.dart +++ b/lib/logic/cubit/provider_volumes/provider_volume_cubit.dart @@ -89,8 +89,7 @@ class ApiProviderVolumeCubit final ServerVolume? volume = await providerApi!.getVolumeProvider().createVolume(); - final diskVolume = DiskVolume(); - diskVolume.providerVolume = volume; + final diskVolume = DiskVolume(providerVolume: volume); await attachVolume(diskVolume); await Future.delayed(const Duration(seconds: 10)); diff --git a/lib/logic/models/disk_size.dart b/lib/logic/models/disk_size.dart index 30d16455..60c2d8a1 100644 --- a/lib/logic/models/disk_size.dart +++ b/lib/logic/models/disk_size.dart @@ -1,9 +1,9 @@ import 'package:easy_localization/easy_localization.dart'; class DiskSize { - DiskSize({final this.byte = 0}); + const DiskSize({final this.byte = 0}); - int byte; + final int byte; double get kibibyte => byte / 1024.0; double get mebibyte => byte / 1024.0 / 1024.0; diff --git a/lib/logic/models/service.dart b/lib/logic/models/service.dart index 1085622a..33475588 100644 --- a/lib/logic/models/service.dart +++ b/lib/logic/models/service.dart @@ -51,7 +51,7 @@ class Service { isMovable: false, status: ServiceStatus.off, storageUsage: ServiceStorageUsage( - used: DiskSize(byte: 0), + used: const DiskSize(byte: 0), volume: '', ), svgIcon: '', diff --git a/lib/ui/pages/server_storage/disk_status.dart b/lib/ui/pages/server_storage/disk_status.dart index eae9596a..dbc01c16 100644 --- a/lib/ui/pages/server_storage/disk_status.dart +++ b/lib/ui/pages/server_storage/disk_status.dart @@ -3,21 +3,65 @@ import 'package:selfprivacy/logic/models/hive/server_details.dart'; import 'package:selfprivacy/logic/models/json/server_disk_volume.dart'; class DiskVolume { - DiskSize sizeUsed = DiskSize(); - DiskSize sizeTotal = DiskSize(); - String name = ''; - bool root = false; - bool isResizable = true; + DiskVolume({ + this.name = '', + this.sizeTotal = const DiskSize(byte: 0), + this.sizeUsed = const DiskSize(byte: 0), + this.root = false, + this.isResizable = false, + this.serverDiskVolume, + this.providerVolume, + }); + + DiskVolume.fromServerDiscVolume( + final ServerDiskVolume volume, + final ServerVolume? providerVolume, + ) : this( + name: volume.name, + sizeTotal: DiskSize( + byte: + volume.totalSpace == 'None' ? 0 : int.parse(volume.totalSpace), + ), + sizeUsed: DiskSize( + byte: volume.usedSpace == 'None' ? 0 : int.parse(volume.usedSpace), + ), + root: volume.root, + isResizable: providerVolume != null, + serverDiskVolume: volume, + providerVolume: providerVolume, + ); + + /// Get the display name of the volume + /// + /// If it is sda1 and root the name is "System disk" + /// If there is a mapping to providerVolume, the name is "Expandable volume" + /// Otherwise the name is the name of the volume + String get displayName { + if (root) { + return 'System disk'; + } else if (providerVolume != null) { + return 'Expandable volume'; + } else { + return name; + } + } + + DiskSize sizeUsed; + DiskSize sizeTotal; + String name; + bool root; + bool isResizable; ServerDiskVolume? serverDiskVolume; ServerVolume? providerVolume; /// from 0.0 to 1.0 - double get percentage => sizeTotal.byte == 0 ? 0 : sizeUsed.byte / sizeTotal.byte; - bool get isDiskOkay => percentage < 0.8 && sizeTotal.gibibyte - sizeUsed.gibibyte > 2.0; + double get percentage => + sizeTotal.byte == 0 ? 0 : sizeUsed.byte / sizeTotal.byte; + bool get isDiskOkay => + percentage < 0.8 && sizeTotal.gibibyte - sizeUsed.gibibyte > 2.0; } class DiskStatus { - DiskStatus.fromVolumes( final List serverVolumes, final List providerVolumes, @@ -25,32 +69,25 @@ class DiskStatus { diskVolumes = serverVolumes.map(( final ServerDiskVolume volume, ) { - final DiskVolume diskVolume = DiskVolume(); - diskVolume.sizeUsed = DiskSize( - byte: volume.usedSpace == 'None' ? 0 : int.parse(volume.usedSpace), - ); - diskVolume.sizeTotal = DiskSize( - byte: volume.totalSpace == 'None' ? 0 : int.parse(volume.totalSpace), - ); - diskVolume.serverDiskVolume = volume; + ServerVolume? providerVolume; - for (final ServerVolume providerVolume in providerVolumes) { - if (providerVolume.linuxDevice == null || + for (final ServerVolume iterableProviderVolume in providerVolumes) { + if (iterableProviderVolume.linuxDevice == null || volume.model == null || volume.serial == null) { continue; } - final String deviceId = providerVolume.linuxDevice!.split('/').last; + final String deviceId = iterableProviderVolume.linuxDevice!.split('/').last; if (deviceId.contains(volume.model!) && deviceId.contains(volume.serial!)) { - diskVolume.providerVolume = providerVolume; + providerVolume = iterableProviderVolume; break; } } - diskVolume.name = volume.name; - diskVolume.root = volume.root; + final DiskVolume diskVolume = DiskVolume.fromServerDiscVolume(volume, providerVolume); + return diskVolume; }).toList(); diff --git a/lib/ui/pages/server_storage/extending_volume.dart b/lib/ui/pages/server_storage/extending_volume.dart index 7023685d..e6ceb11e 100644 --- a/lib/ui/pages/server_storage/extending_volume.dart +++ b/lib/ui/pages/server_storage/extending_volume.dart @@ -22,13 +22,20 @@ class ExtendingVolumePage extends StatefulWidget { } class _ExtendingVolumePageState extends State { + @override + void initState() { + minSize = widget.diskVolumeToResize.sizeTotal; + _currentSliderGbValue = minSize.gibibyte; + super.initState(); + } + bool _isError = false; - double _currentSliderGbValue = -1; + late double _currentSliderGbValue; double _euroPerGb = 1.0; - final DiskSize maxSize = DiskSize(byte: 500000000000); - DiskSize minSize = DiskSize(); + final DiskSize maxSize = const DiskSize(byte: 500000000000); + late DiskSize minSize; final TextEditingController _sizeController = TextEditingController(); final TextEditingController _priceController = TextEditingController(); @@ -52,6 +59,9 @@ class _ExtendingVolumePageState extends State { 'providers.storage.extending_volume_description'.tr(), children: const [ SizedBox(height: 16), + Center( + child: CircularProgressIndicator(), + ), ], ); }