Skip to content

Commit

Permalink
refactor: remove is_foreground from event producer (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickybondarenko authored Jun 4, 2024
1 parent 0983035 commit da06d45
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 31 deletions.
16 changes: 0 additions & 16 deletions Sources/Confidence/ConfidenceAppLifecycleProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence
// Context Keys
private static var versionNameContextKey = "app_version"
private static var buildNumberContextKey = "app_build"
private static var isForegroundContextKey = "is_foreground"
// Event Names
private static let appLaunchedEventName = "app-launched"
private static let appInstalledEventName = "app-installed"
Expand Down Expand Up @@ -88,17 +87,6 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence
UserDefaults.standard.setValue(currentBuild, forKey: Self.userDefaultBuildNameKey)
}

private func updateContext(isForeground: Bool) {
withLock { [weak self] in
guard let self = self else {
return
}
var context = self.currentProducedContext.value
context.updateValue(.init(boolean: isForeground), forKey: Self.isForegroundContextKey)
self.currentProducedContext.send(context)
}
}

private func withLock(callback: @escaping () -> Void) {
queue.sync {
callback()
Expand All @@ -107,10 +95,6 @@ public class ConfidenceAppLifecycleProducer: ConfidenceEventProducer, Confidence

@objc func notificationResponse(notification: NSNotification) {
switch notification.name {
case UIApplication.didEnterBackgroundNotification:
updateContext(isForeground: false)
case UIApplication.willEnterForegroundNotification:
updateContext(isForeground: true)
case UIApplication.didBecomeActiveNotification:
track(eventName: Self.appLaunchedEventName, shouldFlush: true)
default:
Expand Down
6 changes: 2 additions & 4 deletions Sources/Confidence/ConfidenceScreenTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,8 @@ extension UIViewController {
let encoder = JSONEncoder()
do {
let data = try encoder.encode(trackableWithMessage.trackMessage())
let messageString = String(data: data, encoding: .utf8)
if let json = messageString {
message.updateValue(json, forKey: ConfidenceScreenTracker.messageKey)
}
let messageString = String(decoding: data, as: UTF8.self)
message.updateValue(messageString, forKey: ConfidenceScreenTracker.messageKey)
} catch {
}
}
Expand Down
11 changes: 4 additions & 7 deletions Sources/Confidence/EventStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ internal class EventStorageImpl: EventStorage {
}
let encoder = JSONEncoder()
let serialied = try encoder.encode(event)
let delimiter = "\n".data(using: .utf8)
guard let delimiter else {
return
}
let delimiter = Data("\n".utf8)
currentFileHandle.seekToEndOfFile()
try currentFileHandle.write(contentsOf: delimiter)
try currentFileHandle.write(contentsOf: serialied)
Expand All @@ -78,8 +75,8 @@ internal class EventStorageImpl: EventStorage {
let decoder = JSONDecoder()
let fileUrl = folderURL.appendingPathComponent(id)
let data = try Data(contentsOf: fileUrl)
let dataString = String(data: data, encoding: .utf8)
return try dataString?.components(separatedBy: "\n")
let dataString = String(decoding: data, as: UTF8.self)
return try dataString.components(separatedBy: "\n")
.filter { events in
!events.isEmpty
}
Expand All @@ -88,7 +85,7 @@ internal class EventStorageImpl: EventStorage {
return nil
}
return try decoder.decode(ConfidenceEvent.self, from: stringData)
} ?? []
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Confidence/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ extension NetworkClient {
do {
response.decodedError = try decoder.decode(HttpError.self, from: responseData)
} catch {
let message = String(data: responseData, encoding: String.Encoding.utf8)
let message = String(decoding: responseData, as: UTF8.self)
response.decodedError = HttpError(
code: httpURLResponse.statusCode,
message: message ?? "unknown",
message: message,
details: []
)
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/ConfidenceTests/ConfidenceValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ final class ConfidenceConfidenceValueTests: XCTestCase {
]))
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys
let resultString = try XCTUnwrap(String(data: try encoder.encode(value), encoding: .utf8))
let data = try encoder.encode(value)
let resultString = try XCTUnwrap(String(decoding: data, as: UTF8.self))
let resultData = try XCTUnwrap(resultString.data(using: .utf8))
let decodedValue = try JSONDecoder().decode(ConfidenceValue.self, from: resultData)

Expand Down
2 changes: 1 addition & 1 deletion Tests/ConfidenceTests/Helpers/StorageMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class StorageMock: Storage {
func save(data: Encodable) throws {
try storageQueue.sync {
let dataB = try JSONEncoder().encode(data)
self.data = String(data: dataB, encoding: .utf8) ?? ""
self.data = String(decoding: dataB, as: UTF8.self)

saveExpectation?.fulfill()
}
Expand Down

0 comments on commit da06d45

Please sign in to comment.