PreviewLocationViewController.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "PreviewLocationViewController.h"
  21. #import "UIImageView+WebCache.h"
  22. #import "PoiTableViewCell.h"
  23. #import "UserSettings.h"
  24. #import "PointOfInterest.h"
  25. #ifdef DEBUG
  26. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  27. #else
  28. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  29. #endif
  30. static const CLLocationDegrees kDefaultSpan = 0.01;
  31. static const float kPlacesUpdateInterval = 5.0;
  32. @interface PreviewLocationViewController ()
  33. @property CLLocationManager *locationManager;
  34. @end
  35. @implementation PreviewLocationViewController {
  36. CLLocation *lastLocation, *lastLocationPlaces;
  37. NSMutableArray *curPlaces;
  38. NSUInteger placesRequestCount;
  39. NSDate *lastPlacesUpdate;
  40. BOOL loading;
  41. NSString *lastSearchText;
  42. UIBarButtonItem *rightBarButton;
  43. UIBarButtonItem *leftBarButton;
  44. UIView *overlayView;
  45. UIView *lineView;
  46. UIRefreshControl *refreshControl;
  47. }
  48. - (void)dealloc {
  49. self.mapView.delegate = nil;
  50. }
  51. - (void)viewDidLoad {
  52. [super viewDidLoad];
  53. if ([UserSettings sharedUserSettings].enablePoi) {
  54. [self setRefreshControlTitle:NO];
  55. overlayView = [[UIView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
  56. overlayView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
  57. overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  58. [overlayView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(overlayViewTapped)]];
  59. leftBarButton = self.navigationItem.leftBarButtonItem;
  60. rightBarButton = self.navigationItem.rightBarButtonItem;
  61. self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
  62. self.searchController.searchBar.showsScopeBar = NO;
  63. self.searchController.searchBar.scopeButtonTitles = nil;
  64. self.searchController.searchBar.delegate = self;
  65. self.searchController.searchResultsUpdater = self;
  66. self.searchController.delegate = self;
  67. self.searchController.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  68. [self.searchController.searchBar sizeToFit];
  69. self.searchController.dimsBackgroundDuringPresentation = NO;
  70. self.searchController.hidesNavigationBarDuringPresentation = false;
  71. self.definesPresentationContext = YES;
  72. self.navigationItem.titleView = self.searchController.searchBar;
  73. self.navigationItem.leftBarButtonItem = leftBarButton;
  74. self.navigationItem.rightBarButtonItem = rightBarButton;
  75. refreshControl = [UIRefreshControl new];
  76. [refreshControl addTarget:self action:@selector(pulledForRefresh:) forControlEvents:UIControlEventValueChanged];
  77. self.poiTableView.refreshControl = refreshControl;
  78. self.poiTableView.rowHeight = UITableViewAutomaticDimension;
  79. self.poiTableView.estimatedRowHeight = 44.0;
  80. } else {
  81. CGRect frame = self.mapView.frame;
  82. frame.size.height = self.poiTableView.frame.origin.y + self.poiTableView.frame.size.height;
  83. self.mapView.frame = frame;
  84. [self.poiTableView removeFromSuperview];
  85. }
  86. [self setupColors];
  87. }
  88. - (void)setupColors {
  89. [self.view setBackgroundColor:[Colors backgroundLight]];
  90. [Colors updateTableView:self.poiTableView];
  91. [Colors updateSearchBar:_searchController.searchBar];
  92. }
  93. - (void)viewWillAppear:(BOOL)animated {
  94. [super viewWillAppear:animated];
  95. [self updateCanSend];
  96. [self.mapView setRegion:MKCoordinateRegionMake(self.mapView.userLocation.coordinate, MKCoordinateSpanMake(kDefaultSpan, kDefaultSpan)) animated:NO];
  97. if (lineView != nil)
  98. lineView.frame = CGRectMake(0, self.poiTableView.frame.origin.y - 1, self.poiTableView.frame.size.width, 1);
  99. }
  100. - (void)viewWillDisappear:(BOOL)animated {
  101. [super viewWillDisappear:animated];
  102. }
  103. - (void)viewDidAppear:(BOOL)animated {
  104. [self checkLocationAccess];
  105. [self mapView:self.mapView didUpdateUserLocation:self.mapView.userLocation];
  106. [super viewDidAppear:animated];
  107. }
  108. - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  109. [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
  110. lineView.frame = CGRectMake(0, self.poiTableView.frame.origin.y - 1, self.poiTableView.frame.size.width, 1);
  111. }
  112. - (BOOL)shouldAutorotate {
  113. return YES;
  114. }
  115. -(UIInterfaceOrientationMask)supportedInterfaceOrientations {
  116. if (SYSTEM_IS_IPAD) {
  117. return UIInterfaceOrientationMaskAll;
  118. }
  119. return UIInterfaceOrientationMaskAllButUpsideDown;
  120. }
  121. #pragma mark - Private functions
  122. - (void)setRefreshControlTitle:(BOOL)active {
  123. NSString *refreshText = nil;
  124. UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
  125. if (active) {
  126. refreshText = NSLocalizedString(@"refreshing", nil);
  127. } else {
  128. refreshText = NSLocalizedString(@"pull_to_refresh", nil);
  129. }
  130. NSMutableAttributedString *attributedRefreshText = [[NSMutableAttributedString alloc] initWithString:refreshText attributes:@{ NSFontAttributeName: font, NSForegroundColorAttributeName: [Colors fontLight], NSBackgroundColorAttributeName: [UIColor clearColor]}];
  131. refreshControl.attributedTitle = attributedRefreshText;
  132. }
  133. - (void)checkLocationAccess {
  134. CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
  135. if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {
  136. [self showLocationAccessAlert];
  137. } else {
  138. if (status == kCLAuthorizationStatusNotDetermined) {
  139. _locationManager = [[CLLocationManager alloc] init];
  140. [_locationManager requestWhenInUseAuthorization];
  141. }
  142. }
  143. }
  144. - (void)showLocationAccessAlert {
  145. [UIAlertTemplate showAlertWithOwner:self title:NSLocalizedString(@"location_disabled_title", nil) message:NSLocalizedString(@"location_disabled_message", nil) actionOk:nil];
  146. }
  147. - (void)updateCanSend {
  148. self.navigationItem.rightBarButtonItem.enabled = (self.mapView.userLocation != nil && !(self.mapView.userLocation.coordinate.latitude == 0 && self.mapView.userLocation.coordinate.longitude == 0));
  149. }
  150. - (void)pulledForRefresh:(UIRefreshControl *)sender {
  151. [self setRefreshControlTitle:YES];
  152. lastPlacesUpdate = nil;
  153. [self updatePlacesForLocation:self.mapView.userLocation.location noDelay:NO onCompletion:^{
  154. [sender endRefreshing];
  155. [self setRefreshControlTitle:NO];
  156. }];
  157. }
  158. #pragma mark Map view delegate
  159. - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
  160. [self updateCanSend];
  161. if (userLocation == nil)
  162. return;
  163. /* Determine distance to last location. If greater than 1 km, pan map */
  164. CLLocationDistance distance = 0;
  165. if (lastLocation != nil)
  166. distance = [lastLocation distanceFromLocation:userLocation.location];
  167. if (lastLocation == nil || distance > 1000) {
  168. MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(kDefaultSpan, kDefaultSpan));
  169. [mapView setRegion:region animated:(lastLocation != nil)];
  170. lastLocation = userLocation.location;
  171. }
  172. /* Determine distance separately for places reload */
  173. distance = 0;
  174. if (lastLocationPlaces != nil)
  175. distance = [lastLocationPlaces distanceFromLocation:userLocation.location];
  176. if (lastLocationPlaces == nil || distance > 20) {
  177. lastLocationPlaces = userLocation.location;
  178. [self updatePlacesForLocation:lastLocationPlaces noDelay:NO onCompletion:nil];
  179. }
  180. }
  181. - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
  182. if (![view.annotation isKindOfClass:[PointOfInterest class]])
  183. return;
  184. PointOfInterest *poi = view.annotation;
  185. [self.delegate previewLocationController:self didChooseToSendCoordinate:CLLocationCoordinate2DMake(poi.latitude, poi.longitude) accuracy:0 poiName:poi.name poiAddress:poi.address];
  186. [self dismissViewControllerAnimated:YES completion:nil];
  187. }
  188. #pragma mark Table view delegate
  189. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  190. if (curPlaces.count == 0)
  191. return;
  192. PointOfInterest *poi = [curPlaces objectAtIndex:indexPath.row];
  193. [self.delegate previewLocationController:self didChooseToSendCoordinate:CLLocationCoordinate2DMake(poi.latitude, poi.longitude) accuracy:0 poiName:poi.name poiAddress:poi.address];
  194. [self dismissViewControllerAnimated:YES completion:nil];
  195. }
  196. #pragma mark Table view data source
  197. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  198. if ([cell isKindOfClass:[UITableViewCell class]]) {
  199. [Colors updateTableViewCell:cell];
  200. }
  201. if ([cell isKindOfClass:[PoiTableViewCell class]]) {
  202. [((PoiTableViewCell *)cell).addressLabel setTextColor:[Colors fontLight]];
  203. }
  204. }
  205. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  206. if (tableView == self.poiTableView) {
  207. if (curPlaces.count == 0)
  208. return 1;
  209. else
  210. return curPlaces.count;
  211. } else {
  212. /* search bar */
  213. return 0;
  214. }
  215. }
  216. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  217. return UITableViewAutomaticDimension;
  218. }
  219. -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  220. return UITableViewAutomaticDimension;
  221. }
  222. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  223. if (tableView == self.poiTableView) {
  224. if (curPlaces.count == 0) {
  225. if (loading) {
  226. static NSString *CellIdentifier = @"SpinnerCell";
  227. return [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  228. } else if (_searchController.searchBar.text.length == 0) {
  229. static NSString *CellIdentifier = @"EnterQueryCell";
  230. return [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  231. } else {
  232. static NSString *CellIdentifier = @"NoPlacesFoundCell";
  233. return [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  234. }
  235. } else {
  236. static NSString *CellIdentifier = @"PoiCell";
  237. PoiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  238. PointOfInterest *poi = [curPlaces objectAtIndex:indexPath.row];
  239. cell.nameLabel.text = poi.name;
  240. cell.addressLabel.text = poi.address;
  241. return cell;
  242. }
  243. } else {
  244. /* search bar */
  245. return nil;
  246. }
  247. }
  248. #pragma mark - Search controller delegate
  249. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  250. NSString *searchText = searchBar.text;
  251. DDLogVerbose(@"search: %@", searchBar.text);
  252. [_searchController setActive:NO];
  253. _searchController.searchBar.text = searchText;
  254. lastPlacesUpdate = nil;
  255. lastSearchText = searchText;
  256. [self updatePlacesForLocation:self.mapView.userLocation.location noDelay:NO onCompletion:nil];
  257. }
  258. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
  259. DDLogVerbose(@"text changed: %@, active: %d", searchBar.text, _searchController.active);
  260. if (searchText.length == 0 && ![lastSearchText isEqualToString:searchText]) {
  261. lastPlacesUpdate = nil;
  262. lastSearchText = searchText;
  263. [self updatePlacesForLocation:self.mapView.userLocation.location noDelay:NO onCompletion:nil];
  264. }
  265. }
  266. - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
  267. DDLogVerbose(@"searchDisplayControllerWillBeginSearch");
  268. if (_searchController.active && searchBar.text.length > 0)
  269. [self.view addSubview:overlayView];
  270. else
  271. [overlayView removeFromSuperview];
  272. [self.navigationItem setRightBarButtonItem:nil animated:YES];
  273. [self.navigationItem setLeftBarButtonItem:nil animated:YES];
  274. }
  275. - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
  276. DDLogVerbose(@"searchDisplayControllerWillEndSearch");
  277. [overlayView removeFromSuperview];
  278. [self.navigationItem setRightBarButtonItem:rightBarButton animated:YES];
  279. [self.navigationItem setLeftBarButtonItem:leftBarButton animated:YES];
  280. }
  281. -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
  282. if (searchController.searchBar.text.length == 0 && ![lastSearchText isEqualToString:searchController.searchBar.text]) {
  283. lastPlacesUpdate = nil;
  284. lastSearchText = searchController.searchBar.text;
  285. [self updatePlacesForLocation:self.mapView.userLocation.location noDelay:NO onCompletion:nil];
  286. }
  287. if (searchController.active && searchController.searchBar.text.length > 0)
  288. [self.view addSubview:overlayView];
  289. else
  290. [overlayView removeFromSuperview];
  291. }
  292. - (void)overlayViewTapped {
  293. _searchController.active = NO;
  294. }
  295. #pragma mark - IBActions
  296. - (IBAction)cancelAction:(id)sender {
  297. if (_searchController.active)
  298. [_searchController setActive:NO];
  299. else
  300. [self dismissViewControllerAnimated:YES completion:nil];
  301. }
  302. - (IBAction)sendAction:(id)sender {
  303. if (self.mapView.userLocation != nil && !(self.mapView.userLocation.coordinate.latitude == 0 && self.mapView.userLocation.coordinate.longitude == 0))
  304. [self.delegate previewLocationController:self didChooseToSendCoordinate:self.mapView.userLocation.coordinate accuracy:self.mapView.userLocation.location.horizontalAccuracy poiName:nil poiAddress:nil];
  305. [self dismissViewControllerAnimated:YES completion:nil];
  306. }
  307. - (void)updatePlacesForLocation:(CLLocation*)location noDelay:(BOOL)noDelay onCompletion:(void(^)(void))onCompletion {
  308. if (location == nil || (fabs(location.coordinate.latitude) < 0.000001 && fabs(location.coordinate.longitude) < 0.000001)) {
  309. if (onCompletion != nil)
  310. onCompletion();
  311. return;
  312. }
  313. DDLogVerbose(@"updatePlacesForLocation: %@, loading: %d", location, loading);
  314. if (loading || (lastPlacesUpdate != nil && [lastPlacesUpdate timeIntervalSinceNow] > -kPlacesUpdateInterval)) {
  315. /* Another update is already in progress or was completed a short time ago. Check again in a moment. */
  316. DDLogVerbose(@"Already loading or loaded in the past %.1f seconds - %@", kPlacesUpdateInterval, noDelay ? @"ignoring" : @"delaying");
  317. if (!noDelay) {
  318. self.pendingLocation = location;
  319. /* use weak reference to self to avoid retain cycle that will keep us from not using location services anymore */
  320. __weak PreviewLocationViewController *wself = self;
  321. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kPlacesUpdateInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  322. [wself updatePlacesForLocation:wself.pendingLocation noDelay:YES onCompletion:onCompletion];
  323. wself.pendingLocation = nil;
  324. });
  325. } else {
  326. if (onCompletion != nil)
  327. onCompletion();
  328. }
  329. return;
  330. }
  331. lastPlacesUpdate = [NSDate date];
  332. double radius = MAX(50, MAX(location.horizontalAccuracy, location.verticalAccuracy) * 2);
  333. if (_searchController.searchBar.text.length > 0)
  334. radius = 1000;
  335. placesRequestCount++;
  336. loading = YES;
  337. NSUInteger lastPlacesRequestCount = placesRequestCount;
  338. if ([UserSettings sharedUserSettings].enablePoi) {
  339. [self.poiTableView reloadData];
  340. if (_searchController.searchBar.text.length > 0) {
  341. MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc] init];
  342. searchRequest.naturalLanguageQuery = _searchController.searchBar.text;
  343. searchRequest.region = self.mapView.region;
  344. MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
  345. [localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
  346. if (lastPlacesRequestCount < placesRequestCount) {
  347. DDLogInfo(@"Discarding old result from concurrent request");
  348. return;
  349. }
  350. if (curPlaces != nil) {
  351. [self.mapView removeAnnotations:curPlaces];
  352. }
  353. curPlaces = [[NSMutableArray alloc] init];
  354. for (MKMapItem *mapItem in response.mapItems) {
  355. PointOfInterest *poi = [[PointOfInterest alloc] init];
  356. poi.name = mapItem.placemark.name;
  357. poi.address = [self formatAddressForPlacemark:mapItem.placemark];
  358. poi.latitude = mapItem.placemark.coordinate.latitude;
  359. poi.longitude = mapItem.placemark.coordinate.longitude;
  360. [curPlaces addObject:poi];
  361. }
  362. [self.mapView addAnnotations:curPlaces];
  363. loading = NO;
  364. [self.poiTableView reloadData];
  365. [self.poiTableView setContentOffset:CGPointZero animated:YES];
  366. if (onCompletion != nil)
  367. onCompletion();
  368. }];
  369. } else {
  370. loading = NO;
  371. if (onCompletion != nil)
  372. onCompletion();
  373. }
  374. }
  375. }
  376. - (NSString*)formatAddressForPlacemark:(MKPlacemark*)placemark {
  377. NSMutableString *address = [[NSMutableString alloc] initWithString:@""];
  378. if (placemark.thoroughfare != nil && placemark.subThoroughfare != nil) {
  379. if ([[NSLocale currentLocale].languageCode isEqualToString:@"de"]) {
  380. [address appendString:[NSString stringWithFormat:@"%@ %@", placemark.thoroughfare, placemark.subThoroughfare]];
  381. } else if ([[NSLocale currentLocale].languageCode isEqualToString:@"fr"]) {
  382. [address appendString:[NSString stringWithFormat:@"%@, %@", placemark.subThoroughfare, placemark.thoroughfare]];
  383. } else {
  384. [address appendString:[NSString stringWithFormat:@"%@ %@", placemark.subThoroughfare, placemark.thoroughfare]];
  385. }
  386. }
  387. else if (placemark.thoroughfare != nil) {
  388. [address appendString:placemark.thoroughfare];
  389. }
  390. else if (placemark.subThoroughfare != nil) {
  391. [address appendString:placemark.subThoroughfare];
  392. }
  393. if (placemark.postalCode != nil || placemark.locality != nil) {
  394. if (address.length > 0) {
  395. [address appendString:@", "];
  396. }
  397. if (placemark.postalCode != nil) {
  398. [address appendString:[NSString stringWithFormat:@"%@ ", placemark.postalCode]];
  399. }
  400. if (placemark.locality != nil) {
  401. [address appendString:placemark.locality];
  402. }
  403. }
  404. return address;
  405. }
  406. @end