-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TF-2189 Avoid compile runtime InternetConnectionChecker on web
- Loading branch information
Showing
9 changed files
with
131 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
lib/features/network_connection/presentation/web_network_connection_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:core/presentation/extensions/color_extension.dart'; | ||
import 'package:core/presentation/resources/image_paths.dart'; | ||
import 'package:core/presentation/utils/app_toast.dart'; | ||
import 'package:core/presentation/views/toast/tmail_toast.dart'; | ||
import 'package:core/utils/app_logger.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:get/get.dart'; | ||
import 'package:tmail_ui_user/main/localizations/app_localizations.dart'; | ||
import 'package:tmail_ui_user/main/routes/route_navigation.dart'; | ||
|
||
class NetworkConnectionController extends GetxController { | ||
final _imagePaths = Get.find<ImagePaths>(); | ||
final _appToast = Get.find<AppToast>(); | ||
|
||
final _connectivityResult = Rxn<ConnectivityResult>(); | ||
|
||
final Connectivity _connectivity; | ||
|
||
bool _isEnableShowToastDisconnection = true; | ||
|
||
StreamSubscription<ConnectivityResult>? _subscription; | ||
|
||
NetworkConnectionController(this._connectivity); | ||
|
||
@override | ||
void onInit() { | ||
super.onInit(); | ||
_listenNetworkConnectionChanged(); | ||
} | ||
|
||
@override | ||
void onReady() { | ||
super.onReady(); | ||
_getCurrentNetworkConnectionState(); | ||
} | ||
|
||
@override | ||
void onClose() { | ||
_subscription?.cancel(); | ||
super.onClose(); | ||
} | ||
|
||
void _getCurrentNetworkConnectionState() async { | ||
final connectionResult = await _connectivity.checkConnectivity(); | ||
log('NetworkConnectionController::_getCurrentNetworkConnectionState():connectionResult: $connectionResult'); | ||
_setNetworkConnectivityState(connectionResult); | ||
_handleNetworkConnectionState(); | ||
} | ||
|
||
void _listenNetworkConnectionChanged() { | ||
_subscription = _connectivity.onConnectivityChanged.listen( | ||
(result) { | ||
log('NetworkConnectionController::_listenNetworkConnectionChanged()::onConnectivityChanged: $result'); | ||
_setNetworkConnectivityState(result); | ||
_handleNetworkConnectionState(); | ||
}, | ||
onError: (error, stackTrace) { | ||
logError('NetworkConnectionController::_listenNetworkConnectionChanged()::onConnectivityChanged:error: $error | stackTrace: $stackTrace'); | ||
} | ||
); | ||
} | ||
|
||
void _setNetworkConnectivityState(ConnectivityResult newConnectivityResult) { | ||
_connectivityResult.value = newConnectivityResult; | ||
} | ||
|
||
bool isNetworkConnectionAvailable() => _connectivityResult.value != ConnectivityResult.none; | ||
|
||
void _handleNetworkConnectionState() { | ||
if (_isEnableShowToastDisconnection && !isNetworkConnectionAvailable()) { | ||
_showToastLostConnection(); | ||
} else { | ||
ToastView.dismiss(); | ||
} | ||
} | ||
|
||
void _showToastLostConnection() { | ||
if (currentContext != null && currentOverlayContext != null) { | ||
_appToast.showToastMessage( | ||
currentOverlayContext!, | ||
AppLocalizations.of(currentContext!).no_internet_connection, | ||
actionName: AppLocalizations.of(currentContext!).skip, | ||
onActionClick: () { | ||
_isEnableShowToastDisconnection = false; | ||
ToastView.dismiss(); | ||
}, | ||
leadingSVGIcon: _imagePaths.icNotConnection, | ||
backgroundColor: AppColor.textFieldErrorBorderColor, | ||
textColor: Colors.white, | ||
infinityToast: true, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 3 additions & 10 deletions
13
lib/main/bindings/network_connection/network_connection_bindings.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,12 @@ | ||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:core/presentation/resources/image_paths.dart'; | ||
import 'package:core/presentation/utils/app_toast.dart'; | ||
import 'package:get/get.dart'; | ||
import 'package:internet_connection_checker/internet_connection_checker.dart'; | ||
import 'package:tmail_ui_user/features/network_connection/presentation/network_connection_controller.dart'; | ||
import 'package:tmail_ui_user/features/network_connection/presentation/network_connection_controller.dart' | ||
if (dart.library.html) 'package:tmail_ui_user/features/network_connection/presentation/web_network_connection_controller.dart'; | ||
|
||
class NetWorkConnectionBindings extends Bindings { | ||
|
||
@override | ||
void dependencies() { | ||
Get.put(NetworkConnectionController( | ||
Get.find<Connectivity>(), | ||
Get.find<InternetConnectionChecker>(), | ||
Get.find<ImagePaths>(), | ||
Get.find<AppToast>(), | ||
)); | ||
Get.put(NetworkConnectionController(Get.find<Connectivity>())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters