FindPlaceLikelihoodListViewController.m 9.75 KB
Newer Older
Julio Hermosa committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
/*
 * Copyright 2019 Google Inc. All rights reserved.
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under
 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

#import "GooglePlacesDemos/Samples/FindPlaceLikelihoodListViewController.h"

#import <GooglePlaces/GooglePlaces.h>


static NSString *const kCellIdentifier = @"LikelihoodCellIdentifier";

#pragma mark - ButtonCoordinateView

@interface ButtonCoordinateView : UIView

// The button used to trigger the fetch likelihoods from coordinate action.
@property(nonatomic, strong) UIButton *button;

@end

@implementation ButtonCoordinateView {
}

- (instancetype)init {
  if (self = [super init]) {
    [self setupUI];
  }
  return self;
}

- (void)setupUI {
  self.layer.cornerRadius = 3;
  self.layer.masksToBounds = YES;
  self.layer.borderColor = [UIColor clearColor].CGColor;
  self.layer.borderWidth = 1;

  UIStackView *stackView = [[UIStackView alloc] init];
  stackView.axis = UILayoutConstraintAxisHorizontal;
  stackView.spacing = 15;
  stackView.layoutMargins = UIEdgeInsetsMake(5, 0, 5, 0);
  stackView.layoutMarginsRelativeArrangement = YES;
  [self addSubview:stackView];

  UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  [button setTitle:@"Retrieve location" forState:UIControlStateNormal];
  [stackView addArrangedSubview:button];
  _button = button;

  UIStackView *labelsStackView = [[UIStackView alloc] init];
  labelsStackView.axis = UILayoutConstraintAxisVertical;
  [stackView addArrangedSubview:labelsStackView];

  [stackView setTranslatesAutoresizingMaskIntoConstraints:NO];
  [NSLayoutConstraint activateConstraints:@[
    [stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
    [stackView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
    [stackView.topAnchor constraintEqualToAnchor:self.topAnchor],
    [stackView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor]
  ]];
}

// Sets the title and target for the button.
- (void)fillWithButtonTitle:(NSString *)title
                     target:(id)target
                     action:(SEL)action
           forControlEvents:(UIControlEvents)controlEvents {
  [_button setTitle:title forState:UIControlStateNormal];
  [_button addTarget:target action:action forControlEvents:controlEvents];
}


@end

#pragma mark - FindPlaceLikelihoodListViewController

@interface FindPlaceLikelihoodListViewController ()

@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSArray<GMSPlaceLikelihood *> *placeLikelihoods;
@property(nonatomic, strong) UILabel *errorLabel;
@property(nonatomic, strong) ButtonCoordinateView *currentButtonCoordinateView;

@end

@implementation FindPlaceLikelihoodListViewController {
  CLLocationManager *_locationManager;
  GMSPlacesClient *_placesClient;
}

+ (NSString *)demoTitle {
  return @"Find Place Likelihoods";
}

- (void)viewDidLoad {
  [super viewDidLoad];

  _placesClient = [GMSPlacesClient sharedClient];

  // Initializes the location manager to be used for current location.
  CLLocationManager *locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  _locationManager = locationManager;

  self.title = [NSString stringWithFormat:@"Find place likelihoods from location"];
  self.view.backgroundColor = [UIColor whiteColor];

  UIStackView *mainStackView = [[UIStackView alloc] init];
  mainStackView.axis = UILayoutConstraintAxisVertical;
  [self.view addSubview:mainStackView];

  // Adds the location input section.
  UIStackView *locationInputStackView = [[UIStackView alloc] initWithFrame:CGRectZero];
  locationInputStackView.axis = UILayoutConstraintAxisVertical;
  locationInputStackView.layoutMargins = UIEdgeInsetsMake(20, 15, 0, 15);
  locationInputStackView.layoutMarginsRelativeArrangement = YES;
  locationInputStackView.distribution = UIStackViewDistributionFill;
  locationInputStackView.alignment = UIStackViewAlignmentFill;
  locationInputStackView.spacing = 10;
  [mainStackView addArrangedSubview:locationInputStackView];

  ButtonCoordinateView *currentView = [[ButtonCoordinateView alloc] init];
  [currentView fillWithButtonTitle:@"Find from current location"
                            target:self
                            action:@selector(onCurrentLocationTap)
                  forControlEvents:UIControlEventTouchUpInside];
  [locationInputStackView addArrangedSubview:currentView];
  _currentButtonCoordinateView = currentView;


  UILabel *errorLabel = [[UILabel alloc] init];
  errorLabel.textColor = [UIColor redColor];
  errorLabel.numberOfLines = 0;
  [locationInputStackView addArrangedSubview:errorLabel];
  _errorLabel = errorLabel;

  // Adds the likelihood list table.
  UITableView *tableView = [[UITableView alloc] init];
  [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kCellIdentifier];
  tableView.delegate = self;
  tableView.dataSource = self;
  [mainStackView addArrangedSubview:tableView];
  _tableView = tableView;

  mainStackView.translatesAutoresizingMaskIntoConstraints = NO;
  NSArray<NSLayoutConstraint *> *stackViewConstraints = @[
    [mainStackView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
    [mainStackView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
    [mainStackView.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor],
    [mainStackView.bottomAnchor constraintEqualToAnchor:self.bottomLayoutGuide.topAnchor]
  ];
#if defined(__IPHONE_11_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_11_0)
  if (@available(iOS 11.0, *)) {
    stackViewConstraints = @[
      [mainStackView.leadingAnchor
          constraintEqualToAnchor:self.view.safeAreaLayoutGuide.leadingAnchor],
      [mainStackView.trailingAnchor
          constraintEqualToAnchor:self.view.safeAreaLayoutGuide.trailingAnchor],
      [mainStackView.topAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.topAnchor],
      [mainStackView.bottomAnchor
          constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor]
    ];
  }
#endif
  [NSLayoutConstraint activateConstraints:stackViewConstraints];

  [self onCurrentLocationTap];
}

#pragma mark - Button Handlers

// Requests location services authorization if needed, and starts updating location.
- (void)onCurrentLocationTap {
  if (![FindPlaceLikelihoodListViewController areLocationServicesEnabledAndAuthorized]) {
    [_locationManager requestWhenInUseAuthorization];

    return;
  }

  [_locationManager startUpdatingLocation];

  __block FindPlaceLikelihoodListViewController *weakSelf = self;
  GMSPlaceLikelihoodsCallback fetcherCallback =
      ^(NSArray<GMSPlaceLikelihood *> *_Nullable likelihoods, NSError *_Nullable error) {
        [weakSelf handleFindPlaceLikelihoodsResponse:likelihoods error:error];
      };

  [_placesClient findPlaceLikelihoodsFromCurrentLocationWithPlaceFields:GMSPlaceFieldAll
                                                               callback:fetcherCallback];
}


#pragma mark - CLLocationManagerDelegate

// Retries retrieving current location if user has granted location services permission.
- (void)locationManager:(CLLocationManager *)manager
    didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
    // Retry current location fetch once user enables Location Services.
    [self onCurrentLocationTap];
  } else {
    _errorLabel.text = @"Please make sure location services are enabled.";
  }
}


#pragma mark - UITableViewDataSource/Delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (_placeLikelihoods == nil) {
    return 0;
  }
  return _placeLikelihoods.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier
                                                          forIndexPath:indexPath];
  cell.textLabel.numberOfLines = 0;
  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  NSInteger row = indexPath.row;
  NSInteger likelihoodCount = _placeLikelihoods.count;
  if (likelihoodCount > 0 && row < likelihoodCount) {
    GMSPlaceLikelihood *likelihood = _placeLikelihoods[row];
    cell.textLabel.text = likelihood.place.name;
  }

  return cell;
}

#pragma mark - Helpers

// Checks if user has authorized location services required for retrieving device location.
+ (BOOL)areLocationServicesEnabledAndAuthorized {
  if (![CLLocationManager locationServicesEnabled]) {
    return NO;
  }

  CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
  return status == kCLAuthorizationStatusAuthorizedAlways ||
         status == kCLAuthorizationStatusAuthorizedWhenInUse;
}

- (void)handleFindPlaceLikelihoodsResponse:(NSArray<GMSPlaceLikelihood *> *)likelihoods
                                     error:(NSError *)error {
  if (error != nil) {
    _errorLabel.text = @"There was an error fetching likelihoods.";
    return;
  }

  // Filters out Places that don't have a valid name.
  NSPredicate *predicate = [NSPredicate
      predicateWithBlock:^BOOL(GMSPlaceLikelihood *likelihood, NSDictionary<NSString *, id> *b) {
        return likelihood.place.name != nil && [likelihood.place.name length] > 0;
      }];
  _placeLikelihoods = [likelihoods filteredArrayUsingPredicate:predicate];
  _errorLabel.text = nil;
  [_tableView reloadData];
  [self resignFirstResponder];
}


@end