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

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

Open
liubiaocong opened this issue Sep 19, 2019 · 4 comments
Open

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

liubiaocong opened this issue Sep 19, 2019 · 4 comments

Comments

@liubiaocong
Copy link

No description provided.

@devxoul
Copy link
Owner

devxoul commented Sep 28, 2019

Can you provide an example or a description?

@liubiaocong
Copy link
Author

liubiaocong commented Oct 8, 2019

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
Copy link

viettrungphan commented Nov 20, 2019

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
Copy link

cdoky commented Dec 5, 2019

@liubiaocong may be you can see extension for URLNavigator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants