-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathbitcoin_price_state.dart
More file actions
60 lines (53 loc) · 1.71 KB
/
Copy pathbitcoin_price_state.dart
File metadata and controls
60 lines (53 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
part of 'bitcoin_price_bloc.dart';
@freezed
sealed class BitcoinPriceState with _$BitcoinPriceState {
const factory BitcoinPriceState({
@Default(false) bool loadingPrice,
Object? error,
@Default(false) bool startupFailed,
//
List<String>? availableCurrencies,
String? currency,
double? bitcoinPrice,
}) = _BitcoinPriceState;
const BitcoinPriceState._();
String? calculateFiatPrice(int satAmount) {
if (bitcoinPrice == null) return null;
final p = bitcoinPrice! * ConvertAmount.satsToBtc(satAmount);
if (currency == null) return null;
return '${_fiatFormatting(p.toStringAsFixed(2))} ${currency!}';
}
String? displayBTCAmount(int satsAmount, BitcoinUnit unit) {
if (unit == BitcoinUnit.btc) {
final btcAmount = ConvertAmount.satsToBtc(satsAmount).toStringAsFixed(8);
return '${_removeTrailingBTCZeros(btcAmount)} BTC';
} else {
return '${_displaySatsAmount(satsAmount)} sats';
}
}
String _displaySatsAmount(int satsAmount) {
final currency = NumberFormat('#,##0', 'en_US');
return currency.format(satsAmount);
}
String _fiatFormatting(String fiatAmount) {
final currency = NumberFormat('#,##0.00', 'en_US');
return _removeTrailingFiatZeros(currency.format(double.parse(fiatAmount)));
}
String _removeTrailingFiatZeros(String value) {
if (value.endsWith('.00')) {
return value.replaceAll('.00', '');
}
return value;
}
String _removeTrailingBTCZeros(String value) {
if (value.endsWith('.00')) {
return value.replaceAll('.00', '');
}
if (value.contains('.')) {
return value
.replaceAll(RegExp(r'0*$'), '')
.replaceAll(RegExp(r'\.$'), '');
}
return value;
}
}