Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeepLink handler update #1003

Merged
merged 5 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,53 @@
// Copyright © 2024 Alonso. All rights reserved.
//

import Foundation
import UIKit

final class DeepLinkHandler: DeepLinkHandlerProtocol {

func handleDeepLinkUrl(_ url: URL) {
private(set) var rootCoordinators: [RootCoordinator] = []

func register(rootCoordinators: [RootCoordinator]) {
self.rootCoordinators = rootCoordinators
}

func handleDeepLinkUrl(_ url: URL, in window: UIWindow?) {
guard let urlHost = url.host, let host = DeepLinkDestination(rawValue: urlHost) else {
return

Check warning on line 21 in UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift

View check run for this annotation

Codecov / codecov/patch

UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift#L19-L21

Added lines #L19 - L21 were not covered by tests
}
switch host {
case .upcomingMovies:
changeTabBarToSelectedIndex(RootCoordinatorIdentifier.upcomingMovies, from: window)
case .searchMovies:
changeTabBarToSelectedIndex(RootCoordinatorIdentifier.searchMovies, from: window)
case .favorites:
changeTabBarToSelectedIndex(RootCoordinatorIdentifier.account, from: window)
let rootCoordinator = rootCoordinators[index(for: RootCoordinatorIdentifier.account)]
let unwrappedParentCoordinator = rootCoordinator.childCoordinators.last?.unwrappedParentCoordinator ?? rootCoordinator
let coordinator = FavoritesSavedMoviesCoordinator(navigationController: unwrappedParentCoordinator.navigationController)
coordinator.parentCoordinator = unwrappedParentCoordinator
unwrappedParentCoordinator.childCoordinators.append(coordinator)
coordinator.start()

Check warning on line 35 in UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift

View check run for this annotation

Codecov / codecov/patch

UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift#L23-L35

Added lines #L23 - L35 were not covered by tests
}
}

// MARK: - Private

private func changeTabBarToSelectedIndex(_ rootIdentifier: String, from window: UIWindow?) {
let selectedIndex = index(for: rootIdentifier)
guard let tabBarController = window?.rootViewController as? MainTabBarController else {
return

Check warning on line 44 in UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift

View check run for this annotation

Codecov / codecov/patch

UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift#L41-L44

Added lines #L41 - L44 were not covered by tests
}
tabBarController.setSelectedIndex(selectedIndex)

Check warning on line 46 in UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift

View check run for this annotation

Codecov / codecov/patch

UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift#L46

Added line #L46 was not covered by tests
}

private func index(for rootIdentifier: String) -> Int {
let coordinatorIdentifiers = rootCoordinators.map { $0.rootIdentifier }
guard let indexToSelect = coordinatorIdentifiers.firstIndex(of: rootIdentifier) else {
fatalError()

Check warning on line 52 in UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift

View check run for this annotation

Codecov / codecov/patch

UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift#L49-L52

Added lines #L49 - L52 were not covered by tests
}

return indexToSelect

Check warning on line 55 in UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift

View check run for this annotation

Codecov / codecov/patch

UpcomingMovies/Helpers/Handlers/DeepLinkHandler/DeepLinkHandler.swift#L55

Added line #L55 was not covered by tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
// Copyright © 2024 Alonso. All rights reserved.
//

import Foundation
import UIKit

protocol DeepLinkHandlerProtocol {

func handleDeepLinkUrl(_ url: URL)
func register(rootCoordinators: [RootCoordinator])
func handleDeepLinkUrl(_ url: URL, in window: UIWindow?)

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
final class NavigationHandler: NavigationHandlerProtocol {

private let deepLinkHandler: DeepLinkHandlerProtocol
private let rootCoordinators: [RootCoordinator] = MainTabBarBuilder.buildViewCoordinators()

private var currentSelectedIndex: Int = 0
private var rootCoordinators: [RootCoordinator]
private var initialTransitionCompleted: Bool = false
private var onInitialTransition: ((UIWindow?) -> Void)?

// MARK: - Initializers

init(deepLinkHandler: DeepLinkHandlerProtocol) {
self.deepLinkHandler = deepLinkHandler
self.rootCoordinators = MainTabBarBuilder.buildViewCoordinators()
self.deepLinkHandler.register(rootCoordinators: rootCoordinators)
}

// MARK: - NavigationHandlerProtocol
Expand All @@ -32,35 +33,28 @@
UIView.AnimationOptions.transitionCrossDissolve],
animations: {},
completion: { _ in
let mainTabBarController = MainTabBarController(coordinators: self.rootCoordinators)
mainTabBarController.setSelectedIndex(self.currentSelectedIndex)
window.rootViewController = mainTabBarController
})
let mainTabBarController = MainTabBarController(coordinators: self.rootCoordinators)
window.rootViewController = mainTabBarController
self.onInitialTransition?(window) ?? mainTabBarController.setSelectedIndex(0)
self.initialTransitionCompleted = true
})
}

