stack_wallet/lib/widgets/wallet_card.dart

187 lines
6 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
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/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-07-01 20:09:38 +00:00
import 'package:stackwallet/utilities/logger.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/wallets/wallet/wallet.dart';
2023-04-03 22:22:40 +00:00
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
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,
Wallet wallet,
EthContract contract,
) async {
ref.read(tokenServiceStateProvider.state).state = EthTokenWallet(
token: contract,
secureStore: ref.read(secureStoreProvider),
// TODO: [prio=high] FIX THIS BAD as CAST
ethWallet: 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 wallet = ref.read(pWallets).getWallet(walletId);
if (wallet.info.coin == Coin.monero || wallet.info.coin == Coin.wownero) {
await wallet.init();
2023-04-03 22:22:40 +00:00
}
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: walletId,
2023-04-03 22:22:40 +00:00
),
);
}
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, wallet, contract),
context: desktopNavigatorState?.context ?? context,
2023-04-03 22:22:40 +00:00
opaqueBG: true,
message: "Loading ${contract.name}",
2023-07-01 20:09:38 +00:00
isDesktop: Util.isDesktop,
2023-04-03 22:22:40 +00:00
);
if (!success!) {
2023-07-01 20:09:38 +00:00
// TODO: show error dialog here?
Logging.instance.log(
"Failed to load token wallet for $contract",
level: LogLevel.Error,
);
return;
}
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,
2023-10-17 17:21:53 +00:00
arguments: (walletId: walletId, popPrevious: !Util.isDesktop),
2023-04-03 22:22:40 +00:00
);
}
}
}
}
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
);
}
}