CAKE-158 | added isPrimary property to WalletAddressListItem; removed defining primary address cell in the receive_page.dart

OleksandrSobol committed Dec 3, 2020 at 22:47 UTC b200c56c587c856eec22bf8d83cc6781d3cf7848
4 files changed +99 -95
lib/src/screens/receive/receive_page.dart
+76 -84
@@ -95,90 +95,82 @@ class ReceivePage extends BasePage {
95 amountTextFieldFocusNode: _cryptoAmountFocus),
96 ),
97 Observer(
98 - builder: (_) {
99 - var isPrimaryAddress = true;
100 -
101 - return ListView.separated(
102 - padding: EdgeInsets.all(0),
103 - separatorBuilder: (context, _) => Container(
104 - height: 1, color: Theme.of(context).dividerColor),
105 - shrinkWrap: true,
106 - physics: NeverScrollableScrollPhysics(),
107 - itemCount: addressListViewModel.items.length,
108 - itemBuilder: (context, index) {
109 - final item = addressListViewModel.items[index];
110 - Widget cell = Container();
111 -
112 - if (item is WalletAccountListHeader) {
113 - cell = HeaderTile(
114 - onTap: () async => await showPopUp<void>(
115 - context: context,
116 - builder: (_) =>
117 - getIt.get<MoneroAccountListPage>()),
118 - title: S.of(context).accounts,
119 - icon: Icon(
120 - Icons.arrow_forward_ios,
121 - size: 14,
122 - color:
123 - Theme.of(context).textTheme.display1.color,
124 - ));
125 - }
126 -
127 - if (item is WalletAddressListHeader) {
128 - cell = HeaderTile(
129 - onTap: () => Navigator.of(context)
130 - .pushNamed(Routes.newSubaddress),
131 - title: S.of(context).addresses,
132 - icon: Icon(
133 - Icons.add,
134 - size: 20,
135 - color:
136 - Theme.of(context).textTheme.display1.color,
137 - ));
138 - }
139 -
140 - if (item is WalletAddressListItem) {
141 - final isPrimary = isPrimaryAddress;
142 - isPrimaryAddress = false;
143 -
144 - cell = Observer(builder: (_) {
145 - final isCurrent = item.address ==
146 - addressListViewModel.address.address;
147 - final backgroundColor = isCurrent
148 - ? Theme.of(context)
149 - .textTheme
150 - .display3
151 - .decorationColor
152 - : Theme.of(context)
153 - .textTheme
154 - .display2
155 - .decorationColor;
156 - final textColor = isCurrent
157 - ? Theme.of(context).textTheme.display3.color
158 - : Theme.of(context).textTheme.display2.color;
159 -
160 - return AddressCell.fromItem(item,
161 - isCurrent: isCurrent,
162 - isPrimary: isPrimary,
163 - backgroundColor: backgroundColor,
164 - textColor: textColor,
165 - onTap: (_) => addressListViewModel.setAddress(item),
166 - onEdit: () => Navigator.of(context).pushNamed(
167 - Routes.newSubaddress,
168 - arguments: item));
169 - });
170 - }
171 -
172 - return index != 0
173 - ? cell
174 - : ClipRRect(
175 - borderRadius: BorderRadius.only(
176 - topLeft: Radius.circular(30),
177 - topRight: Radius.circular(30)),
178 - child: cell,
179 - );
180 - });
181 - }),
98 + builder: (_) => ListView.separated(
99 + padding: EdgeInsets.all(0),
100 + separatorBuilder: (context, _) => Container(
101 + height: 1, color: Theme.of(context).dividerColor),
102 + shrinkWrap: true,
103 + physics: NeverScrollableScrollPhysics(),
104 + itemCount: addressListViewModel.items.length,
105 + itemBuilder: (context, index) {
106 + final item = addressListViewModel.items[index];
107 + Widget cell = Container();
108 +
109 + if (item is WalletAccountListHeader) {
110 + cell = HeaderTile(
111 + onTap: () async => await showPopUp<void>(
112 + context: context,
113 + builder: (_) =>
114 + getIt.get<MoneroAccountListPage>()),
115 + title: S.of(context).accounts,
116 + icon: Icon(
117 + Icons.arrow_forward_ios,
118 + size: 14,
119 + color:
120 + Theme.of(context).textTheme.display1.color,
121 + ));
122 + }
123 +
124 + if (item is WalletAddressListHeader) {
125 + cell = HeaderTile(
126 + onTap: () => Navigator.of(context)
127 + .pushNamed(Routes.newSubaddress),
128 + title: S.of(context).addresses,
129 + icon: Icon(
130 + Icons.add,
131 + size: 20,
132 + color:
133 + Theme.of(context).textTheme.display1.color,
134 + ));
135 + }
136 +
137 + if (item is WalletAddressListItem) {
138 + cell = Observer(builder: (_) {
139 + final isCurrent = item.address ==
140 + addressListViewModel.address.address;
141 + final backgroundColor = isCurrent
142 + ? Theme.of(context)
143 + .textTheme
144 + .display3
145 + .decorationColor
146 + : Theme.of(context)
147 + .textTheme
148 + .display2
149 + .decorationColor;
150 + final textColor = isCurrent
151 + ? Theme.of(context).textTheme.display3.color
152 + : Theme.of(context).textTheme.display2.color;
153 +
154 + return AddressCell.fromItem(item,
155 + isCurrent: isCurrent,
156 + backgroundColor: backgroundColor,
157 + textColor: textColor,
158 + onTap: (_) => addressListViewModel.setAddress(item),
159 + onEdit: () => Navigator.of(context).pushNamed(
160 + Routes.newSubaddress,
161 + arguments: item));
162 + });
163 + }
164 +
165 + return index != 0
166 + ? cell
167 + : ClipRRect(
168 + borderRadius: BorderRadius.only(
169 + topLeft: Radius.circular(30),
170 + topRight: Radius.circular(30)),
171 + child: cell,
172 + );
173 + })),
174 ],
175 ),
176 ));
lib/src/screens/receive/widgets/address_cell.dart
+1 -2
@@ -6,7 +6,6 @@ import 'package:cake_wallet/view_model/wallet_address_list/wallet_address_list_i
6 class AddressCell extends StatelessWidget {
7 factory AddressCell.fromItem(WalletAddressListItem item,
8 {@required bool isCurrent,
9 - @required bool isPrimary,
9 @required Color backgroundColor,
10 @required Color textColor,
11 Function(String) onTap,
@@ -15,7 +14,7 @@ class AddressCell extends StatelessWidget {
14 address: item.address,
15 name: item.name,
16 isCurrent: isCurrent,
18 - isPrimary: isPrimary,
17 + isPrimary: item.isPrimary,
18 backgroundColor: backgroundColor,
19 textColor: textColor,
20 onTap: onTap,
lib/view_model/wallet_address_list/wallet_address_list_item.dart
+3 -2
@@ -2,10 +2,11 @@ import 'package:flutter/foundation.dart';
2 import 'package:cake_wallet/utils/list_item.dart';
3
4 class WalletAddressListItem extends ListItem {
5 - const WalletAddressListItem({@required this.address, this.name, this.id})
6 - : super();
5 + const WalletAddressListItem({@required this.address, @required this.isPrimary,
6 + this.name, this.id}) : super();
7
8 final int id;
9 + final bool isPrimary;
10 final String address;
11 final String name;
12
lib/view_model/wallet_address_list/wallet_address_list_view_model.dart
+19 -7
@@ -97,16 +97,28 @@ abstract class WalletAddressListViewModelBase with Store {
97 final addressList = ObservableList<ListItem>();
98
99 if (wallet is MoneroWallet) {
100 - addressList.addAll(wallet.subaddressList.subaddresses.map((subaddress) =>
101 - WalletAddressListItem(
102 - id: subaddress.id,
103 - name: subaddress.label,
104 - address: subaddress.address)));
100 + final primaryAddress = wallet.subaddressList.subaddresses.first;
101 + addressList.addAll(wallet.subaddressList.subaddresses.map((subaddress) {
102 + final isPrimary = subaddress == primaryAddress;
103 +
104 + return WalletAddressListItem(
105 + id: subaddress.id,
106 + isPrimary: isPrimary,
107 + name: subaddress.label,
108 + address: subaddress.address);
109 + }));
110 }
111
112 if (wallet is BitcoinWallet) {
108 - final bitcoinAddresses = wallet.addresses.map((addr) =>
109 - WalletAddressListItem(name: addr.label, address: addr.address));
113 + final primaryAddress = wallet.addresses.first;
114 + final bitcoinAddresses = wallet.addresses.map((addr) {
115 + final isPrimary = addr == primaryAddress;
116 +
117 + return WalletAddressListItem(
118 + isPrimary: isPrimary,
119 + name: addr.label,
120 + address: addr.address);
121 + });
122 addressList.addAll(bitcoinAddresses);
123 }
124