devxoul / URLNavigator

⛵️ Elegant URL Routing for Swift
MIT License
3.25k stars 297 forks source link

it can support Enum value (not rawValue) in URL route now?? #133

Open liubiaocong opened 5 years ago

devxoul commented 5 years ago

Can you provide an example or a description?

liubiaocong commented 5 years ago

Like this: enum TestType: String { case test1 = "1" case test2 = "2" }

func TestViewController(taskType: TestType) -> String { return "thor://workflow/unprocessed/review/feedbackpage/(taskType)" }

can it work when I want to get the value of "taskType"?

viettrungphan commented 4 years ago

I structure using enum like this. May it match your requirements

struct DeeplinkManager {
  static let sharedInstance = DeeplinkManager()
  private var navigator: NavigatorType!

  init() {
    navigator = Navigator()
    NavigationMap.initialize(navigator: navigator)
  }

  @discardableResult
  func handleDeeplink(url: URL, animated: Bool = true, completion: (() -> Void)? = nil) -> UIViewController? {
    if !navigator.open(url) {
      return navigator.present(url, wrap: UINavigationController.self, animated: animated, completion: completion)
    }
    return nil
  }
}

//Deeplink kind
enum NavigationMap{
  case chat, chatInbox
  case favourite
}

//Init
extension NavigationMap: CaseIterable {
  static func initialize(navigator: NavigatorType) {
    for deeplink in NavigationMap.allCases {
      navigator.handle(deeplink.pattern(), deeplink.factory(navigator: navigator))
    }
  }
}

//Pattern
extension NavigationMap {
  func pattern() -> String {
    switch self {
    case .chat:
      return "sample://chatLink"
    case .chatInbox:
      return "sample://chat_inbox"
    case .favourite:
      return "sample://favourite"
    }
  }
}

//Handler factory
extension NavigationMap {
  func factory(navigator: NavigatorType) -> URLOpenHandlerFactory {
    return {url, values, context in
      switch self {
      case .chat, .chatInbox:
       self.openChatVC(navigator:navigator)
      case .favourite:
        self.openFavourite(navigator: navigator)
      }
    }
  }
}

extension NavigationMap {
  private func openFavourite(navigator: NavigatorType) {
    if UserProfileManager.sharedInstance.isLogin() {
      let favoriteViewController = FavoriteViewController.instantiate()
      let navController = NavigationController(rootViewController: favoriteViewController)
      navigator.present(navController)
    }
  }
private func openChatVC(navigator: NavigatorType) {
      let chatVC = ChatViewController.instantiate()
      let navController = NavigationController(rootViewController: chatVC)
      navigator.present(navController)
    }
  }
}
cdoky commented 4 years ago

@liubiaocong may be you can see extension for URLNavigator