From ed62f99af89d6ccf2c750377112a3f3dcf5c80b2 Mon Sep 17 00:00:00 2001 From: VladiMihaylenko Date: Tue, 8 Dec 2015 15:26:01 +0300 Subject: [PATCH] [ios] Synchronized recent track's UI and core logic. --- .../Maps/MWMRecentTrackSettingsController.mm | 74 +++++++++++- iphone/Maps/Platform/LocationManager.mm | 105 ++++++++++-------- 2 files changed, 126 insertions(+), 53 deletions(-) diff --git a/iphone/Maps/MWMRecentTrackSettingsController.mm b/iphone/Maps/MWMRecentTrackSettingsController.mm index a8a94727d7..d0566a5187 100644 --- a/iphone/Maps/MWMRecentTrackSettingsController.mm +++ b/iphone/Maps/MWMRecentTrackSettingsController.mm @@ -1,7 +1,19 @@ #import "MWMRecentTrackSettingsController.h" #import "SelectableCell.h" +#include "Framework.h" + +typedef NS_ENUM(NSUInteger, DurationInHours) +{ + One = 1, + Two = 2, + Six = 6, + Twelve = 12, + Day = 24 +}; + @interface MWMRecentTrackSettingsController () + @property (weak, nonatomic) IBOutlet SelectableCell * none; @property (weak, nonatomic) IBOutlet SelectableCell * oneHour; @property (weak, nonatomic) IBOutlet SelectableCell * twoHours; @@ -18,22 +30,74 @@ { [super viewDidLoad]; self.title = L(@"recent_track"); - self.none.accessoryType = UITableViewCellAccessoryCheckmark; - self.selectedCell = self.none; + auto & f = GetFramework(); + + if (!f.IsGpsTrackingEnabled()) + { + _selectedCell = self.none; + } + else + { + switch (f.GetGpsTrackingDuration().count()) + { + case One: + _selectedCell = self.oneHour; + break; + case Two: + _selectedCell = self.twoHours; + break; + case Six: + _selectedCell = self.sixHours; + break; + case Twelve: + _selectedCell = self.twelveHours; + break; + case Day: + _selectedCell = self.oneDay; + break; + default: + NSAssert(false, @"Incorrect hours value"); + break; + } + } + self.selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; +} + +- (void)setSelectedCell:(SelectableCell *)selectedCell +{ + _selectedCell = selectedCell; + auto & f = GetFramework(); + if ([selectedCell isEqual:self.none]) + f.EnableGpsTracking(false); + else + { + if (!f.IsGpsTrackingEnabled()) + f.EnableGpsTracking(true); + + if ([selectedCell isEqual:self.oneHour]) + f.SetGpsTrackingDuration(hours(One)); + else if ([selectedCell isEqual:self.twoHours]) + f.SetGpsTrackingDuration(hours(Two)); + else if ([selectedCell isEqual:self.sixHours]) + f.SetGpsTrackingDuration(hours(Six)); + else if ([selectedCell isEqual:self.twelveHours]) + f.SetGpsTrackingDuration(hours(Twelve)); + else + f.SetGpsTrackingDuration(hours(Day)); + } + selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.selectedCell.accessoryType = UITableViewCellAccessoryNone; self.selectedCell = [tableView cellForRowAtIndexPath:indexPath]; - self.selectedCell.accessoryType = UITableViewCellAccessoryCheckmark; self.selectedCell.selected = NO; } - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section { - if (section != 0) - NSAssert(false, @"Incorrect sections count"); + NSAssert(section == 0, @"Incorrect sections count"); return L(@"recent_track_help_text"); } diff --git a/iphone/Maps/Platform/LocationManager.mm b/iphone/Maps/Platform/LocationManager.mm index 23f1c35721..74f7401878 100644 --- a/iphone/Maps/Platform/LocationManager.mm +++ b/iphone/Maps/Platform/LocationManager.mm @@ -7,6 +7,9 @@ #import "3party/Alohalytics/src/alohalytics_objc.h" +#include "Framework.h" + +#include "map/gps_track.hpp" #include "platform/measurement_utils.hpp" #include "platform/settings.hpp" #include "base/math.hpp" @@ -71,7 +74,8 @@ static NSString * const kAlohalyticsLocationRequestAlwaysFailed = @"$locationAlw - (void)beforeTerminate { - [m_locationManager startMonitoringSignificantLocationChanges]; + if (GetFramework().IsGpsTrackingEnabled()) + [m_locationManager startMonitoringSignificantLocationChanges]; } - (void)onForeground @@ -121,9 +125,7 @@ static NSString * const kAlohalyticsLocationRequestAlwaysFailed = @"$locationAlw { // pass last location known location when new observer is attached // (default CLLocationManagerDelegate behaviour) - location::GpsInfo newInfo; - [self location:[self lastLocation] toGpsInfo:newInfo]; - [observer onLocationUpdate:newInfo]; + [observer onLocationUpdate:gpsInfoFromLocation(self.lastLocation)]; } } } @@ -154,38 +156,6 @@ static NSString * const kAlohalyticsLocationRequestAlwaysFailed = @"$locationAlw return m_isStarted ? m_locationManager.heading : nil; } -- (void)location:(CLLocation *)location toGpsInfo:(location::GpsInfo &)info -{ - info.m_source = location::EAppleNative; - - info.m_latitude = location.coordinate.latitude; - info.m_longitude = location.coordinate.longitude; - info.m_timestamp = [location.timestamp timeIntervalSince1970]; - - if (location.horizontalAccuracy >= 0.0) - info.m_horizontalAccuracy = location.horizontalAccuracy; - - if (location.verticalAccuracy >= 0.0) - { - info.m_verticalAccuracy = location.verticalAccuracy; - info.m_altitude = location.altitude; - } - - if (location.course >= 0.0) - info.m_bearing = location.course; - - if (location.speed >= 0.0) - info.m_speed = location.speed; -} - -- (void)heading:(CLHeading *)heading toCompassInfo:(location::CompassInfo &)info -{ - if (heading.trueHeading >= 0.0) - info.m_bearing = my::DegToRad(heading.trueHeading); - else if (heading.headingAccuracy >= 0.0) - info.m_bearing = my::DegToRad(heading.magneticHeading); -} - - (void)triggerCompass { [self locationManager:m_locationManager didUpdateHeading:m_locationManager.heading]; @@ -193,9 +163,7 @@ static NSString * const kAlohalyticsLocationRequestAlwaysFailed = @"$locationAlw - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading { - location::CompassInfo info; - [self heading:heading toCompassInfo:info]; - [self notifyCompassUpdate:info]; + [self notifyCompassUpdate:compasInfoFromHeading(heading)]; } - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations @@ -205,22 +173,27 @@ static NSString * const kAlohalyticsLocationRequestAlwaysFailed = @"$locationAlw [m_locationManager allowDeferredLocationUpdatesUntilTraveled:300 timeout:15]; } -- (void)processLocation:(CLLocation *)newLocation +- (void)processLocation:(CLLocation *)location { // According to documentation, lat and lon are valid only if horz acc is non-negative. // So we filter out such events completely. - if (newLocation.horizontalAccuracy < 0.) + if (location.horizontalAccuracy < 0.) return; // Save current device time for location. m_lastLocationTime = [NSDate date]; - - location::GpsInfo newInfo; - [self location:newLocation toGpsInfo:newInfo]; - for (id observer in m_observers) - [observer onLocationUpdate:newInfo]; - // TODO(AlexZ): Temporary, remove in the future. - [[Statistics instance] logLocation:newLocation]; + [[Statistics instance] logLocation:location]; + auto const newInfo = gpsInfoFromLocation(location); + if (self.isDaemonMode) + { + GetDefaultGpsTrack().AddPoint(newInfo); + } + else + { + for (id observer in m_observers) + [observer onLocationUpdate:newInfo]; + // TODO(AlexZ): Temporary, remove in the future. + } } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error @@ -376,6 +349,42 @@ static NSString * const kAlohalyticsLocationRequestAlwaysFailed = @"$locationAlw [observer onLocationError:errorCode]; } +location::GpsInfo gpsInfoFromLocation(CLLocation const * l) +{ + location::GpsInfo info; + info.m_source = location::EAppleNative; + + info.m_latitude = l.coordinate.latitude; + info.m_longitude = l.coordinate.longitude; + info.m_timestamp = [l.timestamp timeIntervalSince1970]; + + if (l.horizontalAccuracy >= 0.0) + info.m_horizontalAccuracy = l.horizontalAccuracy; + + if (l.verticalAccuracy >= 0.0) + { + info.m_verticalAccuracy = l.verticalAccuracy; + info.m_altitude = l.altitude; + } + + if (l.course >= 0.0) + info.m_bearing = l.course; + + if (l.speed >= 0.0) + info.m_speed = l.speed; + return info; +} + +location::CompassInfo compasInfoFromHeading(CLHeading const * h) +{ + location::CompassInfo info; + if (h.trueHeading >= 0.0) + info.m_bearing = my::DegToRad(h.trueHeading); + else if (h.headingAccuracy >= 0.0) + info.m_bearing = my::DegToRad(h.magneticHeading); + return info; +} + @end @implementation CLLocation (Mercator)