Created
August 29, 2022 13:02
-
-
Save wotjd/e5da0434fb28ea8d6fd6d3d7825fcfdf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
// MARK: DeepLink | |
final class DeepLink { | |
private let url: URL | |
private lazy var pathComponents = self.paths | |
private var paths: [String] { | |
[self.url.host].compact() + self.url.pathComponents.dropFirst() | |
} | |
var currentPath: String? { | |
self.pathComponents.first | |
} | |
init(url: URL) { | |
self.url = url | |
} | |
func path(at index: Int) -> String? { | |
self.paths[safe: index] | |
} | |
@discardableResult | |
func consume() -> String? { | |
self.pathComponents.dequeue() | |
} | |
} | |
// MARK: DeepLinkContainer | |
final class DeepLinkContainer: ObservableObject { | |
private var deepLink: DeepLink? | |
private var handlers: [(String) -> Bool] = [] | |
func replace(url: URL) { | |
self.deepLink = DeepLink(url: url) | |
let handlers = self.handlers | |
handlers.forEach { | |
self.poke($0, appends: false) | |
} | |
} | |
@discardableResult | |
func poke( | |
_ handler: @escaping (String) -> Bool, | |
appends: Bool = true | |
) -> Bool { | |
if appends { | |
self.handlers.append(handler) | |
} | |
// print("path: \(self.deepLink?.currentPath)") | |
guard | |
let path = self.deepLink?.currentPath, | |
handler(path) | |
else { return false } | |
self.deepLink?.consume() | |
return true | |
} | |
func poke<Flow: RawRepresentable>( | |
_ consume: @escaping (Flow) -> Void | |
) where Flow.RawValue == String { | |
self.poke { | |
guard let flow = Flow(rawValue: $0) else { return false } | |
consume(flow) | |
return true | |
} | |
} | |
func removeLastHanler() { | |
self.handlers.removeLast() | |
// print(self.handlers.count) | |
} | |
} | |
// MARK: Resolver | |
private struct PathResolver: ViewModifier { | |
@EnvironmentObject var deepLinkContainer: DeepLinkContainer | |
let action: (String) -> Bool | |
func body(content: Content) -> some View { | |
content | |
.onAppear { self.deepLinkContainer.poke(self.action) } | |
.onDisappear { self.deepLinkContainer.removeLastHanler() } | |
} | |
} | |
private struct FlowResolver<Flow: RawRepresentable>: ViewModifier where Flow.RawValue == String { | |
@EnvironmentObject var deepLinkContainer: DeepLinkContainer | |
let action: (Flow) -> Void | |
func body(content: Content) -> some View { | |
content | |
.onAppear { self.deepLinkContainer.poke(self.action) } | |
.onDisappear { self.deepLinkContainer.removeLastHanler() } | |
} | |
} | |
// MARK: View Extension | |
extension View { | |
func onDeepLink(perform: @escaping (String) -> Bool) -> some View { | |
self.modifier((PathResolver(action: perform))) | |
} | |
func onDeepLink<Flow: RawRepresentable>( | |
flowType: Flow.Type, | |
perform: @escaping (Flow) -> Void | |
) -> some View where Flow.RawValue == String { | |
self.modifier(FlowResolver(action: perform)) | |
} | |
func onDeepLink(for path: String, perform: @escaping () -> Void) -> some View { | |
self.onDeepLink { | |
guard path == $0 else { return false } | |
perform() | |
return true | |
} | |
} | |
func onDeepLink<Flow: RawRepresentable>( | |
for flow: Flow, | |
perform: @escaping () -> Void | |
) -> some View where Flow.RawValue == String { | |
self.onDeepLink(for: flow.rawValue, perform: perform) | |
} | |
} | |
// usage | |
enum MainFlow: String { | |
case home, login | |
} | |
struct SwiftUIDeepLinkApp: App { | |
@StateObject var deepLinkContainer = DeepLinkContainer() | |
@State var mainFlow = MainFlow.signup | |
var body: some Scene { | |
WindowGroup { | |
Group { | |
Group { | |
switch self.mainFlow { | |
case .home: | |
HomeView() | |
case .login: | |
LoginView() | |
} | |
} | |
.onDeepLink(flowType: MainFlow.self) { | |
self.mainFlow = $0 | |
} | |
} | |
.environmentObject(self.deepLinkContainer) | |
.onOpenURL { self.deepLinkContainer.replace(url: $0) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment