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

registering a route with a path, the scheme is redundant #31

Open
aelam opened this issue Feb 18, 2021 · 9 comments
Open

registering a route with a path, the scheme is redundant #31

aelam opened this issue Feb 18, 2021 · 9 comments

Comments

@aelam
Copy link

aelam commented Feb 18, 2021

("FOOBAR://STATIC", { _ in true }),
("FOOBAR://FOO/BAR", { _ in true }),
("FOOBAR://SPAM/HAM", { _ in false }),
("FOOBAR://:keyword", { _ in true }),
("FOOBAR://FOO/:keyword", { _ in true }),
])
XCTAssertTrue(router.responds(to: URL(string: "foobar://sTATic")!))
XCTAssertTrue(router.responds(to: URL(string: "foobar://foo")!))
XCTAssertTrue(router.responds(to: URL(string: "foobar://foo/bar")!))
XCTAssertTrue(router.responds(to: URL(string: "FOOBAR://FOO/BAR")!))
XCTAssertTrue(router.responds(to: URL(string: "foobar://foo/10000")!))
XCTAssertTrue(router.responds(to: URL(string: "foobar://FOO/10000")!))
XCTAssertFalse(router.responds(to: URL(string: "foobar://aaa/bbb")!))
XCTAssertFalse(router.responds(to: URL(string: "notfoobar://aaa/bbb")!))
XCTAssertFalse(router.responds(to: URL(string: "foobar://spam/ham")!))
XCTAssertFalse(router.responds(to: URL(string: "static")!))
XCTAssertFalse(router.responds(to: URL(string: "foo")!))
XCTAssertFalse(router.responds(to: URL(string: "foo/bar")!))
XCTAssertFalse(router.responds(to: URL(string: "foo/10000")!))
XCTAssertFalse(router.responds(to: URL(string: "aaa/bbb")!))
XCTAssertFalse(router.responds(to: URL(string: "spam/ham")!))
}

When we register it's such a trouble to write the scheme all the time when we've declared the scheme for Router!
If you really wan to do so, you should let the same router supports both scheme and http url too!

L53~L58 should be true

considering the http url, that's a pain for registering too

@giginet
Copy link
Owner

giginet commented Feb 20, 2021

Thanks for your feedback!
I have an idea. I'll implement multiple schemes support soon.

@aelam
Copy link
Author

aelam commented Feb 20, 2021 via email

@aelam
Copy link
Author

aelam commented Feb 22, 2021

there should be like a globalRouter singletom, when people register routes in routers, the routers should be loaded in globalRouters then users can just use globalRouter for routing

@giginet
Copy link
Owner

giginet commented Feb 22, 2021

I don't agree Crossroad should provide a global router. I think libraries should keep avoiding global states as we can.

I expect that AppDelegate has a router and register all routes into that in most use cases.
If you need a global router, you're better to prepare static variables in each apps.

@aelam
Copy link
Author

aelam commented Feb 22, 2021

Image you get 4 modules, A B C D, you don't want A depends on B, but A use a route page1OfB How do you use the router in modules
AppDelegate is not available in any module. supposed the modules are managed by CocoaPods or SwiftPM
the globalRouter should be fully configured by users.

Your case is that everything is in a same project. But image there are 100 modules managed by CocoaPods in your project

@giginet
Copy link
Owner

giginet commented Feb 22, 2021

Multimodule structures are varied by each project. (how to resolve dependencies between each module, abstraction way, etc...)

So I think Crossroad should not provide global routers.
You're better to implement a simple wrapper of Crossroad.Router and share that among whole modules if you need.

By the way, this issue discusses multiple URL scheme support.
So if you need more discussion about a global router. Could you make another issue?

@giginet
Copy link
Owner

giginet commented Mar 2, 2021

@aelam I tried to implement this feature.
However, DSL would be very complex. So it needs radical changes for inner implementations.

First, I'm planning to support multiple schemes( or URLs) for one router.
After the changes, I'll work supporting multiple routes for one handler.

Please moment.

@giginet
Copy link
Owner

giginet commented Nov 22, 2021

@aelam I apologize to keep you waiting.

I released 4.0.0.

Crossroad 4.0.0 supports grouped routes.
https://github.com/giginet/Crossroad/tree/9b004cd661b8058657623bced487d6085e1ca925#multiple-link-sources-support

let customURLScheme: LinkSource = .customURLScheme("pokedex")
let pokedexWeb: LinkSource = .universalLink(URL(string: "https://my-awesome-pokedex.com")!)
let anotherWeb: LinkSource = .universalLink(URL(string: "https://kanto.my-awesome-pokedex.com")!)

let router = try DefaultRouter(accepting: [customURLScheme, pokedexWeb, anotherWeb]) { registry in
    // Pokémon detail pages can be opened from all sources.
    registry.route("/pokemons/:pokedexID") { context in 
        let pokedexID: Int = try context.argument(named: "pokedexID") // Parse 'pokedexID' from URL
        if !Pokedex.isExist(pokedexID) { // Find the Pokémon by ID
            throw PokedexError.pokemonIsNotExist(pokedexID)
        }
        presentPokedexDetailViewController(of: pokedexID)
    }

    // Move related pages can be opened only from Custom URL Schemes
    registry.group(accepting: [customURLScheme]) { group in
        group.route("/moves/:move_name") { context in 
            let moveName: String = try context.argument(named: "move_name")
            presentMoveViewController(for: moveName)
        }
        group.route("/pokemons/:pokedexID/move") { context in 
            let pokedexID: Int = try context.argument(named: "pokedexID")
            presentPokemonMoveViewController(for: pokedexID)
        }
    }
}

You can share handlers between custom URL schemes and universal links.
Could you try this version?

@aelam
Copy link
Author

aelam commented Nov 26, 2021

Thanks for your great work! let me check!

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

2 participants