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

[LOOP-5035] report time zone changes #105

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions TidepoolServiceKit/TidepoolService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public final class TidepoolService: Service, TAPIObserver, ObservableObject {
await tapi.addObserver(self)
deviceLogUploader = DeviceLogUploader(api: tapi)
await deviceLogUploader?.setDelegate(remoteDataServiceDelegate)
observeTimeZoneChanges()
}

public init?(rawState: RawStateValue) {
Expand Down Expand Up @@ -485,6 +486,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