1- import 'package:bb_mobile/core/wallet/domain/entities/wallet .dart' ;
1+ import 'package:bb_mobile/core/utils/result .dart' ;
22import 'package:bb_mobile/core/wallet/domain/usecases/check_wallet_status_usecase.dart' ;
3+ import 'package:bb_mobile/core/wallet/domain/entities/wallet.dart' ;
34import 'package:bb_mobile/features/import_mnemonic/domain/check_duplicate_mnemonic_usecase.dart' ;
5+ import 'package:bb_mobile/features/import_mnemonic/domain/import_mnemonic_failure.dart' ;
46import 'package:bb_mobile/features/import_mnemonic/domain/import_wallet_usecase.dart' ;
5- import 'package:bb_mobile/features/import_mnemonic/errors.dart' ;
67import 'package:bb_mobile/features/import_mnemonic/presentation/state.dart' ;
78import 'package:bip39_mnemonic/bip39_mnemonic.dart' as bip39;
89import 'package:flutter_bloc/flutter_bloc.dart' ;
@@ -18,30 +19,27 @@ class ImportMnemonicCubit extends Cubit<ImportMnemonicState> {
1819 required this ._checkDuplicateMnemonicUsecase,
1920 }) : super (const ImportMnemonicState ());
2021
21- void clearError () => emit (state.copyWith (error : null ));
22+ void clearFailure () => emit (state.copyWith (failure : null ));
2223
2324 void reset () => emit (const ImportMnemonicState ());
2425
2526 Future <void > updateMnemonic (Mnemonic mnemonic) async {
26- try {
27- if (mnemonic.label.isEmpty) throw EmptyMnemonicLabelError ( );
28-
29- emit (state. copyWith (isLoading : true , error : null ));
27+ if (mnemonic.label.isEmpty) {
28+ emit (state. copyWith (failure : const ImportMnemonicEmptyLabelFailure ()) );
29+ return ;
30+ }
3031
31- await _checkDuplicateMnemonicUsecase.execute (
32- mnemonicWords: mnemonic.words,
33- passphrase: mnemonic.passphrase,
34- );
32+ emit (state.copyWith (isLoading: true , failure: null ));
3533
36- emit (state. copyWith (mnemonic : mnemonic, isLoading : false ));
37- _scanAllScriptTypes ( mnemonic);
38- } catch (e) {
39- emit (
40- state. copyWith (
41- error : e is Exception ? e : ImportMnemonicError (e. toString ()),
42- isLoading : false ,
43- ),
44- );
34+ switch ( await _checkDuplicateMnemonicUsecase. execute (
35+ mnemonicWords : mnemonic.words,
36+ passphrase : mnemonic.passphrase,
37+ )) {
38+ case Ok () :
39+ emit (state. copyWith (mnemonic : mnemonic, isLoading : false ));
40+ _scanAllScriptTypes (mnemonic);
41+ case Err ( : final failure) :
42+ emit (state. copyWith (failure : failure, isLoading : false ) );
4543 }
4644 }
4745
@@ -86,28 +84,24 @@ class ImportMnemonicCubit extends Cubit<ImportMnemonicState> {
8684 emit (state.copyWith (scriptType: scriptType));
8785
8886 Future <void > import () async {
89- try {
90- if (state.mnemonic == null ) throw MnemonicIsNullError ();
87+ if (state.mnemonic == null ) {
88+ emit (state.copyWith (failure: const ImportMnemonicNullMnemonicFailure ()));
89+ return ;
90+ }
9191
92- emit (state.copyWith (isLoading: true , error : null ));
92+ emit (state.copyWith (isLoading: true , failure : null ));
9393
94- final mnemonic = state.mnemonic! ;
95- final wallet = await _importWalletUsecase.execute (
96- mnemonicWords: mnemonic.words,
97- label: mnemonic.label,
98- passphrase: mnemonic.passphrase,
99- scriptType: state.scriptType,
100- );
101- emit (state.copyWith (wallet: wallet, isLoading: false ));
102- } on DuplicateMnemonicException catch (e) {
103- emit (state.copyWith (error: e, isLoading: false ));
104- } catch (e) {
105- emit (
106- state.copyWith (
107- error: ImportMnemonicError (e.toString ()),
108- isLoading: false ,
109- ),
110- );
94+ final mnemonic = state.mnemonic! ;
95+ switch (await _importWalletUsecase.execute (
96+ mnemonicWords: mnemonic.words,
97+ label: mnemonic.label,
98+ passphrase: mnemonic.passphrase,
99+ scriptType: state.scriptType,
100+ )) {
101+ case Ok (: final value):
102+ emit (state.copyWith (wallet: value, isLoading: false ));
103+ case Err (: final failure):
104+ emit (state.copyWith (failure: failure, isLoading: false ));
111105 }
112106 }
113107}
0 commit comments