Skip to content

Commit da11d66

Browse files
authored
Merge pull request #10 from canopas/add-debug-flags
Add debug log
2 parents 5ae1fac + 5cffb76 commit da11d66

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

Sources/UIPilot/DebugLog.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// DebugLog.swift
3+
//
4+
//
5+
// Created by Amisha I on 28/03/22.
6+
//
7+
8+
import Foundation
9+
10+
class DebugLog: Logger {
11+
func log(_ value: String) {
12+
print(value)
13+
}
14+
}

Sources/UIPilot/EmptyLog.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// EmptyLog.swift
3+
//
4+
//
5+
// Created by Amisha I on 28/03/22.
6+
//
7+
8+
import Foundation
9+
10+
class EmptyLog: Logger {
11+
func log(_ value: String) {
12+
13+
}
14+
}

Sources/UIPilot/UIPilot.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,50 @@ import SwiftUI
22
import Combine
33

44
public class UIPilot<T: Equatable>: ObservableObject {
5+
56
var paths: [Path<T>] = [] {
67
didSet { updateViewState() }
78
}
89

910
var routeMap: RouteMap<T>? {
1011
didSet { updateViewState() }
1112
}
12-
13+
14+
let logger: Logger
1315
var state: UIPilotViewState<T>!
1416

15-
public init(initial: T) {
17+
public init(initial: T, debug: Bool = false) {
18+
logger = debug ? DebugLog() : EmptyLog()
19+
logger.log("UIPilot - Pilot Initialized.")
20+
1621
state = UIPilotViewState(onPop: { [weak self] in
1722
self?.pop()
1823
})
1924
push(initial)
2025
}
2126

2227
public func push(_ route: T) {
28+
logger.log("UIPilot - Pushing \(route) route.")
2329
self.paths.append(Path(route: route))
2430
}
2531

2632
public func pop() {
2733
if !self.paths.isEmpty {
34+
logger.log("UIPilot - Route popped.")
2835
self.paths.removeLast()
2936
}
3037
}
3138

3239
public func popTo(_ route: T, inclusive: Bool = false) {
40+
logger.log("UIPilot: Popping route \(route).")
41+
3342
if paths.isEmpty {
43+
logger.log("UIPilot - Path is empty.")
3444
return
3545
}
3646

3747
guard var found = paths.firstIndex(where: { $0.route == route }) else {
48+
logger.log("UIPilot - Route not found.")
3849
return
3950
}
4051

@@ -43,12 +54,14 @@ public class UIPilot<T: Equatable>: ObservableObject {
4354
}
4455

4556
for _ in found..<paths.count {
57+
logger.log("UIPilot - Route \(route) Popped.")
4658
pop()
4759
}
4860
}
4961

5062
private func updateViewState() {
5163
if let routeMap = routeMap {
64+
logger.log("UIPilot - Updating route state.")
5265
state.onPathsChanged(paths: paths, routeMap: routeMap)
5366
}
5467
}
@@ -68,9 +81,7 @@ struct Path<T: Equatable>: Equatable, Hashable {
6881
}
6982

7083
struct PathView: View {
71-
7284
private let content: AnyView
73-
7485
@ObservedObject var state: PathViewState
7586

7687
public init(_ content: AnyView, state: PathViewState) {
@@ -119,11 +130,10 @@ class PathViewState: ObservableObject {
119130
class UIPilotViewState<T: Equatable>: ObservableObject {
120131

121132
private let onPop: () -> Void
122-
123133
private var pathViews = [Path<T>: PathView]()
124134

125135
@Published var content: PathView? = nil
126-
136+
127137
init(onPop: @escaping () -> Void) {
128138
self.onPop = onPop
129139
}
@@ -138,7 +148,7 @@ class UIPilotViewState<T: Equatable>: ObservableObject {
138148
var current: PathView? = nil
139149
for path in paths.reversed() {
140150
var content = pathViews[path]
141-
151+
142152
if content == nil {
143153
pathViews[path] = PathView(routeMap(path.route), state: PathViewState())
144154
content = pathViews[path]
@@ -151,10 +161,8 @@ class UIPilotViewState<T: Equatable>: ObservableObject {
151161
self.onPop()
152162
}
153163
}
154-
155164
current = content
156165
}
157-
158166
return current
159167
}
160168

@@ -192,3 +200,7 @@ public struct UIPilotHost<T: Equatable>: View {
192200
.environmentObject(pilot)
193201
}
194202
}
203+
204+
protocol Logger {
205+
func log(_ value: String)
206+
}

0 commit comments

Comments
 (0)