Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit fb76886

Browse files
committed
imprv: Improved the error handling & removed boilerplate code
1 parent 4073d44 commit fb76886

File tree

4 files changed

+48
-105
lines changed

4 files changed

+48
-105
lines changed

lib/features/dapps/subfeatures/open_dapp/open_dapp_presenter.dart

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
3232
late final _accountUseCase = ref.read(accountUseCaseProvider);
3333
late final _authUseCase = ref.read(authUseCaseProvider);
3434
late final _customTokensUseCase = ref.read(customTokensUseCaseProvider);
35+
late final _errorUseCase = ref.read(errorUseCaseProvider);
3536

3637
@override
3738
void initState() {
@@ -232,48 +233,27 @@ class OpenDAppPresenter extends CompletePresenter<OpenDAppState> {
232233
}
233234
} catch (e, s) {
234235
cancel.call();
235-
if (e is RPCError) {
236-
handleError(e.message);
237-
}
238-
addError(e, s);
236+
callErrorHandler(e, s);
239237
} finally {
240238
loading = false;
241239
}
242240
}
243241

244-
void handleError(String message) {
245-
final isBottomSheetShown = checkBalanceErrors(message);
246-
247-
// String errorMessage = message;
248-
// errorMessage = changeErrorMessage(errorMessage);
249-
// addError(errorMessage);
250-
}
251-
252-
bool checkBalanceErrors(String message) {
253-
bool isShown = false;
254-
for (String error in Config.fundErrors) {
255-
if (message.contains(error)) {
256-
final network = state.network!;
257-
final walletAddress = state.account!.address;
258-
showReceiveBottomSheet(
259-
context!,
260-
walletAddress,
261-
network.chainId,
262-
network.symbol,
263-
() {
264-
navigator!.pop();
265-
final chainId = state.network!.chainId;
266-
final l3BridgeUri = Uri.parse(Urls.networkL3Bridge(chainId));
267-
state.webviewController!
268-
.loadUrl(urlRequest: URLRequest(url: l3BridgeUri));
269-
},
270-
_chainConfigurationUseCase.launchUrlInPlatformDefault,
271-
);
272-
isShown = true;
273-
break;
274-
}
242+
void callErrorHandler(dynamic e, StackTrace s) {
243+
final isHandled = _errorUseCase.handleError(
244+
context!,
245+
e,
246+
onL3Tap: () {
247+
navigator!.pop();
248+
final chainId = state.network!.chainId;
249+
final l3BridgeUri = Uri.parse(Urls.networkL3Bridge(chainId));
250+
state.webviewController!
251+
.loadUrl(urlRequest: URLRequest(url: l3BridgeUri));
252+
},
253+
);
254+
if (!isHandled) {
255+
addError(e, s);
275256
}
276-
return isShown;
277257
}
278258

279259
Future<bool?> addEthereumChain(

lib/features/portfolio/presentation/portfolio_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class PortfolioPage extends HookConsumerWidget {
9696
.iconButtonBackgroundActive,
9797
color: ColorsTheme.of(context).iconButtonInvertActive,
9898
icon: MxcIcons.receive,
99-
onTap: () => presenter.showReceiveBottomSheet(),
99+
onTap: () => presenter.showReceiveSheet(),
100100
titleStyle: FontTheme.of(context).subtitle1.primary(),
101101
iconSize: 24,
102102
filled: false,

lib/features/portfolio/presentation/portfolio_page_presenter.dart

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,18 @@ class PortfolioPresenter extends CompletePresenter<PortfolioState> {
100100
}
101101
}
102102

103-
void showReceiveBottomSheet() {
103+
void showReceiveSheet() {
104104
final walletAddress = state.walletAddress!;
105-
if (Config.isMxcChains(state.network!.chainId)) {
106-
showWalletAddressDialogMXCChains(
107-
context: context!,
108-
walletAddress: walletAddress,
109-
onL3Tap: () {
110-
final chainId = state.network!.chainId;
111-
final l3BridgeUri = Urls.networkL3Bridge(chainId);
112-
Navigator.of(context!).push(route.featureDialog(
113-
maintainState: false,
114-
OpenAppPage(
115-
url: l3BridgeUri,
116-
),
117-
));
118-
},
119-
launchUrlInPlatformDefault:
120-
_chainConfigurationUseCase.launchUrlInPlatformDefault);
121-
} else {
122-
final networkSymbol = state.network!.symbol;
123-
showWalletAddressDialogOtherChains(
124-
context: context!,
125-
walletAddress: walletAddress,
126-
networkSymbol: networkSymbol);
127-
}
105+
final chainId = state.network!.chainId;
106+
final networkSymbol = state.network!.symbol;
107+
showReceiveBottomSheet(context!, walletAddress, chainId, networkSymbol, () {
108+
final l3BridgeUri = Urls.networkL3Bridge(chainId);
109+
Navigator.of(context!).push(route.featureDialog(
110+
maintainState: false,
111+
OpenAppPage(
112+
url: l3BridgeUri,
113+
),
114+
));
115+
}, _chainConfigurationUseCase.launchUrlInPlatformDefault, false);
128116
}
129117
}

lib/features/portfolio/subfeatures/token/send_token/send_crypto/send_crypto_presenter.dart

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class SendCryptoPresenter extends CompletePresenter<SendCryptoState> {
5252
late final accountInfo = ref.read(appNavBarContainer.state);
5353
late final _chainConfigurationUseCase =
5454
ref.read(chainConfigurationUseCaseProvider);
55+
late final _errorUseCase = ref.read(errorUseCaseProvider);
5556

5657
late final TextEditingController amountController = TextEditingController();
5758
late final TextEditingController recipientController =
@@ -220,9 +221,7 @@ class SendCryptoPresenter extends CompletePresenter<SendCryptoState> {
220221

221222
return gasFee;
222223
} catch (e, s) {
223-
if (e is RPCError) {
224-
handleError(e.message);
225-
}
224+
callErrorHandler(e, s);
226225
} finally {
227226
loading = false;
228227
}
@@ -264,55 +263,31 @@ class SendCryptoPresenter extends CompletePresenter<SendCryptoState> {
264263

265264
return res;
266265
} catch (e, s) {
267-
if (e is RPCError) {
268-
if (BottomFlowDialog.maybeOf(context!) != null) {
269-
BottomFlowDialog.of(context!).close();
270-
}
271-
handleError(e.message);
266+
if (BottomFlowDialog.maybeOf(context!) != null) {
267+
BottomFlowDialog.of(context!).close();
272268
}
269+
callErrorHandler(e, s);
273270
} finally {
274271
loading = false;
275272
}
276273
}
277274

278-
void handleError(String message) {
279-
final isBottomSheetShown = checkBalanceErrors(message);
280-
281-
// String errorMessage = message;
282-
// errorMessage = changeErrorMessage(errorMessage);
283-
// addError(errorMessage);
284-
}
285-
286-
bool checkBalanceErrors(String message) {
287-
bool isShown = false;
288-
for (String error in Config.fundErrors) {
289-
if (message.contains(error)) {
290-
final network = state.network!;
291-
final walletAddress = state.account!.address;
292-
final chainId = network.chainId;
293-
showReceiveBottomSheet(
294-
context!,
295-
walletAddress,
296-
chainId,
297-
network.symbol,
298-
() {
299-
final l3BridgeUri = Urls.networkL3Bridge(chainId);
300-
Navigator.of(context!).push(route.featureDialog(
301-
maintainState: false,
302-
OpenAppPage(
303-
url: l3BridgeUri,
304-
),
305-
));
306-
},
307-
_chainConfigurationUseCase.launchUrlInPlatformDefault,
308-
);
309-
isShown = true;
310-
break;
311-
}
275+
void callErrorHandler(dynamic e, StackTrace s) {
276+
final isHandled = _errorUseCase.handleError(context!, e, onL3Tap: () {
277+
final chainId = state.network!.chainId;
278+
final l3BridgeUri = Urls.networkL3Bridge(chainId);
279+
Navigator.of(context!).push(route.featureDialog(
280+
maintainState: false,
281+
OpenAppPage(
282+
url: l3BridgeUri,
283+
),
284+
));
285+
});
286+
if (!isHandled) {
287+
addError(e, s);
312288
}
313-
return isShown;
314289
}
315-
290+
316291
@override
317292
Future<void> dispose() async {
318293
super.dispose();

0 commit comments

Comments
 (0)