diff options
Diffstat (limited to 'examples/objective-c/route_guide/ViewControllers.m')
-rw-r--r-- | examples/objective-c/route_guide/ViewControllers.m | 143 |
1 files changed, 113 insertions, 30 deletions
diff --git a/examples/objective-c/route_guide/ViewControllers.m b/examples/objective-c/route_guide/ViewControllers.m index 0b1a1cf482..b2f99c437e 100644 --- a/examples/objective-c/route_guide/ViewControllers.m +++ b/examples/objective-c/route_guide/ViewControllers.m @@ -81,24 +81,29 @@ static NSString * const kHostAddress = @"localhost:50051"; * not to have a feature. */ @interface GetFeatureViewController : UIViewController -@end - -@implementation GetFeatureViewController -- (void)viewDidLoad { - [super viewDidLoad]; +@property (weak, nonatomic) IBOutlet UILabel *outputLabel; - // This only needs to be done once per host, before creating service objects for that host. - [GRPCCall useInsecureConnectionsForHost:kHostAddress]; +@end - RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; +@implementation GetFeatureViewController { + RTGRouteGuide *_service; +} +- (void)execRequest { void (^handler)(RTGFeature *response, NSError *error) = ^(RTGFeature *response, NSError *error) { + // TODO(makdharma): Remove boilerplate by consolidating into one log function. if (response.name.length) { + NSString *str =[NSString stringWithFormat:@"%@\nFound feature called %@ at %@.", self.outputLabel.text, response.location, response.name]; + self.outputLabel.text = str; NSLog(@"Found feature called %@ at %@.", response.name, response.location); } else if (response) { + NSString *str =[NSString stringWithFormat:@"%@\nFound no features at %@", self.outputLabel.text,response.location]; + self.outputLabel.text = str; NSLog(@"Found no features at %@", response.location); } else { + NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + self.outputLabel.text = str; NSLog(@"RPC error: %@", error); } }; @@ -107,8 +112,24 @@ static NSString * const kHostAddress = @"localhost:50051"; point.latitude = 409146138; point.longitude = -746188906; - [service getFeatureWithRequest:point handler:handler]; - [service getFeatureWithRequest:[RTGPoint message] handler:handler]; + [_service getFeatureWithRequest:point handler:handler]; + [_service getFeatureWithRequest:[RTGPoint message] handler:handler]; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + + // This only needs to be done once per host, before creating service objects for that host. + [GRPCCall useInsecureConnectionsForHost:kHostAddress]; + + _service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; +} + +- (void)viewDidAppear:(BOOL)animated { + self.outputLabel.text = @"RPC log:"; + self.outputLabel.numberOfLines = 0; + self.outputLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:8.0]; + [self execRequest]; } @end @@ -121,15 +142,16 @@ static NSString * const kHostAddress = @"localhost:50051"; * the pre-generated database. Prints each response as it comes in. */ @interface ListFeaturesViewController : UIViewController -@end -@implementation ListFeaturesViewController +@property (weak, nonatomic) IBOutlet UILabel *outputLabel; -- (void)viewDidLoad { - [super viewDidLoad]; +@end - RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; +@implementation ListFeaturesViewController { + RTGRouteGuide *_service; +} +- (void)execRequest { RTGRectangle *rectangle = [RTGRectangle message]; rectangle.lo.latitude = 405E6; rectangle.lo.longitude = -750E6; @@ -137,16 +159,33 @@ static NSString * const kHostAddress = @"localhost:50051"; rectangle.hi.longitude = -745E6; NSLog(@"Looking for features between %@ and %@", rectangle.lo, rectangle.hi); - [service listFeaturesWithRequest:rectangle + [_service listFeaturesWithRequest:rectangle eventHandler:^(BOOL done, RTGFeature *response, NSError *error) { if (response) { + NSString *str =[NSString stringWithFormat:@"%@\nFound feature at %@ called %@.", self.outputLabel.text, response.location, response.name]; + self.outputLabel.text = str; NSLog(@"Found feature at %@ called %@.", response.location, response.name); } else if (error) { + NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + self.outputLabel.text = str; NSLog(@"RPC error: %@", error); } }]; } +- (void)viewDidLoad { + [super viewDidLoad]; + + _service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; +} + +- (void)viewDidAppear:(BOOL)animated { + self.outputLabel.text = @"RPC log:"; + self.outputLabel.numberOfLines = 0; + self.outputLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:8.0]; + [self execRequest]; +} + @end @@ -158,13 +197,16 @@ static NSString * const kHostAddress = @"localhost:50051"; * server. */ @interface RecordRouteViewController : UIViewController -@end -@implementation RecordRouteViewController +@property (weak, nonatomic) IBOutlet UILabel *outputLabel; -- (void)viewDidLoad { - [super viewDidLoad]; +@end + +@implementation RecordRouteViewController { + RTGRouteGuide *_service; +} +- (void)execRequest { NSString *dataBasePath = [NSBundle.mainBundle pathForResource:@"route_guide_db" ofType:@"json"]; NSData *dataBaseContent = [NSData dataWithContentsOfFile:dataBasePath]; @@ -174,25 +216,46 @@ static NSString * const kHostAddress = @"localhost:50051"; RTGPoint *location = [RTGPoint message]; location.longitude = [((NSNumber *) feature[@"location"][@"longitude"]) intValue]; location.latitude = [((NSNumber *) feature[@"location"][@"latitude"]) intValue]; + NSString *str =[NSString stringWithFormat:@"%@\nVisiting point %@", self.outputLabel.text, location]; + self.outputLabel.text = str; NSLog(@"Visiting point %@", location); return location; }]; - RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; - - [service recordRouteWithRequestsWriter:locations + [_service recordRouteWithRequestsWriter:locations handler:^(RTGRouteSummary *response, NSError *error) { if (response) { + NSString *str =[NSString stringWithFormat: + @"%@\nFinished trip with %i points\nPassed %i features\n" + "Travelled %i meters\nIt took %i seconds", + self.outputLabel.text, response.pointCount, response.featureCount, + response.distance, response.elapsedTime]; + self.outputLabel.text = str; NSLog(@"Finished trip with %i points", response.pointCount); NSLog(@"Passed %i features", response.featureCount); NSLog(@"Travelled %i meters", response.distance); NSLog(@"It took %i seconds", response.elapsedTime); } else { + NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + self.outputLabel.text = str; NSLog(@"RPC error: %@", error); } }]; } +- (void)viewDidLoad { + [super viewDidLoad]; + + _service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; +} + +- (void)viewDidAppear:(BOOL)animated { + self.outputLabel.text = @"RPC log:"; + self.outputLabel.numberOfLines = 0; + self.outputLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:8.0]; + [self execRequest]; +} + @end @@ -203,13 +266,16 @@ static NSString * const kHostAddress = @"localhost:50051"; * the server. */ @interface RouteChatViewController : UIViewController -@end -@implementation RouteChatViewController +@property (weak, nonatomic) IBOutlet UILabel *outputLabel; -- (void)viewDidLoad { - [super viewDidLoad]; +@end +@implementation RouteChatViewController { + RTGRouteGuide *_service; +} + +- (void)execRequest { NSArray *notes = @[[RTGRouteNote noteWithMessage:@"First message" latitude:0 longitude:0], [RTGRouteNote noteWithMessage:@"Second message" latitude:0 longitude:1], [RTGRouteNote noteWithMessage:@"Third message" latitude:1 longitude:0], @@ -219,13 +285,16 @@ static NSString * const kHostAddress = @"localhost:50051"; return note; }]; - RTGRouteGuide *service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; - - [service routeChatWithRequestsWriter:notesWriter + [_service routeChatWithRequestsWriter:notesWriter eventHandler:^(BOOL done, RTGRouteNote *note, NSError *error) { if (note) { + NSString *str =[NSString stringWithFormat:@"%@\nGot message %@ at %@", + self.outputLabel.text, note.message, note.location]; + self.outputLabel.text = str; NSLog(@"Got message %@ at %@", note.message, note.location); } else if (error) { + NSString *str =[NSString stringWithFormat:@"%@\nRPC error: %@", self.outputLabel.text, error]; + self.outputLabel.text = str; NSLog(@"RPC error: %@", error); } if (done) { @@ -234,4 +303,18 @@ static NSString * const kHostAddress = @"localhost:50051"; }]; } +- (void)viewDidLoad { + [super viewDidLoad]; + + _service = [[RTGRouteGuide alloc] initWithHost:kHostAddress]; +} + +- (void)viewDidAppear:(BOOL)animated { + // TODO(makarandd): Set these properties through UI builder + self.outputLabel.text = @"RPC log:"; + self.outputLabel.numberOfLines = 0; + self.outputLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:8.0]; + [self execRequest]; +} + @end |