Skip to content

Commit

Permalink
[LOOP-5035] report time zone changes (#105)
Browse files Browse the repository at this point in the history
* report time zone changes

* removed unneeded print statement

* set device log uploader when remoteDataServiceDelegate is set
  • Loading branch information
nhamming authored Sep 18, 2024
1 parent 5caeee3 commit e192e32
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
2 changes: 0 additions & 2 deletions TidepoolServiceKit/DeviceLogUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,8 @@ actor DeviceLogUploader {
do {
let metatdata = try await api.uploadDeviceLogs(logs: data, start: start, end: end)
log.default("metadata: %@", String(describing: metatdata))
print("hi")
} catch {
log.error("error uploading device logs:: %@", String(describing: error))
print("hi")
}
}
}
Expand Down
44 changes: 41 additions & 3 deletions TidepoolServiceKit/TidepoolService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ public final class TidepoolService: Service, TAPIObserver, ObservableObject {

public weak var stateDelegate: StatefulPluggableDelegate?

public weak var remoteDataServiceDelegate: RemoteDataServiceDelegate?

public weak var remoteDataServiceDelegate: RemoteDataServiceDelegate? {
didSet {
Task {
await setDeviceLogUploaderDelegate()
}
}
}

public lazy var sessionStorage: SessionStorage = KeychainManager()

public let tapi: TAPI = TAPI(clientId: BuildDetails.default.tidepoolServiceClientId, redirectURL: BuildDetails.default.tidepoolServiceRedirectURL)
Expand All @@ -72,6 +78,10 @@ public final class TidepoolService: Service, TAPIObserver, ObservableObject {
private let tidepoolKitLog = OSLog(category: "TidepoolKit")

private var deviceLogUploader: DeviceLogUploader?

private func setDeviceLogUploaderDelegate() async {
await deviceLogUploader?.setDelegate(remoteDataServiceDelegate)
}

public init(hostIdentifier: String, hostVersion: String) {
self.id = UUID().uuidString
Expand All @@ -87,7 +97,8 @@ public final class TidepoolService: Service, TAPIObserver, ObservableObject {
await tapi.setLogging(self)
await tapi.addObserver(self)
deviceLogUploader = DeviceLogUploader(api: tapi)
await deviceLogUploader?.setDelegate(remoteDataServiceDelegate)
await setDeviceLogUploaderDelegate()
observeTimeZoneChanges()
}

public init?(rawState: RawStateValue) {
Expand Down Expand Up @@ -485,6 +496,33 @@ extension TidepoolService: RemoteDataService {

return (created, updated, lastControllerSettingsDatum, lastCGMSettingsDatum, lastPumpSettingsDatum)
}

private func uploadTimeZoneChangeData(from fromTimeZone: TimeZone, to toTimeZone: TimeZone, method: TTimeChangeDeviceEventDatum.Method = .automatic, at date: Date = Date()) async throws {
guard let userId = userId, let hostIdentifier = hostIdentifier, let hostVersion = hostVersion else {
throw TidepoolServiceError.configuration
}

let timeZoneChangeData = TTimeChangeDeviceEventDatum(time: date,
from: TTimeChangeDeviceEventDatum.Info(timeZoneName: fromTimeZone.identifier),
to: TTimeChangeDeviceEventDatum.Info(timeZoneName: toTimeZone.identifier),
method: method)
let _ = try await createData([timeZoneChangeData])
}

private func observeTimeZoneChanges() {
NotificationCenter.default.addObserver(forName: .NSSystemTimeZoneDidChange, object: nil, queue: .main) { notification in
if let previousTimeZone = notification.object as? TimeZone {
let currentTimeZone = TimeZone.current
Task {
do {
try await self.uploadTimeZoneChangeData(from: previousTimeZone, to: currentTimeZone)
} catch {
self.log.error("Failed to upload time zone change data - %{public}@", error.localizedDescription)
}
}
}
}
}

private func createData(_ data: [TDatum]) async throws -> Bool {
if let error = error {
Expand Down

0 comments on commit e192e32

Please sign in to comment.