Skip to content

Commit 3fbc6da

Browse files
committed
feat: use _applyUri for paste
1 parent 15a7a34 commit 3fbc6da

File tree

2 files changed

+118
-122
lines changed

2 files changed

+118
-122
lines changed

lib/pages/send_view/send_view.dart

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,44 @@ class _SendViewState extends ConsumerState<SendView> {
135135

136136
Set<UTXO> selectedUTXOs = {};
137137

138+
void _applyUri(PaymentUriData paymentData) {
139+
try {
140+
// auto fill address
141+
_address = paymentData.address.trim();
142+
sendToController.text = _address!;
143+
144+
// autofill notes field
145+
if (paymentData.message != null) {
146+
noteController.text = paymentData.message!;
147+
} else if (paymentData.label != null) {
148+
noteController.text = paymentData.label!;
149+
}
150+
151+
// autofill amount field
152+
if (paymentData.amount != null) {
153+
final Amount amount = Decimal.parse(paymentData.amount!).toAmount(
154+
fractionDigits: coin.fractionDigits,
155+
);
156+
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
157+
amount,
158+
withUnitName: false,
159+
);
160+
ref.read(pSendAmount.notifier).state = amount;
161+
}
162+
163+
_setValidAddressProviders(_address);
164+
setState(() {
165+
_addressToggleFlag = sendToController.text.isNotEmpty;
166+
});
167+
} catch (e, s) {
168+
Logging.instance.e(
169+
"Failed to apply uri in SendView: ",
170+
error: e,
171+
stackTrace: s,
172+
);
173+
}
174+
}
175+
138176
Future<void> _scanQr() async {
139177
try {
140178
// ref
@@ -167,35 +205,7 @@ class _SendViewState extends ConsumerState<SendView> {
167205

168206
if (paymentData != null &&
169207
paymentData.coin?.uriScheme == coin.uriScheme) {
170-
// auto fill address
171-
_address = paymentData.address.trim();
172-
sendToController.text = _address!;
173-
174-
// autofill notes field
175-
if (paymentData.message != null) {
176-
noteController.text = paymentData.message!;
177-
} else if (paymentData.label != null) {
178-
noteController.text = paymentData.label!;
179-
}
180-
181-
// autofill amount field
182-
if (paymentData.amount != null) {
183-
final Amount amount = Decimal.parse(paymentData.amount!).toAmount(
184-
fractionDigits: coin.fractionDigits,
185-
);
186-
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
187-
amount,
188-
withUnitName: false,
189-
);
190-
ref.read(pSendAmount.notifier).state = amount;
191-
}
192-
193-
_setValidAddressProviders(_address);
194-
setState(() {
195-
_addressToggleFlag = sendToController.text.isNotEmpty;
196-
});
197-
198-
// now check for non standard encoded basic address
208+
_applyUri(paymentData);
199209
} else {
200210
_address = qrResult.rawContent.split("\n").first.trim();
201211
sendToController.text = _address ?? "";
@@ -1353,27 +1363,24 @@ class _SendViewState extends ConsumerState<SendView> {
13531363
final trimmed = newValue.trim();
13541364

13551365
if ((trimmed.length - (_address?.length ?? 0)).abs() > 1) {
1356-
if (coin is Monero && Uri.parse(trimmed).scheme == "monero") {
1357-
final parsedUri = Uri.parse(trimmed);
1358-
final addr = parsedUri.path;
1359-
sendToController.text = addr;
1360-
_address = addr;
1361-
cryptoAmountController.text = parsedUri.queryParameters["tx_amount"] ?? "";
1362-
} else if (coin is Bitcoin && Uri.parse(trimmed).scheme == "bitcoin") {
1363-
final parsedUri = Uri.parse(trimmed);
1364-
final addr = parsedUri.path;
1365-
sendToController.text = addr;
1366-
_address = addr;
1367-
cryptoAmountController.text = parsedUri.queryParameters["amount"] ?? "";
1366+
final parsed = AddressUtils.parsePaymentUri(
1367+
trimmed,
1368+
logging: Logging.instance,
1369+
);
1370+
if (parsed != null) {
1371+
_applyUri(parsed);
1372+
} else {
1373+
_address = newValue;
1374+
sendToController.text = newValue;
13681375
}
13691376
} else {
1370-
_address = trimmed;
1377+
_address = newValue;
13711378
}
13721379

13731380
_setValidAddressProviders(_address);
13741381

13751382
setState(() {
1376-
_addressToggleFlag = trimmed.isNotEmpty;
1383+
_addressToggleFlag = newValue.isNotEmpty;
13771384
});
13781385
},
13791386
focusNode: _addressFocusNode,
@@ -1454,32 +1461,26 @@ class _SendViewState extends ConsumerState<SendView> {
14541461
}
14551462

14561463
final trimmed = content.trim();
1457-
1458-
if (coin is Monero && Uri.parse(trimmed).scheme == "monero") {
1459-
final parsedUri = Uri.parse(trimmed);
1460-
final addr = parsedUri.path;
1461-
sendToController.text = addr;
1462-
_address = addr;
1463-
cryptoAmountController.text = parsedUri.queryParameters["tx_amount"] ?? "";
1464-
} else if (coin is Bitcoin && Uri.parse(trimmed).scheme == "bitcoin") {
1465-
final parsedUri = Uri.parse(trimmed);
1466-
final addr = parsedUri.path;
1467-
sendToController.text = addr;
1468-
_address = addr;
1469-
cryptoAmountController.text = parsedUri.queryParameters["amount"] ?? "";
1464+
final parsed = AddressUtils.parsePaymentUri(
1465+
trimmed,
1466+
logging: Logging.instance,
1467+
);
1468+
if (parsed != null) {
1469+
_applyUri(parsed);
14701470
} else {
1471-
sendToController.text = trimmed;
1472-
_address = trimmed;
1471+
sendToController.text =
1472+
content;
1473+
_address = content;
1474+
1475+
_setValidAddressProviders(_address,);
1476+
1477+
setState(() {
1478+
_addressToggleFlag =
1479+
sendToController
1480+
.text
1481+
.isNotEmpty;
1482+
});
14731483
}
1474-
1475-
_setValidAddressProviders(_address,);
1476-
1477-
setState(() {
1478-
_addressToggleFlag =
1479-
sendToController
1480-
.text
1481-
.isNotEmpty;
1482-
});
14831484
}
14841485
},
14851486
child: sendToController

lib/pages_desktop_specific/my_stack_view/wallet_view/sub_widgets/desktop_send.dart

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -660,33 +660,7 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
660660

661661
if (paymentData != null &&
662662
paymentData.coin?.uriScheme == coin.uriScheme) {
663-
// Auto fill address.
664-
_address = paymentData.address.trim();
665-
sendToController.text = _address!;
666-
667-
// Amount.
668-
if (paymentData.amount != null) {
669-
final Amount amount = Decimal.parse(paymentData.amount!).toAmount(
670-
fractionDigits: coin.fractionDigits,
671-
);
672-
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
673-
amount,
674-
withUnitName: false,
675-
);
676-
ref.read(pSendAmount.notifier).state = amount;
677-
}
678-
679-
// Note/message.
680-
if (paymentData.message != null) {
681-
_note = paymentData.message;
682-
} else if (paymentData.label != null) {
683-
_note = paymentData.label;
684-
}
685-
686-
_setValidAddressProviders(_address);
687-
setState(() {
688-
_addressToggleFlag = sendToController.text.isNotEmpty;
689-
});
663+
_applyUri(paymentData);
690664
} else {
691665
_address = qrCodeData.split("\n").first.trim();
692666
sendToController.text = _address ?? "";
@@ -736,6 +710,40 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
736710
}
737711
}
738712

713+
void _applyUri(PaymentUriData paymentData) {
714+
try {
715+
// auto fill address
716+
_address = paymentData.address;
717+
sendToController.text = _address!;
718+
719+
// autofill notes field.
720+
if (paymentData.message != null) {
721+
_note = paymentData.message;
722+
} else if (paymentData.label != null) {
723+
_note = paymentData.label;
724+
}
725+
726+
// autofill amount field
727+
if (paymentData.amount != null) {
728+
final amount = Decimal.parse(paymentData.amount!).toAmount(
729+
fractionDigits: coin.fractionDigits,
730+
);
731+
cryptoAmountController.text = ref
732+
.read(pAmountFormatter(coin))
733+
.format(amount, withUnitName: false);
734+
ref.read(pSendAmount.notifier).state = amount;
735+
}
736+
737+
// Trigger validation after pasting.
738+
_setValidAddressProviders(_address);
739+
setState(() {
740+
_addressToggleFlag = sendToController.text.isNotEmpty;
741+
});
742+
} catch (e, s) {
743+
Logging.instance.e("Error applying URI", error: e, stackTrace: s);
744+
}
745+
}
746+
739747
Future<void> pasteAddress() async {
740748
final ClipboardData? data = await clipboard.getData(Clipboard.kTextPlain);
741749
if (data?.text != null && data!.text!.isNotEmpty) {
@@ -751,33 +759,7 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
751759
);
752760
if (paymentData != null &&
753761
paymentData.coin?.uriScheme == coin.uriScheme) {
754-
// auto fill address
755-
_address = paymentData.address;
756-
sendToController.text = _address!;
757-
758-
// autofill notes field.
759-
if (paymentData.message != null) {
760-
_note = paymentData.message;
761-
} else if (paymentData.label != null) {
762-
_note = paymentData.label;
763-
}
764-
765-
// autofill amoutn field
766-
if (paymentData.amount != null) {
767-
final amount = Decimal.parse(paymentData.amount!).toAmount(
768-
fractionDigits: coin.fractionDigits,
769-
);
770-
cryptoAmountController.text = ref
771-
.read(pAmountFormatter(coin))
772-
.format(amount, withUnitName: false);
773-
ref.read(pSendAmount.notifier).state = amount;
774-
}
775-
776-
// Trigger validation after pasting.
777-
_setValidAddressProviders(_address);
778-
setState(() {
779-
_addressToggleFlag = sendToController.text.isNotEmpty;
780-
});
762+
_applyUri(paymentData);
781763
} else {
782764
content = content.split("\n").first.trim();
783765
if (coin is Epiccash) {
@@ -1439,7 +1421,20 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
14391421
selectAll: false,
14401422
),
14411423
onChanged: (newValue) {
1442-
_address = newValue;
1424+
final trimmed = newValue;
1425+
1426+
if ((trimmed.length - (_address?.length ?? 0)).abs() > 1) {
1427+
final parsed = AddressUtils.parsePaymentUri(trimmed, logging: Logging.instance);
1428+
if (parsed != null) {
1429+
_applyUri(parsed);
1430+
} else {
1431+
_address = newValue;
1432+
sendToController.text = newValue;
1433+
}
1434+
} else {
1435+
_address = newValue;
1436+
}
1437+
14431438
_setValidAddressProviders(_address);
14441439

14451440
setState(() {

0 commit comments

Comments
 (0)