func handleUrlOpeningNavigation(for url: URL?, and window: UIWindow?) {
guard let url = url, let urlHost = url.host else { return }
guard let url else { return }

Check warning on line 44 in UpcomingMovies/Helpers/Handlers/NavigationHandler/NavigationHandler.swift

View check run for this annotation

Codecov / codecov/patch

UpcomingMovies/Helpers/Handlers/NavigationHandler/NavigationHandler.swift#L44

Added line #L44 was not covered by tests

if url.scheme == AppExtension.scheme {
guard let host = DeepLinkDestination(rawValue: urlHost) else { return }
switch host {
case .upcomingMovies:
changeTabBarToSelectedIndex(RootCoordinatorIdentifier.upcomingMovies, from: window)
case .searchMovies:
changeTabBarToSelectedIndex(RootCoordinatorIdentifier.searchMovies, from: window)
case .favorites:
changeTabBarToSelectedIndex(RootCoordinatorIdentifier.account, from: window)
// TODO: - Refactor
let rootCoordinator = rootCoordinators[index(for: RootCoordinatorIdentifier.account)]
let unwrappedParentCoordinator = rootCoordinator.childCoordinators.last?.unwrappedParentCoordinator ?? rootCoordinator
let coordinator = FavoritesSavedMoviesCoordinator(navigationController: unwrappedParentCoordinator.navigationController)
coordinator.parentCoordinator = unwrappedParentCoordinator
unwrappedParentCoordinator.childCoordinators.append(coordinator)
coordinator.start()
self.onInitialTransition = { window in self.handleDeepLink(url, and: window) }
if initialTransitionCompleted {
onInitialTransition?(window)

Check warning on line 49 in UpcomingMovies/Helpers/Handlers/NavigationHandler/NavigationHandler.swift

View check run for this annotation

Codecov / codecov/patch

UpcomingMovies/Helpers/Handlers/NavigationHandler/NavigationHandler.swift#L47-L49

Added lines #L47 - L49 were not covered by tests
}
}
}

private func handleDeepLink(_ url: URL, and window: UIWindow?) {
deepLinkHandler.handleDeepLinkUrl(url, in: window)

Check warning on line 55 in UpcomingMovies/Helpers/Handlers/NavigationHandler/NavigationHandler.swift

View check run for this annotation

Codecov / codecov/patch

UpcomingMovies/Helpers/Handlers/NavigationHandler/NavigationHandler.swift#L54-L55

Added lines #L54 - L55 were not covered by tests
}

func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem, and window: UIWindow?) {
guard let shorcut = AppShortcutItem(rawValue: shortcutItem.type) else { return }
switch shorcut {
Expand All @@ -73,7 +67,6 @@

private func changeTabBarToSelectedIndex(_ rootIdentifier: String, from window: UIWindow?) {
let selectedIndex = index(for: rootIdentifier)
currentSelectedIndex = selectedIndex
guard let tabBarController = window?.rootViewController as? MainTabBarController else {
return
}
Expand Down
Loading