stack_wallet/lib/widgets/wallet_card.dart

184 lines
5.8 KiB
Dart
Raw Normal View History

import 'dart:async';
2022-08-26 08:11:35 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:stackwallet/models/isar/models/ethereum/eth_contract.dart';
import 'package:stackwallet/pages/token_view/token_view.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/pages/wallet_view/wallet_view.dart';
2023-04-03 23:05:48 +00:00
import 'package:stackwallet/pages_desktop_specific/my_stack_view/wallet_view/desktop_token_view.dart';
2023-04-03 22:22:40 +00:00
import 'package:stackwallet/pages_desktop_specific/my_stack_view/wallet_view/desktop_wallet_view.dart';
import 'package:stackwallet/providers/db/main_db_provider.dart';
import 'package:stackwallet/providers/global/secure_store_provider.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/providers/providers.dart';
import 'package:stackwallet/services/coins/ethereum/ethereum_wallet.dart';
import 'package:stackwallet/services/coins/manager.dart';
import 'package:stackwallet/services/ethereum/ethereum_token_service.dart';
import 'package:stackwallet/services/transaction_notification_tracker.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/utilities/constants.dart';
2023-01-02 17:08:13 +00:00
import 'package:stackwallet/utilities/enums/coin_enum.dart';
2023-03-28 22:21:26 +00:00
import 'package:stackwallet/utilities/show_loading.dart';
2023-04-03 22:22:40 +00:00
import 'package:stackwallet/utilities/util.dart';
import 'package:stackwallet/widgets/conditional_parent.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
import 'package:stackwallet/widgets/dialogs/basic_dialog.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/widgets/rounded_white_container.dart';
2022-09-19 17:32:33 +00:00
import 'package:stackwallet/widgets/wallet_info_row/wallet_info_row.dart';
2022-08-26 08:11:35 +00:00
import 'package:tuple/tuple.dart';
class SimpleWalletCard extends ConsumerWidget {
const SimpleWalletCard({
2022-08-26 08:11:35 +00:00
Key? key,
required this.walletId,
this.contractAddress,
2022-08-26 08:11:35 +00:00
this.popPrevious = false,
2023-04-03 22:22:40 +00:00
this.desktopNavigatorState,
2022-08-26 08:11:35 +00:00
}) : super(key: key);
final String walletId;
final String? contractAddress;
2022-08-26 08:11:35 +00:00
final bool popPrevious;
2023-04-03 22:22:40 +00:00
final NavigatorState? desktopNavigatorState;
2022-08-26 08:11:35 +00:00
Future<bool> _loadTokenWallet(
BuildContext context,
WidgetRef ref,
Manager manager,
EthContract contract,
) async {
ref.read(tokenServiceStateProvider.state).state = EthTokenWallet(
token: contract,
secureStore: ref.read(secureStoreProvider),
ethWallet: manager.wallet as EthereumWallet,
tracker: TransactionNotificationTracker(
walletId: walletId,
),
);
try {
await ref.read(tokenServiceProvider)!.initialize();
return true;
} catch (_) {
await showDialog<void>(
barrierDismissible: false,
context: context,
builder: (context) => BasicDialog(
title: "Failed to load token data",
desktopHeight: double.infinity,
desktopWidth: 450,
rightButton: PrimaryButton(
label: "OK",
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).pop();
if (desktopNavigatorState == null) {
Navigator.of(context).pop();
}
},
),
),
);
return false;
}
}
2023-04-03 22:22:40 +00:00
void _openWallet(BuildContext context, WidgetRef ref) async {
final nav = Navigator.of(context);
final manager =
ref.read(walletsChangeNotifierProvider).getManager(walletId);
if (manager.coin == Coin.monero || manager.coin == Coin.wownero) {
await manager.initializeExisting();
}
if (context.mounted) {
if (popPrevious) nav.pop();
if (desktopNavigatorState != null) {
unawaited(
desktopNavigatorState!.pushNamed(
DesktopWalletView.routeName,
arguments: walletId,
2022-08-26 08:11:35 +00:00
),
2023-04-03 22:22:40 +00:00
);
} else {
unawaited(
nav.pushNamed(
WalletView.routeName,
arguments: Tuple2(
walletId,
ref
.read(walletsChangeNotifierProvider)
.getManagerProvider(walletId),
),
),
);
}
2023-03-28 22:21:26 +00:00
2023-04-03 22:22:40 +00:00
if (contractAddress != null) {
final contract =
ref.read(mainDBProvider).getEthContractSync(contractAddress!)!;
final success = await showLoading<bool>(
whileFuture: _loadTokenWallet(
desktopNavigatorState?.context ?? context,
ref,
manager,
contract),
context: desktopNavigatorState?.context ?? context,
2023-04-03 22:22:40 +00:00
opaqueBG: true,
message: "Loading ${contract.name}",
);
if (!success) {
return;
}
if (desktopNavigatorState == null) {
// pop loading
nav.pop();
}
2023-04-03 22:22:40 +00:00
if (desktopNavigatorState != null) {
2023-04-03 23:05:48 +00:00
await desktopNavigatorState!.pushNamed(
DesktopTokenView.routeName,
arguments: walletId,
);
2023-04-03 22:22:40 +00:00
} else {
await nav.pushNamed(
TokenView.routeName,
arguments: walletId,
);
}
}
}
}
2023-03-28 22:21:26 +00:00
2023-04-03 22:22:40 +00:00
@override
Widget build(BuildContext context, WidgetRef ref) {
return ConditionalParent(
condition: !Util.isDesktop,
builder: (child) => RoundedWhiteContainer(
padding: const EdgeInsets.all(0),
child: MaterialButton(
// splashColor: Theme.of(context).extension<StackColors>()!.highlight,
key: Key("walletsSheetItemButtonKey_$walletId"),
padding: const EdgeInsets.all(10),
2023-04-03 22:22:40 +00:00
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
),
onPressed: () => _openWallet(context, ref),
child: child,
2022-08-26 08:11:35 +00:00
),
),
2023-04-03 22:22:40 +00:00
child: WalletInfoRow(
walletId: walletId,
contractAddress: contractAddress,
onPressedDesktop:
Util.isDesktop ? () => _openWallet(context, ref) : null,
),
2022-08-26 08:11:35 +00:00
);
}
}