diff --git a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/Model/ObservablePublisher.swift b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/Model/ObservablePublisher.swift index cf08ec248..fb5231ebf 100644 --- a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/Model/ObservablePublisher.swift +++ b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/Model/ObservablePublisher.swift @@ -1,9 +1,11 @@ import Combine import AblyAssetTrackingPublisher import Foundation +import Logging class ObservablePublisher: ObservableObject { private let publisher: AblyAssetTrackingPublisher.Publisher + private let logger: Logger? let configInfo: PublisherConfigInfo let locationHistoryDataHandler: LocationHistoryDataHandlerProtocol? @@ -22,11 +24,12 @@ class ObservablePublisher: ObservableObject { @Published private(set) var lastError: ErrorInformation? // After initialising an ObservablePublisher instance, you need to manually set the publisher’s delegate to the created instance. - init(publisher: AblyAssetTrackingPublisher.Publisher, configInfo: PublisherConfigInfo, locationHistoryDataHandler: LocationHistoryDataHandlerProtocol? = nil) { + init(publisher: AblyAssetTrackingPublisher.Publisher, configInfo: PublisherConfigInfo, locationHistoryDataHandler: LocationHistoryDataHandlerProtocol? = nil, logger: Logger? = nil) { self.publisher = publisher self.configInfo = configInfo self.routingProfile = publisher.routingProfile self.locationHistoryDataHandler = locationHistoryDataHandler + self.logger = logger } func stop(completion: @escaping ResultHandler<Void>) { @@ -89,6 +92,19 @@ extension ObservablePublisher: PublisherDelegate { } func publisher(sender: AblyAssetTrackingPublisher.Publisher, didFinishRecordingLocationHistoryData locationHistoryData: LocationHistoryData) { + if SettingsModel.shared.logLocationHistoryJSON { + do { + let jsonData = try JSONEncoder().encode(locationHistoryData) + if let jsonString = String(data: jsonData, encoding: .utf8) { + logger?.log(level: .debug, "Received location history data: \(jsonString)") + } else { + logger?.log(level: .error, "Failed to convert location history data to string") + } + } catch { + logger?.log(level: .error, "Failed to serialize location history data to JSON: \(error.localizedDescription)") + } + } + locationHistoryDataHandler?.handleLocationHistoryData(locationHistoryData) } diff --git a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/Model/SettingsModel.swift b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/Model/SettingsModel.swift index e822d2d6b..850d09bf2 100644 --- a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/Model/SettingsModel.swift +++ b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/Model/SettingsModel.swift @@ -67,6 +67,15 @@ class SettingsModel { } } + var logLocationHistoryJSON: Bool { + get { + UserDefaults.standard.bool(forKey: "logLocationHistoryJSON") + } + set { + UserDefaults.standard.set(newValue, forKey: "logLocationHistoryJSON") + } + } + var vehicleProfile: VehicleProfile { get { guard let profile: VehicleProfile = UserDefaults.standard.get("vehicleProfile") else { diff --git a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/View/Settings/SettingsView.swift b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/View/Settings/SettingsView.swift index b87471ce2..b6e0b4dda 100644 --- a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/View/Settings/SettingsView.swift +++ b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/View/Settings/SettingsView.swift @@ -45,6 +45,15 @@ struct SettingsView: View { } header: { Text("Other settings") } + + Section { + Toggle(isOn: $viewModel.logLocationHistoryJSON) { + Text("Log location history JSON") + Text("Causes the app to emit a `debug` level log message when a `LocationHistoryData` is received from the Asset Tracking SDK. The log message will contain a JSON serialization of this history data.") + } + } header: { + Text("Developer settings") + } } .listStyle(.grouped) .navigationBarTitle("Settings") diff --git a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/ViewModel/CreatePublisherViewModel.swift b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/ViewModel/CreatePublisherViewModel.swift index 977c97991..db7b3c6c1 100644 --- a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/ViewModel/CreatePublisherViewModel.swift +++ b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/ViewModel/CreatePublisherViewModel.swift @@ -136,7 +136,7 @@ class CreatePublisherViewModel: ObservableObject { let configInfo = ObservablePublisher.PublisherConfigInfo(areRawLocationsEnabled: areRawLocationsEnabled, constantResolution: constantResolution) - let observablePublisher = ObservablePublisher(publisher: publisher, configInfo: configInfo, locationHistoryDataHandler: locationHistoryDataHandler) + let observablePublisher = ObservablePublisher(publisher: publisher, configInfo: configInfo, locationHistoryDataHandler: locationHistoryDataHandler, logger: logger) publisher.delegate = observablePublisher return observablePublisher diff --git a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/ViewModel/SettingsViewModel.swift b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/ViewModel/SettingsViewModel.swift index 6ccee8baa..e7b755b62 100644 --- a/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/ViewModel/SettingsViewModel.swift +++ b/Examples/PublisherExampleSwiftUI/PublisherExampleSwiftUI/ViewModel/SettingsViewModel.swift @@ -9,6 +9,12 @@ class SettingsViewModel: ObservableObject { } } + @Published var logLocationHistoryJSON: Bool = SettingsModel.shared.logLocationHistoryJSON { + didSet { + SettingsModel.shared.logLocationHistoryJSON = logLocationHistoryJSON + } + } + @Published var defaultResolutionMinimumDisplacement: String = "\(SettingsModel.shared.defaultResolution.minimumDisplacement)" @Published var defaultResolutionDesiredInterval: String = "\(SettingsModel.shared.defaultResolution.desiredInterval)"