Skip to content

Commit

Permalink
TF-2189 Avoid compile runtime InternetConnectionChecker on web
Browse files Browse the repository at this point in the history
  • Loading branch information
dab246 committed Oct 4, 2023
1 parent c0a7af2 commit 7052f3e
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 85 deletions.
3 changes: 2 additions & 1 deletion lib/features/composer/presentation/composer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller
import 'package:tmail_ui_user/features/manage_account/domain/state/get_all_identities_state.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/get_all_identities_interactor.dart';
import 'package:tmail_ui_user/features/manage_account/presentation/extensions/identity_extension.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';
import 'package:tmail_ui_user/features/sending_queue/domain/extensions/sending_email_extension.dart';
import 'package:tmail_ui_user/features/sending_queue/domain/model/sending_email.dart';
import 'package:tmail_ui_user/features/sending_queue/presentation/model/sending_email_action_type.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ import 'package:tmail_ui_user/features/manage_account/presentation/extensions/da
import 'package:tmail_ui_user/features/manage_account/presentation/extensions/vacation_response_extension.dart';
import 'package:tmail_ui_user/features/manage_account/presentation/model/account_menu_item.dart';
import 'package:tmail_ui_user/features/manage_account/presentation/model/manage_account_arguments.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';
import 'package:tmail_ui_user/features/offline_mode/config/work_manager_constants.dart';
import 'package:tmail_ui_user/features/offline_mode/controller/work_manager_controller.dart';
import 'package:tmail_ui_user/features/offline_mode/work_manager/one_time_work_request.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,29 @@
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:core/utils/platform_info.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:internet_connection_checker/internet_connection_checker.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 {
static const Duration _timeoutInternetConnection = Duration(milliseconds: 5000);
static const Duration _timeIntervalInternetConnection = Duration(milliseconds: 5000);

final _connectivityResult = Rxn<ConnectivityResult>();
final _internetConnectionStatus = Rxn<InternetConnectionStatus>();

final Connectivity _connectivity;
final InternetConnectionChecker _internetConnectionChecker;
final ImagePaths _imagePaths;
final AppToast _appToast;

bool _isEnableShowToastDisconnection = true;
final _internetConnectionChecker = InternetConnectionChecker.createInstance(
checkTimeout: _timeoutInternetConnection,
checkInterval: _timeIntervalInternetConnection
);

StreamSubscription<ConnectivityResult>? _subscription;
StreamSubscription<InternetConnectionStatus>? _internetSubscription;

NetworkConnectionController(
this._connectivity,
this._internetConnectionChecker,
this._imagePaths,
this._appToast,
);
NetworkConnectionController(this._connectivity);

@override
void onInit() {
Expand All @@ -58,45 +49,39 @@ class NetworkConnectionController extends GetxController {
void _getCurrentNetworkConnectionState() async {
final listConnectionResult = await Future.wait([
_connectivity.checkConnectivity(),
if (PlatformInfo.isMobile)
_internetConnectionChecker.connectionStatus,
_internetConnectionChecker.connectionStatus,
]);
log('NetworkConnectionController::_getCurrentNetworkConnectionState():listConnectionResult: $listConnectionResult');

if (listConnectionResult[0] is ConnectivityResult) {
_setNetworkConnectivityState(listConnectionResult[0] as ConnectivityResult);
}

if (PlatformInfo.isMobile && listConnectionResult[1] is InternetConnectionStatus) {
if (listConnectionResult[1] is InternetConnectionStatus) {
_setInternetConnectivityStatus(listConnectionResult[1] as InternetConnectionStatus);
}

_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');
}
);

if (PlatformInfo.isMobile) {
_internetSubscription = _internetConnectionChecker.onStatusChange.listen(
(status) {
log('NetworkConnectionController::_listenNetworkConnectionChanged()::onStatusChange: $status');
_setInternetConnectivityStatus(status);
},
onError: (error, stackTrace) {
logError('NetworkConnectionController::_listenNetworkConnectionChanged()::onStatusChange:error: $error | stackTrace: $stackTrace');
}
);
}
_internetSubscription = _internetConnectionChecker.onStatusChange.listen(
(status) {
log('NetworkConnectionController::_listenNetworkConnectionChanged()::onStatusChange: $status');
_setInternetConnectivityStatus(status);
},
onError: (error, stackTrace) {
logError('NetworkConnectionController::_listenNetworkConnectionChanged()::onStatusChange:error: $error | stackTrace: $stackTrace');
}
);
}

void _setNetworkConnectivityState(ConnectivityResult newConnectivityResult) {
Expand All @@ -108,39 +93,7 @@ class NetworkConnectionController extends GetxController {
}

bool isNetworkConnectionAvailable() {
if (PlatformInfo.isWeb) {
return _connectivityResult.value != ConnectivityResult.none;
} else {
return _connectivityResult.value != ConnectivityResult.none &&
_internetConnectionStatus.value == InternetConnectionStatus.connected;
}
}

void _handleNetworkConnectionState() {
if (PlatformInfo.isWeb) {
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,
);
}
return _connectivityResult.value != ConnectivityResult.none &&
_internetConnectionStatus.value == InternetConnectionStatus.connected;
}
}
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,
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import 'package:tmail_ui_user/features/caching/utils/cache_utils.dart';
import 'package:tmail_ui_user/features/email/domain/state/delete_sending_email_state.dart';
import 'package:tmail_ui_user/features/email/presentation/model/composer_arguments.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/controller/mailbox_dashboard_controller.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';
import 'package:tmail_ui_user/features/offline_mode/controller/work_manager_controller.dart';
import 'package:tmail_ui_user/features/offline_mode/model/sending_state.dart';
import 'package:tmail_ui_user/features/sending_queue/domain/extensions/list_sending_email_extension.dart';
Expand Down
3 changes: 2 additions & 1 deletion lib/features/thread/presentation/thread_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import 'package:tmail_ui_user/features/mailbox_dashboard/presentation/model/sear
import 'package:tmail_ui_user/features/manage_account/domain/model/create_new_email_rule_filter_request.dart';
import 'package:tmail_ui_user/features/manage_account/domain/state/create_new_rule_filter_state.dart';
import 'package:tmail_ui_user/features/manage_account/domain/usecases/create_new_email_rule_filter_interactor.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';
import 'package:tmail_ui_user/features/rules_filter_creator/presentation/model/rules_filter_creator_arguments.dart';
import 'package:tmail_ui_user/features/search/email/presentation/search_email_bindings.dart';
import 'package:tmail_ui_user/features/thread/domain/constants/thread_constants.dart';
Expand Down
2 changes: 0 additions & 2 deletions lib/main/bindings/network/network_bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_appauth/flutter_appauth.dart';
import 'package:get/get.dart';
import 'package:internet_connection_checker/internet_connection_checker.dart';
import 'package:jmap_dart_client/http/http_client.dart';
import 'package:tmail_ui_user/features/email/data/local/html_analyzer.dart';
import 'package:tmail_ui_user/features/email/data/network/email_api.dart';
Expand Down Expand Up @@ -103,7 +102,6 @@ class NetworkBindings extends Bindings {

void _bindingConnection() {
Get.put(Connectivity());
Get.put(InternetConnectionChecker());
}

void _bindingException() {
Expand Down
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>()));
}
}
3 changes: 2 additions & 1 deletion lib/main/exceptions/remote_exception_thrower.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import 'package:get/get_connect/http/src/status/http_status.dart';
import 'package:jmap_dart_client/jmap/core/error/method/error_method_response.dart';
import 'package:jmap_dart_client/jmap/core/error/method/exception/error_method_response_exception.dart';
import 'package:tmail_ui_user/features/login/domain/exceptions/authentication_exception.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';
import 'package:tmail_ui_user/main/exceptions/exception_thrower.dart';
import 'package:tmail_ui_user/main/exceptions/remote_exception.dart';
import 'package:tmail_ui_user/main/routes/route_navigation.dart';
Expand Down

0 comments on commit 7052f3e

Please sign in to comment.