Skip to content

Commit d57bbd7

Browse files
authored
History Recorder (#3960)
* vk-history-recorder: added HistoryRecorder; untied Navigation and HistoryRecorderHandler; Updated relative parts and tests to use new handler; swapped method deprecation for HistoryRecording; CHANGELOG updated; appended code doc for HistoryRecroding
1 parent 002fb9b commit d57bbd7

File tree

10 files changed

+91
-74
lines changed

10 files changed

+91
-74
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* All logs that Navigation SDK produces are now sent to the `MapboxCommon` framework. You can intercept these logs in your own code using `LogConfiguration.registerLogWriterBackend(forLogWriter:)` method from `MapboxCommon` framework. ([#3944](https://github.com/mapbox/mapbox-navigation-ios/pull/3944))
5252
* Fixed an issue where popped window doesn't get updated in appearance when style changes on phone. ([#3954](https://github.com/mapbox/mapbox-navigation-ios/pull/3954))
5353
* Fixed an issue where detailed feedback items don't change color in different style. ([#3954](https://github.com/mapbox/mapbox-navigation-ios/pull/3954))
54+
* Update method deprecation for `HistoryRecording` protocol. Static methods are now preferred over instance ones. ([#3960](https://github.com/mapbox/mapbox-navigation-ios/pull/3960))
5455

5556
## v2.5.1
5657

MapboxNavigation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
2B01E4B7274671550002A5F7 /* RoutingProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B01E4B4274671540002A5F7 /* RoutingProvider.swift */; };
3939
2B01E4B8274671550002A5F7 /* Directions+RoutingProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B01E4B5274671550002A5F7 /* Directions+RoutingProvider.swift */; };
4040
2B07444124B4832400615E87 /* TokenTestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B07444024B4832400615E87 /* TokenTestViewController.swift */; };
41+
2B27557F2860B01A002D1CAD /* HistoryRecorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B27557E2860B01A002D1CAD /* HistoryRecorder.swift */; };
4142
2B28AD7D284F68A1001CD1FB /* AlternativeRouteDetectionStrategy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B28AD7C284F68A1001CD1FB /* AlternativeRouteDetectionStrategy.swift */; };
4243
2B28E22127EB48C60029E4C1 /* RerouteControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B28E22027EB48C60029E4C1 /* RerouteControllerDelegate.swift */; };
4344
2B3ED38C2609FA7900861A84 /* ArrivalController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B3ED38B2609FA7900861A84 /* ArrivalController.swift */; };
@@ -597,6 +598,7 @@
597598
2B01E4B4274671540002A5F7 /* RoutingProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoutingProvider.swift; sourceTree = "<group>"; };
598599
2B01E4B5274671550002A5F7 /* Directions+RoutingProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Directions+RoutingProvider.swift"; sourceTree = "<group>"; };
599600
2B07444024B4832400615E87 /* TokenTestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokenTestViewController.swift; sourceTree = "<group>"; };
601+
2B27557E2860B01A002D1CAD /* HistoryRecorder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HistoryRecorder.swift; sourceTree = "<group>"; };
600602
2B28AD7C284F68A1001CD1FB /* AlternativeRouteDetectionStrategy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AlternativeRouteDetectionStrategy.swift; sourceTree = "<group>"; };
601603
2B28E22027EB48C60029E4C1 /* RerouteControllerDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RerouteControllerDelegate.swift; sourceTree = "<group>"; };
602604
2B3ED38B2609FA7900861A84 /* ArrivalController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrivalController.swift; sourceTree = "<group>"; };
@@ -1988,6 +1990,7 @@
19881990
C5ADFBCC1DDCC7840011824B /* MapboxCoreNavigation.h */,
19891991
2BEF16462775C8FD0085E3C6 /* MapMatchingResult.swift */,
19901992
41B901EA271048BD007F9F78 /* HistoryRecording.swift */,
1993+
2B27557E2860B01A002D1CAD /* HistoryRecorder.swift */,
19911994
E2108B2F285866C200CB0875 /* NavigationLog.swift */,
19921995
2BF398C2274FE99A000C9A72 /* HandlerFactory.swift */,
19931996
2BBED92E265E2C7D00F90032 /* NativeHandlersFactory.swift */,
@@ -2920,6 +2923,7 @@
29202923
E2C623A726CBFCE1005769FA /* OnMainQueue.swift in Sources */,
29212924
C5C94C1C1DDCD2340097296A /* LegacyRouteController.swift in Sources */,
29222925
3A8187C924BDAE9C00708F19 /* URLSession.swift in Sources */,
2926+
2B27557F2860B01A002D1CAD /* HistoryRecorder.swift in Sources */,
29232927
359574A81F28CC5A00838209 /* CLLocation.swift in Sources */,
29242928
DA2A01F025F308B100AAB4C6 /* RoadGraphPath.swift in Sources */,
29252929
DAF27248264E028B00C0AC37 /* Geometry.swift in Sources */,

