-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathVisualTrackingManager.swift
128 lines (105 loc) · 3.77 KB
/
VisualTrackingManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// Copyright 2020 PLAID, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import KarteCore
import UIKit
internal class VisualTrackingManager {
static let shared = VisualTrackingManager()
private var receivers: [ActionReceiver] = []
var tracer: ActionTracer?
var tracker: ActionTracker?
var isPaired: Bool {
tracer?.pairingClient?.isPaired ?? false
}
private var notificationObserverToken: NSObjectProtocol?
init() {
}
func configure(app: KarteApp) {
let tracer = ActionTracer(app: app)
self.tracer = tracer
let tracker = ActionTracker(app: app)
self.tracker = tracker
register(receiver: tracer)
register(receiver: tracker)
let config: VisualTrackingConfiguration = app.libraryConfiguration() ?? VisualTrackingConfiguration()
if config.automaticallyCollectLogs {
UIApplicationProxy.shared.swizzleMethods()
UIGestureRecognizerProxy.shared.swizzleMethods()
UINavigationControllerProxy.shared.swizzleMethods()
UIViewControllerProxy.shared.swizzleMethods()
}
notificationObserverToken = NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { [weak self] _ in
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {[weak self] in
if let config = self?.tracker?.app.configuration as? ExperimentalConfiguration, config.operationMode == OperationMode.ingest {
self?.tracker?.refreshDefinitions()
}
}
if let notificationObserverToken = self?.notificationObserverToken {
NotificationCenter.default.removeObserver(notificationObserverToken)
}
}
}
func unconfigure(app: KarteApp) {
tracer?.stopPairing()
receivers.removeAll()
tracer = nil
tracker = nil
}
func register(receiver: ActionReceiver) {
receivers.append(receiver)
}
func dispatch(action: ActionProtocol?) {
guard let action = action else {
Logger.warn(tag: .visualTracking, message: "Action is invalid.")
return
}
receivers.forEach { receiver in
receiver.receive(action: action)
}
}
deinit {
}
}
extension VisualTrackingManager: ActionModule, DeepLinkModule, TrackModule {
var name: String {
String(describing: type(of: self))
}
var queue: DispatchQueue? {
nil
}
func receive(response: [String: JSONValue], request: TrackRequest) {
tracker?.refreshDefinitions(response: response)
}
func reset(sceneId: SceneId) {
}
func resetAll() {
}
func handle(app: UIApplication, open url: URL) -> Bool {
guard let account = Account(url: url) else {
return false
}
tracer?.startPairing(account: account)
return true
}
func intercept(urlRequest: URLRequest) throws -> URLRequest {
guard let tracker = tracker else {
return urlRequest
}
return try tracker.intercept(urlRequest: urlRequest)
}
func provideEventRejectionFilterRules() -> [TrackEventRejectionFilterRule] {
return []
}
}