2022-08-26 19:28:01 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
class DiskSize {
|
2022-10-26 16:26:09 +00:00
|
|
|
const DiskSize({this.byte = 0});
|
2022-08-24 05:35:49 +00:00
|
|
|
|
2022-09-06 11:03:21 +00:00
|
|
|
DiskSize.fromKibibyte(final double kibibyte)
|
|
|
|
: this(byte: (kibibyte * 1024).round());
|
|
|
|
DiskSize.fromMebibyte(final double mebibyte)
|
|
|
|
: this(byte: (mebibyte * 1024 * 1024).round());
|
|
|
|
DiskSize.fromGibibyte(final double gibibyte)
|
|
|
|
: this(byte: (gibibyte * 1024 * 1024 * 1024).round());
|
|
|
|
|
2022-09-06 10:27:18 +00:00
|
|
|
final int byte;
|
2022-08-26 19:28:01 +00:00
|
|
|
|
2022-09-06 09:17:44 +00:00
|
|
|
double get kibibyte => byte / 1024.0;
|
|
|
|
double get mebibyte => byte / 1024.0 / 1024.0;
|
|
|
|
double get gibibyte => byte / 1024.0 / 1024.0 / 1024.0;
|
2022-08-24 05:35:49 +00:00
|
|
|
|
2022-09-06 11:03:21 +00:00
|
|
|
DiskSize operator +(final DiskSize other) =>
|
|
|
|
DiskSize(byte: byte + other.byte);
|
|
|
|
DiskSize operator -(final DiskSize other) =>
|
|
|
|
DiskSize(byte: byte - other.byte);
|
|
|
|
DiskSize operator *(final double other) =>
|
|
|
|
DiskSize(byte: (byte * other).round());
|
|
|
|
|
2022-08-26 19:28:01 +00:00
|
|
|
@override
|
|
|
|
String toString() {
|
|
|
|
if (byte < 1024) {
|
2022-10-21 02:54:55 +00:00
|
|
|
return '${byte.toStringAsFixed(0)} ${tr('storage.bytes')}';
|
2022-08-26 19:28:01 +00:00
|
|
|
} else if (byte < 1024 * 1024) {
|
2022-10-03 23:32:35 +00:00
|
|
|
return 'storage.kb'.tr(args: [kibibyte.toStringAsFixed(1)]);
|
2022-08-26 19:28:01 +00:00
|
|
|
} else if (byte < 1024 * 1024 * 1024) {
|
2022-10-03 23:32:35 +00:00
|
|
|
return 'storage.mb'.tr(args: [mebibyte.toStringAsFixed(1)]);
|
2022-08-26 19:28:01 +00:00
|
|
|
} else {
|
2022-10-03 23:32:35 +00:00
|
|
|
return 'storage.gb'.tr(args: [gibibyte.toStringAsFixed(1)]);
|
2022-08-26 19:28:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-24 05:35:49 +00:00
|
|
|
}
|