diff --git a/lib/receive/bloc/receive_cubit.dart b/lib/receive/bloc/receive_cubit.dart index b1994ab2..2c1c6ce3 100644 --- a/lib/receive/bloc/receive_cubit.dart +++ b/lib/receive/bloc/receive_cubit.dart @@ -12,28 +12,33 @@ import 'package:flutter_bloc/flutter_bloc.dart'; class ReceiveCubit extends Cubit { ReceiveCubit({ - required this.walletBloc, + WalletBloc? walletBloc, required this.hiveStorage, required this.walletAddress, required this.walletRepository, required this.settingsCubit, required this.networkCubit, required this.currencyCubit, - }) : super(const ReceiveState()) { + }) : super(ReceiveState(walletBloc: walletBloc)) { loadAddress(); } final SettingsCubit settingsCubit; - final WalletBloc walletBloc; + // final WalletBloc walletBloc; final WalletAddress walletAddress; final HiveStorage hiveStorage; final WalletRepository walletRepository; final NetworkCubit networkCubit; final CurrencyCubit currencyCubit; + void updateWalletBloc(WalletBloc walletBloc) { + emit(state.copyWith(walletBloc: walletBloc)); + } + void loadAddress() async { emit(state.copyWith(loadingAddress: true, errLoadingAddress: '')); + if (state.walletBloc == null) return; - final address = walletBloc.state.wallet!.lastGeneratedAddress; + final address = state.walletBloc!.state.wallet!.lastGeneratedAddress; emit( state.copyWith( @@ -42,7 +47,7 @@ class ReceiveCubit extends Cubit { ); final label = await walletAddress.getLabel( address: address!.address, - wallet: walletBloc.state.wallet!, + wallet: state.walletBloc!.state.wallet!, ); final labelUpdated = address.copyWith(label: label); @@ -64,13 +69,16 @@ class ReceiveCubit extends Cubit { ), ); currencyCubit.updateAmountDirect(0); - if (walletBloc.state.bdkWallet == null) { + + if (state.walletBloc == null) return; + + if (state.walletBloc!.state.bdkWallet == null) { emit(state.copyWith(errLoadingAddress: 'Wallet Sync Required')); return; } final (updatedWallet, err) = await walletAddress.newAddress( - wallet: walletBloc.state.wallet!, - bdkWallet: walletBloc.state.bdkWallet!, + wallet: state.walletBloc!.state.wallet!, + bdkWallet: state.walletBloc!.state.bdkWallet!, ); if (err != null) { emit( @@ -81,7 +89,7 @@ class ReceiveCubit extends Cubit { return; } - walletBloc.add(UpdateWallet(updatedWallet!, updateTypes: [UpdateWalletTypes.addresses])); + state.walletBloc!.add(UpdateWallet(updatedWallet!, updateTypes: [UpdateWalletTypes.addresses])); final addressGap = updatedWallet.addressGap(); if (addressGap >= 5 && addressGap <= 20) { @@ -127,20 +135,22 @@ class ReceiveCubit extends Cubit { } void saveDefaultAddressLabel() async { + if (state.walletBloc == null) return; + if (state.privateLabel == (state.defaultAddress?.label ?? '')) return; emit(state.copyWith(savingLabel: true, errSavingLabel: '')); final (a, w) = await walletAddress.addAddressToWallet( address: (state.defaultAddress!.index, state.defaultAddress!.address), - wallet: walletBloc.state.wallet!, + wallet: state.walletBloc!.state.wallet!, label: state.privateLabel, kind: state.defaultAddress!.kind, state: state.defaultAddress!.state, spendable: state.defaultAddress!.spendable, ); - walletBloc.add(UpdateWallet(w, updateTypes: [UpdateWalletTypes.addresses])); + state.walletBloc!.add(UpdateWallet(w, updateTypes: [UpdateWalletTypes.addresses])); emit( state.copyWith( @@ -165,6 +175,8 @@ class ReceiveCubit extends Cubit { } void saveFinalInvoiceClicked() async { + if (state.walletBloc == null) return; + if (currencyCubit.state.amount <= 0) { emit(state.copyWith(errCreatingInvoice: 'Enter correct amount')); return; @@ -174,12 +186,12 @@ class ReceiveCubit extends Cubit { final (_, w) = await walletAddress.addAddressToWallet( address: (state.defaultAddress!.index, state.defaultAddress!.address), - wallet: walletBloc.state.wallet!, + wallet: state.walletBloc!.state.wallet!, label: state.description, kind: AddressKind.deposit, ); - walletBloc.add(UpdateWallet(w, updateTypes: [UpdateWalletTypes.addresses])); + state.walletBloc!.add(UpdateWallet(w, updateTypes: [UpdateWalletTypes.addresses])); emit( state.copyWith( diff --git a/lib/receive/bloc/state.dart b/lib/receive/bloc/state.dart index 8d176020..e3d9a41f 100644 --- a/lib/receive/bloc/state.dart +++ b/lib/receive/bloc/state.dart @@ -1,4 +1,5 @@ import 'package:bb_mobile/_model/address.dart'; +import 'package:bb_mobile/wallet/bloc/wallet_bloc.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; part 'state.freezed.dart'; @@ -18,6 +19,7 @@ class ReceiveState with _$ReceiveState { @Default('') String savedDescription, @Default(true) bool creatingInvoice, @Default('') String errCreatingInvoice, + WalletBloc? walletBloc, // Address? newInvoiceAddress, }) = _ReceiveState; const ReceiveState._(); diff --git a/lib/receive/bloc/state.freezed.dart b/lib/receive/bloc/state.freezed.dart index 29673f16..e23d7cec 100644 --- a/lib/receive/bloc/state.freezed.dart +++ b/lib/receive/bloc/state.freezed.dart @@ -28,6 +28,7 @@ mixin _$ReceiveState { String get savedDescription => throw _privateConstructorUsedError; bool get creatingInvoice => throw _privateConstructorUsedError; String get errCreatingInvoice => throw _privateConstructorUsedError; + WalletBloc? get walletBloc => throw _privateConstructorUsedError; @JsonKey(ignore: true) $ReceiveStateCopyWith get copyWith => @@ -52,7 +53,8 @@ abstract class $ReceiveStateCopyWith<$Res> { String description, String savedDescription, bool creatingInvoice, - String errCreatingInvoice}); + String errCreatingInvoice, + WalletBloc? walletBloc}); $AddressCopyWith<$Res>? get defaultAddress; } @@ -82,6 +84,7 @@ class _$ReceiveStateCopyWithImpl<$Res, $Val extends ReceiveState> Object? savedDescription = null, Object? creatingInvoice = null, Object? errCreatingInvoice = null, + Object? walletBloc = freezed, }) { return _then(_value.copyWith( loadingAddress: null == loadingAddress @@ -132,6 +135,10 @@ class _$ReceiveStateCopyWithImpl<$Res, $Val extends ReceiveState> ? _value.errCreatingInvoice : errCreatingInvoice // ignore: cast_nullable_to_non_nullable as String, + walletBloc: freezed == walletBloc + ? _value.walletBloc + : walletBloc // ignore: cast_nullable_to_non_nullable + as WalletBloc?, ) as $Val); } @@ -168,7 +175,8 @@ abstract class _$$ReceiveStateImplCopyWith<$Res> String description, String savedDescription, bool creatingInvoice, - String errCreatingInvoice}); + String errCreatingInvoice, + WalletBloc? walletBloc}); @override $AddressCopyWith<$Res>? get defaultAddress; @@ -197,6 +205,7 @@ class __$$ReceiveStateImplCopyWithImpl<$Res> Object? savedDescription = null, Object? creatingInvoice = null, Object? errCreatingInvoice = null, + Object? walletBloc = freezed, }) { return _then(_$ReceiveStateImpl( loadingAddress: null == loadingAddress @@ -247,6 +256,10 @@ class __$$ReceiveStateImplCopyWithImpl<$Res> ? _value.errCreatingInvoice : errCreatingInvoice // ignore: cast_nullable_to_non_nullable as String, + walletBloc: freezed == walletBloc + ? _value.walletBloc + : walletBloc // ignore: cast_nullable_to_non_nullable + as WalletBloc?, )); } } @@ -266,7 +279,8 @@ class _$ReceiveStateImpl extends _ReceiveState { this.description = '', this.savedDescription = '', this.creatingInvoice = true, - this.errCreatingInvoice = ''}) + this.errCreatingInvoice = '', + this.walletBloc}) : super._(); @override @@ -304,10 +318,12 @@ class _$ReceiveStateImpl extends _ReceiveState { @override @JsonKey() final String errCreatingInvoice; + @override + final WalletBloc? walletBloc; @override String toString() { - return 'ReceiveState(loadingAddress: $loadingAddress, errLoadingAddress: $errLoadingAddress, defaultAddress: $defaultAddress, privateLabel: $privateLabel, savingLabel: $savingLabel, errSavingLabel: $errSavingLabel, labelSaved: $labelSaved, savedInvoiceAmount: $savedInvoiceAmount, description: $description, savedDescription: $savedDescription, creatingInvoice: $creatingInvoice, errCreatingInvoice: $errCreatingInvoice)'; + return 'ReceiveState(loadingAddress: $loadingAddress, errLoadingAddress: $errLoadingAddress, defaultAddress: $defaultAddress, privateLabel: $privateLabel, savingLabel: $savingLabel, errSavingLabel: $errSavingLabel, labelSaved: $labelSaved, savedInvoiceAmount: $savedInvoiceAmount, description: $description, savedDescription: $savedDescription, creatingInvoice: $creatingInvoice, errCreatingInvoice: $errCreatingInvoice, walletBloc: $walletBloc)'; } @override @@ -338,7 +354,9 @@ class _$ReceiveStateImpl extends _ReceiveState { (identical(other.creatingInvoice, creatingInvoice) || other.creatingInvoice == creatingInvoice) && (identical(other.errCreatingInvoice, errCreatingInvoice) || - other.errCreatingInvoice == errCreatingInvoice)); + other.errCreatingInvoice == errCreatingInvoice) && + (identical(other.walletBloc, walletBloc) || + other.walletBloc == walletBloc)); } @override @@ -355,7 +373,8 @@ class _$ReceiveStateImpl extends _ReceiveState { description, savedDescription, creatingInvoice, - errCreatingInvoice); + errCreatingInvoice, + walletBloc); @JsonKey(ignore: true) @override @@ -377,7 +396,8 @@ abstract class _ReceiveState extends ReceiveState { final String description, final String savedDescription, final bool creatingInvoice, - final String errCreatingInvoice}) = _$ReceiveStateImpl; + final String errCreatingInvoice, + final WalletBloc? walletBloc}) = _$ReceiveStateImpl; const _ReceiveState._() : super._(); @override @@ -405,6 +425,8 @@ abstract class _ReceiveState extends ReceiveState { @override String get errCreatingInvoice; @override + WalletBloc? get walletBloc; + @override @JsonKey(ignore: true) _$$ReceiveStateImplCopyWith<_$ReceiveStateImpl> get copyWith => throw _privateConstructorUsedError; diff --git a/lib/receive/receive_page.dart b/lib/receive/receive_page.dart index 32f9ec56..226711b9 100644 --- a/lib/receive/receive_page.dart +++ b/lib/receive/receive_page.dart @@ -222,10 +222,10 @@ class _WalletName extends StatelessWidget { Widget build(BuildContext context) { final loading = context.select((ReceiveCubit x) => x.state.loadingAddress); - final walletName = context.select((ReceiveCubit x) => x.walletBloc.state.wallet?.name); + final walletName = context.select((ReceiveCubit _) => _.state.walletBloc?.state.wallet?.name); - final fingerprint = - context.select((ReceiveCubit x) => x.walletBloc.state.wallet?.sourceFingerprint ?? ''); + final fingerprint = context + .select((ReceiveCubit _) => _.state.walletBloc?.state.wallet?.sourceFingerprint ?? ''); return AnimatedContainer( duration: 500.ms,