MasterViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2016 Google Inc. All rights reserved.
  3. *
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  6. * file except in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under
  11. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. * ANY KIND, either express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #import "GoogleMapsDemos/MasterViewController.h"
  16. #import <GoogleMaps/GoogleMaps.h>
  17. #import "GoogleMapsDemos/DemoAppDelegate.h"
  18. #import "GoogleMapsDemos/Samples/Samples.h"
  19. @implementation MasterViewController {
  20. NSArray *_demos;
  21. NSArray *_demoSections;
  22. BOOL _isPhone;
  23. UIPopoverController *_popover;
  24. UIBarButtonItem *_samplesButton;
  25. __weak UIViewController *_controller;
  26. CLLocationManager *_locationManager;
  27. }
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. _isPhone = [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone;
  31. if (!_isPhone) {
  32. self.clearsSelectionOnViewWillAppear = NO;
  33. } else {
  34. UIBarButtonItem *backButton =
  35. [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", @"Back")
  36. style:UIBarButtonItemStylePlain
  37. target:nil
  38. action:nil];
  39. [self.navigationItem setBackBarButtonItem:backButton];
  40. }
  41. self.title = NSLocalizedString(@"Maps SDK Demos", @"Maps SDK Demos");
  42. self.title = [NSString stringWithFormat:@"%@: %@", self.title, [GMSServices SDKVersion]];
  43. self.tableView.autoresizingMask =
  44. UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  45. self.tableView.delegate = self;
  46. self.tableView.dataSource = self;
  47. _demoSections = [Samples loadSections];
  48. _demos = [Samples loadDemos];
  49. if (!_isPhone) {
  50. [self loadDemo:0 atIndex:0];
  51. }
  52. }
  53. #pragma mark - UITableViewController
  54. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  55. return _demoSections.count;
  56. }
  57. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  58. return 35.0;
  59. }
  60. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  61. return [_demoSections objectAtIndex:section];
  62. }
  63. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  64. NSArray *demosInSection = [_demos objectAtIndex:section];
  65. return demosInSection.count;
  66. }
  67. - (UITableViewCell *)tableView:(UITableView *)tableView
  68. cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  69. static NSString *cellIdentifier = @"Cell";
  70. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  71. if (cell == nil) {
  72. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  73. reuseIdentifier:cellIdentifier];
  74. if (_isPhone) {
  75. [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
  76. }
  77. }
  78. NSDictionary *demo = [[_demos objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  79. cell.textLabel.text = [demo objectForKey:@"title"];
  80. cell.detailTextLabel.text = [demo objectForKey:@"description"];
  81. return cell;
  82. }
  83. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  84. // The user has chosen a sample; load it and clear the selection!
  85. [self loadDemo:indexPath.section atIndex:indexPath.row];
  86. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  87. }
  88. #pragma mark - Split view
  89. - (void)splitViewController:(UISplitViewController *)splitController
  90. willHideViewController:(UIViewController *)viewController
  91. withBarButtonItem:(UIBarButtonItem *)barButtonItem
  92. forPopoverController:(UIPopoverController *)popoverController {
  93. _popover = popoverController;
  94. _samplesButton = barButtonItem;
  95. _samplesButton.title = NSLocalizedString(@"Samples", @"Samples");
  96. _samplesButton.style = UIBarButtonItemStyleDone;
  97. [self updateSamplesButton];
  98. }
  99. - (void)splitViewController:(UISplitViewController *)splitController
  100. willShowViewController:(UIViewController *)viewController
  101. invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem {
  102. _popover = nil;
  103. _samplesButton = nil;
  104. [self updateSamplesButton];
  105. }
  106. #pragma mark - Private methods
  107. - (void)loadDemo:(NSUInteger)section atIndex:(NSUInteger)index {
  108. NSDictionary *demo = [[_demos objectAtIndex:section] objectAtIndex:index];
  109. UIViewController *controller = [[[demo objectForKey:@"controller"] alloc] init];
  110. _controller = controller;
  111. if (controller != nil) {
  112. controller.title = [demo objectForKey:@"title"];
  113. if (_isPhone) {
  114. [self.navigationController pushViewController:controller animated:YES];
  115. } else {
  116. [self.appDelegate setSample:controller];
  117. [_popover dismissPopoverAnimated:YES];
  118. }
  119. [self updateSamplesButton];
  120. }
  121. }
  122. // This method is invoked when the left 'back' button in the split view
  123. // controller on iPad should be updated (either made visible or hidden).
  124. // It assumes that the left bar button item may be safely modified to contain
  125. // the samples button.
  126. - (void)updateSamplesButton {
  127. _controller.navigationItem.leftBarButtonItem = _samplesButton;
  128. }
  129. @end