From d2cdecfe3c4bfccab187780da37f11d8235f37b1 Mon Sep 17 00:00:00 2001 From: Maikel Stuivenberg Date: Mon, 1 Jul 2024 10:02:48 +0200 Subject: [PATCH] Don't wait for logging + Cache internet connection status --- .../certificate_check_interceptor.dart | 4 ++-- lib/core/network/network_info.dart | 18 +++++++++++++---- lib/core/network/token_interceptor.dart | 20 ++++++++++++------- 3 files changed, 29 insertions(+), 13 deletions(-) diff --git a/lib/core/network/certificate_check_interceptor.dart b/lib/core/network/certificate_check_interceptor.dart index 3ef3fac9e..cac9156aa 100644 --- a/lib/core/network/certificate_check_interceptor.dart +++ b/lib/core/network/certificate_check_interceptor.dart @@ -12,9 +12,10 @@ class CertificateCheckInterceptor extends InterceptorContract { @override Future interceptRequest({required BaseRequest request}) async { /// Don't check for certificate if there is no internet connection - if (await getIt().isConnected == false) { + if (getIt().isConnected == false) { return request; } + final apiURL = request.url.host; // do not check certificate for the certs endpoint if (apiURL.contains('certs')) { @@ -30,7 +31,6 @@ class CertificateCheckInterceptor extends InterceptorContract { allowedSHAFingerprints: [storedSHAFigerprint], timeout: 50, ); - if (!secure.contains('CONNECTION_SECURE')) { await LoggingInfo.instance.error( request.url.toString(), diff --git a/lib/core/network/network_info.dart b/lib/core/network/network_info.dart index be4a06df6..84a4aa096 100644 --- a/lib/core/network/network_info.dart +++ b/lib/core/network/network_info.dart @@ -1,14 +1,24 @@ import 'package:internet_connection_checker_plus/internet_connection_checker_plus.dart'; mixin NetworkInfo { - Future get isConnected; + bool get isConnected; } class NetworkInfoImpl implements NetworkInfo { - NetworkInfoImpl(this.connectionChecker); + NetworkInfoImpl(this.connectionChecker) { + // First check if there is an internet connection + connectionChecker.hasInternetAccess.then((value) { + isConnected = value; + }); - final InternetConnection connectionChecker; + // Listen for changes in the internet connection status + connectionChecker.onStatusChange.listen((status) { + isConnected = status == InternetStatus.connected; + }); + } @override - Future get isConnected => connectionChecker.hasInternetAccess; + bool isConnected = true; + + final InternetConnection connectionChecker; } diff --git a/lib/core/network/token_interceptor.dart b/lib/core/network/token_interceptor.dart index 04537dab8..6a3cb2651 100644 --- a/lib/core/network/token_interceptor.dart +++ b/lib/core/network/token_interceptor.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:developer'; @@ -37,17 +38,22 @@ class TokenInterceptor implements InterceptorContract { request.headers['Authorization'] = 'Bearer ${session.accessToken}'; } } catch (e, stackTrace) { - await LoggingInfo.instance.error( - e.toString(), - methodName: stackTrace.toString(), + unawaited( + LoggingInfo.instance.error( + e.toString(), + methodName: stackTrace.toString(), + ), ); } - await LoggingInfo.instance.logRequest( - request.method, - request.url.toString(), - correlationId, + unawaited( + LoggingInfo.instance.logRequest( + "API - " + request.method, + request.url.toString(), + correlationId, + ), ); + return request; }