Refactor disk_size.dart and disk_status.dart to use getters

This commit is contained in:
Inex Code 2022-09-06 12:17:44 +03:00
parent 979e8ee37a
commit 58479256c5
3 changed files with 18 additions and 30 deletions

View file

@ -5,20 +5,20 @@ class DiskSize {
int byte;
double asKb() => byte / 1024.0;
double asMb() => byte / 1024.0 / 1024.0;
double asGb() => byte / 1024.0 / 1024.0 / 1024.0;
double get kibibyte => byte / 1024.0;
double get mebibyte => byte / 1024.0 / 1024.0;
double get gibibyte => byte / 1024.0 / 1024.0 / 1024.0;
@override
String toString() {
if (byte < 1024) {
return '${byte.toStringAsFixed(0)} ${tr('bytes')}';
} else if (byte < 1024 * 1024) {
return 'providers.storage.kb'.tr(args: [asKb().toStringAsFixed(1)]);
return 'providers.storage.kb'.tr(args: [kibibyte.toStringAsFixed(1)]);
} else if (byte < 1024 * 1024 * 1024) {
return 'providers.storage.mb'.tr(args: [asMb().toStringAsFixed(1)]);
return 'providers.storage.mb'.tr(args: [mebibyte.toStringAsFixed(1)]);
} else {
return 'providers.storage.gb'.tr(args: [asGb().toStringAsFixed(1)]);
return 'providers.storage.gb'.tr(args: [gibibyte.toStringAsFixed(1)]);
}
}
}

View file

@ -12,25 +12,16 @@ class DiskVolume {
ServerVolume? providerVolume;
/// from 0.0 to 1.0
double percentage = 0.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() {
isDiskOkay = false;
diskVolumes = [];
}
DiskStatus.fromVolumes(
final List<ServerDiskVolume> serverVolumes,
final List<ServerVolume> providerVolumes,
) {
isDiskOkay = true;
if (providerVolumes.isEmpty || serverVolumes.isEmpty) {
isDiskOkay = false;
}
diskVolumes = serverVolumes.map((
final ServerDiskVolume volume,
) {
@ -60,18 +51,15 @@ class DiskStatus {
diskVolume.name = volume.name;
diskVolume.root = volume.root;
diskVolume.percentage =
volume.usedSpace != 'None' && volume.totalSpace != 'None'
? 1.0 / diskVolume.sizeTotal.byte * diskVolume.sizeUsed.byte
: 0.0;
if (diskVolume.percentage >= 0.8 ||
diskVolume.sizeTotal.asGb() - diskVolume.sizeUsed.asGb() <= 2.0) {
isDiskOkay = false;
}
return diskVolume;
}).toList();
}
DiskStatus() {
diskVolumes = [];
}
bool get isDiskOkay => diskVolumes.every((final volume) => volume.isDiskOkay);
bool isDiskOkay = false;
List<DiskVolume> diskVolumes = [];
}

View file

@ -34,7 +34,7 @@ class _ExtendingVolumePageState extends State<ExtendingVolumePage> {
final TextEditingController _priceController = TextEditingController();
void _updateErrorStatuses() {
_isError = minSize.asGb() > _currentSliderGbValue;
_isError = minSize.gibibyte > _currentSliderGbValue;
}
@override
@ -62,7 +62,7 @@ class _ExtendingVolumePageState extends State<ExtendingVolumePage> {
.toStringAsFixed(2);
minSize = widget.diskVolumeToResize.sizeTotal;
if (_currentSliderGbValue < 0) {
_currentSliderGbValue = minSize.asGb();
_currentSliderGbValue = minSize.gibibyte;
}
return BrandHeroScreen(
@ -111,9 +111,9 @@ class _ExtendingVolumePageState extends State<ExtendingVolumePage> {
),
const SizedBox(height: 16),
Slider(
min: minSize.asGb(),
min: minSize.gibibyte,
value: _currentSliderGbValue,
max: maxSize.asGb(),
max: maxSize.gibibyte,
onChanged: (final double value) {
setState(() {
_currentSliderGbValue = value;