Add create and restore wallet options to dropdown menu

This commit is contained in:
Godwin Asuquo 2023-02-17 16:41:49 +02:00
parent 75d52794f4
commit 4bc81242fc
8 changed files with 97 additions and 18 deletions

View file

@ -0,0 +1,20 @@
import 'package:cw_core/wallet_type.dart';
abstract class DesktopDropdownItem {
final String name;
final WalletType type;
final bool isCurrent;
final dynamic key;
final bool isEnabled;
final String? optionName;
DesktopDropdownItem({this.name = '', this.type = WalletType.none, this.isCurrent = false, this.key, this.isEnabled = true, this.optionName});
}
class DropdownOption extends DesktopDropdownItem {
DropdownOption({required this.name, required this.optionName});
final String name;
final String optionName;
}

View file

@ -1,5 +1,4 @@
import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/screens/dashboard/desktop_widgets/desktop_dashboard_view.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
import 'package:cake_wallet/entities/load_current_wallet.dart'; import 'package:cake_wallet/entities/load_current_wallet.dart';

View file

@ -24,6 +24,7 @@ class DesktopDashboardPage extends StatelessWidget {
final WalletAddressListViewModel addressListViewModel; final WalletAddressListViewModel addressListViewModel;
static final GlobalKey<NavigatorState> desktopKey = GlobalKey<NavigatorState>(); static final GlobalKey<NavigatorState> desktopKey = GlobalKey<NavigatorState>();
static final GlobalKey kex = GlobalKey();
bool _isEffectsInstalled = false; bool _isEffectsInstalled = false;
StreamSubscription<bool>? _onInactiveSub; StreamSubscription<bool>? _onInactiveSub;

View file

@ -1,4 +1,5 @@
import 'package:another_flushbar/flushbar.dart'; import 'package:another_flushbar/flushbar.dart';
import 'package:cake_wallet/entities/desktop_dropdown_item.dart';
import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/routes.dart'; import 'package:cake_wallet/routes.dart';
import 'package:cake_wallet/src/screens/auth/auth_page.dart'; import 'package:cake_wallet/src/screens/auth/auth_page.dart';
@ -7,6 +8,7 @@ import 'package:cake_wallet/utils/show_bar.dart';
import 'package:cake_wallet/utils/show_pop_up.dart'; import 'package:cake_wallet/utils/show_pop_up.dart';
import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart'; import 'package:cake_wallet/view_model/wallet_list/wallet_list_item.dart';
import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart'; import 'package:cake_wallet/view_model/wallet_list/wallet_list_view_model.dart';
import 'package:cake_wallet/wallet_type_utils.dart';
import 'package:cw_core/wallet_type.dart'; import 'package:cw_core/wallet_type.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -33,8 +35,8 @@ class _DesktopWalletSelectionDropDownState extends State<DesktopWalletSelectionD
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final themeData = Theme.of(context); final themeData = Theme.of(context);
return DropdownButton<WalletListItem>( return DropdownButton<DesktopDropdownItem>(
items: widget.walletListViewModel.wallets items: widget.walletListViewModel.dropdownItems
.map((wallet) => DropdownMenuItem( .map((wallet) => DropdownMenuItem(
child: ConstrainedBox( child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: 500), constraints: BoxConstraints(maxWidth: 500),
@ -47,7 +49,12 @@ class _DesktopWalletSelectionDropDownState extends State<DesktopWalletSelectionD
if (selectedWallet!.isCurrent || !selectedWallet.isEnabled) { if (selectedWallet!.isCurrent || !selectedWallet.isEnabled) {
return; return;
} }
if(selectedWallet is DropdownOption){
_handleCreateOption(selectedWallet);
return;
}
final confirmed = await showPopUp<bool>( final confirmed = await showPopUp<bool>(
context: context, context: context,
builder: (dialogContext) { builder: (dialogContext) {
@ -61,25 +68,25 @@ class _DesktopWalletSelectionDropDownState extends State<DesktopWalletSelectionD
}) ?? }) ??
false; false;
if (confirmed) { if (confirmed && selectedWallet is WalletListItem) {
await _loadWallet(selectedWallet); await _loadWallet(selectedWallet);
} }
}, },
dropdownColor: themeData.textTheme.bodyText1?.decorationColor, dropdownColor: themeData.textTheme.bodyText1?.decorationColor,
style: TextStyle(color: themeData.primaryTextTheme.headline6?.color), style: TextStyle(color: themeData.primaryTextTheme.headline6?.color),
selectedItemBuilder: (context) => widget.walletListViewModel.wallets selectedItemBuilder: (context) => widget.walletListViewModel.dropdownItems
.map((wallet) => ConstrainedBox( .map((wallet) => ConstrainedBox(
constraints: BoxConstraints(maxWidth: 500), constraints: BoxConstraints(maxWidth: 500),
child: walletListItemTile(wallet), child: walletListItemTile(wallet),
)) ))
.toList(), .toList(),
value: widget.walletListViewModel.wallets.firstWhere((element) => element.isCurrent), value: widget.walletListViewModel.dropdownItems.firstWhere((element) => element.isCurrent),
underline: const SizedBox(), underline: const SizedBox(),
focusColor: Colors.transparent, focusColor: Colors.transparent,
); );
} }
Widget walletListItemTile(WalletListItem wallet) { Widget walletListItemTile(DesktopDropdownItem wallet) {
return Container( return Container(
height: tileHeight, height: tileHeight,
padding: EdgeInsets.symmetric(horizontal: 20), padding: EdgeInsets.symmetric(horizontal: 20),
@ -152,6 +159,32 @@ class _DesktopWalletSelectionDropDownState extends State<DesktopWalletSelectionD
} }
} }
void _handleCreateOption(DropdownOption option) {
if (option.optionName == 'create_wallet') {
_navigateToCreateWallet();
} else {
_navigateToRestoreWallet();
}
}
void _navigateToCreateWallet(){
if (isSingleCoin) {
Navigator.of(context).pushNamed(Routes.newWallet, arguments: widget.walletListViewModel.currentWalletType);
} else {
Navigator.of(context).pushNamed(Routes.newWalletType);
}
}
void _navigateToRestoreWallet(){
if (isSingleCoin) {
Navigator.of(context).pushNamed(Routes.restoreWallet, arguments: widget.walletListViewModel.currentWalletType);
} else {
Navigator.of(context).pushNamed(Routes.restoreWalletType);
}
}
void changeProcessText(String text) { void changeProcessText(String text) {
_progressBar = createBar<void>(text, duration: null)..show(context); _progressBar = createBar<void>(text, duration: null)..show(context);
} }

View file

@ -16,7 +16,8 @@ class DesktopSettingsPage extends StatefulWidget {
} }
class _DesktopSettingsPageState extends State<DesktopSettingsPage> { class _DesktopSettingsPageState extends State<DesktopSettingsPage> {
final int itemCount = SettingActions.all.length; final List<SettingActions> _allItems = [];
int? currentPage; int? currentPage;
void _onItemChange(int index) { void _onItemChange(int index) {
@ -25,6 +26,13 @@ class _DesktopSettingsPageState extends State<DesktopSettingsPage> {
}); });
} }
@override
void initState() {
final allActions = List<SettingActions>.from(SettingActions.all);
allActions.remove(SettingActions.walletSettingAction);
_allItems.addAll(allActions); super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -48,8 +56,8 @@ class _DesktopSettingsPageState extends State<DesktopSettingsPage> {
child: ListView.separated( child: ListView.separated(
padding: EdgeInsets.only(top: 0), padding: EdgeInsets.only(top: 0),
itemBuilder: (_, index) { itemBuilder: (_, index) {
final item = SettingActions.all[index]; final item = _allItems[index];
final isLastTile = index == itemCount; final isLastTile = index == _allItems.length - 1;
return SettingActionButton( return SettingActionButton(
isLastTile: isLastTile, isLastTile: isLastTile,
selectionActive: currentPage != null, selectionActive: currentPage != null,
@ -71,7 +79,7 @@ class _DesktopSettingsPageState extends State<DesktopSettingsPage> {
height: 1, height: 1,
color: Theme.of(context).primaryTextTheme.caption!.decorationColor!, color: Theme.of(context).primaryTextTheme.caption!.decorationColor!,
), ),
itemCount: itemCount, itemCount: _allItems.length,
), ),
), ),
], ],

View file

@ -234,7 +234,7 @@ class WalletListBodyState extends State<WalletListBody> {
auth.hideProgressText(); auth.hideProgressText();
auth.close(); auth.close();
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
navigatorKey.currentState!.pushNamedAndRemoveUntil(Routes.dashboard, (route) => false); Navigator.of(context).pop();
}); });
} catch (e) { } catch (e) {
auth.changeProcessText( auth.changeProcessText(
@ -246,7 +246,7 @@ class WalletListBodyState extends State<WalletListBody> {
changeProcessText(S.of(context).wallet_list_loading_wallet(wallet.name)); changeProcessText(S.of(context).wallet_list_loading_wallet(wallet.name));
await widget.walletListViewModel.loadWallet(wallet); await widget.walletListViewModel.loadWallet(wallet);
hideProgressText(); hideProgressText();
navigatorKey.currentState!.pushNamedAndRemoveUntil(Routes.dashboard, (route) => false); Navigator.of(context).pop();
} catch (e) { } catch (e) {
changeProcessText(S.of(context).wallet_list_failed_to_load(wallet.name, e.toString())); changeProcessText(S.of(context).wallet_list_failed_to_load(wallet.name, e.toString()));
} }

View file

@ -1,17 +1,18 @@
import 'package:flutter/foundation.dart'; import 'package:cake_wallet/entities/desktop_dropdown_item.dart';
import 'package:cw_core/wallet_type.dart'; import 'package:cw_core/wallet_type.dart';
class WalletListItem { class WalletListItem extends DesktopDropdownItem {
const WalletListItem( WalletListItem(
{required this.name, {required this.name,
required this.type, required this.type,
required this.key, required this.key,
this.isCurrent = false, this.isCurrent = false,
this.isEnabled = true}); this.isEnabled = true, this.optionName = '',});
final String name; final String name;
final WalletType type; final WalletType type;
final bool isCurrent; final bool isCurrent;
final dynamic key; final dynamic key;
final bool isEnabled; final bool isEnabled;
final String optionName;
} }

View file

@ -1,5 +1,7 @@
import 'package:cake_wallet/core/auth_service.dart'; import 'package:cake_wallet/core/auth_service.dart';
import 'package:cake_wallet/core/wallet_loading_service.dart'; import 'package:cake_wallet/core/wallet_loading_service.dart';
import 'package:cake_wallet/entities/desktop_dropdown_item.dart';
import 'package:cake_wallet/generated/i18n.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:mobx/mobx.dart'; import 'package:mobx/mobx.dart';
import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/di.dart';
@ -27,6 +29,21 @@ abstract class WalletListViewModelBase with Store {
@observable @observable
ObservableList<WalletListItem> wallets; ObservableList<WalletListItem> wallets;
@computed
ObservableList<DesktopDropdownItem> get dropdownItems {
final items = <DesktopDropdownItem>[];
for (final wallet in wallets) {
items.add(wallet);
}
items.addAll([
DropdownOption(name: S.current.create_new, optionName: "create_wallet"),
DropdownOption(name: S.current.restore_wallet, optionName: "restore_wallet"),
]);
return ObservableList<DesktopDropdownItem>.of(items);
}
final AppStore _appStore; final AppStore _appStore;
final Box<WalletInfo> _walletInfoSource; final Box<WalletInfo> _walletInfoSource;
final WalletLoadingService _walletLoadingService; final WalletLoadingService _walletLoadingService;