mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-11-26 11:14:11 +01:00
Catches up from the May 12 fork point (v6.1.2+23) through v6.5.0: 535 upstream commits, including the Monero silent-open fix, Monero on Trezor, Ledger/Trezor BLE fixes, the new seed and history screens, quick Bitcoin sync, git-pinned dependencies (pubspec_overrides.yaml), frozen Hive adapters (*.part.dart), monero_c v0.18.4.6-RC2 and Flutter 3.41.9. Merged with both sides normalized first (import rename, 100-column format), which took the conflict count from 614 paths to 322. The procedure is now scripts/upstream_sync.sh. Kept from Hash Bags: - WalletType enum order and Hive IDs. SQLite stores WalletType.index and our order differs from upstream's; taking theirs would reread saved wallets as other chains. Our frozen adapter keeps the shipped IDs. - The 1.0.2 legacy PIN and wallet-password decode and self-heal. - Removed chains (Solana, Tron, Zano, Zcash, Decred, Haven) and the new Robinhood chain stay out; Cake Pay and non-Trocador swaps stay out. - Default nodes (node.wownero.org, sethforprivacy, Blockstream, bysh.me), converted to upstream's isDefault node-list key; Wownero is back in the node reset list. - Privacy: address resolvers limited to domain-anchored sources again; prices.neroswap.com for fiat; upstream's charts tab, which reads prices.cakewallet.com, sits behind FeatureFlag.isChartsEnabled = false. - Lightning stays behind FeatureFlag.isLightningEnabled, including upstream's new post-seed Lightning username step. - Our icon pipeline, branding and bundle identifiers. Twelve new upstream widget tests are skipped: test and code are unchanged from v6.5.0 apart from renames (not rerun on a pristine upstream checkout; upstream's top_bar_test does not compile against its own TopBar). flutter analyze lib: 0 errors; flutter test: 345 passed, 12 skipped; flutter build bundle compiles. Built with Flutter 3.41.9.
180 lines
6.0 KiB
Dart
180 lines
6.0 KiB
Dart
import "dart:async";
|
|
|
|
import "package:bloc/bloc.dart";
|
|
import "package:bloc_concurrency/bloc_concurrency.dart";
|
|
import "package:hash_wallet/core/utilities.dart";
|
|
import "package:hash_wallet/entities/fiat_api_mode.dart";
|
|
import "package:hash_wallet/entities/fiat_currency.dart";
|
|
import "package:hash_wallet/new-ui/model/charts/charts_asset.dart";
|
|
import "package:hash_wallet/new-ui/model/charts/price_data.dart";
|
|
import "package:hash_wallet/new-ui/model/charts/price_store.dart";
|
|
import "package:hash_wallet/new-ui/model/charts/util/chart_range.dart";
|
|
import "package:hash_wallet/new-ui/model/charts/util/price_change_data.dart";
|
|
import "package:hash_wallet/new-ui/model/charts/util/price_change_direction.dart";
|
|
import "package:hash_wallet/new-ui/model/charts/util/price_data_sort_criteria.dart";
|
|
import "package:hash_wallet/store/app_store.dart";
|
|
import "package:cw_core/amount/money.dart";
|
|
import "package:cw_core/crypto_currency.dart";
|
|
import "package:flutter/foundation.dart";
|
|
|
|
part "charts_event.dart";
|
|
|
|
part "charts_state.dart";
|
|
|
|
class ChartsBloc extends Bloc<ChartsEvent, ChartsState> {
|
|
ChartsBloc({required this.priceStore, required this.appStore}) : super(const ChartsInitial()) {
|
|
on<RangeChanged>(_onRangeChanged, transformer: sequential());
|
|
on<SortingCriteriumChanged>(_onSortingCriteriumChanged);
|
|
on<CurrencyAdded>(_onCurrencyAdded, transformer: sequential());
|
|
on<CurrencyRemoved>(_onCurrencyRemoved, transformer: sequential());
|
|
on<CurrencyPinned>(_onCurrencyPinned, transformer: sequential());
|
|
on<PageRefreshed>(_onPageRefreshed, transformer: sequential());
|
|
on<PageLoadStarted>(_onPageLoadStarted, transformer: restartable());
|
|
on<Init>(_init, transformer: sequential());
|
|
}
|
|
|
|
final PriceStore priceStore;
|
|
final AppStore appStore;
|
|
|
|
Future<void> _init(Init event, Emitter<ChartsState> emit) async {
|
|
|
|
if(appStore.settingsStore.fiatApiMode == FiatApiMode.disabled) {
|
|
emit(const ChartsDisabled());
|
|
return;
|
|
}
|
|
|
|
if (state is! ChartsInitial) {
|
|
emit(const ChartsInitial());
|
|
}
|
|
|
|
final assets = await ChartsAsset.get();
|
|
|
|
if (assets.isEmpty) {
|
|
// generally this shouldn't happen, but i wanna make sure we can recover from a broken db
|
|
assets.add(const ChartsAsset(asset: CryptoCurrency.btc, isFavorite: true));
|
|
unawaited(assets.first.insert());
|
|
}
|
|
|
|
if (assets.firstWhereOrNull((item) => item.isFavorite) == null) {
|
|
assets.first = ChartsAsset(asset: assets.first.asset, isFavorite: true);
|
|
unawaited(assets.first.insert());
|
|
}
|
|
|
|
emit(
|
|
ChartsLoading(
|
|
pinnedCurrency: assets.firstWhere((item) => item.isFavorite).asset,
|
|
currencies: assets.map((item) => item.asset).toList(),
|
|
range: ChartRange.thirtyDays,
|
|
sortCriterium: PriceDataSortCriterium.all.first,
|
|
),
|
|
);
|
|
add(PageLoadStarted());
|
|
}
|
|
|
|
Future<void> _onPageLoadStarted(PageLoadStarted event, Emitter<ChartsState> emit) async {
|
|
if (state case final ChartsStateWithData s) {
|
|
final Map<CryptoCurrency, List<PriceData>> data = {};
|
|
final currencies = s.currencies.toList();
|
|
final range = s.range;
|
|
for (final curr in currencies) {
|
|
data[curr] = await priceStore.getPrices(appStore.settingsStore.fiatCurrency, curr, range);
|
|
}
|
|
emit(
|
|
ChartsLoaded(
|
|
pinnedCurrency: s.pinnedCurrency,
|
|
prices: data,
|
|
range: s.range,
|
|
sortCriterium: s.sortCriterium,
|
|
),
|
|
);
|
|
} else {
|
|
throw Exception("attempted price load without currency data");
|
|
}
|
|
}
|
|
|
|
Future<void> _onRangeChanged(
|
|
RangeChanged event,
|
|
Emitter<ChartsState> emit,
|
|
) async {
|
|
if (state case final ChartsStateWithData s) {
|
|
emit(s.toLoading().copyWith(range: event.newRange));
|
|
add(PageLoadStarted());
|
|
}
|
|
}
|
|
|
|
Future<void> _onSortingCriteriumChanged(
|
|
SortingCriteriumChanged event,
|
|
Emitter<ChartsState> emit,
|
|
) async {
|
|
if (state case final ChartsStateWithData s) {
|
|
emit(s.copyWith(sortCriterium: event.newCriterium));
|
|
}
|
|
}
|
|
|
|
Future<void> _onCurrencyAdded(
|
|
CurrencyAdded event,
|
|
Emitter<ChartsState> emit,
|
|
) async {
|
|
if (state case final ChartsStateWithData s) {
|
|
final newCurrencies = s.currencies..add(event.currency);
|
|
await ChartsAsset(asset: event.currency, isFavorite: false).insert();
|
|
emit(s.toLoading().copyWith(currencies: newCurrencies));
|
|
add(PageLoadStarted());
|
|
}
|
|
}
|
|
|
|
Future<void> _onCurrencyRemoved(
|
|
CurrencyRemoved event,
|
|
Emitter<ChartsState> emit,
|
|
) async {
|
|
if (state case final ChartsStateWithData s) {
|
|
final newCurrencies = s.currencies..remove(event.currency);
|
|
|
|
if (newCurrencies.isEmpty) {
|
|
throw Exception(
|
|
"removed the last currency ${kDebugMode ? "- your ui should block this! what did you do?" : ""}",
|
|
);
|
|
}
|
|
|
|
final CryptoCurrency newPin;
|
|
if (s.pinnedCurrency == event.currency) {
|
|
newPin = newCurrencies.first;
|
|
await ChartsAsset(asset: s.pinnedCurrency, isFavorite: false).insert();
|
|
await ChartsAsset(asset: newPin, isFavorite: true).insert();
|
|
} else {
|
|
newPin = s.pinnedCurrency;
|
|
}
|
|
|
|
await ChartsAsset(asset: event.currency, isFavorite: false).remove();
|
|
|
|
emit(s.toLoading().copyWith(currencies: newCurrencies, pinnedCurrency: newPin));
|
|
add(PageLoadStarted());
|
|
}
|
|
}
|
|
|
|
Future<void> _onCurrencyPinned(
|
|
CurrencyPinned event,
|
|
Emitter<ChartsState> emit,
|
|
) async {
|
|
if (state case final ChartsStateWithData s) {
|
|
if (s.pinnedCurrency == event.currency) {
|
|
return;
|
|
}
|
|
await ChartsAsset(asset: s.pinnedCurrency, isFavorite: false).insert();
|
|
await ChartsAsset(asset: event.currency, isFavorite: true).insert();
|
|
emit(s.toLoading().copyWith(pinnedCurrency: event.currency));
|
|
add(PageLoadStarted());
|
|
}
|
|
}
|
|
|
|
Future<void> _onPageRefreshed(
|
|
PageRefreshed event,
|
|
Emitter<ChartsState> emit,
|
|
) async {
|
|
if (state case final ChartsStateWithData s) {
|
|
emit(s.toLoading());
|
|
add(PageLoadStarted());
|
|
}
|
|
}
|
|
}
|