From 0c197660a8dd3e3aa5377e6fb5acf569777a1b87 Mon Sep 17 00:00:00 2001 From: M Date: Thu, 22 Oct 2020 21:24:24 +0300 Subject: [PATCH] Integrated wallet names generation --- lib/di.dart | 8 ++++-- lib/entities/generate_name.dart | 18 +++++++++++++ lib/router.dart | 14 ++++++++-- lib/src/screens/pin_code/pin_code_widget.dart | 14 ++++++++++ .../setup_pin_code/setup_pin_code.dart | 12 ++++++--- .../screens/wallet_list/wallet_list_page.dart | 27 +++++++++++++++++-- lib/view_model/wallet_creation_vm.dart | 22 ++------------- .../wallet_list/wallet_list_view_model.dart | 6 +++-- lib/view_model/wallet_restore_view_model.dart | 2 -- 9 files changed, 89 insertions(+), 34 deletions(-) create mode 100644 lib/entities/generate_name.dart diff --git a/lib/di.dart b/lib/di.dart index bac4eeff3..26d291cfd 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -14,6 +14,7 @@ import 'package:cake_wallet/src/screens/exchange_trade/exchange_trade_page.dart' import 'package:cake_wallet/src/screens/faq/faq_page.dart'; import 'package:cake_wallet/src/screens/nodes/node_create_or_edit_page.dart'; import 'package:cake_wallet/src/screens/nodes/nodes_list_page.dart'; +import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart'; import 'package:cake_wallet/src/screens/rescan/rescan_page.dart'; import 'package:cake_wallet/src/screens/restore/wallet_restore_page.dart'; import 'package:cake_wallet/src/screens/seed/wallet_seed_page.dart'; @@ -237,7 +238,10 @@ Future setup( () => SendTemplatePage(sendViewModel: getIt.get())); getIt.registerFactory(() => WalletListViewModel( - walletInfoSource, getIt.get(), getIt.get())); + walletInfoSource, + getIt.get(), + getIt.get(), + getIt.get(param1: WalletType.monero))); getIt.registerFactory(() => WalletListPage(walletListViewModel: getIt.get())); @@ -361,7 +365,7 @@ Future setup( getIt.get(), getIt.get())); getIt.registerFactoryParam( + void Function(PinCodeState, String), void>( (onSuccessfulPinSetup, _) => SetupPinCodePage( getIt.get(), onSuccessfulPinSetup: onSuccessfulPinSetup)); diff --git a/lib/entities/generate_name.dart b/lib/entities/generate_name.dart new file mode 100644 index 000000000..b6418dfe6 --- /dev/null +++ b/lib/entities/generate_name.dart @@ -0,0 +1,18 @@ +import 'dart:math'; + +import 'package:flutter/services.dart'; + +Future generateName() async { + final randomThing = Random(); + final adjectiveStringRaw = + await rootBundle.loadString('assets/text/Wallet_Adjectives.txt'); + final nounStringRaw = + await rootBundle.loadString('assets/text/Wallet_Nouns.txt'); + final adjectives = List.from(adjectiveStringRaw.split('\n')); + final nouns = List.from(nounStringRaw.split('\n')); + final chosenAdjective = adjectives[randomThing.nextInt(adjectives.length)]; + final chosenNoun = nouns[randomThing.nextInt(nouns.length)]; + final returnString = chosenAdjective + ' ' + chosenNoun; + + return returnString; +} \ No newline at end of file diff --git a/lib/router.dart b/lib/router.dart index fc07b3b66..4b70dd79e 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -1,4 +1,5 @@ import 'package:cake_wallet/entities/contact_record.dart'; +import 'package:cake_wallet/src/screens/pin_code/pin_code_widget.dart'; import 'package:cake_wallet/src/screens/restore/wallet_restore_page.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -57,8 +58,17 @@ Route createRoute(RouteSettings settings) { case Routes.newWalletFromWelcome: return CupertinoPageRoute( builder: (_) => getIt.get( - param1: (BuildContext context, dynamic _) => - Navigator.pushNamed(context, Routes.newWallet)), + param1: (PinCodeState context, dynamic _) async { + try { + context.changeProcessText('Creating new wallet'); // FIXME: Unnamed constant + final newWalletVM = getIt.get(param1: WalletType.monero); + await newWalletVM.create(options: 'English'); // FIXME: Unnamed constant + context.hideProgressText(); + await Navigator.of(context.context).pushNamed(Routes.seed, arguments: true); + } catch(e) { + context.changeProcessText('Error: ${e.toString()}'); + } + }), fullscreenDialog: true); case Routes.newWalletType: diff --git a/lib/src/screens/pin_code/pin_code_widget.dart b/lib/src/screens/pin_code/pin_code_widget.dart index 2911a3936..c5e82ad13 100644 --- a/lib/src/screens/pin_code/pin_code_widget.dart +++ b/lib/src/screens/pin_code/pin_code_widget.dart @@ -1,3 +1,5 @@ +import 'package:cake_wallet/utils/show_bar.dart'; +import 'package:flushbar/flushbar.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:cake_wallet/generated/i18n.dart'; @@ -33,6 +35,7 @@ class PinCodeState extends State { String pin; String title; double _aspectRatio; + Flushbar _progressBar; int currentPinLength() => pin.length; @@ -80,6 +83,17 @@ class PinCodeState extends State { setState(() {}); } + void changeProcessText(String text) { + hideProgressText(); + _progressBar = createBar(text, duration: null) + ..show(_key.currentContext); + } + + void hideProgressText() { + _progressBar?.dismiss(); + _progressBar = null; + } + @override Widget build(BuildContext context) => Scaffold( key: _key, body: body(context), resizeToAvoidBottomPadding: false); diff --git a/lib/src/screens/setup_pin_code/setup_pin_code.dart b/lib/src/screens/setup_pin_code/setup_pin_code.dart index 6cd54625d..e116d3e03 100644 --- a/lib/src/screens/setup_pin_code/setup_pin_code.dart +++ b/lib/src/screens/setup_pin_code/setup_pin_code.dart @@ -8,19 +8,23 @@ import 'package:cake_wallet/view_model/setup_pin_code_view_model.dart'; import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; class SetupPinCodePage extends BasePage { - SetupPinCodePage(this.pinCodeViewModel, {this.onSuccessfulPinSetup}); + SetupPinCodePage(this.pinCodeViewModel, {this.onSuccessfulPinSetup}) + : pinCodeStateKey = GlobalKey(); final SetupPinCodeViewModel pinCodeViewModel; - final void Function(BuildContext, String) onSuccessfulPinSetup; + final void Function(PinCodeState, String) onSuccessfulPinSetup; + final GlobalKey pinCodeStateKey; @override String get title => S.current.setup_pin; @override Widget body(BuildContext context) => PinCodeWidget( + key: pinCodeStateKey, hasLengthSwitcher: true, onFullPin: (String pin, PinCodeState state) async { - if (pinCodeViewModel.isOriginalPinCodeFull && !pinCodeViewModel.isRepeatedPinCodeFull) { + if (pinCodeViewModel.isOriginalPinCodeFull && + !pinCodeViewModel.isRepeatedPinCodeFull) { state.title = S.current.enter_your_pin_again; state.clear(); return; @@ -53,7 +57,7 @@ class SetupPinCodePage extends BasePage { buttonText: S.of(context).ok, buttonAction: () { Navigator.of(context).pop(); - onSuccessfulPinSetup(context, pin); + onSuccessfulPinSetup(pinCodeStateKey.currentState, pin); state.reset(); }, alertBarrierDismissible: false, diff --git a/lib/src/screens/wallet_list/wallet_list_page.dart b/lib/src/screens/wallet_list/wallet_list_page.dart index 75f78c658..05b35795b 100644 --- a/lib/src/screens/wallet_list/wallet_list_page.dart +++ b/lib/src/screens/wallet_list/wallet_list_page.dart @@ -1,8 +1,10 @@ import 'package:cake_wallet/src/screens/auth/auth_page.dart'; import 'package:cake_wallet/src/screens/wallet_list/widgets/wallet_menu_alert.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; +import 'package:cake_wallet/utils/show_bar.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart'; +import 'package:flushbar/flushbar.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; @@ -42,6 +44,7 @@ class WalletListBodyState extends State { Image.asset('assets/images/bitcoin.png', height: 24, width: 24); final scrollController = ScrollController(); final double tileHeight = 60; + Flushbar _progressBar; @override Widget build(BuildContext context) { @@ -164,8 +167,7 @@ class WalletListBodyState extends State { ), bottomSection: Column(children: [ PrimaryImageButton( - onPressed: () => - Navigator.of(context).pushNamed(Routes.newWallet), + onPressed: () => _generateNewWallet(), image: newWalletImage, text: S.of(context).wallet_list_create_new_wallet, color: Theme.of(context).accentTextTheme.subtitle.decorationColor, @@ -237,4 +239,25 @@ class WalletListBodyState extends State { auth.close(); }); } + + Future _generateNewWallet() async { + try { + changeProcessText('Creating new wallet'); // FIXME: Unnamed constant + await widget.walletListViewModel.walletNewVM.create(options: 'English'); // FIXME: Unnamed constant + hideProgressText(); + await Navigator.of(context).pushNamed(Routes.seed, arguments: true); + } catch(e) { + changeProcessText('Error: ${e.toString()}'); + } + } + + void changeProcessText(String text) { + _progressBar = createBar(text, duration: null) + ..show(context); + } + + void hideProgressText() { + _progressBar?.dismiss(); + _progressBar = null; + } } diff --git a/lib/view_model/wallet_creation_vm.dart b/lib/view_model/wallet_creation_vm.dart index 4c8ba6772..01893c954 100644 --- a/lib/view_model/wallet_creation_vm.dart +++ b/lib/view_model/wallet_creation_vm.dart @@ -8,8 +8,7 @@ import 'package:cake_wallet/entities/pathForWallet.dart'; import 'package:cake_wallet/entities/wallet_info.dart'; import 'package:cake_wallet/entities/wallet_type.dart'; import 'package:cake_wallet/store/app_store.dart'; -import 'package:flutter/services.dart'; -import 'dart:math'; +import 'package:cake_wallet/entities/generate_name.dart'; part 'wallet_creation_vm.g.dart'; @@ -33,26 +32,9 @@ abstract class WalletCreationVMBase with Store { final Box _walletInfoSource; final AppStore _appStore; - Future generateName() async { - final adjectiveStringRaw = - await rootBundle.loadString('assets/text/Wallet_Adjectives.txt'); - final nounStringRaw = - await rootBundle.loadString('assets/text/Wallet_Nouns.txt'); - - final randomThing = new Random(); - - final adjectives = new List.from(adjectiveStringRaw.split("\n")); - final nouns = new List.from(nounStringRaw.split("\n")); - - final chosenAdjective = adjectives[randomThing.nextInt(adjectives.length)]; - final chosenNoun = nouns[randomThing.nextInt(nouns.length)]; - - final returnString = chosenAdjective + " " + chosenNoun; - return returnString; - } - Future create({dynamic options}) async { try { + name = await generateName(); state = IsExecutingState(); final dirPath = await pathForWalletDir(name: name, type: type); final path = await pathForWallet(name: name, type: type); diff --git a/lib/view_model/wallet_list/wallet_list_view_model.dart b/lib/view_model/wallet_list/wallet_list_view_model.dart index 84885941e..cf9e61088 100644 --- a/lib/view_model/wallet_list/wallet_list_view_model.dart +++ b/lib/view_model/wallet_list/wallet_list_view_model.dart @@ -1,3 +1,4 @@ +import 'package:cake_wallet/view_model/wallet_new_vm.dart'; import 'package:hive/hive.dart'; import 'package:mobx/mobx.dart'; import 'package:cake_wallet/di.dart'; @@ -12,8 +13,8 @@ part 'wallet_list_view_model.g.dart'; class WalletListViewModel = WalletListViewModelBase with _$WalletListViewModel; abstract class WalletListViewModelBase with Store { - WalletListViewModelBase( - this._walletInfoSource, this._appStore, this._keyService) { + WalletListViewModelBase(this._walletInfoSource, this._appStore, + this._keyService, this.walletNewVM) { wallets = ObservableList(); _updateList(); } @@ -24,6 +25,7 @@ abstract class WalletListViewModelBase with Store { final AppStore _appStore; final Box _walletInfoSource; final KeyService _keyService; + final WalletNewVM walletNewVM; @action Future loadWallet(WalletListItem wallet) async { diff --git a/lib/view_model/wallet_restore_view_model.dart b/lib/view_model/wallet_restore_view_model.dart index 871f4b5cf..52e95b818 100644 --- a/lib/view_model/wallet_restore_view_model.dart +++ b/lib/view_model/wallet_restore_view_model.dart @@ -1,7 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:hive/hive.dart'; import 'package:mobx/mobx.dart'; -import 'package:uuid/uuid.dart'; import 'package:cake_wallet/store/app_store.dart'; import 'package:cake_wallet/core/wallet_base.dart'; import 'package:cake_wallet/core/generate_wallet_password.dart'; @@ -37,7 +36,6 @@ abstract class WalletRestoreViewModelBase extends WalletCreationVM with Store { WalletCredentials getCredentials(dynamic options) { final password = generateWalletPassword(type); final height = options['height'] as int; - name = Uuid().v4().substring(0, 10); if (mode == WalletRestoreMode.seed) { final seed = options['seed'] as String;