diff --git a/TidepoolServiceKit/DeviceLogUploader.swift b/TidepoolServiceKit/DeviceLogUploader.swift index 61b5a18..4ed7344 100644 --- a/TidepoolServiceKit/DeviceLogUploader.swift +++ b/TidepoolServiceKit/DeviceLogUploader.swift @@ -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") } } } diff --git a/TidepoolServiceKit/TidepoolService.swift b/TidepoolServiceKit/TidepoolService.swift index b1a1b1f..3089277 100644 --- a/TidepoolServiceKit/TidepoolService.swift +++ b/TidepoolServiceKit/TidepoolService.swift @@ -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) @@ -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 @@ -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) { @@ -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 {