Show more accurate usage stats

This commit is contained in:
Vishnu 2021-06-28 18:24:27 +05:30
parent 16abaf20fb
commit 8ba259e147
3 changed files with 14 additions and 9 deletions

View file

@ -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);
});
}
});

View file

@ -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();

View file

@ -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];
}