clean up wallets list on desktop and add keys to widgets in list

This commit is contained in:
julian 2024-07-11 10:41:59 -06:00
parent 3a1b86f02d
commit 2da9c9ef85
2 changed files with 44 additions and 43 deletions

View file

@ -12,7 +12,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
import 'package:isar/isar.dart'; import 'package:isar/isar.dart';
import 'package:tuple/tuple.dart';
import '../../app_config.dart'; import '../../app_config.dart';
import '../../models/add_wallet_list_entity/sub_classes/coin_entity.dart'; import '../../models/add_wallet_list_entity/sub_classes/coin_entity.dart';
@ -60,6 +59,8 @@ class WalletsOverview extends ConsumerStatefulWidget {
ConsumerState<WalletsOverview> createState() => _EthWalletsOverviewState(); ConsumerState<WalletsOverview> createState() => _EthWalletsOverviewState();
} }
typedef WalletListItemData = ({Wallet wallet, List<EthContract> contracts});
class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> { class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> {
final isDesktop = Util.isDesktop; final isDesktop = Util.isDesktop;
@ -68,28 +69,29 @@ class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> {
String _searchString = ""; String _searchString = "";
final List<Tuple2<Wallet, List<EthContract>>> wallets = []; final Map<String, WalletListItemData> wallets = {};
List<Tuple2<Wallet, List<EthContract>>> _filter(String searchTerm) { List<WalletListItemData> _filter(String searchTerm) {
if (searchTerm.isEmpty) { if (searchTerm.isEmpty) {
return wallets; return wallets.values.toList()
..sort((a, b) => a.wallet.info.name.compareTo(b.wallet.info.name));
} }
final List<Tuple2<Wallet, List<EthContract>>> results = []; final Map<String, WalletListItemData> results = {};
final term = searchTerm.toLowerCase(); final term = searchTerm.toLowerCase();
for (final tuple in wallets) { for (final entry in wallets.entries) {
bool includeManager = false; bool includeManager = false;
// search wallet name and total balance // search wallet name and total balance
includeManager |= _elementContains(tuple.item1.info.name, term); includeManager |= _elementContains(entry.value.wallet.info.name, term);
includeManager |= _elementContains( includeManager |= _elementContains(
tuple.item1.info.cachedBalance.total.decimal.toString(), entry.value.wallet.info.cachedBalance.total.decimal.toString(),
term, term,
); );
final List<EthContract> contracts = []; final List<EthContract> contracts = [];
for (final contract in tuple.item2) { for (final contract in entry.value.contracts) {
if (_elementContains(contract.name, term)) { if (_elementContains(contract.name, term)) {
contracts.add(contract); contracts.add(contract);
} else if (_elementContains(contract.symbol, term)) { } else if (_elementContains(contract.symbol, term)) {
@ -102,11 +104,12 @@ class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> {
} }
if (includeManager || contracts.isNotEmpty) { if (includeManager || contracts.isNotEmpty) {
results.add(Tuple2(tuple.item1, contracts)); results.addEntries([entry]);
} }
} }
return results; return results.values.toList()
..sort((a, b) => a.wallet.info.name.compareTo(b.wallet.info.name));
} }
bool _elementContains(String element, String term) { bool _elementContains(String element, String term) {
@ -141,13 +144,11 @@ class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> {
} }
// add tuple to list // add tuple to list
wallets.add( wallets[data.walletId] = (
Tuple2( wallet: ref.read(pWallets).getWallet(
ref.read(pWallets).getWallet( data.walletId,
data.walletId, ),
), contracts: contracts,
contracts,
),
); );
} }
} else { } else {
@ -155,13 +156,11 @@ class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> {
for (final data in walletsData) { for (final data in walletsData) {
// desktop single coin apps may cause issues so lets just ignore the error and move on // desktop single coin apps may cause issues so lets just ignore the error and move on
try { try {
wallets.add( wallets[data.walletId] = (
Tuple2( wallet: ref.read(pWallets).getWallet(
ref.read(pWallets).getWallet( data.walletId,
data.walletId, ),
), contracts: [],
[],
),
); );
} catch (_) { } catch (_) {
// lol bandaid for single coin based apps // lol bandaid for single coin based apps
@ -315,24 +314,27 @@ class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> {
final data = _filter(_searchString); final data = _filter(_searchString);
return ListView.separated( return ListView.separated(
itemBuilder: (_, index) { itemBuilder: (_, index) {
final element = data[index]; final entry = data[index];
final wallet = entry.wallet;
if (element.item1.cryptoCurrency.hasTokenSupport) { if (wallet.cryptoCurrency.hasTokenSupport) {
if (isDesktop) { if (isDesktop) {
return DesktopExpandingWalletCard( return DesktopExpandingWalletCard(
key: Key( key: Key(
"${element.item1.info.name}_${element.item2.map((e) => e.address).join()}", "${wallet.walletId}_${entry.contracts.map((e) => e.address).join()}",
), ),
data: element, data: entry,
navigatorState: widget.navigatorState!, navigatorState: widget.navigatorState!,
); );
} else { } else {
return MasterWalletCard( return MasterWalletCard(
walletId: element.item1.walletId, key: Key(wallet.walletId),
walletId: wallet.walletId,
); );
} }
} else { } else {
return ConditionalParent( return ConditionalParent(
key: Key(wallet.walletId),
condition: isDesktop, condition: isDesktop,
builder: (child) => RoundedWhiteContainer( builder: (child) => RoundedWhiteContainer(
padding: const EdgeInsets.symmetric( padding: const EdgeInsets.symmetric(
@ -345,7 +347,7 @@ class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> {
child: child, child: child,
), ),
child: SimpleWalletCard( child: SimpleWalletCard(
walletId: element.item1.walletId, walletId: wallet.walletId,
popPrevious: widget popPrevious: widget
.overrideSimpleWalletCardPopPreviousValueWith == .overrideSimpleWalletCardPopPreviousValueWith ==
null null

View file

@ -11,19 +11,18 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
import '../../../models/isar/models/ethereum/eth_contract.dart';
import '../../../pages/wallets_view/wallets_overview.dart';
import '../../../themes/stack_colors.dart'; import '../../../themes/stack_colors.dart';
import '../../../utilities/assets.dart'; import '../../../utilities/assets.dart';
import '../../../utilities/constants.dart'; import '../../../utilities/constants.dart';
import '../../../utilities/text_styles.dart'; import '../../../utilities/text_styles.dart';
import '../../../wallets/wallet/wallet.dart';
import '../../../widgets/animated_widgets/rotate_icon.dart'; import '../../../widgets/animated_widgets/rotate_icon.dart';
import '../../../widgets/expandable.dart'; import '../../../widgets/expandable.dart';
import '../../../widgets/rounded_white_container.dart'; import '../../../widgets/rounded_white_container.dart';
import '../../../widgets/wallet_card.dart'; import '../../../widgets/wallet_card.dart';
import '../../../widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart'; import '../../../widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart';
import '../../../widgets/wallet_info_row/sub_widgets/wallet_info_row_coin_icon.dart'; import '../../../widgets/wallet_info_row/sub_widgets/wallet_info_row_coin_icon.dart';
import 'package:tuple/tuple.dart';
class DesktopExpandingWalletCard extends StatefulWidget { class DesktopExpandingWalletCard extends StatefulWidget {
const DesktopExpandingWalletCard({ const DesktopExpandingWalletCard({
@ -32,7 +31,7 @@ class DesktopExpandingWalletCard extends StatefulWidget {
required this.navigatorState, required this.navigatorState,
}); });
final Tuple2<Wallet, List<EthContract>> data; final WalletListItemData data;
final NavigatorState navigatorState; final NavigatorState navigatorState;
@override @override
@ -48,9 +47,9 @@ class _DesktopExpandingWalletCardState
@override @override
void initState() { void initState() {
if (widget.data.item1.cryptoCurrency.hasTokenSupport) { if (widget.data.wallet.cryptoCurrency.hasTokenSupport) {
tokenContractAddresses.addAll( tokenContractAddresses.addAll(
widget.data.item2.map((e) => e.address), widget.data.contracts.map((e) => e.address),
); );
} }
@ -63,7 +62,7 @@ class _DesktopExpandingWalletCardState
padding: EdgeInsets.zero, padding: EdgeInsets.zero,
borderColor: Theme.of(context).extension<StackColors>()!.backgroundAppBar, borderColor: Theme.of(context).extension<StackColors>()!.backgroundAppBar,
child: Expandable( child: Expandable(
initialState: widget.data.item1.cryptoCurrency.hasTokenSupport initialState: widget.data.wallet.cryptoCurrency.hasTokenSupport
? ExpandableState.expanded ? ExpandableState.expanded
: ExpandableState.collapsed, : ExpandableState.collapsed,
controller: expandableController, controller: expandableController,
@ -89,13 +88,13 @@ class _DesktopExpandingWalletCardState
child: Row( child: Row(
children: [ children: [
WalletInfoCoinIcon( WalletInfoCoinIcon(
coin: widget.data.item1.info.coin, coin: widget.data.wallet.info.coin,
), ),
const SizedBox( const SizedBox(
width: 12, width: 12,
), ),
Text( Text(
widget.data.item1.info.name, widget.data.wallet.info.name,
style: STextStyles.desktopTextExtraSmall(context) style: STextStyles.desktopTextExtraSmall(context)
.copyWith( .copyWith(
color: Theme.of(context) color: Theme.of(context)
@ -109,7 +108,7 @@ class _DesktopExpandingWalletCardState
Expanded( Expanded(
flex: 4, flex: 4,
child: WalletInfoRowBalance( child: WalletInfoRowBalance(
walletId: widget.data.item1.walletId, walletId: widget.data.wallet.walletId,
), ),
), ),
], ],
@ -173,7 +172,7 @@ class _DesktopExpandingWalletCardState
bottom: 14, bottom: 14,
), ),
child: SimpleWalletCard( child: SimpleWalletCard(
walletId: widget.data.item1.walletId, walletId: widget.data.wallet.walletId,
popPrevious: true, popPrevious: true,
desktopNavigatorState: widget.navigatorState, desktopNavigatorState: widget.navigatorState,
), ),
@ -187,7 +186,7 @@ class _DesktopExpandingWalletCardState
bottom: 14, bottom: 14,
), ),
child: SimpleWalletCard( child: SimpleWalletCard(
walletId: widget.data.item1.walletId, walletId: widget.data.wallet.walletId,
contractAddress: e, contractAddress: e,
popPrevious: true, popPrevious: true,
desktopNavigatorState: widget.navigatorState, desktopNavigatorState: widget.navigatorState,