Skip to content

Instantly share code, notes, and snippets.

@aronbalog
Created March 26, 2018 02:06
Show Gist options
  • Save aronbalog/fa43b18e43b9f20fd86f00b2996ccc8f to your computer and use it in GitHub Desktop.
Save aronbalog/fa43b18e43b9f20fd86f00b2996ccc8f to your computer and use it in GitHub Desktop.
CoreNavigation 1.0.0 routing

Routing:

Defining view controller
class UserProfileViewController: UIViewController, DataReceivingViewController {
    typealias DataType = User

    func didReceiveData(_ data: User) {
        // configure UI with data
    }
}
Defining route
struct UserProfile: Route, RoutePatternsAware {
    typealias Destination = UserProfileViewController

    let userId: String
    
    var parameters: [String : Any]? {
        return [
            "userId":self.userId
        ]
    }

    /*-------- routing part --------*/
    
    static var patterns: [String] = [
        "https://myapp.com/user/:userId(.*)"
    ]
    
    static func route(handler: RouteHandler<UserProfile>) {
        let userId = handler.parameters?["userId"] as? String
        
        // e.g. fetch user from network
        fetchUser(userId, completion: { (user: User) in
            // notify handler
            handler.complete(data: user)
        })
    }
}
Presenting route
Using strong route
present { $0
    // configure navigation
    .to(UserProfile(userId: "123456")
    .animated(false)
    ...
}

or

UserProfile(userId: "123456").present { $0
    // configure navigation
    .animated(false)
    ...
}

or

UserProfile(userId: "123456").present()
Using soft route
present { $0
    // configure navigation
    .to("https://myapp.com/user/123456")
    .animated(false)
    ...
}

or

"https://myapp.com/user/123456".present { $0
    // configure navigation
    .animated(false)
    ...
}

or

"https://myapp.com/user/123456".present()
Getting view controller
Using strong route
UserProfile(userId: "123456").viewController { (viewController: UserProfileViewController) in
    ...
}
Using soft route
"https://myapp.com/user/123456".viewController { (viewController: UIViewController) in
    ...
}
@zorantodorovic
Copy link

zorantodorovic commented Mar 29, 2018

Moze li se presentanje routea obavit ovako (animated je izvan closurea):

UserProfile(userId: "123456")
.present { $0
    // configure navigation
}
.animated()

?

@aronbalog
Copy link
Author

aronbalog commented Mar 29, 2018

Ne, animated mora ic u konfiguraciju od navigacije.

UserProfile(userId: "123456")
.present { $0
    // configure navigation
    .animated(true)
}

bi bilo ispravno.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment