Skip to content

Commit

Permalink
receive - new flow - bloc updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mocodesmo committed Jan 23, 2024
1 parent c084d23 commit 1060c4a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
38 changes: 25 additions & 13 deletions lib/receive/bloc/receive_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,33 @@ import 'package:flutter_bloc/flutter_bloc.dart';

class ReceiveCubit extends Cubit<ReceiveState> {
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(
Expand All @@ -42,7 +47,7 @@ class ReceiveCubit extends Cubit<ReceiveState> {
);
final label = await walletAddress.getLabel(
address: address!.address,
wallet: walletBloc.state.wallet!,
wallet: state.walletBloc!.state.wallet!,
);
final labelUpdated = address.copyWith(label: label);

Expand All @@ -64,13 +69,16 @@ class ReceiveCubit extends Cubit<ReceiveState> {
),
);
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(
Expand All @@ -81,7 +89,7 @@ class ReceiveCubit extends Cubit<ReceiveState> {
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) {
Expand Down Expand Up @@ -127,20 +135,22 @@ class ReceiveCubit extends Cubit<ReceiveState> {
}

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(
Expand All @@ -165,6 +175,8 @@ class ReceiveCubit extends Cubit<ReceiveState> {
}

void saveFinalInvoiceClicked() async {
if (state.walletBloc == null) return;

if (currencyCubit.state.amount <= 0) {
emit(state.copyWith(errCreatingInvoice: 'Enter correct amount'));
return;
Expand All @@ -174,12 +186,12 @@ class ReceiveCubit extends Cubit<ReceiveState> {

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(
Expand Down
2 changes: 2 additions & 0 deletions lib/receive/bloc/state.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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._();
Expand Down
36 changes: 29 additions & 7 deletions lib/receive/bloc/state.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReceiveState> get copyWith =>
Expand All @@ -52,7 +53,8 @@ abstract class $ReceiveStateCopyWith<$Res> {
String description,
String savedDescription,
bool creatingInvoice,
String errCreatingInvoice});
String errCreatingInvoice,
WalletBloc? walletBloc});

$AddressCopyWith<$Res>? get defaultAddress;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?,
));
}
}
Expand All @@ -266,7 +279,8 @@ class _$ReceiveStateImpl extends _ReceiveState {
this.description = '',
this.savedDescription = '',
this.creatingInvoice = true,
this.errCreatingInvoice = ''})
this.errCreatingInvoice = '',
this.walletBloc})
: super._();

@override
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -355,7 +373,8 @@ class _$ReceiveStateImpl extends _ReceiveState {
description,
savedDescription,
creatingInvoice,
errCreatingInvoice);
errCreatingInvoice,
walletBloc);

@JsonKey(ignore: true)
@override
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions lib/receive/receive_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1060c4a

Please sign in to comment.