diff --git a/lib/di.dart b/lib/di.dart index 02a86d1e5..5cc698d27 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -2,7 +2,6 @@ import 'package:cake_wallet/core/yat_service.dart'; import 'package:cake_wallet/entities/parse_address_from_domain.dart'; import 'package:cake_wallet/entities/wake_lock.dart'; import 'package:cake_wallet/ionia/ionia_anypay.dart'; -import 'package:cake_wallet/ionia/ionia_category.dart'; import 'package:cake_wallet/ionia/ionia_gift_card.dart'; import 'package:cake_wallet/ionia/ionia_tip.dart'; import 'package:cake_wallet/src/screens/ionia/cards/ionia_custom_redeem_page.dart'; @@ -12,7 +11,6 @@ import 'package:cake_wallet/view_model/ionia/ionia_auth_view_model.dart'; import 'package:cake_wallet/view_model/ionia/ionia_buy_card_view_model.dart'; import 'package:cake_wallet/view_model/ionia/ionia_custom_tip_view_model.dart'; import 'package:cake_wallet/view_model/ionia/ionia_custom_redeem_view_model.dart'; -import 'package:cake_wallet/view_model/ionia/ionia_filter_view_model.dart'; import 'package:cake_wallet/ionia/ionia_service.dart'; import 'package:cake_wallet/ionia/ionia_api.dart'; import 'package:cake_wallet/ionia/ionia_merchant.dart'; @@ -686,8 +684,6 @@ Future setup( getIt.get(), getIt.get(), getIt.get().wallet)); - - getIt.registerFactory(() => IoniaFilterViewModel()); getIt.registerFactory(() => IoniaGiftCardsListViewModel(ioniaService: getIt.get())); diff --git a/lib/src/screens/ionia/cards/ionia_manage_cards_page.dart b/lib/src/screens/ionia/cards/ionia_manage_cards_page.dart index f8b28e69e..a71dae087 100644 --- a/lib/src/screens/ionia/cards/ionia_manage_cards_page.dart +++ b/lib/src/screens/ionia/cards/ionia_manage_cards_page.dart @@ -1,5 +1,3 @@ -import 'package:cake_wallet/di.dart'; -import 'package:cake_wallet/ionia/ionia_category.dart'; import 'package:cake_wallet/ionia/ionia_create_state.dart'; import 'package:cake_wallet/ionia/ionia_merchant.dart'; import 'package:cake_wallet/routes.dart'; @@ -13,7 +11,6 @@ import 'package:cake_wallet/utils/debounce.dart'; import 'package:cake_wallet/typography.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; import 'package:cake_wallet/view_model/ionia/ionia_gift_cards_list_view_model.dart'; -import 'package:cake_wallet/view_model/ionia/ionia_filter_view_model.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:cake_wallet/generated/i18n.dart'; @@ -28,6 +25,9 @@ class IoniaManageCardsPage extends BasePage { }); } }); + + _cardsListViewModel.getMerchants(); + } final IoniaGiftCardsListViewModel _cardsListViewModel; @@ -108,15 +108,27 @@ class IoniaManageCardsPage extends BasePage { @override Widget body(BuildContext context) { - final filterIcon = InkWell( + final filterButton = InkWell( onTap: () async { - final selectedFilters = await showCategoryFilter(context, _cardsListViewModel); - _cardsListViewModel.setSelectedFilter(selectedFilters); + await showCategoryFilter(context); + _cardsListViewModel.getMerchants(); }, - child: Image.asset( - 'assets/images/filter.png', - color: Theme.of(context).textTheme.caption.decorationColor, - )); + child: Container( + width: 32, + padding: EdgeInsets.all(8), + decoration: BoxDecoration( + color: Colors.white.withOpacity(0.15), + border: Border.all( + color: Colors.white.withOpacity(0.2), + ), + borderRadius: BorderRadius.circular(10), + ), + child: Image.asset( + 'assets/images/filter.png', + color: Theme.of(context).textTheme.caption.decorationColor, + ), + ) + ); return Padding( padding: const EdgeInsets.all(14.0), @@ -132,18 +144,7 @@ class IoniaManageCardsPage extends BasePage { controller: _searchController, )), SizedBox(width: 10), - Container( - width: 32, - padding: EdgeInsets.all(8), - decoration: BoxDecoration( - color: Theme.of(context).textTheme.title.backgroundColor, - border: Border.all( - color: Colors.white.withOpacity(0.2), - ), - borderRadius: BorderRadius.circular(10), - ), - child: filterIcon, - ) + filterButton ], ), ), @@ -158,16 +159,12 @@ class IoniaManageCardsPage extends BasePage { ); } - Future> showCategoryFilter( - BuildContext context, - IoniaGiftCardsListViewModel viewModel, - ) async { - return await showPopUp>( + Future showCategoryFilter(BuildContext context) async { + return showPopUp( context: context, builder: (BuildContext context) { return IoniaFilterModal( - filterViewModel: getIt.get(), - selectedCategories: viewModel.selectedFilters, + ioniaGiftCardsListViewModel: _cardsListViewModel, ); }, ); diff --git a/lib/src/screens/ionia/widgets/ionia_filter_modal.dart b/lib/src/screens/ionia/widgets/ionia_filter_modal.dart index 242b19e51..fe987cf30 100644 --- a/lib/src/screens/ionia/widgets/ionia_filter_modal.dart +++ b/lib/src/screens/ionia/widgets/ionia_filter_modal.dart @@ -1,22 +1,18 @@ -import 'package:cake_wallet/ionia/ionia_category.dart'; import 'package:cake_wallet/src/screens/ionia/widgets/rounded_checkbox.dart'; -import 'package:cake_wallet/view_model/ionia/ionia_filter_view_model.dart'; import 'package:cake_wallet/src/widgets/alert_background.dart'; import 'package:cake_wallet/typography.dart'; import 'package:cake_wallet/generated/i18n.dart'; +import 'package:cake_wallet/view_model/ionia/ionia_gift_cards_list_view_model.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; +import 'package:cake_wallet/palette.dart'; class IoniaFilterModal extends StatelessWidget { - IoniaFilterModal({ - @required this.filterViewModel, - @required this.selectedCategories, - }) { - filterViewModel.setSelectedCategories(this.selectedCategories); + IoniaFilterModal({@required this.ioniaGiftCardsListViewModel}){ + ioniaGiftCardsListViewModel.resetIoniaCategories(); } - final IoniaFilterViewModel filterViewModel; - final List selectedCategories; + final IoniaGiftCardsListViewModel ioniaGiftCardsListViewModel; @override Widget build(BuildContext context) { @@ -48,7 +44,7 @@ class IoniaFilterModal extends StatelessWidget { child: Padding( padding: const EdgeInsets.only(left: 24, right: 24), child: TextField( - onChanged: filterViewModel.onSearchFilter, + onChanged: ioniaGiftCardsListViewModel.onSearchFilter, style: textMedium( color: Theme.of(context).primaryTextTheme.title.color, ), @@ -73,13 +69,13 @@ class IoniaFilterModal extends StatelessWidget { return ListView.builder( padding: EdgeInsets.zero, shrinkWrap: true, - itemCount: filterViewModel.ioniaCategories.length, + itemCount: ioniaGiftCardsListViewModel.ioniaCategories.length, itemBuilder: (_, index) { - final category = filterViewModel.ioniaCategories[index]; + final category = ioniaGiftCardsListViewModel.ioniaCategories[index]; return Padding( padding: const EdgeInsets.only(left: 24, right: 24, bottom: 24), child: InkWell( - onTap: () => filterViewModel.selectFilter(category), + onTap: () => ioniaGiftCardsListViewModel.setSelectedFilter(category), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ @@ -98,9 +94,9 @@ class IoniaFilterModal extends StatelessWidget { ], ), Observer(builder: (_) { - final value = filterViewModel.selectedIndices; + final value = ioniaGiftCardsListViewModel.selectedIndices; return RoundedCheckbox( - value: value.contains(category.index), + value: value.contains(category), ); }), ], @@ -114,13 +110,13 @@ class IoniaFilterModal extends StatelessWidget { ), ), InkWell( - onTap: () => Navigator.pop(context, filterViewModel.selectedCategories), + onTap: () => Navigator.pop(context), child: Container( margin: EdgeInsets.only(bottom: 40), child: CircleAvatar( child: Icon( Icons.close, - color: Colors.black, + color: Palette.darkBlueCraiola, ), backgroundColor: Colors.white, ), diff --git a/lib/view_model/ionia/ionia_filter_view_model.dart b/lib/view_model/ionia/ionia_filter_view_model.dart deleted file mode 100644 index 43d2790e0..000000000 --- a/lib/view_model/ionia/ionia_filter_view_model.dart +++ /dev/null @@ -1,58 +0,0 @@ -import 'package:cake_wallet/ionia/ionia_category.dart'; -import 'package:mobx/mobx.dart'; - -part 'ionia_filter_view_model.g.dart'; - -class IoniaFilterViewModel = IoniaFilterViewModelBase with _$IoniaFilterViewModel; - -abstract class IoniaFilterViewModelBase with Store { - IoniaFilterViewModelBase() { - selectedIndices = ObservableList(); - ioniaCategories = IoniaCategory.allCategories; - } - - List get selectedCategories => ioniaCategories.where(_isSelected).toList(); - - @observable - ObservableList selectedIndices; - - @observable - List ioniaCategories; - - @action - void selectFilter(IoniaCategory ioniaCategory) { - if (ioniaCategory == IoniaCategory.all && !selectedIndices.contains(0)) { - selectedIndices.clear(); - selectedIndices.add(0); - return; - } - if (selectedIndices.contains(ioniaCategory.index) && ioniaCategory.index != 0) { - selectedIndices.remove(ioniaCategory.index); - return; - } - selectedIndices.add(ioniaCategory.index); - selectedIndices.remove(0); - } - - @action - void onSearchFilter(String text) { - if (text.isEmpty) { - ioniaCategories = IoniaCategory.allCategories; - } else { - ioniaCategories = IoniaCategory.allCategories - .where( - (e) => e.title.toLowerCase().contains(text.toLowerCase()), - ) - .toList(); - } - } - - @action - void setSelectedCategories(List selectedCategories) { - selectedIndices = ObservableList.of(selectedCategories.map((e) => e.index)); - } - - bool _isSelected(IoniaCategory ioniaCategory) { - return selectedIndices.contains(ioniaCategory.index); - } -} diff --git a/lib/view_model/ionia/ionia_gift_cards_list_view_model.dart b/lib/view_model/ionia/ionia_gift_cards_list_view_model.dart index a04fce3e4..ec603dd89 100644 --- a/lib/view_model/ionia/ionia_gift_cards_list_view_model.dart +++ b/lib/view_model/ionia/ionia_gift_cards_list_view_model.dart @@ -12,14 +12,13 @@ class IoniaGiftCardsListViewModel = IoniaGiftCardsListViewModelBase with _$Ionia abstract class IoniaGiftCardsListViewModelBase with Store { IoniaGiftCardsListViewModelBase({ @required this.ioniaService, - }) : + }) : cardState = IoniaNoCardState(), ioniaMerchants = [], + ioniaCategories = IoniaCategory.allCategories, + selectedIndices = ObservableList.of([IoniaCategory.all]), scrollOffsetFromTop = 0.0 { - selectedFilters = []; _getAuthStatus().then((value) => isLoggedIn = value); - - _getMerchants(); } final IoniaService ioniaService; @@ -28,8 +27,6 @@ abstract class IoniaGiftCardsListViewModelBase with Store { String searchString; - List selectedFilters; - @observable double scrollOffsetFromTop; @@ -48,6 +45,12 @@ abstract class IoniaGiftCardsListViewModelBase with Store { @observable bool isLoggedIn; + @observable + List ioniaCategories; + + @observable + ObservableList selectedIndices; + Future _getAuthStatus() async { return await ioniaService.isLogined(); } @@ -88,9 +91,10 @@ abstract class IoniaGiftCardsListViewModelBase with Store { } } - void _getMerchants() { + + void getMerchants() { merchantState = IoniaLoadingMerchantState(); - ioniaService.getMerchantsByFilter(categories: selectedFilters).then((value) { + ioniaService.getMerchantsByFilter(categories: selectedIndices).then((value) { value.sort((a, b) => a.legalName.toLowerCase().compareTo(b.legalName.toLowerCase())); ioniaMerchants = ioniaMerchantList = value; merchantState = IoniaLoadedMerchantState(); @@ -99,9 +103,42 @@ abstract class IoniaGiftCardsListViewModelBase with Store { } @action - void setSelectedFilter(List filters) { - selectedFilters = filters; - _getMerchants(); + void setSelectedFilter(IoniaCategory category) { + if (category == IoniaCategory.all) { + selectedIndices.clear(); + selectedIndices.add(category); + return; + } + + if (category != IoniaCategory.all) { + selectedIndices.remove(IoniaCategory.all); + } + + if (selectedIndices.contains(category)) { + selectedIndices.remove(category); + + if (selectedIndices.isEmpty) { + selectedIndices.add(IoniaCategory.all); + } + return; + } + selectedIndices.add(category); + } + + @action + void onSearchFilter(String text) { + if (text.isEmpty) { + ioniaCategories = IoniaCategory.allCategories; + } else { + ioniaCategories = IoniaCategory.allCategories + .where((e) => e.title.toLowerCase().contains(text.toLowerCase()),) + .toList(); + } + } + + @action + void resetIoniaCategories() { + ioniaCategories = IoniaCategory.allCategories; } void setScrollOffsetFromTop(double scrollOffset) {