Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: portfolio amount value #1504

Merged
merged 6 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class PortfolioTile extends StatelessWidget {
create: () => (
sl<TokenFiatBalanceService>()
.watchTotalInvestmentsBalance(),
Amount.zero(currency: Currency.usd),
Amount.unknown(currency: Currency.usd),
),
builder: (context, balance) => Flexible(
child: FittedBox(
Expand Down Expand Up @@ -147,7 +147,9 @@ class _TokenItem extends StatelessWidget {
String fiatAmountText;

fiatAmountText = fiatAmount.value < _minFiatAmount
? r'<$0.01'
? fiatAmount.value < 0
? '-'
: r'<$0.01'
: fiatAmount.format(context.locale, maxDecimals: 2);

return _Card(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class TokenFiatBalanceService {
static const _zeroFiat =
FiatAmount(value: 0, fiatCurrency: defaultFiatCurrency);

static const _unknownAmount =
FiatAmount(value: -1, fiatCurrency: defaultFiatCurrency);

Stream<FiatAmount?> watch(Token token) {
const fiatCurrency = defaultFiatCurrency;
final conversionRate = _conversionRatesRepository.watchRate(
Expand All @@ -53,9 +56,10 @@ class TokenFiatBalanceService {
.flatMap(
(tokens) => Rx.combineLatest(
tokens.map(watch),
(values) => values
.whereNotNull()
.fold(_zeroFiat, (total, next) => (total + next) as FiatAmount),
(values) => values.whereNotNull().fold(
_unknownAmount,
(total, next) => (total + next) as FiatAmount,
),
),
)
.distinct()
Expand All @@ -67,7 +71,7 @@ class TokenFiatBalanceService {
.flatMap(
(cryptoAmounts) => Rx.combineLatest(
cryptoAmounts.map((c) => watch(c.token).map((fiat) => (c, fiat))),
(values) => values.map((e) => (e.$1, e.$2 ?? _zeroFiat)),
(values) => values.map((e) => (e.$1, e.$2 ?? _unknownAmount)),
),
)
.map((amount) => amount.toIList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ String formatAmount({
required bool roundInteger,
String? symbol,
}) {
if (value.toDouble() < 0) return ' ';

final minimumDigits = roundInteger && value.isInteger ? 0 : 2;

final formatter = NumberFormat.decimalPattern(locale.languageCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ sealed class Amount with _$Amount {
factory Amount.zero({required Currency currency}) =>
Amount(value: 0, currency: currency);

factory Amount.unknown({required Currency currency}) =>
Amount(value: -1, currency: currency);

factory Amount.fromToken({required int value, required Token token}) =>
Amount(value: value, currency: Currency.crypto(token: token));

Expand All @@ -51,6 +54,8 @@ sealed class Amount with _$Amount {

bool get isZero => decimal == Decimal.zero;

bool get isUnknown => value < 0;
Jaxiii marked this conversation as resolved.
Show resolved Hide resolved

Amount operator +(Amount other) {
_ensureSameCurrency(other);

Expand Down
Loading