From 06da03571b7cdd4bde6d58b07774aecf3475512e Mon Sep 17 00:00:00 2001 From: "sangho.Cho" <97chos@naver.com> Date: Wed, 8 May 2024 17:26:33 +0900 Subject: [PATCH] feat: Modified the TabEventSubscriber example to support deep links. --- .../TabEventSubscriber/Info.plist | 18 ++++ .../TabEventSubscriberApp.swift | 88 ++++++++++++------- .../Core/BaseComponent/LinkItem.swift | 4 +- 3 files changed, 78 insertions(+), 32 deletions(-) create mode 100644 Examples/TabNavigator/03-TabEventSubscriber/TabEventSubscriber/Info.plist diff --git a/Examples/TabNavigator/03-TabEventSubscriber/TabEventSubscriber/Info.plist b/Examples/TabNavigator/03-TabEventSubscriber/TabEventSubscriber/Info.plist new file mode 100644 index 0000000..2869a87 --- /dev/null +++ b/Examples/TabNavigator/03-TabEventSubscriber/TabEventSubscriber/Info.plist @@ -0,0 +1,18 @@ + + + + + CFBundleURLTypes + + + CFBundleTypeRole + Editor + CFBundleURLSchemes + + tab-e-s + + + + + + diff --git a/Examples/TabNavigator/03-TabEventSubscriber/TabEventSubscriber/TabEventSubscriberApp.swift b/Examples/TabNavigator/03-TabEventSubscriber/TabEventSubscriber/TabEventSubscriberApp.swift index 21800df..686a917 100644 --- a/Examples/TabNavigator/03-TabEventSubscriber/TabEventSubscriber/TabEventSubscriberApp.swift +++ b/Examples/TabNavigator/03-TabEventSubscriber/TabEventSubscriber/TabEventSubscriberApp.swift @@ -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 ?? "") + } +} diff --git a/Sources/LinkNavigator/Core/BaseComponent/LinkItem.swift b/Sources/LinkNavigator/Core/BaseComponent/LinkItem.swift index e097c6a..d96c103 100644 --- a/Sources/LinkNavigator/Core/BaseComponent/LinkItem.swift +++ b/Sources/LinkNavigator/Core/BaseComponent/LinkItem.swift @@ -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