Skip to content

Commit

Permalink
feat: cache usdc balance (#1348)
Browse files Browse the repository at this point in the history
Co-authored-by: ookami-kb <[email protected]>
  • Loading branch information
justinenerio and ookami-kb authored Mar 30, 2024
1 parent 7c90034 commit 8b4948a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,8 @@ class _Buttons extends StatelessWidget {
Widget build(BuildContext context) => DecoratedBox(
decoration: const BoxDecoration(color: Color(0xff202020)),
child: ValueStreamBuilder<bool>(
create: () => (
sl<WatchUserFiatBalance>()
.call()
.$1
.map((event) => event ?? Amount.zero(currency: Currency.usd))
.map((event) => event.isZero),
true,
),
create: () =>
sl<WatchUserFiatBalance>().call().map((it) => it.isZero),
builder: (context, isZeroAmount) => Padding(
padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 18),
child: Column(
Expand Down Expand Up @@ -169,13 +163,7 @@ class _Amount extends StatelessWidget {

@override
Widget build(BuildContext context) => ValueStreamBuilder<Amount>(
create: () => (
sl<WatchUserFiatBalance>()
.call()
.$1
.map((event) => event ?? Amount.zero(currency: Currency.usd)),
Amount.zero(currency: Currency.usd),
),
create: () => sl<WatchUserFiatBalance>().call(),
builder: (context, amount) {
final formattedAmount = amount.format(
DeviceLocale.localeOf(context),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import 'dart:async';

import 'package:espressocash_common/espressocash_common.dart';
import 'package:flutter/material.dart';
import 'package:injectable/injectable.dart';
import 'package:rxdart/rxdart.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../authenticated/auth_scope.dart';

@Singleton(scope: authScope)
class BalancesRepository extends ChangeNotifier {
BalancesRepository(this._storage);

final SharedPreferences _storage;

@PostConstruct()
void init() {
final balance = _storage.getInt(_usdcBalanceKey);

if (balance == null) return;

_usdcBalance.add(
CryptoAmount(
value: balance,
cryptoCurrency: const CryptoCurrency(token: Token.usdc),
),
);
notifyListeners();
}

final _usdcBalance = BehaviorSubject<CryptoAmount>.seeded(
const CryptoAmount(
value: 0,
Expand All @@ -20,6 +42,7 @@ class BalancesRepository extends ChangeNotifier {
}

_usdcBalance.add(balance);
_storage.setInt(_usdcBalanceKey, balance.value);
notifyListeners();
}

Expand All @@ -28,8 +51,12 @@ class BalancesRepository extends ChangeNotifier {
(Stream<CryptoAmount>, CryptoAmount) watch() => (_usdcBalance.stream, read());

@override
@disposeMethod
void dispose() {
_usdcBalance.close();
_storage.remove(_usdcBalanceKey);
super.dispose();
}
}

const _usdcBalanceKey = 'usdcBalance';
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,38 @@ import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/material.dart';
import 'package:injectable/injectable.dart';
import 'package:rxdart/rxdart.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../authenticated/auth_scope.dart';
import 'conversion_rates_client.dart';

@lazySingleton
@Singleton(scope: authScope)
class ConversionRatesRepository extends ChangeNotifier {
ConversionRatesRepository({
required SharedPreferences storage,
required ConversionRatesClient coingeckoClient,
}) : _coingeckoClient = coingeckoClient;
}) : _coingeckoClient = coingeckoClient,
_storage = storage;

final BehaviorSubject<IMap<FiatCurrency, Decimal>> _value =
BehaviorSubject.seeded(const IMapConst({}));

final ConversionRatesClient _coingeckoClient;
final SharedPreferences _storage;

@PostConstruct()
void init() {
final rate = _storage.getDouble(_usdcRateKey);

if (rate == null) return;

_value.add(
IMapConst({
Currency.usd: Decimal.tryParse(rate.toString()) ?? Decimal.zero,
}),
);
notifyListeners();
}

Decimal? readRate({required FiatCurrency to}) => _value.value[to];

Expand Down Expand Up @@ -48,6 +67,18 @@ class ConversionRatesRepository extends ChangeNotifier {
);

_value.add(value);
await _storage.setDouble(_usdcRateKey, data.usd ?? 0);

notifyListeners();
});

@override
@disposeMethod
void dispose() {
_value.close();
_storage.remove(_usdcRateKey);
super.dispose();
}
}

const _usdcRateKey = 'usdcRate';
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class WatchUserFiatBalance {
final ConversionRatesRepository _conversionRatesRepository;
final BalancesRepository _balancesRepository;

(Stream<FiatAmount?>, FiatAmount?) call() {
(Stream<FiatAmount>, FiatAmount) call() {
const fiatCurrency = defaultFiatCurrency;
final conversionRate =
_conversionRatesRepository.watchRate(to: fiatCurrency);
Expand All @@ -28,17 +28,15 @@ class WatchUserFiatBalance {
Rx.combineLatest2(
balance.$1,
conversionRate,
(cryptoAmount, rate) {
if (rate == null) return null;

return cryptoAmount.convert(rate: rate, to: fiatCurrency)
as FiatAmount;
},
(cryptoAmount, rate) => rate == null
? const FiatAmount(value: 0, fiatCurrency: Currency.usd)
: cryptoAmount.convert(rate: rate, to: fiatCurrency) as FiatAmount,
).distinct(),
_conversionRatesRepository.readRate(to: fiatCurrency)?.let(
(rate) =>
balance.$2.convert(rate: rate, to: fiatCurrency) as FiatAmount,
),
(rate) => balance.$2.convert(rate: rate, to: fiatCurrency)
as FiatAmount,
) ??
const FiatAmount(value: 0, fiatCurrency: Currency.usd),
);
}
}
4 changes: 4 additions & 0 deletions packages/espressocash_app/lib/ui/value_stream_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import 'package:flutter/widgets.dart';
typedef ValueWidgetBuilder<T> = Widget Function(BuildContext context, T value);
typedef ValueStreamFactory<T> = (Stream<T>, T) Function();

extension ValueExt<T> on (Stream<T>, T) {
(Stream<U>, U) map<U>(U Function(T value) fn) => ($1.map<U>(fn), fn($2));
}

class ValueStreamBuilder<T> extends StatefulWidget {
const ValueStreamBuilder({
super.key,
Expand Down

0 comments on commit 8b4948a

Please sign in to comment.