SDKDemoMasterViewController.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/SDKDemoMasterViewController.h"
  5. #import "SDKDemos/PlacesSamples/Samples+Places.h"
  6. #import "SDKDemos/SDKDemoAppDelegate.h"
  7. #import <GoogleMaps/GoogleMaps.h>
  8. #import "SDKDemos/Samples/Samples.h"
  9. @implementation SDKDemoMasterViewController {
  10. NSArray *demos_;
  11. NSArray *demoSections_;
  12. BOOL isPhone_;
  13. UIPopoverController *popover_;
  14. UIBarButtonItem *samplesButton_;
  15. __weak UIViewController *controller_;
  16. CLLocationManager *locationManager_;
  17. }
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. isPhone_ = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone;
  21. if (!isPhone_) {
  22. self.clearsSelectionOnViewWillAppear = NO;
  23. } else {
  24. UIBarButtonItem *backButton =
  25. [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"Back")
  26. style:UIBarButtonItemStyleBordered
  27. target:nil
  28. action:nil];
  29. [self.navigationItem setBackBarButtonItem:backButton];
  30. }
  31. self.title = NSLocalizedString(@"Maps SDK Demos", @"Maps SDK Demos");
  32. self.title = [NSString stringWithFormat:@"%@: %@", self.title, [GMSServices SDKVersion]];
  33. self.tableView.autoresizingMask =
  34. UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
  35. self.tableView.delegate = self;
  36. self.tableView.dataSource = self;
  37. demoSections_ = [Samples loadSections];
  38. demos_ = [Samples loadDemos];
  39. [self addPlacesDemos];
  40. if (!isPhone_) {
  41. [self loadDemo:0 atIndex:0];
  42. }
  43. }
  44. - (void)addPlacesDemos {
  45. NSMutableArray *sections = [NSMutableArray arrayWithArray:demoSections_];
  46. [sections insertObject:@"Places" atIndex:0];
  47. demoSections_ = [sections copy];
  48. NSMutableArray *demos = [NSMutableArray arrayWithArray:demos_];
  49. [demos insertObject:[Samples placesDemos]
  50. atIndex:0];
  51. demos_ = [demos copy];
  52. }
  53. #pragma mark - UITableViewController
  54. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  55. return demoSections_.count;
  56. }
  57. - (CGFloat)tableView:(UITableView *)tableView
  58. heightForHeaderInSection:(NSInteger)section {
  59. return 35.0;
  60. }
  61. - (NSString *)tableView:(UITableView *)tableView
  62. titleForHeaderInSection:(NSInteger)section {
  63. return [demoSections_ objectAtIndex:section];
  64. }
  65. - (NSInteger)tableView:(UITableView *)tableView
  66. numberOfRowsInSection:(NSInteger)section {
  67. return [[demos_ objectAtIndex: section] count];
  68. }
  69. - (UITableViewCell *)tableView:(UITableView *)tableView
  70. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  71. static NSString *cellIdentifier = @"Cell";
  72. UITableViewCell *cell =
  73. [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  74. if (cell == nil) {
  75. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  76. reuseIdentifier:cellIdentifier];
  77. if (isPhone_) {
  78. [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
  79. }
  80. }
  81. NSDictionary *demo = [[demos_ objectAtIndex:indexPath.section]
  82. objectAtIndex:indexPath.row];
  83. cell.textLabel.text = [demo objectForKey:@"title"];
  84. cell.detailTextLabel.text = [demo objectForKey:@"description"];
  85. return cell;
  86. }
  87. - (void)tableView:(UITableView *)tableView
  88. didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  89. // The user has chosen a sample; load it and clear the selection!
  90. [self loadDemo:indexPath.section atIndex:indexPath.row];
  91. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  92. }
  93. #pragma mark - Split view
  94. - (void)splitViewController:(UISplitViewController *)splitController
  95. willHideViewController:(UIViewController *)viewController
  96. withBarButtonItem:(UIBarButtonItem *)barButtonItem
  97. forPopoverController:(UIPopoverController *)popoverController {
  98. popover_ = popoverController;
  99. samplesButton_ = barButtonItem;
  100. samplesButton_.title = NSLocalizedString(@"Samples", @"Samples");
  101. samplesButton_.style = UIBarButtonItemStyleDone;
  102. [self updateSamplesButton];
  103. }
  104. - (void)splitViewController:(UISplitViewController *)splitController
  105. willShowViewController:(UIViewController *)viewController
  106. invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
  107. popover_ = nil;
  108. samplesButton_ = nil;
  109. [self updateSamplesButton];
  110. }
  111. #pragma mark - Private methods
  112. - (void)loadDemo:(NSUInteger)section
  113. atIndex:(NSUInteger)index {
  114. NSDictionary *demo = [[demos_ objectAtIndex:section] objectAtIndex:index];
  115. UIViewController *controller =
  116. [[[demo objectForKey:@"controller"] alloc] init];
  117. controller_ = controller;
  118. if (controller != nil) {
  119. controller.title = [demo objectForKey:@"title"];
  120. if (isPhone_) {
  121. [self.navigationController pushViewController:controller animated:YES];
  122. } else {
  123. [self.appDelegate setSample:controller];
  124. [popover_ dismissPopoverAnimated:YES];
  125. }
  126. [self updateSamplesButton];
  127. }
  128. }
  129. // This method is invoked when the left 'back' button in the split view
  130. // controller on iPad should be updated (either made visible or hidden).
  131. // It assumes that the left bar button item may be safely modified to contain
  132. // the samples button.
  133. - (void)updateSamplesButton {
  134. controller_.navigationItem.leftBarButtonItem = samplesButton_;
  135. }
  136. @end