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 all 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 @@ -11,7 +11,6 @@ import '../../../ui/value_stream_builder.dart';
import '../../conversion_rates/services/token_fiat_balance_service.dart';
import '../../conversion_rates/widgets/extensions.dart';
import '../../currency/models/amount.dart';
import '../../currency/models/currency.dart';
import '../../tokens/widgets/token_icon.dart';

class PortfolioWidget extends StatefulWidget {
Expand Down Expand Up @@ -71,26 +70,28 @@ class PortfolioTile extends StatelessWidget {
),
),
const SizedBox(width: 8),
ValueStreamBuilder<Amount>(
ValueStreamBuilder<Amount?>(
create: () => (
sl<TokenFiatBalanceService>()
.watchTotalInvestmentsBalance(),
Amount.zero(currency: Currency.usd),
null,
),
builder: (context, balance) => Flexible(
child: FittedBox(
child: Text(
balance.format(DeviceLocale.localeOf(context)),
style: Theme.of(context)
.textTheme
.displayMedium
?.copyWith(
fontSize: 25,
fontWeight: FontWeight.w600,
builder: (context, balance) => balance != null
? Flexible(
child: FittedBox(
child: Text(
balance.format(DeviceLocale.localeOf(context)),
style: Theme.of(context)
.textTheme
.displayMedium
?.copyWith(
fontSize: 25,
fontWeight: FontWeight.w600,
),
),
),
),
),
),
)
: const SizedBox.shrink(),
),
],
),
Expand Down Expand Up @@ -137,18 +138,15 @@ class _TokenItem extends StatelessWidget {
});

final CryptoAmount cryptoAmount;
final FiatAmount fiatAmount;
final FiatAmount? fiatAmount;

static const double _iconSize = 36.0;
static const double _minFiatAmount = 0.01;

@override
Widget build(BuildContext context) {
String fiatAmountText;

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

return _Card(
child: ListTile(
Expand Down Expand Up @@ -214,3 +212,17 @@ class _Card extends StatelessWidget {
child: child,
);
}

extension TotalPortfolioTextExtension on BuildContext {
String portfilioTotalAmountText(FiatAmount? fiatAmount, num minFiatAmount) {
if (fiatAmount != null) {
if (fiatAmount.value < minFiatAmount) {
return r'<$0.01';
}

return fiatAmount.format(locale, maxDecimals: 2);
}

return '-';
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:collection/collection.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:injectable/injectable.dart';
import 'package:rxdart/rxdart.dart';
Expand All @@ -10,7 +9,7 @@ import '../../currency/models/currency.dart';
import '../../tokens/token.dart';
import '../data/repository.dart';

typedef CryptoFiatAmount = (CryptoAmount, FiatAmount);
typedef CryptoFiatAmount = (CryptoAmount, FiatAmount?);

@injectable
class TokenFiatBalanceService {
Expand Down Expand Up @@ -48,32 +47,48 @@ class TokenFiatBalanceService {
Stream<FiatAmount> watchMainBalance() =>
watch(Token.usdc).map((it) => it ?? _zeroFiat);

Stream<FiatAmount> watchTotalInvestmentsBalance() => _balancesRepository
Stream<FiatAmount?> watchTotalInvestmentsBalance() => _balancesRepository
.watchUserTokens(ignoreTokens: [Token.usdc])
.flatMap(
(tokens) => Rx.combineLatest(
tokens.map(watch),
(values) => values
.whereNotNull()
.fold(_zeroFiat, (total, next) => (total + next) as FiatAmount),
(values) => values.fold<FiatAmount?>(
null,
(total, next) {
if (next == null) return total;
if (total == null) return next;

return total + next as FiatAmount;
},
),
),
)
.distinct()
.doOnData(_logTotalCryptoBalance);
.doOnData((total) {
if (total != null) {
_logTotalCryptoBalance(total);
}
});

Stream<IList<CryptoFiatAmount>> watchInvestmentBalances() =>
_balancesRepository
.watchTokenBalances(ignoreTokens: [Token.usdc])
.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)).toList(),
),
)
.map((amount) => amount.toIList())
.map(
(amounts) =>
amounts.sort((a, b) => b.$2.value.compareTo(a.$2.value)),
(amounts) => amounts.sort(
(a, b) {
final aValue = a.$2?.value ?? 0;
final bValue = b.$2?.value ?? 0;

return bValue.compareTo(aValue);
},
),
)
.distinct();

Expand Down
Loading