Skip to content

Commit

Permalink
* Extract arguments from the URL query string
Browse files Browse the repository at this point in the history
  • Loading branch information
joehewitt committed Jul 7, 2009
1 parent 914f076 commit b9b9a88
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
7 changes: 2 additions & 5 deletions samples/TTAppMapDemo/Classes/AppDelegate.m
Expand Up @@ -21,11 +21,8 @@ - (void)applicationDidFinishLaunching:(UIApplication*)application {
controller:[ContentController class] selector:@selector(showFood:)];
[appMap addURL:@"tt://about/(showAbout)" parent:@"tt://menu/5"
controller:[ContentController class] selector:@selector(showAbout:)];
[appMap addURL:@"tt://order"
modal:[ContentController class] selector:@selector(showOrder)];

// [appMap addURL:@"tt://menu/(showFruit)/modal"
// modal:[DummyController class] selector:@selector(showFruit:)];
[appMap addURL:@"tt://order?waitress=(orderWithWaitress)"
modal:[ContentController class] selector:@selector(orderWithWaitress:)];

TTLoadURL(@"tt://tabBar");
}
Expand Down
4 changes: 2 additions & 2 deletions samples/TTAppMapDemo/Classes/ContentController.m
Expand Up @@ -48,13 +48,13 @@ - (void)updateView {
///////////////////////////////////////////////////////////////////////////////////////////////////
// URLs

- (void)showOrder {
- (void)orderWithWaitress:(NSString*)waitress {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:@selector(dismiss)] autorelease];

self.title = @"Place Your Order";
self.content = @"Well, what do you want?";
self.content = [NSString stringWithFormat:@"%@ will take your order now.", waitress];
}

- (void)showFood:(NSString*)food {
Expand Down
3 changes: 2 additions & 1 deletion samples/TTAppMapDemo/Classes/MenuController.m
Expand Up @@ -112,7 +112,8 @@ - (void)showMenu:(MenuPage)page {

self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:@"Order" style:UIBarButtonItemStyleBordered
target:@"tt://order" action:@selector(loadAsURL)] autorelease];
target:@"tt://order?waitress=Tammy" action:@selector(loadAsURL)]
autorelease];
}

@end
Expand Down
53 changes: 41 additions & 12 deletions src/TTAppMap.m
Expand Up @@ -258,6 +258,7 @@ - (void)setSelector:(SEL)selector {
// Look up the index and type of each argument in the method
NSString* selectorName = [NSString stringWithCString:sel_getName(_selector)];
NSArray* argNames = [selectorName componentsSeparatedByString:@":"];

for (id<TTURLPatternText> pattern in _path) {
if ([pattern isKindOfClass:[TTURLWildcard class]]) {
TTURLWildcard* wildcard = (TTURLWildcard*)pattern;
Expand All @@ -271,6 +272,20 @@ - (void)setSelector:(SEL)selector {
}
}
}

for (id<TTURLPatternText> pattern in [_query objectEnumerator]) {
if ([pattern isKindOfClass:[TTURLWildcard class]]) {
TTURLWildcard* wildcard = (TTURLWildcard*)pattern;
wildcard.argIndex = [argNames indexOfObject:wildcard.name];
if (wildcard.argIndex == NSNotFound) {
TTWARN(@"Argument %@ not found in @selector(%s)", wildcard.name, sel_getName(_selector));
} else {
char argType[256];
method_getArgumentType(method, wildcard.argIndex+2, argType, 256);
wildcard.argType = [self convertArgumentType:argType];
}
}
}
}
}
}
Expand Down Expand Up @@ -299,48 +314,62 @@ - (BOOL)matchURL:(NSURL*)URL {
return YES;
}

- (void)setArgumentsFromURL:(NSURL*)URL forInvocation:(NSInvocation*)invocation {
NSArray* pathComponents = URL.path.pathComponents;
for (NSInteger i = 0; i < _path.count; ++i) {
id<TTURLPatternText> patternText = [_path objectAtIndex:i];
NSString* text = i == 0 ? URL.host : [pathComponents objectAtIndex:i];
- (void)setArgument:(NSString*)text pattern:(id<TTURLPatternText>)patternText
forInvocation:(NSInvocation*)invocation {
if ([patternText isKindOfClass:[TTURLWildcard class]]) {
TTURLWildcard* wildcard = (TTURLWildcard*)patternText;
NSInteger index = wildcard.argIndex+2;
NSInteger index = wildcard.argIndex;
if (index != NSNotFound) {
switch (wildcard.argType) {
case TTURLArgumentTypeInteger: {
int val = [text intValue];
[invocation setArgument:&val atIndex:index];
[invocation setArgument:&val atIndex:index+2];
break;
}
case TTURLArgumentTypeLongLong: {
long long val = [text longLongValue];
[invocation setArgument:&val atIndex:index];
[invocation setArgument:&val atIndex:index+2];
break;
}
case TTURLArgumentTypeFloat: {
float val = [text floatValue];
[invocation setArgument:&val atIndex:index];
[invocation setArgument:&val atIndex:index+2];
break;
}
case TTURLArgumentTypeDouble: {
double val = [text doubleValue];
[invocation setArgument:&val atIndex:index];
[invocation setArgument:&val atIndex:index+2];
break;
}
case TTURLArgumentTypeBool: {
BOOL val = [text boolValue];
[invocation setArgument:&val atIndex:index];
[invocation setArgument:&val atIndex:index+2];
break;
}
default: {
[invocation setArgument:&text atIndex:index];
[invocation setArgument:&text atIndex:index+2];
break;
}
}
}
}
}

- (void)setArgumentsFromURL:(NSURL*)URL forInvocation:(NSInvocation*)invocation {
NSArray* pathComponents = URL.path.pathComponents;
for (NSInteger i = 0; i < _path.count; ++i) {
id<TTURLPatternText> patternText = [_path objectAtIndex:i];
NSString* text = i == 0 ? URL.host : [pathComponents objectAtIndex:i];
[self setArgument:text pattern:patternText forInvocation:invocation];
}

NSDictionary* query = [URL.query queryDictionaryUsingEncoding:NSUTF8StringEncoding];
for (NSString* name in [query keyEnumerator]) {
id<TTURLPatternText> patternText = [_query objectForKey:name];
if (patternText) {
NSString* text = [query objectForKey:name];
[self setArgument:text pattern:patternText forInvocation:invocation];
}
}
}

Expand Down

0 comments on commit b9b9a88

Please sign in to comment.