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

View file

@ -11,19 +11,18 @@
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_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 '../../../utilities/assets.dart';
import '../../../utilities/constants.dart';
import '../../../utilities/text_styles.dart';
import '../../../wallets/wallet/wallet.dart';
import '../../../widgets/animated_widgets/rotate_icon.dart';
import '../../../widgets/expandable.dart';
import '../../../widgets/rounded_white_container.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_coin_icon.dart';
import 'package:tuple/tuple.dart';
class DesktopExpandingWalletCard extends StatefulWidget {
const DesktopExpandingWalletCard({
@ -32,7 +31,7 @@ class DesktopExpandingWalletCard extends StatefulWidget {
required this.navigatorState,
});
final Tuple2<Wallet, List<EthContract>> data;
final WalletListItemData data;
final NavigatorState navigatorState;
@override
@ -48,9 +47,9 @@ class _DesktopExpandingWalletCardState
@override
void initState() {
if (widget.data.item1.cryptoCurrency.hasTokenSupport) {
if (widget.data.wallet.cryptoCurrency.hasTokenSupport) {
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,
borderColor: Theme.of(context).extension<StackColors>()!.backgroundAppBar,
child: Expandable(
initialState: widget.data.item1.cryptoCurrency.hasTokenSupport
initialState: widget.data.wallet.cryptoCurrency.hasTokenSupport
? ExpandableState.expanded
: ExpandableState.collapsed,
controller: expandableController,
@ -89,13 +88,13 @@ class _DesktopExpandingWalletCardState
child: Row(
children: [
WalletInfoCoinIcon(
coin: widget.data.item1.info.coin,
coin: widget.data.wallet.info.coin,
),
const SizedBox(
width: 12,
),
Text(
widget.data.item1.info.name,
widget.data.wallet.info.name,
style: STextStyles.desktopTextExtraSmall(context)
.copyWith(
color: Theme.of(context)
@ -109,7 +108,7 @@ class _DesktopExpandingWalletCardState
Expanded(
flex: 4,
child: WalletInfoRowBalance(
walletId: widget.data.item1.walletId,
walletId: widget.data.wallet.walletId,
),
),
],
@ -173,7 +172,7 @@ class _DesktopExpandingWalletCardState
bottom: 14,
),
child: SimpleWalletCard(
walletId: widget.data.item1.walletId,
walletId: widget.data.wallet.walletId,
popPrevious: true,
desktopNavigatorState: widget.navigatorState,
),
@ -187,7 +186,7 @@ class _DesktopExpandingWalletCardState
bottom: 14,
),
child: SimpleWalletCard(
walletId: widget.data.item1.walletId,
walletId: widget.data.wallet.walletId,
contractAddress: e,
popPrevious: true,
desktopNavigatorState: widget.navigatorState,