Skip to content

Commit 577af5e

Browse files
Merge pull request #1865 from EnsembleUI/fix-no-internet-issue
feat: add internet connectivity check
2 parents d3bea1a + 4d93c82 commit 577af5e

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

modules/ensemble/lib/ensemble_app.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
3030
import 'package:flutter_localized_locales/flutter_localized_locales.dart';
3131
import 'package:workmanager/workmanager.dart';
3232
import 'package:yaml/yaml.dart';
33+
import 'package:connectivity_plus/connectivity_plus.dart';
3334

3435
const String backgroundUploadTask = 'backgroundUploadTask';
3536
const String backgroundBluetoothSubscribeTask = 'backgroundBluetoothSubscribeTask';
@@ -141,11 +142,22 @@ class EnsembleAppState extends State<EnsembleApp> with WidgetsBindingObserver {
141142
// the entire App to reload (e.g. change Locale at runtime)
142143
Key? appKey;
143144

145+
bool _hasInternet = true;
146+
late final StreamSubscription<List<ConnectivityResult>>
147+
_connectivitySubscription;
148+
144149
@override
145150
void initState() {
146151
super.initState();
147152
WidgetsBinding.instance.addObserver(this);
148153
config = initApp();
154+
155+
// Initialize connectivity listener.
156+
_updateConnectivity();
157+
_connectivitySubscription = Connectivity()
158+
.onConnectivityChanged
159+
.listen((_) => _updateConnectivity());
160+
149161
// Initialize native features.
150162
if (!kIsWeb) {
151163
Workmanager().initialize(callbackDispatcher, isInDebugMode: false);
@@ -173,6 +185,23 @@ class EnsembleAppState extends State<EnsembleApp> with WidgetsBindingObserver {
173185
}
174186
}
175187

188+
/// Check the device’s connectivity and update the state.
189+
Future<void> _updateConnectivity() async {
190+
final result = await Connectivity().checkConnectivity();
191+
final hasInternetNow = result.any((r) => r != ConnectivityResult.none);
192+
193+
// If connectivity has been restored, reinitialize the app
194+
if (!_hasInternet && hasInternetNow) {
195+
setState(() {
196+
config = initApp();
197+
});
198+
}
199+
200+
setState(() {
201+
_hasInternet = hasInternetNow;
202+
});
203+
}
204+
176205
@override
177206
void didChangeAppLifecycleState(AppLifecycleState state) async {
178207
super.didChangeAppLifecycleState(state);
@@ -192,6 +221,7 @@ class EnsembleAppState extends State<EnsembleApp> with WidgetsBindingObserver {
192221

193222
@override
194223
void dispose() {
224+
_connectivitySubscription.cancel();
195225
WidgetsBinding.instance.removeObserver(this);
196226
super.dispose();
197227
}

modules/ensemble/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ dependencies:
143143
flutter_slidable: ^3.1.1
144144
accordion: ^2.6.0
145145
session_storage: ^0.0.1
146+
connectivity_plus: ^6.1.3
146147

147148
dependency_overrides:
148149
http: ^0.13.5

starter/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ flutter:
160160
- .env
161161
- ensemble/
162162

163-
# put your app's images in this folder
163+
# put your app's images in this folder
164164
- ensemble/assets/
165165

166166
# list all your Apps directories here. It's a Flutter requirement
@@ -172,4 +172,4 @@ flutter:
172172
- ensemble/apps/helloApp/translations/
173173

174174
# # config folder contains appConfig.json and secrets.json
175-
- ensemble/apps/helloApp/config/
175+
- ensemble/apps/helloApp/config/

0 commit comments

Comments
 (0)