| 1 | import 'dart:math'; |
| 2 | |
| 3 | import 'package:cake_wallet/core/address_validator.dart'; |
| 4 | import 'package:cake_wallet/entities/new_ui_entities/list_item/list_item_regular_row.dart'; |
| 5 | import 'package:cake_wallet/entities/new_ui_entities/list_item/list_item_toggle.dart'; |
| 6 | import 'package:cake_wallet/entities/sort_balance_types.dart'; |
| 7 | import 'package:cake_wallet/generated/i18n.dart'; |
| 8 | import 'package:cake_wallet/new-ui/widgets/coins_page/token_image_widget.dart'; |
| 9 | import 'package:cake_wallet/routes.dart'; |
| 10 | import 'package:cake_wallet/src/screens/base_page.dart'; |
| 11 | import 'package:cake_wallet/src/screens/dashboard/favorite_token_modal.dart'; |
| 12 | import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; |
| 13 | import 'package:cake_wallet/src/screens/settings/widgets/settings_switcher_cell.dart'; |
| 14 | import 'package:cake_wallet/src/widgets/new_list_row/new_list_section.dart'; |
| 15 | import 'package:cake_wallet/src/widgets/picker.dart'; |
| 16 | import 'package:cake_wallet/utils/show_pop_up.dart'; |
| 17 | import 'package:cw_core/crypto_currency.dart'; |
| 18 | import 'package:flutter/material.dart'; |
| 19 | import 'package:flutter_mobx/flutter_mobx.dart'; |
| 20 | import 'package:cake_wallet/view_model/dashboard/home_settings_view_model.dart'; |
| 21 | |
| 22 | class HomeSettingsPage extends BasePage { |
| 23 | HomeSettingsPage(this._homeSettingsViewModel); |
| 24 | |
| 25 | final HomeSettingsViewModel _homeSettingsViewModel; |
| 26 | |
| 27 | final TextEditingController _searchController = TextEditingController(); |
| 28 | |
| 29 | @override |
| 30 | String? get title => S.current.home_screen_settings; |
| 31 | |
| 32 | @override |
| 33 | Widget body(BuildContext context) { |
| 34 | return SingleChildScrollView( |
| 35 | child: Column( |
| 36 | children: [ |
| 37 | // Observer( |
| 38 | // builder: (_) => SettingsPickerCell<SortBalanceBy>( |
| 39 | // title: S.current.sort_by, |
| 40 | // items: SortBalanceBy.values, |
| 41 | // selectedItem: _homeSettingsViewModel.sortBalanceBy, |
| 42 | // onItemSelected: _homeSettingsViewModel.setSortBalanceBy, |
| 43 | // ), |
| 44 | // ), |
| 45 | // Divider(color: Theme.of(context).colorScheme.outlineVariant), |
| 46 | // Observer( |
| 47 | // builder: (_) => SettingsSwitcherCell( |
| 48 | // title: S.of(context).show_combined_balance, |
| 49 | // value: _homeSettingsViewModel.showCombinedBalance, |
| 50 | // onValueChange: (_, bool value) { |
| 51 | // _homeSettingsViewModel.setShowCombinedBalance(value); |
| 52 | // }, |
| 53 | // ), |
| 54 | // ), |
| 55 | // Observer( |
| 56 | // builder: (_) => _homeSettingsViewModel.showCombinedBalance |
| 57 | // ? SizedBox.shrink() |
| 58 | // : SettingsCellWithArrow( |
| 59 | // title: S.of(context).favorite_token, |
| 60 | // handler: (context) async { |
| 61 | // |
| 62 | // }, |
| 63 | // )), |
| 64 | Padding( |
| 65 | padding: const EdgeInsets.all(12.0), |
| 66 | child: Observer( |
| 67 | builder: (_) => NewListSections(sections: { |
| 68 | "": [ |
| 69 | ListItemRegularRow( |
| 70 | keyValue: "sort balance by", |
| 71 | label: S.of(context).sort_by, |
| 72 | trailingText: _homeSettingsViewModel.sortBalanceBy.toString(), |
| 73 | onTap: () { |
| 74 | showPopUp<void>( |
| 75 | context: context, |
| 76 | builder: (context) => Picker( |
| 77 | items: SortBalanceBy.values, |
| 78 | selectedAtIndex: |
| 79 | SortBalanceBy.values.indexOf(_homeSettingsViewModel.sortBalanceBy), |
| 80 | mainAxisAlignment: MainAxisAlignment.start, |
| 81 | onItemSelected: _homeSettingsViewModel.setSortBalanceBy, |
| 82 | isSeparated: false, |
| 83 | ), |
| 84 | ); |
| 85 | }), |
| 86 | ListItemToggle( |
| 87 | keyValue: "show combined", |
| 88 | label: S.of(context).show_combined_balance, |
| 89 | value: _homeSettingsViewModel.showCombinedBalance, |
| 90 | onChanged: (val) => _homeSettingsViewModel.setShowCombinedBalance(val)), |
| 91 | if (!_homeSettingsViewModel.showCombinedBalance) |
| 92 | ListItemRegularRow( |
| 93 | keyValue: "fav token", |
| 94 | label: S.of(context).favorite_token, |
| 95 | trailingText: _homeSettingsViewModel.favoriteToken.title, |
| 96 | showArrow: true, |
| 97 | onTap: () async { |
| 98 | final res = await showModalBottomSheet( |
| 99 | context: context, |
| 100 | builder: (context) => |
| 101 | FavoriteTokenModal(tokens: _homeSettingsViewModel.enabledTokens)); |
| 102 | if (res != null && res is CryptoCurrency) { |
| 103 | _homeSettingsViewModel.setFavoriteToken(res); |
| 104 | } |
| 105 | }) |
| 106 | ] |
| 107 | }), |
| 108 | ), |
| 109 | ), |
| 110 | Divider(color: Theme.of(context).colorScheme.outlineVariant), |
| 111 | const SizedBox(height: 20), |
| 112 | Row( |
| 113 | children: [ |
| 114 | Expanded( |
| 115 | child: Padding( |
| 116 | padding: const EdgeInsetsDirectional.only(start: 16), |
| 117 | child: BaseTextFormField( |
| 118 | controller: _searchController, |
| 119 | textStyle: Theme.of(context).textTheme.bodyMedium?.copyWith( |
| 120 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 121 | ), |
| 122 | hintText: S.of(context).search_add_token, |
| 123 | prefixIcon: Image.asset( |
| 124 | "assets/images/search_icon.png", |
| 125 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 126 | ), |
| 127 | alignLabelWithHint: false, |
| 128 | contentPadding: const EdgeInsets.symmetric(vertical: 4, horizontal: 16), |
| 129 | onChanged: (String text) => _homeSettingsViewModel.changeSearchText(text), |
| 130 | ), |
| 131 | ), |
| 132 | ), |
| 133 | RawMaterialButton( |
| 134 | onPressed: () async { |
| 135 | Navigator.pushNamed( |
| 136 | context, |
| 137 | Routes.editToken, |
| 138 | arguments: { |
| 139 | 'homeSettingsViewModel': _homeSettingsViewModel, |
| 140 | if (AddressValidator(type: _homeSettingsViewModel.nativeToken) |
| 141 | .isValid(_searchController.text)) |
| 142 | 'contractAddress': _searchController.text, |
| 143 | }, |
| 144 | ); |
| 145 | }, |
| 146 | elevation: 0, |
| 147 | fillColor: Theme.of(context).colorScheme.surfaceContainer, |
| 148 | child: Icon( |
| 149 | Icons.add, |
| 150 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 151 | size: 22.0, |
| 152 | ), |
| 153 | padding: EdgeInsets.all(12), |
| 154 | shape: CircleBorder(), |
| 155 | splashColor: Theme.of(context).colorScheme.surfaceContainer, |
| 156 | ), |
| 157 | ], |
| 158 | ), |
| 159 | Padding( |
| 160 | padding: const EdgeInsets.only(bottom: 16, left: 16, right: 16), |
| 161 | child: Observer( |
| 162 | builder: (_) => ListView.builder( |
| 163 | itemCount: _homeSettingsViewModel.tokens.length, |
| 164 | shrinkWrap: true, |
| 165 | physics: NeverScrollableScrollPhysics(), |
| 166 | itemBuilder: (context, index) { |
| 167 | return Container( |
| 168 | margin: EdgeInsets.only(top: 16), |
| 169 | child: Observer( |
| 170 | builder: (_) { |
| 171 | final token = _homeSettingsViewModel.tokens.elementAt(index); |
| 172 | |
| 173 | return SettingsSwitcherCell( |
| 174 | title: "${token.name} " |
| 175 | "(${token.title})", |
| 176 | value: token.enabled, |
| 177 | onValueChange: (_, bool value) { |
| 178 | _homeSettingsViewModel.changeTokenAvailability(token, value); |
| 179 | }, |
| 180 | onTap: (_) { |
| 181 | Navigator.pushNamed(context, Routes.editToken, arguments: { |
| 182 | 'homeSettingsViewModel': _homeSettingsViewModel, |
| 183 | 'token': token, |
| 184 | }); |
| 185 | }, |
| 186 | leading: TokenImageWidget( |
| 187 | imageUrl: token.iconPath ?? '', |
| 188 | size: 40, |
| 189 | errorWidget: Container( |
| 190 | height: 30.0, |
| 191 | width: 30.0, |
| 192 | child: Center( |
| 193 | child: Text( |
| 194 | token.title.substring(0, min(token.title.length, 2)), |
| 195 | style: Theme.of(context).textTheme.bodySmall?.copyWith( |
| 196 | fontSize: 11, |
| 197 | color: Theme.of(context).colorScheme.onSurfaceVariant, |
| 198 | ), |
| 199 | ), |
| 200 | ), |
| 201 | decoration: BoxDecoration( |
| 202 | shape: BoxShape.circle, |
| 203 | color: Theme.of(context).colorScheme.surfaceContainerHighest, |
| 204 | ), |
| 205 | ), |
| 206 | ), |
| 207 | decoration: BoxDecoration( |
| 208 | color: Theme.of(context).colorScheme.surfaceContainer, |
| 209 | borderRadius: BorderRadius.circular(30), |
| 210 | ), |
| 211 | ); |
| 212 | }, |
| 213 | ), |
| 214 | ); |
| 215 | }, |
| 216 | ), |
| 217 | ), |
| 218 | ), |
| 219 | ], |
| 220 | ), |
| 221 | ); |
| 222 | } |
| 223 | } |