From 34abdee9eb7cb41679ed0f5b052a9bdf31b10c68 Mon Sep 17 00:00:00 2001 From: Jimmy Sanghani Date: Wed, 31 Aug 2022 14:40:07 +0530 Subject: [PATCH] Remove AnyView dependency * Remove AnyView dependency * Update version --- README.md | 4 +-- Sources/UIPilot/UIPilot.swift | 53 ++++++++++++++++++----------------- UIPilot.podspec | 2 +- docs/index.md | 4 +-- 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index da1b168..ecc3a2d 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Once you have your Swift package set up, adding UIPilot as a dependency is as ea ```swift dependencies: [ - .package(url: "https://github.com/canopas/UIPilot.git", .upToNextMajor(from: "1.2.1")) + .package(url: "https://github.com/canopas/UIPilot.git", .upToNextMajor(from: "1.3.0")) ] ``` @@ -47,7 +47,7 @@ dependencies: [ [CocoaPods][] is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate UIPilot into your Xcode project using CocoaPods, specify it in your Podfile: target 'YourAppName' do - pod 'UIPilot', '~> 1.2.1' + pod 'UIPilot', '~> 1.3.0' end [CocoaPods]: https://cocoapods.org diff --git a/Sources/UIPilot/UIPilot.swift b/Sources/UIPilot/UIPilot.swift index 64a776e..7feb889 100644 --- a/Sources/UIPilot/UIPilot.swift +++ b/Sources/UIPilot/UIPilot.swift @@ -60,7 +60,7 @@ public class UIPilot: ObservableObject { paths.removeLast(numToPop) } - func getView(_ paths: [UIPilotPath], _ routeMap: RouteMap, _ pathViews: [UIPilotPath: PathView]) -> (PathView?, [UIPilotPath: PathView]) { + func getView(_ paths: [UIPilotPath], _ routeMap: RouteMap, _ pathViews: [UIPilotPath: Screen]) -> (PathView?, [UIPilotPath: Screen]) { return viewGenerator.generate(paths, routeMap, pathViews) } } @@ -78,11 +78,11 @@ struct UIPilotPath: Equatable, Hashable { } } -struct PathView: View { - private let content: AnyView - @ObservedObject var state: PathViewState +struct PathView: View { + private let content: Screen + @ObservedObject var state: PathViewState - public init(_ content: AnyView, state: PathViewState) { + public init(_ content: Screen, state: PathViewState) { self.content = content self.state = state } @@ -100,7 +100,7 @@ struct PathView: View { } } -class PathViewState: ObservableObject { +class PathViewState: ObservableObject { @Published var isActive: Bool = false { didSet { @@ -111,7 +111,7 @@ class PathViewState: ObservableObject { } @Published - var next: PathView? { + var next: PathView? { didSet { isActive = next != nil } @@ -119,7 +119,7 @@ class PathViewState: ObservableObject { var onPop: () -> Void - init(next: PathView? = nil, onPop: @escaping () -> Void = {}) { + init(next: PathView? = nil, onPop: @escaping () -> Void = {}) { self.next = next self.onPop = onPop } @@ -129,20 +129,22 @@ class PathViewGenerator { var onPop: ((UIPilotPath) -> Void)? - func generate(_ paths: [UIPilotPath], _ routeMap: RouteMap, _ pathViews: [UIPilotPath: PathView]) -> (PathView?, [UIPilotPath: PathView]) { + func generate( + _ paths: [UIPilotPath], + @ViewBuilder _ routeMap: RouteMap, + _ pathViews: [UIPilotPath: Screen]) -> (PathView?, + [UIPilotPath: Screen]) { var pathViews = recycleViews(paths, pathViews: pathViews) - var current: PathView? + var current: PathView? for path in paths.reversed() { - var content = pathViews[path] + let view = pathViews[path] ?? routeMap(path.route) + pathViews[path] = view + + let content = PathView(view, state: PathViewState()) - if content == nil { - pathViews[path] = PathView(routeMap(path.route), state: PathViewState()) - content = pathViews[path] - } - - content?.state.next = current - content?.state.onPop = current == nil ? {} : { [weak self] in + content.state.next = current + content.state.onPop = current == nil ? {} : { [weak self] in if let self = self { self.onPop?(path) } @@ -152,7 +154,7 @@ class PathViewGenerator { return (current, pathViews) } - private func recycleViews(_ paths: [UIPilotPath], pathViews: [UIPilotPath: PathView]) -> [UIPilotPath: PathView] { + private func recycleViews(_ paths: [UIPilotPath], pathViews: [UIPilotPath: Screen]) -> [UIPilotPath: Screen] { var pathViews = pathViews for key in pathViews.keys { if !paths.contains(key) { @@ -163,21 +165,22 @@ class PathViewGenerator { } } -public typealias RouteMap = (T) -> AnyView +public typealias RouteMap = (T) -> Screen -public struct UIPilotHost: View { +public struct UIPilotHost: View { @ObservedObject private var pilot: UIPilot - private let routeMap: RouteMap + @ViewBuilder + let routeMap: RouteMap @State - var pathViews = [UIPilotPath: PathView]() + var pathViews = [UIPilotPath: Screen]() @State - var content: PathView? + var content: PathView? - public init(_ pilot: UIPilot, _ routeMap: @escaping RouteMap) { + public init(_ pilot: UIPilot, @ViewBuilder _ routeMap: @escaping RouteMap) { self.pilot = pilot self.routeMap = routeMap } diff --git a/UIPilot.podspec b/UIPilot.podspec index 0d364c5..f66e4d7 100644 --- a/UIPilot.podspec +++ b/UIPilot.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "UIPilot" - s.version = "1.2.1" + s.version = "1.3.0" s.summary = "The missing type-safe, SwiftUI navigation library." s.description = <<-DESC diff --git a/docs/index.md b/docs/index.md index c8da772..1184f03 100644 --- a/docs/index.md +++ b/docs/index.md @@ -323,7 +323,7 @@ Once you have your Swift package set up, adding UIPilot as a dependency is as ea ```swift dependencies: [ - .package(url: "https://github.com/canopas/UIPilot.git", .upToNextMajor(from: "1.2.1")) + .package(url: "https://github.com/canopas/UIPilot.git", .upToNextMajor(from: "1.3.0")) ] ``` @@ -332,7 +332,7 @@ dependencies: [ [CocoaPods][] is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate UIPilot into your Xcode project using CocoaPods, specify it in your Podfile: target 'YourAppName' do - pod 'UIPilot', '~> 1.2.1' + pod 'UIPilot', '~> 1.3.0' end [CocoaPods]: https://cocoapods.org