Skip to content
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
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the method defaults to .automatic, but truly the method is not included in the notification and thus is unknown. Does this matter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will still accomplish it's main goal of letting data science track which timezone the user is in.

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