Skip to content

Commit 06da035

Browse files
committed
feat: Modified the TabEventSubscriber example to support deep links.
1 parent d9ea178 commit 06da035

3 files changed

Lines changed: 78 additions & 32 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleURLTypes</key>
6+
<array>
7+
<dict>
8+
<key>CFBundleTypeRole</key>
9+
<string>Editor</string>
10+
<key>CFBundleURLSchemes</key>
11+
<array>
12+
<string>tab-e-s</string>
13+
</array>
14+
</dict>
15+
<dict/>
16+
</array>
17+
</dict>
18+
</plist>

Examples/TabNavigator/03-TabEventSubscriber/TabEventSubscriber/TabEventSubscriberApp.swift

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,69 @@ struct TabEventSubscriberApp: App {
88
routeBuilderItemList: AppRouterBuilderGroup().routers,
99
dependency: AppDependency())
1010

11+
let tabList: [TabItem] = [
12+
.init(
13+
tag: .zero,
14+
tabItem: .init(
15+
title: "tab1",
16+
image: UIImage(systemName: "house"),
17+
selectedImage: UIImage(systemName: "house.fill")),
18+
linkItem: .init(path: "tab1")),
19+
.init(
20+
tag: 1,
21+
tabItem: .init(
22+
title: "tab2",
23+
image: UIImage(systemName: "folder"),
24+
selectedImage: UIImage(systemName: "folder.fill")),
25+
linkItem: .init(path: "tab2")),
26+
.init(
27+
tag: 2,
28+
tabItem: .init(
29+
title: "tab3",
30+
image: UIImage(systemName: "heart"),
31+
selectedImage: UIImage(systemName: "heart.fill")),
32+
linkItem: .init(path: "tab3")),
33+
.init(
34+
tag: 3,
35+
tabItem: .init(
36+
title: "tab4",
37+
image: UIImage(systemName: "person"),
38+
selectedImage: UIImage(systemName: "person.fill")),
39+
linkItem: .init(path: "tab4")),
40+
]
41+
1142
var body: some Scene {
1243
WindowGroup {
1344
TabLinkNavigationView(
1445
linkNavigator: tabLinkNavigator,
1546
isHiddenDefaultTabbar: false,
16-
tabItemList: [
17-
.init(
18-
tag: .zero,
19-
tabItem: .init(
20-
title: "tab1",
21-
image: UIImage(systemName: "house"),
22-
selectedImage: UIImage(systemName: "house.fill")),
23-
linkItem: .init(path: "tab1")),
24-
.init(
25-
tag: 1,
26-
tabItem: .init(
27-
title: "tab2",
28-
image: UIImage(systemName: "folder"),
29-
selectedImage: UIImage(systemName: "folder.fill")),
30-
linkItem: .init(path: "tab2")),
31-
.init(
32-
tag: 2,
33-
tabItem: .init(
34-
title: "tab3",
35-
image: UIImage(systemName: "heart"),
36-
selectedImage: UIImage(systemName: "heart.fill")),
37-
linkItem: .init(path: "tab3")),
38-
.init(
39-
tag: 3,
40-
tabItem: .init(
41-
title: "tab4",
42-
image: UIImage(systemName: "person"),
43-
selectedImage: UIImage(systemName: "person.fill")),
44-
linkItem: .init(path: "tab4")),
45-
])
47+
tabItemList: tabList)
48+
.onOpenURL { url in
49+
/// You can test deep links by setting the URL Scheme to "tab-e-s".
50+
/// Example:
51+
/// tab-e-s://navigation/tab2/step1/step2?message=opened+by+deep+link
52+
53+
guard let tabPath = getTabPathByDeeplink(url: url),
54+
let linkItem = getLinkItemByDeepLink(url: url),
55+
let targetTab = tabLinkNavigator.targetPartialNavigator(tabPath: tabPath) else { return }
56+
57+
tabLinkNavigator.moveTab(targetPath: tabPath)
58+
targetTab.replace(linkItem: linkItem, isAnimated: true)
59+
}
4660
}
4761
}
4862
}
63+
64+
extension TabEventSubscriberApp {
65+
fileprivate func getTabPathByDeeplink(url: URL) -> String? {
66+
let components = URLComponents(string: url.absoluteString)
67+
return components?.path.split(separator: "/").map(String.init).first
68+
}
69+
70+
fileprivate func getLinkItemByDeepLink(url: URL) -> LinkItem? {
71+
let components = URLComponents(string: url.absoluteString)
72+
guard let paths = components?.path.split(separator: "/").map(String.init) else { return .none }
73+
74+
return .init(pathList: paths, itemsString: components?.query ?? "")
75+
}
76+
}

Sources/LinkNavigator/Core/BaseComponent/LinkItem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ public struct LinkItem {
5353
}
5454

5555
extension LinkItem {
56-
@available(*, deprecated, message: "Please use init(pathList:itemsString:isConvertBase64:) instead.")
56+
@available(*, deprecated, message: "Please use init(pathList:itemsString:isBase64EncodedItemsString:) instead.")
5757
public init(pathList: [String], items: String) {
5858
self.pathList = pathList
5959
encodedItemString = items
6060
}
6161

62-
@available(*, deprecated, message: "Please use init(path:itemsString:isConvertBase64:) instead.")
62+
@available(*, deprecated, message: "Please use init(path:itemsString:isBase64EncodedItemsString:) instead.")
6363
public init(path: String, items: String) {
6464
pathList = [path]
6565
encodedItemString = items

0 commit comments

Comments
 (0)