Sources/MapboxCoreNavigation/CoreNavigationNavigator.swift

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,15 @@ class Navigator {
9696
let factory = NativeHandlersFactory(tileStorePath: tileStorePath ?? "",
9797
credentials: NavigationSettings.shared.directions.credentials,
9898
tilesVersion: Self.tilesVersion,
99-
historyDirectoryURL: Self.historyDirectoryURL,
10099
datasetProfileIdentifier: Self.datasetProfileIdentifier,
101100
routingProviderSource: NavigationSettings.shared.routingProviderSource.nativeSource)
102101
tileStore = factory.tileStore
103-
historyRecorder = factory.historyRecorder
104102
cacheHandle = factory.cacheHandle
105103
roadGraph = factory.roadGraph
106104
navigator = factory.navigator
107105
roadObjectStore = RoadObjectStore(navigator.roadObjectStore())
108106
roadObjectMatcher = RoadObjectMatcher(MapboxNavigationNative.RoadObjectMatcher(cache: cacheHandle))
109-
rerouteController = RerouteController(navigator, config: factory.navigatorConfig)
107+
rerouteController = RerouteController(navigator, config: NativeHandlersFactory.navigatorConfig)
110108

111109
subscribeNavigator()
112110
setupAlternativesControllerIfNeeded()
@@ -125,19 +123,17 @@ class Navigator {
125123
let factory = NativeHandlersFactory(tileStorePath: NavigationSettings.shared.tileStoreConfiguration.navigatorLocation.tileStoreURL?.path ?? "",
126124
credentials: NavigationSettings.shared.directions.credentials,
127125
tilesVersion: version ?? Self.tilesVersion,
128-
historyDirectoryURL: Self.historyDirectoryURL,
129126
targetVersion: version.map { _ in Self.tilesVersion },
130127
datasetProfileIdentifier: Self.datasetProfileIdentifier,
131128
routingProviderSource: NavigationSettings.shared.routingProviderSource.nativeSource)
132129
tileStore = factory.tileStore
133-
historyRecorder = factory.historyRecorder
134130
cacheHandle = factory.cacheHandle
135131
roadGraph = factory.roadGraph
136132
navigator = factory.navigator
137133

138134
roadObjectStore.native = navigator.roadObjectStore()
139135
roadObjectMatcher.native = MapboxNavigationNative.RoadObjectMatcher(cache: cacheHandle)
140-
rerouteController = RerouteController(navigator, config: factory.navigatorConfig)
136+
rerouteController = RerouteController(navigator, config: NativeHandlersFactory.navigatorConfig)
141137

142138
subscribeNavigator()
143139
setupAlternativesControllerIfNeeded()
@@ -206,18 +202,7 @@ class Navigator {
206202
self.navigatorAlternativesObserver = nil
207203
}
208204
}
209-
210-
// MARK: History
211-
212-
/**
213-
Path to the directory where history file could be stored when `HistoryRecording.stopRecordingHistory(writingFileWith:)` is called.
214-
215-
Setting `nil` disables history recording. Defaults to `nil`.
216-
*/
217-
static var historyDirectoryURL: URL? = nil
218-
219-
private(set) var historyRecorder: HistoryRecorderHandle?
220-
205+
221206
// MARK: Electronic horizon
222207

223208
private(set) var roadGraph: RoadGraph

Sources/MapboxCoreNavigation/HandlerFactory.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ protocol HandlerData {
55
var tileStorePath: String { get }
66
var credentials: Credentials { get }
77
var tilesVersion: String { get }
8-
var historyDirectoryURL: URL? { get }
98
var targetVersion: String? { get }
109
var configFactoryType: ConfigFactory.Type { get }
1110
var datasetProfileIdentifier: ProfileIdentifier { get }
@@ -25,7 +24,6 @@ class HandlerFactory<HandlerType, Arguments> {
2524
let tileStorePath: String
2625
let credentials: Credentials
2726
let tilesVersion: String
28-
let historyDirectoryURL: URL?
2927
let targetVersion: String?
3028
let configFactoryType: ConfigFactory.Type
3129
let datasetProfileIdentifier: ProfileIdentifier
@@ -34,7 +32,6 @@ class HandlerFactory<HandlerType, Arguments> {
3432
self.tileStorePath = data.tileStorePath
3533
self.credentials = data.credentials
3634
self.tilesVersion = data.tilesVersion
37-
self.historyDirectoryURL = data.historyDirectoryURL
3835
self.targetVersion = data.targetVersion
3936
self.configFactoryType = data.configFactoryType
4037
self.datasetProfileIdentifier = data.datasetProfileIdentifier
@@ -44,7 +41,6 @@ class HandlerFactory<HandlerType, Arguments> {
4441
return lhs.tileStorePath != rhs.tileStorePath ||
4542
lhs.credentials != rhs.credentials ||
4643
lhs.tilesVersion != rhs.tilesVersion ||
47-
lhs.historyDirectoryURL?.absoluteString != rhs.historyDirectoryURL?.absoluteString ||
4844
lhs.targetVersion != rhs.targetVersion ||
4945
lhs.configFactoryType != rhs.configFactoryType ||
5046
lhs.datasetProfileIdentifier != rhs.datasetProfileIdentifier
@@ -76,12 +72,6 @@ class HandlerFactory<HandlerType, Arguments> {
7672
}
7773
}
7874

79-
let historyRecorderHandlerFactory = HandlerFactory { (path: String,
80-
configHandle: ConfigHandle) in
81-
HistoryRecorderHandle.build(forHistoryDir: path,
82-
config: configHandle)
83-
}
84-
8575
let cacheHandlerFactory = HandlerFactory { (tilesConfig: TilesConfig,
8676
config: ConfigHandle,
8777
historyRecorder: HistoryRecorderHandle?) in
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import MapboxNavigationNative
2+
3+
4+
class HistoryRecorder {
5+
/**
6+
Path to the directory where history file could be stored when `HistoryRecording.stopRecordingHistory(writingFileWith:)` is called.
7+
8+
Setting `nil` disables history recording. Defaults to `nil`. Updating value from `nil` to `non-nil` value results in recreating the shared instance since `nil` guaranteed an invalid handler. Further updates have no effect.
9+
*/
10+
static var historyDirectoryURL: URL? = nil
11+
{
12+
didSet {
13+
if _historyRecorder?.handle == nil && historyDirectoryURL != nil {
14+
_historyRecorder = nil
15+
} else if oldValue != nil {
16+
Log.warning("`historyDirectoryURL` is updated excessively.", category: .settings)
17+
}
18+
}
19+
}
20+
21+
/**
22+
The shared instance
23+
*/
24+
static var shared: HistoryRecorder {
25+
guard let historyRecorder = _historyRecorder else {
26+
let historyRecorder = HistoryRecorder()
27+
_historyRecorder = historyRecorder
28+
return historyRecorder
29+
}
30+
return historyRecorder
31+
}
32+
33+
/// `True` when `HistoryRecorder.shared` requested at least once.
34+
static var isSharedInstanceCreated: Bool {
35+
_historyRecorder != nil
36+
}
37+
38+
// Used in tests to recreate the history recorder
39+
static func _recreateHistoryRecorder() { _historyRecorder = nil }
40+
41+
private static var _historyRecorder: HistoryRecorder?
42+
43+
private(set) var handle: HistoryRecorderHandle? = nil
44+
45+
private init() {
46+
Self.historyDirectoryURL.flatMap {
47+
handle = HistoryRecorderHandle.build(forHistoryDir: $0.path,
48+
config: NativeHandlersFactory.configHandle())
49+
}
50+
}
51+
}

Sources/MapboxCoreNavigation/HistoryRecording.swift

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public protocol HistoryRecording {
1414
/**
1515
Path to the directory where history file could be stored when `stopRecordingHistory(writingFileWith:)` is called.
1616

17-
Setting `nil` disables history recording. Defaults to `nil`.
17+
Setting `nil` disables history recording. Defaults to `nil`. Updating value from `nil` to `non-nil` value results in recreating the shared instance since `nil` guaranteed an invalid handler. Further updates have no effect.
1818
*/
1919
static var historyDirectoryURL: URL? { get set }
2020

@@ -23,14 +23,14 @@ public protocol HistoryRecording {
2323

2424
- postcondition: Use the `stopRecordingHistory(writingFileWith:)` method to stop recording history and write the recorded history to a file.
2525
*/
26-
@available(*, deprecated, message: "Use corresponding instance method instead.")
2726
static func startRecordingHistory()
2827

2928
/**
3029
Starts recording history for debugging purposes.
3130

3231
- postcondition: Use the `stopRecordingHistory(writingFileWith:)` method to stop recording history and write the recorded history to a file.
3332
*/
33+
@available(*, deprecated, message: "Use corresponding static method instead.")
3434
func startRecordingHistory()
3535

3636
/**
@@ -41,7 +41,6 @@ public protocol HistoryRecording {
4141

4242
- precondition: Use the `startRecordingHistory()` method to begin recording history. If the `startRecordingHistory()` method has not been called, this method has no effect.
4343
*/
44-
@available(*, deprecated, message: "Use corresponding instance method instead.")
4544
static func pushHistoryEvent(type: String, jsonData: Data?) throws
4645

4746
/**
@@ -52,6 +51,7 @@ public protocol HistoryRecording {
5251

5352
- precondition: Use the `startRecordingHistory()` method to begin recording history. If the `startRecordingHistory()` method has not been called, this method has no effect.
5453
*/
54+
@available(*, deprecated, message: "Use corresponding static method instead.")
5555
func pushHistoryEvent(type: String, jsonData: Data?) throws
5656

5757
/**
@@ -64,7 +64,6 @@ public protocol HistoryRecording {
6464

6565
- parameter completionHandler: A closure to be executed when the history file is ready.
6666
*/
67-
@available(*, deprecated, message: "Use corresponding instance method instead.")
6867
static func stopRecordingHistory(writingFileWith completionHandler: @escaping HistoryFileWritingCompletionHandler)
6968

7069
/**
@@ -77,6 +76,7 @@ public protocol HistoryRecording {
7776

7877
- parameter completionHandler: A closure to be executed when the history file is ready.
7978
*/
79+
@available(*, deprecated, message: "Use corresponding static method instead.")
8080
func stopRecordingHistory(writingFileWith completionHandler: @escaping HistoryFileWritingCompletionHandler)
8181
}
8282

@@ -86,10 +86,10 @@ public protocol HistoryRecording {
8686
public extension HistoryRecording {
8787
static var historyDirectoryURL: URL? {
8888
get {
89-
Navigator.historyDirectoryURL
89+
HistoryRecorder.historyDirectoryURL
9090
}
9191
set {
92-
Navigator.historyDirectoryURL = newValue
92+
HistoryRecorder.historyDirectoryURL = newValue
9393
}
9494
}
9595

@@ -102,9 +102,7 @@ public extension HistoryRecording {
102102
}
103103

104104
static func startRecordingHistoryImplementation() {
105-
if Navigator.isSharedInstanceCreated {
106-
Navigator.shared.historyRecorder?.startRecording()
107-
}
105+
HistoryRecorder.shared.handle?.startRecording()
108106
}
109107

110108
static func pushHistoryEvent(type: String, jsonData: Data?) throws {
@@ -124,8 +122,8 @@ public extension HistoryRecording {
124122
}
125123
jsonString = value
126124
}
127-
if Navigator.isSharedInstanceCreated {
128-
Navigator.shared.historyRecorder?.pushHistory(forEventType: type, eventJson: jsonString ?? "")
125+
if HistoryRecorder.isSharedInstanceCreated {
126+
HistoryRecorder.shared.handle?.pushHistory(forEventType: type, eventJson: jsonString ?? "")
129127
}
130128
}
131129

@@ -138,8 +136,8 @@ public extension HistoryRecording {
138136
}
139137

140138
private static func stopRecordingHistoryImplementation(writingFileWith completionHandler: @escaping HistoryFileWritingCompletionHandler) {
141-
guard Navigator.isSharedInstanceCreated,
142-
let historyRecorder = Navigator.shared.historyRecorder else {
139+
guard HistoryRecorder.isSharedInstanceCreated,
140+
let historyRecorder = HistoryRecorder.shared.handle else {
143141
completionHandler(nil)
144142
return
145143
}

Sources/MapboxCoreNavigation/MapboxRoutingProvider.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,12 @@ public class MapboxRoutingProvider: RoutingProvider {
122122
let factory = NativeHandlersFactory(tileStorePath: settings.tileStoreConfiguration.navigatorLocation.tileStoreURL?.path ?? "",
123123
credentials: settings.directions.credentials,
124124
tilesVersion: Navigator.tilesVersion,
125-
historyDirectoryURL: Navigator.historyDirectoryURL,
126125
datasetProfileIdentifier: datasetProfileIdentifier ?? Navigator.datasetProfileIdentifier,
127126
routingProviderSource: source.nativeSource)
128127
return RouterFactory.build(for: source.nativeSource,
129-
cache: factory.cacheHandle,
130-
config: factory.configHandle,
131-
historyRecorder: factory.historyRecorder)
128+
cache: factory.cacheHandle,
129+
config: NativeHandlersFactory.configHandle(),
130+
historyRecorder: HistoryRecorder.shared.handle)
132131
} ()
133132

134133
private func complete(requestId: RequestId, with result: @escaping () -> Void) {

0 commit comments

Comments
 (0)