From 990763805999b018cdff690cfa17e1afbe7b3207 Mon Sep 17 00:00:00 2001 From: Arkadiusz Nowak <78044505+aknowak01@users.noreply.github.com> Date: Thu, 28 Mar 2024 15:16:37 +0100 Subject: [PATCH] Feature: Default network token display (#11) Branch adds display of default network token in balance_page.dart when account doesn't have any tokens. List of changes: - modified balances_list_controller.dart by updating getPageData() method with check-up of account balances if there are any tokens. If there are no tokens then default network token is added with value equal zero. - modified a_list_block.dart by updating sort method for proper sorting because previously if the default balance was in favorites, corresponding non-favorite element would still be added to Set and overwrite favorite element. --- .../kira_list/abstract_list/a_list_bloc.dart | 16 ++++++++---- .../abstract_list/models/page_data.dart | 14 +++++++++++ .../balances_list_controller.dart | 25 ++++++++++++++++++- .../balance_list_item_builder.dart | 4 +++ .../desktop/balance_list_item_desktop.dart | 3 +++ .../balance_list_item_desktop_title.dart | 5 +++- .../mobile/balance_list_item_mobile.dart | 5 +++- .../balance_page/balance_page.dart | 2 ++ pubspec.yaml | 2 +- 9 files changed, 67 insertions(+), 9 deletions(-) diff --git a/lib/blocs/widgets/kira/kira_list/abstract_list/a_list_bloc.dart b/lib/blocs/widgets/kira/kira_list/abstract_list/a_list_bloc.dart index def5ea36..6e8ee9d2 100644 --- a/lib/blocs/widgets/kira/kira_list/abstract_list/a_list_bloc.dart +++ b/lib/blocs/widgets/kira/kira_list/abstract_list/a_list_bloc.dart @@ -105,11 +105,17 @@ abstract class AListBloc extends Bloc activeSortOption = sortBloc!.state.activeSortOption; - List favouritesList = favouritesBloc?.favouritesList ?? []; - Set uniqueListItems = { - ...activeSortOption.sort(filterList(favouritesList.toSet().toList())), - ...activeSortOption.sort(listItems), - }; + List favouritesList = favouritesBloc?.favouritesList.toSet().toList() ?? []; + List sortedList = activeSortOption.sort(listItems); + + Set uniqueListItems = activeSortOption.sort(filterList(favouritesList)).toSet(); + + for (T item in sortedList) { + if (uniqueListItems.contains(item) == false) { + uniqueListItems.add(item); + } + } + return uniqueListItems.toList(); } diff --git a/lib/blocs/widgets/kira/kira_list/abstract_list/models/page_data.dart b/lib/blocs/widgets/kira/kira_list/abstract_list/models/page_data.dart index 835e2c0f..c7047576 100644 --- a/lib/blocs/widgets/kira/kira_list/abstract_list/models/page_data.dart +++ b/lib/blocs/widgets/kira/kira_list/abstract_list/models/page_data.dart @@ -19,6 +19,20 @@ class PageData extends Equatable { cacheExpirationDateTime = null, lastPageBool = false; + PageData copyWith({ + List? listItems, + bool? lastPageBool, + DateTime? blockDateTime, + DateTime? cacheExpirationDateTime, + }) { + return PageData( + listItems: listItems ?? this.listItems, + lastPageBool: lastPageBool ?? this.lastPageBool, + blockDateTime: blockDateTime ?? this.blockDateTime, + cacheExpirationDateTime: cacheExpirationDateTime ?? this.cacheExpirationDateTime, + ); + } + @override List get props => [listItems, lastPageBool, blockDateTime, cacheExpirationDateTime]; } diff --git a/lib/shared/controllers/menu/my_account_page/balances_page/balances_list_controller.dart b/lib/shared/controllers/menu/my_account_page/balances_page/balances_list_controller.dart index 34293dad..a4d1c450 100644 --- a/lib/shared/controllers/menu/my_account_page/balances_page/balances_list_controller.dart +++ b/lib/shared/controllers/menu/my_account_page/balances_page/balances_list_controller.dart @@ -1,3 +1,5 @@ +import 'package:decimal/decimal.dart'; +import 'package:miro/blocs/generic/network_module/network_module_bloc.dart'; import 'package:miro/blocs/widgets/kira/kira_list/abstract_list/controllers/i_list_controller.dart'; import 'package:miro/blocs/widgets/kira/kira_list/abstract_list/models/page_data.dart'; import 'package:miro/config/locator.dart'; @@ -6,6 +8,8 @@ import 'package:miro/infra/services/api_kira/query_balance_service.dart'; import 'package:miro/infra/services/cache/favourites_cache_service.dart'; import 'package:miro/shared/models/balances/balance_model.dart'; import 'package:miro/shared/models/list/pagination_details_model.dart'; +import 'package:miro/shared/models/tokens/token_alias_model.dart'; +import 'package:miro/shared/models/tokens/token_amount_model.dart'; import 'package:miro/shared/models/wallet/wallet_address.dart'; class BalancesListController implements IListController { @@ -45,6 +49,25 @@ class BalancesListController implements IListController { QueryBalanceReq(address: walletAddress.bech32Address, limit: paginationDetailsModel.limit, offset: paginationDetailsModel.offset), forceRequestBool: forceRequestBool, ); - return balancesPageData; + + if (balancesPageData.listItems.isEmpty) { + Set favouriteBalances = favouriteCacheService.getAll(); + TokenAliasModel defaultTokenAliasModel = globalLocator().tokenDefaultDenomModel.defaultTokenAliasModel!; + + BalanceModel defaultBalanceModel = BalanceModel( + tokenAmountModel: TokenAmountModel( + tokenAliasModel: defaultTokenAliasModel, + defaultDenominationAmount: Decimal.fromInt(0), + ), + favourite: favouriteBalances.contains(defaultTokenAliasModel.defaultTokenDenominationModel.name), + ); + + return balancesPageData.copyWith( + listItems: [defaultBalanceModel], + lastPageBool: true, + ); + } else { + return balancesPageData; + } } } diff --git a/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/balance_list_item_builder.dart b/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/balance_list_item_builder.dart index 8a10b1fc..e6e426bf 100644 --- a/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/balance_list_item_builder.dart +++ b/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/balance_list_item_builder.dart @@ -18,10 +18,12 @@ import 'package:miro/views/widgets/generic/responsive/responsive_widget.dart'; class BalanceListItemBuilder extends StatefulWidget { final BalanceModel balanceModel; final ScrollController scrollController; + final bool sendButtonActiveBool; const BalanceListItemBuilder({ required this.balanceModel, required this.scrollController, + required this.sendButtonActiveBool, Key? key, }) : super(key: key); @@ -52,6 +54,7 @@ class _BalanceListItemBuilder extends State { favouritePressedCallback: _onFavouriteButtonPressed, onSendButtonPressed: _handleSendButtonPressed, hoverNotifier: hoverNotifier, + sendButtonActiveBool: widget.sendButtonActiveBool, ), mediumScreen: BalanceListItemMobile( balanceModel: widget.balanceModel, @@ -59,6 +62,7 @@ class _BalanceListItemBuilder extends State { favouritePressedCallback: _onFavouriteButtonPressed, hoverNotifier: hoverNotifier, onSendButtonPressed: _handleSendButtonPressed, + sendButtonActiveBool: widget.sendButtonActiveBool, ), ), ); diff --git a/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/desktop/balance_list_item_desktop.dart b/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/desktop/balance_list_item_desktop.dart index 384d6ffd..22df3030 100644 --- a/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/desktop/balance_list_item_desktop.dart +++ b/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/desktop/balance_list_item_desktop.dart @@ -11,6 +11,7 @@ class BalanceListItemDesktop extends StatelessWidget { final ValueNotifier hoverNotifier; final VoidCallback onSendButtonPressed; final double sectionsSpace; + final bool sendButtonActiveBool; const BalanceListItemDesktop({ required this.balanceModel, @@ -18,6 +19,7 @@ class BalanceListItemDesktop extends StatelessWidget { required this.favouritePressedCallback, required this.hoverNotifier, required this.onSendButtonPressed, + required this.sendButtonActiveBool, this.sectionsSpace = 15, Key? key, }) : super(key: key); @@ -38,6 +40,7 @@ class BalanceListItemDesktop extends StatelessWidget { hoverNotifier: hoverNotifier, favouritePressedCallback: favouritePressedCallback, onSendButtonPressed: onSendButtonPressed, + sendButtonActiveBool: sendButtonActiveBool, sectionsSpace: sectionsSpace, ), children: [ diff --git a/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/desktop/balance_list_item_desktop_title.dart b/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/desktop/balance_list_item_desktop_title.dart index 5ba42af0..10c579e8 100644 --- a/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/desktop/balance_list_item_desktop_title.dart +++ b/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/desktop/balance_list_item_desktop_title.dart @@ -11,6 +11,7 @@ class BalanceListItemDesktopTitle extends StatelessWidget { final ValueChanged favouritePressedCallback; final ValueNotifier hoverNotifier; final VoidCallback onSendButtonPressed; + final bool sendButtonActiveBool; const BalanceListItemDesktopTitle({ required this.sectionsSpace, @@ -18,6 +19,7 @@ class BalanceListItemDesktopTitle extends StatelessWidget { required this.favouritePressedCallback, required this.hoverNotifier, required this.onSendButtonPressed, + required this.sendButtonActiveBool, Key? key, }) : super(key: key); @@ -62,7 +64,8 @@ class BalanceListItemDesktopTitle extends StatelessWidget { KiraOutlinedButton( height: 40, width: 70, - onPressed: onSendButtonPressed, + disabled: sendButtonActiveBool == false, + onPressed: sendButtonActiveBool ? onSendButtonPressed : null, title: S.of(context).balancesSend, ), ], diff --git a/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/mobile/balance_list_item_mobile.dart b/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/mobile/balance_list_item_mobile.dart index c71cd016..17181595 100644 --- a/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/mobile/balance_list_item_mobile.dart +++ b/lib/views/pages/menu/my_account_page/balance_page/balance_list_item/mobile/balance_list_item_mobile.dart @@ -12,6 +12,7 @@ class BalanceListItemMobile extends StatelessWidget { final ValueChanged favouritePressedCallback; final ValueNotifier hoverNotifier; final VoidCallback onSendButtonPressed; + final bool sendButtonActiveBool; const BalanceListItemMobile({ required this.balanceModel, @@ -19,6 +20,7 @@ class BalanceListItemMobile extends StatelessWidget { required this.favouritePressedCallback, required this.hoverNotifier, required this.onSendButtonPressed, + required this.sendButtonActiveBool, Key? key, }) : super(key: key); @@ -51,7 +53,8 @@ class BalanceListItemMobile extends StatelessWidget { child: KiraOutlinedButton( height: 40, width: double.infinity, - onPressed: onSendButtonPressed, + disabled: sendButtonActiveBool == false, + onPressed: sendButtonActiveBool ? onSendButtonPressed : null, title: S.of(context).balancesSend, ), ), diff --git a/lib/views/pages/menu/my_account_page/balance_page/balance_page.dart b/lib/views/pages/menu/my_account_page/balance_page/balance_page.dart index 0f48b96c..ad262c53 100644 --- a/lib/views/pages/menu/my_account_page/balance_page/balance_page.dart +++ b/lib/views/pages/menu/my_account_page/balance_page/balance_page.dart @@ -1,3 +1,4 @@ +import 'package:decimal/decimal.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:miro/blocs/widgets/kira/kira_list/favourites/favourites_bloc.dart'; @@ -57,6 +58,7 @@ class _BalancePage extends State { return SliverInfinityList( itemBuilder: (BalanceModel balanceModel) => BalanceListItemBuilder( + sendButtonActiveBool: balanceModel.tokenAmountModel.getAmountInDefaultDenomination() != Decimal.zero, key: Key('${balanceModel.hashCode}'), balanceModel: balanceModel, scrollController: widget.parentScrollController, diff --git a/pubspec.yaml b/pubspec.yaml index 1848c6d7..f548023f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -12,7 +12,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 1.27.0 +version: 1.28.0 environment: sdk: ">=3.1.3"