Ver Fonte

Merge pull request #598 from ente-io/storage-card-bug-fix

Storage card bug fix
Neeraj Gupta há 2 anos atrás
pai
commit
ad9cc29b1e
2 ficheiros alterados com 17 adições e 10 exclusões
  1. 6 3
      lib/ui/settings/details_section_widget.dart
  2. 11 7
      lib/utils/data_util.dart

+ 6 - 3
lib/ui/settings/details_section_widget.dart

@@ -133,9 +133,12 @@ class _DetailsSectionWidgetState extends State<DetailsSectionWidget> {
     final freeSpaceInBytes = userDetails.getFreeStorage();
     final shouldShowFreeSpaceInMBs = freeSpaceInBytes < hundredMBinBytes;
 
-    final usedSpaceInGB =
-        convertBytesToGBs(userDetails.getFamilyOrPersonalUsage());
-    final totalStorageInGB = convertBytesToGBs(userDetails.getTotalStorage());
+    final usedSpaceInGB = roundBytesUsedToGBs(
+      userDetails.getFamilyOrPersonalUsage(),
+      userDetails.getFreeStorage(),
+    );
+    final totalStorageInGB =
+        convertBytesToGBs(userDetails.getTotalStorage()).truncate();
 
     return Padding(
       padding: EdgeInsets.fromLTRB(

+ 11 - 7
lib/utils/data_util.dart

@@ -19,18 +19,22 @@ String formatBytes(int bytes, [int decimals = 2]) {
   return ((bytes / pow(k, i)).toStringAsFixed(dm)) + ' ' + storageUnits[i];
 }
 
-//shows decimals only if less than 10GB & omits decimal if decimal is 0
-num convertBytesToGBs(int bytes) {
+//shows 1st decimal only if less than 10GB & omits decimal if decimal is 0
+num roundBytesUsedToGBs(int usedBytes, int freeSpace) {
   const tenGBinBytes = 10737418240;
-  int precision = 0;
-  if (bytes < tenGBinBytes) {
-    precision = 1;
+  num bytesInGB = convertBytesToGBs(usedBytes);
+  if ((usedBytes >= tenGBinBytes && freeSpace >= tenGBinBytes) ||
+      bytesInGB % 1 == 0) {
+    bytesInGB = bytesInGB.truncate();
   }
-  final bytesInGB =
-      num.parse((bytes / (pow(1024, 3))).toStringAsPrecision(precision));
   return bytesInGB;
 }
 
+//Eg: 0.3 GB, 11.0 GB, 532.3 GB
+num convertBytesToGBs(int bytes) {
+  return num.parse((bytes / (pow(1024, 3))).toStringAsFixed(1));
+}
+
 int convertBytesToMBs(int bytes) {
   return (bytes / pow(1024, 2)).round();
 }