Show more accurate usage stats
This commit is contained in:
parent
16abaf20fb
commit
8ba259e147
3 changed files with 14 additions and 9 deletions
|
@ -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);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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];
|
||||
}
|
Loading…
Reference in a new issue