1
+ import 'dart:async' ;
2
+
1
3
import 'package:flutter/material.dart' ;
2
4
import 'package:flutter_riverpod/flutter_riverpod.dart' ;
3
5
import 'package:threebotlogin/helpers/logger.dart' ;
@@ -7,6 +9,7 @@ import 'package:threebotlogin/services/tfchain_service.dart';
7
9
import 'package:gridproxy_client/models/contracts.dart' ;
8
10
import 'package:threebotlogin/widgets/wallets/contract_details.dart' ;
9
11
import 'package:threebotlogin/helpers/contract_helpers.dart' ;
12
+ import 'package:connectivity_plus/connectivity_plus.dart' ;
10
13
11
14
class WalletContractsWidget extends ConsumerStatefulWidget {
12
15
const WalletContractsWidget ({super .key, required this .wallet});
@@ -33,35 +36,74 @@ class _WalletContractsWidgetState extends ConsumerState<WalletContractsWidget> {
33
36
loading = true ;
34
37
failed = false ;
35
38
});
39
+
36
40
try {
37
- final twinId = await getTwinId (widget.wallet.tfchainSecret);
38
- contracts = await getContractsByTwinId (twinId);
39
- } catch (e) {
40
- logger.e ('Failed to load contracts: $e ' );
41
- setState (() {
42
- failed = true ;
43
- });
44
- if (context.mounted) {
45
- final loadingContractsFailure = SnackBar (
46
- content: Text (
47
- 'Failed to load contracts' ,
48
- style: Theme .of (context)
49
- .textTheme
50
- .bodyMedium!
51
- .copyWith (color: Theme .of (context).colorScheme.errorContainer),
52
- ),
53
- duration: const Duration (seconds: 3 ),
41
+ final connectivityResult = await (Connectivity ().checkConnectivity ());
42
+
43
+ if (connectivityResult.contains (ConnectivityResult .none)) {
44
+ _handleFailure (
45
+ 'No internet connection. Please check your network.' ,
54
46
);
55
- ScaffoldMessenger .of (context).clearSnackBars ();
56
- ScaffoldMessenger .of (context).showSnackBar (loadingContractsFailure);
47
+ return ;
57
48
}
58
- } finally {
49
+
50
+ final twinId = await getTwinId (widget.wallet.tfchainSecret).timeout (
51
+ const Duration (minutes: 1 ),
52
+ onTimeout: () {
53
+ throw TimeoutException ('Loading contracts timed out' );
54
+ },
55
+ );
56
+
57
+ contracts = await getContractsByTwinId (twinId).timeout (
58
+ const Duration (minutes: 1 ),
59
+ onTimeout: () {
60
+ throw TimeoutException ('Loading contracts timed out' );
61
+ },
62
+ );
63
+
59
64
setState (() {
60
65
loading = false ;
66
+ failed = false ;
61
67
});
68
+ } on TimeoutException catch (e) {
69
+ _handleFailure (
70
+ 'Loading contracts timed out. Please check your network.' ,
71
+ error: e,
72
+ );
73
+ } catch (e) {
74
+ _handleFailure (
75
+ 'Failed to load contracts. Please try again.' ,
76
+ error: e,
77
+ );
62
78
}
63
79
}
64
80
81
+ void _handleFailure (String userMessage, {Object ? error}) {
82
+ if (error != null ) {
83
+ logger.e ('Load contracts failed' , error: error);
84
+ }
85
+
86
+ if (mounted) {
87
+ final errorSnackbar = SnackBar (
88
+ content: Text (
89
+ userMessage,
90
+ style: Theme .of (context)
91
+ .textTheme
92
+ .bodyMedium!
93
+ .copyWith (color: Theme .of (context).colorScheme.errorContainer),
94
+ ),
95
+ duration: const Duration (seconds: 3 ),
96
+ );
97
+ ScaffoldMessenger .of (context).clearSnackBars ();
98
+ ScaffoldMessenger .of (context).showSnackBar (errorSnackbar);
99
+ }
100
+
101
+ setState (() {
102
+ loading = false ;
103
+ failed = true ;
104
+ });
105
+ }
106
+
65
107
@override
66
108
Widget build (BuildContext context) {
67
109
if (loading) {
0 commit comments