Skip to content

Commit

Permalink
Adds extraParams to openUrl to allow more robust data passing through…
Browse files Browse the repository at this point in the history
… routing when a controller decides to open an url
  • Loading branch information
NorthStar committed Aug 22, 2014
1 parent ea91489 commit aa06aea
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 41 deletions.
11 changes: 11 additions & 0 deletions Routable/Routable.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ typedef void (^RouterOpenCallback)(NSDictionary *params);
*/
- (void)open:(NSString *)url animated:(BOOL)animated;

/**
Triggers the appropriate functionality for a mapped URL, such as an anonymous function or opening a `UIViewController`
@param url The URL being opened (i.e. "users/16")
@param animated Whether or not `UIViewController` transitions are animated.
@param extraParams more paramters to pass in while opening a `UIViewController`; take priority over route-specific default parameters
@exception RouteNotFoundException Thrown if url does not have a valid mapping
@exception NavigationControllerNotProvided Thrown if url opens a `UIViewController` and navigationController has not been assigned
@exception RoutableInitializerNotFound Thrown if the mapped `UIViewController` instance does not implement -initWithRouterParams: or +allocWithRouterParams:
*/
- (void)open:(NSString *)url animated:(BOOL)animated extraParams:(NSDictionary *)extraParams;

/**
Get params of a given URL, simply return the params dictionary NOT using a block
@param url The URL being detected (i.e. "users/16")
Expand Down
95 changes: 54 additions & 41 deletions Routable/Routable.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,23 @@ @interface RouterParams : NSObject

@property (readwrite, nonatomic, strong) UPRouterOptions *routerOptions;
@property (readwrite, nonatomic, strong) NSDictionary *openParams;
@property (readwrite, nonatomic, strong) NSDictionary *extraParams;
@property (readwrite, nonatomic, strong) NSDictionary *controllerParams;

@end

@implementation RouterParams

- (instancetype)initWithRouterOptions: (UPRouterOptions *)routerOptions openParams: (NSDictionary *)openParams {
- (instancetype)initWithRouterOptions: (UPRouterOptions *)routerOptions openParams: (NSDictionary *)openParams extraParams: (NSDictionary *)extraParams{
[self setRouterOptions:routerOptions];
[self setExtraParams: extraParams];
[self setOpenParams:openParams];
return self;
}

- (NSDictionary *)controllerParams {
NSMutableDictionary *controllerParams = [NSMutableDictionary dictionaryWithDictionary:self.routerOptions.defaultParams];
[controllerParams addEntriesFromDictionary:self.extraParams];
[controllerParams addEntriesFromDictionary:self.openParams];
return controllerParams;
}
Expand Down Expand Up @@ -247,7 +250,14 @@ - (void)open:(NSString *)url {
}

- (void)open:(NSString *)url animated:(BOOL)animated {
RouterParams *params = [self routerParamsForUrl:url];
[self open:url animated:animated extraParams:nil];
}

- (void)open:(NSString *)url
animated:(BOOL)animated
extraParams:(NSDictionary *)extraParams
{
RouterParams *params = [self routerParamsForUrl:url extraParams: extraParams];
UPRouterOptions *options = params.routerOptions;

if (options.callback) {
Expand Down Expand Up @@ -294,7 +304,6 @@ - (void)open:(NSString *)url animated:(BOOL)animated {
[self.navigationController pushViewController:controller animated:animated];
}
}

- (NSDictionary*)paramsOfUrl:(NSString*)url {
return [[self routerParamsForUrl:url] controllerParams];
}
Expand All @@ -316,53 +325,57 @@ - (void)pop:(BOOL)animated {
}

///////
- (RouterParams *)routerParamsForUrl:(NSString *)url {
if (!url) {
//if we wait, caching this as key would throw an exception
if (_ignoresExceptions) {
return nil;
- (RouterParams *)routerParamsForUrl:(NSString *)url extraParams: (NSDictionary *)extraParams {
if (!url) {
//if we wait, caching this as key would throw an exception
if (_ignoresExceptions) {
return nil;
}
@throw [NSException exceptionWithName:@"RouteNotFoundException"
reason:[NSString stringWithFormat:ROUTE_NOT_FOUND_FORMAT, url]
userInfo:nil];
}
@throw [NSException exceptionWithName:@"RouteNotFoundException"
reason:[NSString stringWithFormat:ROUTE_NOT_FOUND_FORMAT, url]
userInfo:nil];
}

if ([self.cachedRoutes objectForKey:url]) {
return [self.cachedRoutes objectForKey:url];
}
if ([self.cachedRoutes objectForKey:url]) {
return [self.cachedRoutes objectForKey:url];
}

NSArray *givenParts = url.pathComponents;
NSArray *legacyParts = [url componentsSeparatedByString:@"/"];
if ([legacyParts count] != [givenParts count]) {
NSLog(@"Routable Warning - your URL %@ has empty path components - this will throw an error in an upcoming release", url);
givenParts = legacyParts;
}
NSArray *givenParts = url.pathComponents;
NSArray *legacyParts = [url componentsSeparatedByString:@"/"];
if ([legacyParts count] != [givenParts count]) {
NSLog(@"Routable Warning - your URL %@ has empty path components - this will throw an error in an upcoming release", url);
givenParts = legacyParts;
}

__block RouterParams *openParams = nil;
[self.routes enumerateKeysAndObjectsUsingBlock:
^(NSString *routerUrl, UPRouterOptions *routerOptions, BOOL *stop) {
__block RouterParams *openParams = nil;
[self.routes enumerateKeysAndObjectsUsingBlock:
^(NSString *routerUrl, UPRouterOptions *routerOptions, BOOL *stop) {

NSArray *routerParts = [routerUrl pathComponents];
if ([routerParts count] == [givenParts count]) {
NSArray *routerParts = [routerUrl pathComponents];
if ([routerParts count] == [givenParts count]) {

NSDictionary *givenParams = [self paramsForUrlComponents:givenParts routerUrlComponents:routerParts];
if (givenParams) {
openParams = [[RouterParams alloc] initWithRouterOptions:routerOptions openParams:givenParams];
*stop = YES;
NSDictionary *givenParams = [self paramsForUrlComponents:givenParts routerUrlComponents:routerParts];
if (givenParams) {
openParams = [[RouterParams alloc] initWithRouterOptions:routerOptions openParams:givenParams extraParams: extraParams];
*stop = YES;
}
}
}
}];
}];

if (!openParams) {
if (_ignoresExceptions) {
return nil;
if (!openParams) {
if (_ignoresExceptions) {
return nil;
}
@throw [NSException exceptionWithName:@"RouteNotFoundException"
reason:[NSString stringWithFormat:ROUTE_NOT_FOUND_FORMAT, url]
userInfo:nil];
}
@throw [NSException exceptionWithName:@"RouteNotFoundException"
reason:[NSString stringWithFormat:ROUTE_NOT_FOUND_FORMAT, url]
userInfo:nil];
}
[self.cachedRoutes setObject:openParams forKey:url];
return openParams;
[self.cachedRoutes setObject:openParams forKey:url];
return openParams;
}

- (RouterParams *)routerParamsForUrl:(NSString *)url {
return [self routerParamsForUrl:url extraParams: nil];
}

- (NSDictionary *)paramsForUrlComponents:(NSArray *)givenUrlComponents
Expand Down

0 comments on commit aa06aea

Please sign in to comment.