From 73c437ca0a8c046576821d4edc0c69122e0699e8 Mon Sep 17 00:00:00 2001 From: Volodymyr Nazarkevych Date: Thu, 2 May 2024 16:20:39 +0300 Subject: [PATCH 1/2] added logger for getting data from the local storage --- .../CommonMain/Caching/CachingManager.swift | 9 +++++++-- .../Features/FeaturesViewModel.swift | 18 ++++++++++++------ Sources/CommonMain/Utils/Constants.swift | 1 + 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Sources/CommonMain/Caching/CachingManager.swift b/Sources/CommonMain/Caching/CachingManager.swift index ba7c80c..8a57b0c 100644 --- a/Sources/CommonMain/Caching/CachingManager.swift +++ b/Sources/CommonMain/Caching/CachingManager.swift @@ -55,8 +55,13 @@ public class CachingManager: CachingLayer { // Check if file exists if fileManager.fileExists(atPath: filePath) { // Read File Contents - guard let jsonContents = fileManager.contents(atPath: filePath) else { return nil } - return jsonContents + if let jsonContents = fileManager.contents(atPath: filePath) { + return jsonContents + } else { + logger.error("Can't read file content") + } + } else { + logger.error("File doesn't exist") } return nil diff --git a/Sources/CommonMain/Features/FeaturesViewModel.swift b/Sources/CommonMain/Features/FeaturesViewModel.swift index 1dffa8c..c582989 100644 --- a/Sources/CommonMain/Features/FeaturesViewModel.swift +++ b/Sources/CommonMain/Features/FeaturesViewModel.swift @@ -45,7 +45,7 @@ class FeaturesViewModel { } } else { delegate?.featuresFetchFailed(error: .failedToLoadData, isRemote: false) - logger.error("Failed load local data") + logger.error("Failed load data from local storage or data is empty") } if let apiUrl = apiUrl { @@ -88,12 +88,18 @@ class FeaturesViewModel { if let encryptedString = jsonPetitions.encryptedFeatures, !encryptedString.isEmpty { if let encryptionKey = encryptionKey, !encryptionKey.isEmpty { let crypto: CryptoProtocol = Crypto() - guard let features = crypto.getFeaturesFromEncryptedFeatures(encryptedString: encryptedString, encryptionKey: encryptionKey) else { return } - - if let featureData = try? JSONEncoder().encode(features) { - manager.putData(fileName: Constants.featureCache, content: featureData) + if let features = crypto.getFeaturesFromEncryptedFeatures(encryptedString: encryptedString, encryptionKey: encryptionKey) { + if let featureData = try? JSONEncoder().encode(features) { + manager.putData(fileName: Constants.featureCache, content: featureData) + } else { + logger.error("Failed encode features") + } + delegate?.featuresFetchedSuccessfully(features: features, isRemote: true) + } else { + delegate?.featuresFetchFailed(error: .failedEncryptedFeatures, isRemote: true) + logger.error("Failed get features from encrypted features") + return } - delegate?.featuresFetchedSuccessfully(features: features, isRemote: true) } else { delegate?.featuresFetchFailed(error: .failedMissingKey, isRemote: true) return diff --git a/Sources/CommonMain/Utils/Constants.swift b/Sources/CommonMain/Utils/Constants.swift index eba519f..95bb8eb 100644 --- a/Sources/CommonMain/Utils/Constants.swift +++ b/Sources/CommonMain/Utils/Constants.swift @@ -65,6 +65,7 @@ public struct BucketRange: Codable { case failedToLoadData = 0 case failedParsedData = 1 case failedMissingKey = 2 + case failedEncryptedFeatures = 3 } /// Meta info about the variations From d62e4a7e4657f6a7886c17ef31527267c186afce Mon Sep 17 00:00:00 2001 From: Volodymyr Nazarkevych Date: Thu, 2 May 2024 16:50:15 +0300 Subject: [PATCH 2/2] fixed bug with disconnect SSE --- Sources/CommonMain/Features/FeaturesViewModel.swift | 6 ++++++ Sources/CommonMain/Network/SSEHandler.swift | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Sources/CommonMain/Features/FeaturesViewModel.swift b/Sources/CommonMain/Features/FeaturesViewModel.swift index c582989..affe9b8 100644 --- a/Sources/CommonMain/Features/FeaturesViewModel.swift +++ b/Sources/CommonMain/Features/FeaturesViewModel.swift @@ -29,6 +29,12 @@ class FeaturesViewModel { self?.prepareFeaturesData(data: jsonData) } streamingUpdate.connect() + + streamingUpdate.onDissconnect { _, shouldReconnect, _ in + if let shouldReconnect = shouldReconnect, shouldReconnect { + streamingUpdate.connect() + } + } } /// Fetch Features diff --git a/Sources/CommonMain/Network/SSEHandler.swift b/Sources/CommonMain/Network/SSEHandler.swift index 57c7e1b..b54227a 100644 --- a/Sources/CommonMain/Network/SSEHandler.swift +++ b/Sources/CommonMain/Network/SSEHandler.swift @@ -161,7 +161,7 @@ extension SSEHandler { private func shouldReconnect(statusCode: Int) -> Bool { switch statusCode { case 200: - return false + return true case _ where statusCode > 200 && statusCode < 300: return true default: