Skip to content

Commit

Permalink
Feature: Default network token display (#11)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aknowak01 authored Mar 28, 2024
1 parent 71b9cf3 commit 9907638
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 9 deletions.
16 changes: 11 additions & 5 deletions lib/blocs/widgets/kira/kira_list/abstract_list/a_list_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,17 @@ abstract class AListBloc<T extends AListItem> extends Bloc<AListEvent, AListStat
return listItems;
}
SortOption<T> activeSortOption = sortBloc!.state.activeSortOption;
List<T> favouritesList = favouritesBloc?.favouritesList ?? <T>[];
Set<T> uniqueListItems = <T>{
...activeSortOption.sort(filterList(favouritesList.toSet().toList())),
...activeSortOption.sort(listItems),
};
List<T> favouritesList = favouritesBloc?.favouritesList.toSet().toList() ?? <T>[];
List<T> sortedList = activeSortOption.sort(listItems);

Set<T> uniqueListItems = activeSortOption.sort(filterList(favouritesList)).toSet();

for (T item in sortedList) {
if (uniqueListItems.contains(item) == false) {
uniqueListItems.add(item);
}
}

return uniqueListItems.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ class PageData<T> extends Equatable {
cacheExpirationDateTime = null,
lastPageBool = false;

PageData<T> copyWith({
List<T>? listItems,
bool? lastPageBool,
DateTime? blockDateTime,
DateTime? cacheExpirationDateTime,
}) {
return PageData<T>(
listItems: listItems ?? this.listItems,
lastPageBool: lastPageBool ?? this.lastPageBool,
blockDateTime: blockDateTime ?? this.blockDateTime,
cacheExpirationDateTime: cacheExpirationDateTime ?? this.cacheExpirationDateTime,
);
}

@override
List<Object?> get props => <Object?>[listItems, lastPageBool, blockDateTime, cacheExpirationDateTime];
}
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<BalanceModel> {
Expand Down Expand Up @@ -45,6 +49,25 @@ class BalancesListController implements IListController<BalanceModel> {
QueryBalanceReq(address: walletAddress.bech32Address, limit: paginationDetailsModel.limit, offset: paginationDetailsModel.offset),
forceRequestBool: forceRequestBool,
);
return balancesPageData;

if (balancesPageData.listItems.isEmpty) {
Set<String> favouriteBalances = favouriteCacheService.getAll();
TokenAliasModel defaultTokenAliasModel = globalLocator<NetworkModuleBloc>().tokenDefaultDenomModel.defaultTokenAliasModel!;

BalanceModel defaultBalanceModel = BalanceModel(
tokenAmountModel: TokenAmountModel(
tokenAliasModel: defaultTokenAliasModel,
defaultDenominationAmount: Decimal.fromInt(0),
),
favourite: favouriteBalances.contains(defaultTokenAliasModel.defaultTokenDenominationModel.name),
);

return balancesPageData.copyWith(
listItems: <BalanceModel>[defaultBalanceModel],
lastPageBool: true,
);
} else {
return balancesPageData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -52,13 +54,15 @@ class _BalanceListItemBuilder extends State<BalanceListItemBuilder> {
favouritePressedCallback: _onFavouriteButtonPressed,
onSendButtonPressed: _handleSendButtonPressed,
hoverNotifier: hoverNotifier,
sendButtonActiveBool: widget.sendButtonActiveBool,
),
mediumScreen: BalanceListItemMobile(
balanceModel: widget.balanceModel,
expansionChangedCallback: _onExpansionChanged,
favouritePressedCallback: _onFavouriteButtonPressed,
hoverNotifier: hoverNotifier,
onSendButtonPressed: _handleSendButtonPressed,
sendButtonActiveBool: widget.sendButtonActiveBool,
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ class BalanceListItemDesktop extends StatelessWidget {
final ValueNotifier<bool> hoverNotifier;
final VoidCallback onSendButtonPressed;
final double sectionsSpace;
final bool sendButtonActiveBool;

const BalanceListItemDesktop({
required this.balanceModel,
required this.expansionChangedCallback,
required this.favouritePressedCallback,
required this.hoverNotifier,
required this.onSendButtonPressed,
required this.sendButtonActiveBool,
this.sectionsSpace = 15,
Key? key,
}) : super(key: key);
Expand All @@ -38,6 +40,7 @@ class BalanceListItemDesktop extends StatelessWidget {
hoverNotifier: hoverNotifier,
favouritePressedCallback: favouritePressedCallback,
onSendButtonPressed: onSendButtonPressed,
sendButtonActiveBool: sendButtonActiveBool,
sectionsSpace: sectionsSpace,
),
children: <Widget>[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ class BalanceListItemDesktopTitle extends StatelessWidget {
final ValueChanged<bool> favouritePressedCallback;
final ValueNotifier<bool> hoverNotifier;
final VoidCallback onSendButtonPressed;
final bool sendButtonActiveBool;

const BalanceListItemDesktopTitle({
required this.sectionsSpace,
required this.balanceModel,
required this.favouritePressedCallback,
required this.hoverNotifier,
required this.onSendButtonPressed,
required this.sendButtonActiveBool,
Key? key,
}) : super(key: key);

Expand Down Expand Up @@ -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,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ class BalanceListItemMobile extends StatelessWidget {
final ValueChanged<bool> favouritePressedCallback;
final ValueNotifier<bool> hoverNotifier;
final VoidCallback onSendButtonPressed;
final bool sendButtonActiveBool;

const BalanceListItemMobile({
required this.balanceModel,
required this.expansionChangedCallback,
required this.favouritePressedCallback,
required this.hoverNotifier,
required this.onSendButtonPressed,
required this.sendButtonActiveBool,
Key? key,
}) : super(key: key);

Expand Down Expand Up @@ -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,
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -57,6 +58,7 @@ class _BalancePage extends State<BalancePage> {

return SliverInfinityList<BalanceModel>(
itemBuilder: (BalanceModel balanceModel) => BalanceListItemBuilder(
sendButtonActiveBool: balanceModel.tokenAmountModel.getAmountInDefaultDenomination() != Decimal.zero,
key: Key('${balanceModel.hashCode}'),
balanceModel: balanceModel,
scrollController: widget.parentScrollController,
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 9907638

Please sign in to comment.