Skip to content

Commit

Permalink
Merge pull request #1003 from DeluxeAlonso/feature/code-cleanup
Browse files Browse the repository at this point in the history
DeepLink handler update
  • Loading branch information
DeluxeAlonso authored Oct 27, 2024
2 parents 787f9e3 + 9fa5d43 commit 8dd2ecb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 28 deletions.
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
}
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()
}
}

// MARK: - Private

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

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

return indexToSelect
}

}
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 @@ import UIKit
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 @@ final class NavigationHandler: NavigationHandlerProtocol {
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 }

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)
}
}
}

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

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

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

0 comments on commit 8dd2ecb

Please sign in to comment.