Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug disconnection SSE #58

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Sources/CommonMain/Caching/CachingManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions Sources/CommonMain/Features/FeaturesViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,7 +51,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 {
Expand Down Expand Up @@ -88,12 +94,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
Expand Down
2 changes: 1 addition & 1 deletion Sources/CommonMain/Network/SSEHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions Sources/CommonMain/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading