Comments (5)
Hi @orta, let me comment about JLRoutes returning a BOOL.
As I told you on twitter we use a very similar thing at what you want and also using JLRoutes.
We have a wrapper on top of it that accepts our custom "Route" object.
The nice thing is that those objects are created with the "url" and a handlerBlock. Each module of the app can register it's own routes creating this objects and registering them. In the handlerBlock the VC and all the necessary dependencies are created, and that block accepts a params dictionary with the params that the caller wanted to pass (via URL params or a custom dictionary, it doesn't matter). Apart from that it also receives a block that is the one that gets called with the created VC, is the "completionBlock" that the caller uses to present the new screen.
In that way we have not modified the original JLRoutes, the Router only gives us VC (doesn't do anything with presenting them) and we can also use it asynchronously.
That's what we have come up with, I'm impatient to see how your approach will look like in the end ;) Cheers.
from mobile.
I see the pattern you're talking about, that's interesting because it also allows for asynchronous view controllers in a way that our current use case doesn't.
from mobile.
Alright thanks a lot @alexito4 - so I've hacked up a quick example based on my interpretation:
@import Foundation;
@import UIKit;
@interface Route : NSObject
@property (nonatomic, strong) NSString *pattern;
@property (nonatomic, copy) UIViewController * (^generator)(NSDictionary *params);
@end
@interface ARSwitchboardTwo : NSObject
+ (instancetype)sharedInstance;
- (void)registerRoute:(Route *)route;
- (void)routePath:(NSString *)path completion:(void(^)(UIViewController *controller))completion;
@end
#import "ARSwitchboardTwo.h"
@import JLRoutes;
@interface _ARInternalRoute : NSObject
@property (nonatomic, copy) NSDictionary *params;
@property (nonatomic, strong) Route *route;
@end
@implementation _ARInternalRoute
@end
@implementation Route
@end
@interface ARSwitchboardTwo()
@property (readonly, nonatomic, strong) JLRoutes *routes;
@property (readonly, nonatomic, strong) NSMutableArray <Route *> *internalRoutes;
@property (readwrite, nonatomic, strong) _ARInternalRoute *currentRoute;
@end
@implementation ARSwitchboardTwo
+ (instancetype)sharedInstance
{
static ARSwitchboardTwo *sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[ARSwitchboardTwo alloc] init];
});
return sharedInstance;
}
- (instancetype)init
{
self = [super init];
if (!self) {
return nil;
}
_routes = [[JLRoutes alloc] init];
_internalRoutes = [NSMutableArray array];
return self;
}
- (void)registerRoute:(Route *)route
{
[self.internalRoutes addObject:route];
__weak typeof(self) weakSelf = self;
[self.routes addRoute:route.pattern handler:^BOOL(NSDictionary *parameters) {
_ARInternalRoute *current = [[_ARInternalRoute alloc] init];
current.route = route;
current.params = parameters;
weakSelf.currentRoute = current;
return YES;
}];
}
- (void)routePath:(NSString *)path completion:(void (^)(UIViewController *))completion
{
NSURL *url = [NSURL URLWithString:path];
if ([self.routes routeURL:url withParameters:nil]) {
Route *route = self.currentRoute.route;
NSDictionary *params = self.currentRoute.params;
UIViewController *result = route.generator(params);
completion(result);
} else {
self.currentRoute = nil;
completion(nil);
}
}
@end
Then anything can register their own Routes
;
@implementation TwoViewController
+ (void)load
{
Route *route = [[Route alloc] init];
route.pattern = @"/two/:id";
route.generator = ^UIViewController * (NSDictionary *params) {
TwoViewController *twoVC = [[TwoViewController alloc] init];
twoVC.view.backgroundColor = [UIColor redColor];
return twoVC;
};
[[ARSwitchboardTwo sharedInstance] registerRoute:route];
}
@end
and any other class can use their own completion based routing:
- (IBAction)go:(id)sender
{
[[ARSwitchboardTwo sharedInstance] routePath:@"/two/thing" completion:^(UIViewController *controller) {
[self.navigationController pushViewController:controller animated:YES];
}];
}
Full demo - http://cl.ly/0U0Z0l1m1N2Q/RoutingExample.zip
from mobile.
@orta I like the result a lot BOOL
return value, so a fork does seem in order to me.
from mobile.
A lot of this is done now, and on my fork.
from mobile.
Related Issues (20)
- Blog Post: Getting started with React Native HOT 6
- Blog Post: Migrating Force/Microgravity to OSS by Default HOT 2
- Blog Post: Artsy's usage of Bots
- Analytics on Mobile HOT 1
- Public Speaking HOT 5
- Dev tools blog post
- Strongify/Weakify dance blog post HOT 1
- README needs an update again HOT 1
- PR a day HOT 3
- Colour palette for Artsy OSS (general / mobile projects) HOT 5
- Artsy OSS / Mobile resources HOT 4
- Drawing and programming views HOT 1
- Breaking into OSS in Cocoa HOT 1
- Create a blog post with ‘rules’ on communication and design, specifically in the context of OSS. HOT 1
- Process HOT 11
- Blog post: MVVM + FBSnapshots for the greater good
- Blog Post: SDWebImage and Snapshot Testing HOT 1
- Incorrect links in the playbook.md file HOT 2
- Bug Report Process HOT 2
- Post:How the design of CocoaDocs influences Danger's infrastructure HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mobile.