Skip to content

Commit

Permalink
feat: Modified the TabEventSubscriber example to support deep links.
Browse files Browse the repository at this point in the history
  • Loading branch information
97chos committed May 8, 2024
1 parent d9ea178 commit 06da035
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>tab-e-s</string>
</array>
</dict>
<dict/>
</array>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,69 @@ struct TabEventSubscriberApp: App {
routeBuilderItemList: AppRouterBuilderGroup().routers,
dependency: AppDependency())

let tabList: [TabItem] = [
.init(
tag: .zero,
tabItem: .init(
title: "tab1",
image: UIImage(systemName: "house"),
selectedImage: UIImage(systemName: "house.fill")),
linkItem: .init(path: "tab1")),
.init(
tag: 1,
tabItem: .init(
title: "tab2",
image: UIImage(systemName: "folder"),
selectedImage: UIImage(systemName: "folder.fill")),
linkItem: .init(path: "tab2")),
.init(
tag: 2,
tabItem: .init(
title: "tab3",
image: UIImage(systemName: "heart"),
selectedImage: UIImage(systemName: "heart.fill")),
linkItem: .init(path: "tab3")),
.init(
tag: 3,
tabItem: .init(
title: "tab4",
image: UIImage(systemName: "person"),
selectedImage: UIImage(systemName: "person.fill")),
linkItem: .init(path: "tab4")),
]

var body: some Scene {
WindowGroup {
TabLinkNavigationView(
linkNavigator: tabLinkNavigator,
isHiddenDefaultTabbar: false,
tabItemList: [
.init(
tag: .zero,
tabItem: .init(
title: "tab1",
image: UIImage(systemName: "house"),
selectedImage: UIImage(systemName: "house.fill")),
linkItem: .init(path: "tab1")),
.init(
tag: 1,
tabItem: .init(
title: "tab2",
image: UIImage(systemName: "folder"),
selectedImage: UIImage(systemName: "folder.fill")),
linkItem: .init(path: "tab2")),
.init(
tag: 2,
tabItem: .init(
title: "tab3",
image: UIImage(systemName: "heart"),
selectedImage: UIImage(systemName: "heart.fill")),
linkItem: .init(path: "tab3")),
.init(
tag: 3,
tabItem: .init(
title: "tab4",
image: UIImage(systemName: "person"),
selectedImage: UIImage(systemName: "person.fill")),
linkItem: .init(path: "tab4")),
])
tabItemList: tabList)
.onOpenURL { url in
/// You can test deep links by setting the URL Scheme to "tab-e-s".
/// Example:
/// tab-e-s://navigation/tab2/step1/step2?message=opened+by+deep+link

guard let tabPath = getTabPathByDeeplink(url: url),
let linkItem = getLinkItemByDeepLink(url: url),
let targetTab = tabLinkNavigator.targetPartialNavigator(tabPath: tabPath) else { return }

tabLinkNavigator.moveTab(targetPath: tabPath)
targetTab.replace(linkItem: linkItem, isAnimated: true)
}
}
}
}

extension TabEventSubscriberApp {
fileprivate func getTabPathByDeeplink(url: URL) -> String? {
let components = URLComponents(string: url.absoluteString)
return components?.path.split(separator: "/").map(String.init).first
}

fileprivate func getLinkItemByDeepLink(url: URL) -> LinkItem? {
let components = URLComponents(string: url.absoluteString)
guard let paths = components?.path.split(separator: "/").map(String.init) else { return .none }

return .init(pathList: paths, itemsString: components?.query ?? "")
}
}
4 changes: 2 additions & 2 deletions Sources/LinkNavigator/Core/BaseComponent/LinkItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public struct LinkItem {
}

extension LinkItem {
@available(*, deprecated, message: "Please use init(pathList:itemsString:isConvertBase64:) instead.")
@available(*, deprecated, message: "Please use init(pathList:itemsString:isBase64EncodedItemsString:) instead.")
public init(pathList: [String], items: String) {
self.pathList = pathList
encodedItemString = items
}

@available(*, deprecated, message: "Please use init(path:itemsString:isConvertBase64:) instead.")
@available(*, deprecated, message: "Please use init(path:itemsString:isBase64EncodedItemsString:) instead.")
public init(path: String, items: String) {
pathList = [path]
encodedItemString = items
Expand Down

0 comments on commit 06da035

Please sign in to comment.