From b200c56c587c856eec22bf8d83cc6781d3cf7848 Mon Sep 17 00:00:00 2001 From: OleksandrSobol Date: Thu, 3 Dec 2020 22:47:00 +0200 Subject: [PATCH] CAKE-158 | added isPrimary property to WalletAddressListItem; removed defining primary address cell in the receive_page.dart --- lib/src/screens/receive/receive_page.dart | 150 +++++++++--------- .../screens/receive/widgets/address_cell.dart | 3 +- .../wallet_address_list_item.dart | 5 +- .../wallet_address_list_view_model.dart | 26 ++- 4 files changed, 94 insertions(+), 90 deletions(-) diff --git a/lib/src/screens/receive/receive_page.dart b/lib/src/screens/receive/receive_page.dart index 303d2aab2..5126a1527 100644 --- a/lib/src/screens/receive/receive_page.dart +++ b/lib/src/screens/receive/receive_page.dart @@ -95,90 +95,82 @@ class ReceivePage extends BasePage { amountTextFieldFocusNode: _cryptoAmountFocus), ), Observer( - builder: (_) { - var isPrimaryAddress = true; + builder: (_) => ListView.separated( + padding: EdgeInsets.all(0), + separatorBuilder: (context, _) => Container( + height: 1, color: Theme.of(context).dividerColor), + shrinkWrap: true, + physics: NeverScrollableScrollPhysics(), + itemCount: addressListViewModel.items.length, + itemBuilder: (context, index) { + final item = addressListViewModel.items[index]; + Widget cell = Container(); - return ListView.separated( - padding: EdgeInsets.all(0), - separatorBuilder: (context, _) => Container( - height: 1, color: Theme.of(context).dividerColor), - shrinkWrap: true, - physics: NeverScrollableScrollPhysics(), - itemCount: addressListViewModel.items.length, - itemBuilder: (context, index) { - final item = addressListViewModel.items[index]; - Widget cell = Container(); + if (item is WalletAccountListHeader) { + cell = HeaderTile( + onTap: () async => await showPopUp( + context: context, + builder: (_) => + getIt.get()), + title: S.of(context).accounts, + icon: Icon( + Icons.arrow_forward_ios, + size: 14, + color: + Theme.of(context).textTheme.display1.color, + )); + } - if (item is WalletAccountListHeader) { - cell = HeaderTile( - onTap: () async => await showPopUp( - context: context, - builder: (_) => - getIt.get()), - title: S.of(context).accounts, - icon: Icon( - Icons.arrow_forward_ios, - size: 14, - color: - Theme.of(context).textTheme.display1.color, - )); - } + if (item is WalletAddressListHeader) { + cell = HeaderTile( + onTap: () => Navigator.of(context) + .pushNamed(Routes.newSubaddress), + title: S.of(context).addresses, + icon: Icon( + Icons.add, + size: 20, + color: + Theme.of(context).textTheme.display1.color, + )); + } - if (item is WalletAddressListHeader) { - cell = HeaderTile( - onTap: () => Navigator.of(context) - .pushNamed(Routes.newSubaddress), - title: S.of(context).addresses, - icon: Icon( - Icons.add, - size: 20, - color: - Theme.of(context).textTheme.display1.color, - )); - } + if (item is WalletAddressListItem) { + cell = Observer(builder: (_) { + final isCurrent = item.address == + addressListViewModel.address.address; + final backgroundColor = isCurrent + ? Theme.of(context) + .textTheme + .display3 + .decorationColor + : Theme.of(context) + .textTheme + .display2 + .decorationColor; + final textColor = isCurrent + ? Theme.of(context).textTheme.display3.color + : Theme.of(context).textTheme.display2.color; - if (item is WalletAddressListItem) { - final isPrimary = isPrimaryAddress; - isPrimaryAddress = false; + return AddressCell.fromItem(item, + isCurrent: isCurrent, + backgroundColor: backgroundColor, + textColor: textColor, + onTap: (_) => addressListViewModel.setAddress(item), + onEdit: () => Navigator.of(context).pushNamed( + Routes.newSubaddress, + arguments: item)); + }); + } - cell = Observer(builder: (_) { - final isCurrent = item.address == - addressListViewModel.address.address; - final backgroundColor = isCurrent - ? Theme.of(context) - .textTheme - .display3 - .decorationColor - : Theme.of(context) - .textTheme - .display2 - .decorationColor; - final textColor = isCurrent - ? Theme.of(context).textTheme.display3.color - : Theme.of(context).textTheme.display2.color; - - return AddressCell.fromItem(item, - isCurrent: isCurrent, - isPrimary: isPrimary, - backgroundColor: backgroundColor, - textColor: textColor, - onTap: (_) => addressListViewModel.setAddress(item), - onEdit: () => Navigator.of(context).pushNamed( - Routes.newSubaddress, - arguments: item)); - }); - } - - return index != 0 - ? cell - : ClipRRect( - borderRadius: BorderRadius.only( - topLeft: Radius.circular(30), - topRight: Radius.circular(30)), - child: cell, - ); - }); - }), + return index != 0 + ? cell + : ClipRRect( + borderRadius: BorderRadius.only( + topLeft: Radius.circular(30), + topRight: Radius.circular(30)), + child: cell, + ); + })), ], ), )); diff --git a/lib/src/screens/receive/widgets/address_cell.dart b/lib/src/screens/receive/widgets/address_cell.dart index 3f33c439e..703067dcf 100644 --- a/lib/src/screens/receive/widgets/address_cell.dart +++ b/lib/src/screens/receive/widgets/address_cell.dart @@ -6,7 +6,6 @@ import 'package:cake_wallet/view_model/wallet_address_list/wallet_address_list_i class AddressCell extends StatelessWidget { factory AddressCell.fromItem(WalletAddressListItem item, {@required bool isCurrent, - @required bool isPrimary, @required Color backgroundColor, @required Color textColor, Function(String) onTap, @@ -15,7 +14,7 @@ class AddressCell extends StatelessWidget { address: item.address, name: item.name, isCurrent: isCurrent, - isPrimary: isPrimary, + isPrimary: item.isPrimary, backgroundColor: backgroundColor, textColor: textColor, onTap: onTap, diff --git a/lib/view_model/wallet_address_list/wallet_address_list_item.dart b/lib/view_model/wallet_address_list/wallet_address_list_item.dart index c6d8915ab..286cb8f94 100644 --- a/lib/view_model/wallet_address_list/wallet_address_list_item.dart +++ b/lib/view_model/wallet_address_list/wallet_address_list_item.dart @@ -2,10 +2,11 @@ import 'package:flutter/foundation.dart'; import 'package:cake_wallet/utils/list_item.dart'; class WalletAddressListItem extends ListItem { - const WalletAddressListItem({@required this.address, this.name, this.id}) - : super(); + const WalletAddressListItem({@required this.address, @required this.isPrimary, + this.name, this.id}) : super(); final int id; + final bool isPrimary; final String address; final String name; diff --git a/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart b/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart index e8c897bfc..aca1ef808 100644 --- a/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart +++ b/lib/view_model/wallet_address_list/wallet_address_list_view_model.dart @@ -97,16 +97,28 @@ abstract class WalletAddressListViewModelBase with Store { final addressList = ObservableList(); if (wallet is MoneroWallet) { - addressList.addAll(wallet.subaddressList.subaddresses.map((subaddress) => - WalletAddressListItem( - id: subaddress.id, - name: subaddress.label, - address: subaddress.address))); + final primaryAddress = wallet.subaddressList.subaddresses.first; + addressList.addAll(wallet.subaddressList.subaddresses.map((subaddress) { + final isPrimary = subaddress == primaryAddress; + + return WalletAddressListItem( + id: subaddress.id, + isPrimary: isPrimary, + name: subaddress.label, + address: subaddress.address); + })); } if (wallet is BitcoinWallet) { - final bitcoinAddresses = wallet.addresses.map((addr) => - WalletAddressListItem(name: addr.label, address: addr.address)); + final primaryAddress = wallet.addresses.first; + final bitcoinAddresses = wallet.addresses.map((addr) { + final isPrimary = addr == primaryAddress; + + return WalletAddressListItem( + isPrimary: isPrimary, + name: addr.label, + address: addr.address); + }); addressList.addAll(bitcoinAddresses); }