Merge pull request #911 from cypherstack/desktopreceive

Add address type dropdown to desktop receive view
This commit is contained in:
julian-CStack 2024-07-04 11:20:52 -06:00 committed by GitHub
commit a8a5a3eb80
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -29,11 +29,15 @@ import '../../../../utilities/address_utils.dart';
import '../../../../utilities/assets.dart';
import '../../../../utilities/clipboard_interface.dart';
import '../../../../utilities/constants.dart';
import '../../../../utilities/enums/derive_path_type_enum.dart';
import '../../../../utilities/text_styles.dart';
import '../../../../utilities/util.dart';
import '../../../../wallets/crypto_currency/crypto_currency.dart';
import '../../../../wallets/isar/providers/eth/current_token_wallet_provider.dart';
import '../../../../wallets/isar/providers/wallet_info_provider.dart';
import '../../../../wallets/wallet/impl/bitcoin_wallet.dart';
import '../../../../wallets/wallet/intermediate/bip39_hd_wallet.dart';
import '../../../../wallets/wallet/wallet_mixin_interfaces/bcash_interface.dart';
import '../../../../wallets/wallet/wallet_mixin_interfaces/multi_address_interface.dart';
import '../../../../wallets/wallet/wallet_mixin_interfaces/spark_interface.dart';
import '../../../../widgets/conditional_parent.dart';
@ -65,10 +69,13 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
late final String walletId;
late final ClipboardInterface clipboard;
late final bool supportsSpark;
late final bool showMultiType;
String? _sparkAddress;
String? _qrcodeContent;
bool _showSparkAddress = true;
int _currentIndex = 0;
final List<AddressType> _walletAddressTypes = [];
final Map<AddressType, String> _addressMap = {};
final Map<AddressType, StreamSubscription<Address?>> _addressSubMap = {};
Future<void> generateNewAddress() async {
final wallet = ref.read(pWallets).getWallet(walletId);
@ -95,12 +102,31 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
),
);
await wallet.generateNewReceivingAddress();
final Address? address;
if (wallet is Bip39HDWallet && wallet is! BCashInterface) {
final type = DerivePathType.values.firstWhere(
(e) => e.getAddressType() == _walletAddressTypes[_currentIndex],
);
address = await wallet.generateNextReceivingAddress(
derivePathType: type,
);
await ref.read(mainDBProvider).isar.writeTxn(() async {
await ref.read(mainDBProvider).isar.addresses.put(address!);
});
} else {
await wallet.generateNewReceivingAddress();
address = null;
}
shouldPop = true;
if (mounted) {
Navigator.of(context, rootNavigator: true).pop();
setState(() {
_addressMap[_walletAddressTypes[_currentIndex]] =
address?.value ?? ref.read(pWalletReceivingAddress(walletId));
});
}
}
}
@ -139,11 +165,9 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
if (mounted) {
Navigator.of(context, rootNavigator: true).pop();
if (_sparkAddress != address.value) {
setState(() {
_sparkAddress = address.value;
});
}
setState(() {
_addressMap[AddressType.spark] = address.value;
});
}
}
}
@ -155,29 +179,56 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
walletId = widget.walletId;
coin = ref.read(pWalletInfo(walletId)).coin;
clipboard = widget.clipboard;
final wallet = ref.read(pWallets).getWallet(walletId);
supportsSpark = ref.read(pWallets).getWallet(walletId) is SparkInterface;
showMultiType = supportsSpark ||
ref.read(pWallets).getWallet(walletId) is MultiAddressInterface;
if (supportsSpark) {
_streamSub = ref
.read(mainDBProvider)
.isar
.addresses
.where()
.walletIdEqualTo(walletId)
.filter()
.typeEqualTo(AddressType.spark)
.sortByDerivationIndexDesc()
.findFirst()
.asStream()
.listen((event) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
setState(() {
_sparkAddress = event?.value;
});
}
_walletAddressTypes.add(wallet.info.mainAddressType);
if (showMultiType) {
if (supportsSpark) {
_walletAddressTypes.insert(0, AddressType.spark);
} else {
_walletAddressTypes.addAll(
(wallet as Bip39HDWallet)
.supportedAddressTypes
.where((e) => e != wallet.info.mainAddressType),
);
}
}
if (_walletAddressTypes.length > 1 && wallet is BitcoinWallet) {
_walletAddressTypes.removeWhere((e) => e == AddressType.p2pkh);
}
_addressMap[_walletAddressTypes[_currentIndex]] =
ref.read(pWalletReceivingAddress(walletId));
if (showMultiType) {
for (final type in _walletAddressTypes) {
_addressSubMap[type] = ref
.read(mainDBProvider)
.isar
.addresses
.where()
.walletIdEqualTo(walletId)
.filter()
.typeEqualTo(type)
.sortByDerivationIndexDesc()
.findFirst()
.asStream()
.listen((event) {
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) {
setState(() {
_addressMap[type] =
event?.value ?? _addressMap[type] ?? "[No address yet]";
});
}
});
});
});
}
}
super.initState();
@ -193,47 +244,41 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
Widget build(BuildContext context) {
debugPrint("BUILD: $runtimeType");
if (supportsSpark) {
if (_showSparkAddress) {
_qrcodeContent = _sparkAddress;
} else {
_qrcodeContent = ref.watch(pWalletReceivingAddress(walletId));
}
final String address;
if (showMultiType) {
address = _addressMap[_walletAddressTypes[_currentIndex]]!;
} else {
_qrcodeContent = ref.watch(pWalletReceivingAddress(walletId));
address = ref.watch(pWalletReceivingAddress(walletId));
}
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ConditionalParent(
condition: supportsSpark,
condition: showMultiType,
builder: (child) => Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
DropdownButtonHideUnderline(
child: DropdownButton2<bool>(
value: _showSparkAddress,
child: DropdownButton2<int>(
value: _currentIndex,
items: [
DropdownMenuItem(
value: true,
child: Text(
"Spark address",
style: STextStyles.desktopTextMedium(context),
for (int i = 0; i < _walletAddressTypes.length; i++)
DropdownMenuItem(
value: i,
child: Text(
supportsSpark &&
_walletAddressTypes[i] == AddressType.p2pkh
? "Transparent address"
: "${_walletAddressTypes[i].readableName} address",
style: STextStyles.w500_14(context),
),
),
),
DropdownMenuItem(
value: false,
child: Text(
"Transparent address",
style: STextStyles.desktopTextMedium(context),
),
),
],
onChanged: (value) {
if (value is bool && value != _showSparkAddress) {
if (value != null && value != _currentIndex) {
setState(() {
_showSparkAddress = value;
_currentIndex = value;
});
}
},
@ -251,6 +296,16 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
),
),
),
buttonStyleData: ButtonStyleData(
decoration: BoxDecoration(
color: Theme.of(context)
.extension<StackColors>()!
.textFieldDefaultBG,
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
),
),
dropdownStyleData: DropdownStyleData(
offset: const Offset(0, -10),
elevation: 0,
@ -274,95 +329,7 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
const SizedBox(
height: 12,
),
if (_showSparkAddress)
MouseRegion(
cursor: SystemMouseCursors.click,
child: GestureDetector(
onTap: () {
clipboard.setData(
ClipboardData(text: _sparkAddress ?? "Error"),
);
showFloatingFlushBar(
type: FlushBarType.info,
message: "Copied to clipboard",
iconAsset: Assets.svg.copy,
context: context,
);
},
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Theme.of(context)
.extension<StackColors>()!
.backgroundAppBar,
width: 1,
),
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
),
child: RoundedWhiteContainer(
child: Column(
children: [
Row(
children: [
Text(
"Your ${widget.contractAddress == null ? coin.ticker : ref.watch(
pCurrentTokenWallet.select(
(value) => value!.tokenContract.symbol,
),
)} SPARK address",
style: STextStyles.itemSubtitle(context),
),
const Spacer(),
Row(
children: [
SvgPicture.asset(
Assets.svg.copy,
width: 15,
height: 15,
color: Theme.of(context)
.extension<StackColors>()!
.infoItemIcons,
),
const SizedBox(
width: 4,
),
Text(
"Copy",
style: STextStyles.link2(context),
),
],
),
],
),
const SizedBox(
height: 8,
),
Row(
children: [
Expanded(
child: Text(
_sparkAddress ?? "Error",
style:
STextStyles.desktopTextExtraExtraSmall(
context,
).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textDark,
),
),
),
],
),
],
),
),
),
),
),
if (!_showSparkAddress) child,
child,
],
),
child: MouseRegion(
@ -435,7 +402,7 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
children: [
Expanded(
child: Text(
ref.watch(pWalletReceivingAddress(walletId)),
address,
style: STextStyles.desktopTextExtraExtraSmall(
context,
).copyWith(
@ -467,7 +434,8 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
supportsSpark)
SecondaryButton(
buttonHeight: ButtonHeight.l,
onPressed: supportsSpark && _showSparkAddress
onPressed: supportsSpark &&
_walletAddressTypes[_currentIndex] == AddressType.spark
? generateNewSparkAddress
: generateNewAddress,
label: "Generate new address",
@ -479,7 +447,7 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
child: QR(
data: AddressUtils.buildUriString(
coin,
_qrcodeContent ?? "",
address,
{},
),
size: 200,
@ -518,7 +486,7 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
RouteGenerator.generateRoute(
RouteSettings(
name: GenerateUriQrCodeView.routeName,
arguments: Tuple2(coin, _qrcodeContent ?? ""),
arguments: Tuple2(coin, address),
),
),
],
@ -535,7 +503,7 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
shouldUseMaterialRoute: RouteGenerator.useMaterialPageRoute,
builder: (_) => GenerateUriQrCodeView(
coin: coin,
receivingAddress: _qrcodeContent ?? "",
receivingAddress: address,
),
settings: const RouteSettings(
name: GenerateUriQrCodeView.routeName,