Skip to content

Commit 574392a

Browse files
committed
refactor(shopinbit): await send-from navigation before returning true
1 parent cd2a1b8 commit 574392a

3 files changed

Lines changed: 26 additions & 30 deletions

File tree

lib/pages/shopinbit/shopinbit_car_research_payment_view.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class _ShopInBitCarResearchPaymentViewState
8989
bool get _payNowEnabled =>
9090
!_isTerminal && _flowState == _PaymentFlowState.idle;
9191

92-
void _confirmPayment() {
92+
Future<void> _confirmPayment() async {
9393
// Keep polling while the user is in the send flow.
9494
final method = _methods[_selectedMethod];
9595
final ticker = method.toUpperCase();
@@ -100,7 +100,7 @@ class _ShopInBitCarResearchPaymentViewState
100100
coin: AppConfig.getCryptoCurrencyForTicker(ticker),
101101
);
102102

103-
final navigated = tryNavigateToShopInBitWalletSend(
103+
final navigated = await tryNavigateToShopInBitWalletSend(
104104
ref: ref,
105105
context: context,
106106
ticker: ticker,
@@ -113,6 +113,7 @@ class _ShopInBitCarResearchPaymentViewState
113113
);
114114

115115
if (navigated) return;
116+
if (!mounted) return;
116117

117118
// No compatible wallet coin found: surface an info flushbar and keep
118119
// the user on this screen so they can pay externally and then use the
@@ -826,7 +827,7 @@ class _ShopInBitCarResearchPaymentViewState
826827
enabled: _payNowEnabled,
827828
onPressed: _payNowEnabled
828829
? (hasWallets
829-
? _confirmPayment
830+
? () => unawaited(_confirmPayment())
830831
: () => unawaited(_checkForPayment()))
831832
: null,
832833
),

lib/pages/shopinbit/shopinbit_payment_shared.dart

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:async';
2-
31
import 'package:decimal/decimal.dart';
42
import 'package:flutter/material.dart';
53
import 'package:flutter_riverpod/flutter_riverpod.dart';
@@ -127,7 +125,8 @@ bool hasShopInBitWalletForTicker({
127125
return wallets.wallets.any((e) => e.info.coin == coin);
128126
}
129127

130-
void _pushShopInBitSendFrom({
128+
// Pushes the send-from view and awaits it.
129+
Future<void> _pushShopInBitSendFrom({
131130
required BuildContext context,
132131
required CryptoCurrency coin,
133132
required Amount? amount,
@@ -136,26 +135,24 @@ void _pushShopInBitSendFrom({
136135
EthContract? tokenContract,
137136
bool popDesktopBeforeShow = false,
138137
String? routeOnSuccessName,
139-
}) {
138+
}) async {
140139
if (Util.isDesktop) {
141140
if (popDesktopBeforeShow) {
142141
Navigator.of(context, rootNavigator: true).pop();
143142
}
144-
unawaited(
145-
showDialog<void>(
146-
context: context,
147-
builder: (_) => ShopInBitSendFromView(
148-
coin: coin,
149-
amount: amount,
150-
address: address,
151-
model: model,
152-
shouldPopRoot: true,
153-
tokenContract: tokenContract,
154-
),
143+
await showDialog<void>(
144+
context: context,
145+
builder: (_) => ShopInBitSendFromView(
146+
coin: coin,
147+
amount: amount,
148+
address: address,
149+
model: model,
150+
shouldPopRoot: true,
151+
tokenContract: tokenContract,
155152
),
156153
);
157154
} else {
158-
Navigator.of(context).push(
155+
await Navigator.of(context).push(
159156
RouteGenerator.getRoute<dynamic>(
160157
shouldUseMaterialRoute: RouteGenerator.useMaterialPageRoute,
161158
builder: (_) => ShopInBitSendFromView(
@@ -172,11 +169,8 @@ void _pushShopInBitSendFrom({
172169
}
173170
}
174171

175-
// Tries to launch the in-wallet send flow for [ticker]/[address]. Returns
176-
// true when navigation happened. Returns false when no compatible wallet
177-
// or token contract was found, leaving the caller to handle the
178-
// "pay externally" path (flushbar, status change, etc).
179-
bool tryNavigateToShopInBitWalletSend({
172+
// Tries to launch the in-wallet send flow for [ticker]/[address].
173+
Future<bool> tryNavigateToShopInBitWalletSend({
180174
required WidgetRef ref,
181175
required BuildContext context,
182176
required String ticker,
@@ -186,12 +180,12 @@ bool tryNavigateToShopInBitWalletSend({
186180
required ShopInBitOrderModel model,
187181
bool popDesktopBeforeShow = false,
188182
String? routeOnSuccessName,
189-
}) {
183+
}) async {
190184
if (address.isEmpty) return false;
191185

192186
final coin = AppConfig.getCryptoCurrencyForTicker(ticker);
193187
if (coin != null) {
194-
_pushShopInBitSendFrom(
188+
await _pushShopInBitSendFrom(
195189
context: context,
196190
coin: coin,
197191
amount: amount,
@@ -211,7 +205,7 @@ bool tryNavigateToShopInBitWalletSend({
211205
if (tokenContract != null) {
212206
final ethCoin = AppConfig.getCryptoCurrencyForTicker("ETH");
213207
if (ethCoin != null) {
214-
_pushShopInBitSendFrom(
208+
await _pushShopInBitSendFrom(
215209
context: context,
216210
coin: ethCoin,
217211
amount: amount,

lib/pages/shopinbit/shopinbit_payment_view.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class _ShopInBitPaymentViewState extends ConsumerState<ShopInBitPaymentView> {
244244
}
245245
}
246246

247-
void _confirmPayment() {
247+
Future<void> _confirmPayment() async {
248248
_pollTimer?.cancel();
249249
final method = _methods[_selectedMethod];
250250
final ticker = method.toUpperCase();
@@ -256,7 +256,7 @@ class _ShopInBitPaymentViewState extends ConsumerState<ShopInBitPaymentView> {
256256
amountFallback: _paymentInfo?.due,
257257
);
258258

259-
if (tryNavigateToShopInBitWalletSend(
259+
if (await tryNavigateToShopInBitWalletSend(
260260
ref: ref,
261261
context: context,
262262
ticker: ticker,
@@ -268,6 +268,7 @@ class _ShopInBitPaymentViewState extends ConsumerState<ShopInBitPaymentView> {
268268
)) {
269269
return;
270270
}
271+
if (!mounted) return;
271272

272273
widget.model.status = ShopInBitOrderStatus.paymentPending;
273274
widget.model.paymentMethod = method;
@@ -306,7 +307,7 @@ class _ShopInBitPaymentViewState extends ConsumerState<ShopInBitPaymentView> {
306307
void _onOwnedCoinTap(int methodIndex) {
307308
if (!_payNowEnabled) return;
308309
_selectedMethod = methodIndex;
309-
_confirmPayment();
310+
unawaited(_confirmPayment());
310311
}
311312

312313
void _onUnownedCoinTap(int methodIndex) {

0 commit comments

Comments
 (0)