Forráskód Böngészése

Show more accurate usage stats

Vishnu 4 éve
szülő
commit
8ba259e147

+ 3 - 7
lib/ui/settings/account_section_widget.dart

@@ -18,7 +18,7 @@ class AccountSectionWidget extends StatefulWidget {
 }
 
 class AccountSectionWidgetState extends State<AccountSectionWidget> {
-  double _usageInGBs;
+  String _usage;
 
   @override
   void initState() {
@@ -62,11 +62,7 @@ class AccountSectionWidgetState extends State<AccountSectionWidget> {
               Text("total data backed up"),
               Container(
                 height: 20,
-                child: _usageInGBs == null
-                    ? loadWidget
-                    : Text(
-                        _usageInGBs.toString() + " GB",
-                      ),
+                child: _usage == null ? loadWidget : Text(_usage),
               ),
             ],
           ),
@@ -130,7 +126,7 @@ class AccountSectionWidgetState extends State<AccountSectionWidget> {
     BillingService.instance.fetchUsage().then((usage) async {
       if (mounted) {
         setState(() {
-          _usageInGBs = convertBytesToGBs(usage);
+          _usage = formatBytes(usage);
         });
       }
     });

+ 1 - 2
lib/ui/subscription_page.dart

@@ -168,8 +168,7 @@ class _SubscriptionPageState extends State<SubscriptionPage> {
             if (snapshot.hasData) {
               return Padding(
                 padding: const EdgeInsets.all(16.0),
-                child: Text("current usage is " +
-                    convertBytesToReadableFormat(snapshot.data).toString()),
+                child: Text("current usage is " + formatBytes(snapshot.data)),
               );
             } else if (snapshot.hasError) {
               return Container();

+ 10 - 0
lib/utils/data_util.dart

@@ -1,3 +1,5 @@
+import 'dart:math';
+
 double convertBytesToGBs(final int bytes, {int precision = 2}) {
   return double.parse(
       (bytes / (1024 * 1024 * 1024)).toStringAsFixed(precision));
@@ -13,3 +15,11 @@ String convertBytesToReadableFormat(int bytes) {
   }
   return bytes.toString() + " " + kStorageUnits[storageUnitIndex];
 }
+
+String formatBytes(int bytes, [int decimals = 2]) {
+    if (bytes == 0) return '0 bytes';
+    const k = 1024;
+    int dm = decimals < 0 ? 0 : decimals;
+    int i = (log(bytes) / log(k)).floor();
+    return ((bytes / pow(k, i)).toStringAsFixed(dm)) + ' ' + kStorageUnits[i];
+}