Skip to content

Commit

Permalink
Merge pull request #3520 from osmandapp/Sun-position-widget-shows-dat…
Browse files Browse the repository at this point in the history
…a-only-for-current-location

Show time for current map center
  • Loading branch information
alex-osm authored Mar 28, 2024
2 parents 639e6fe + 2743c8b commit 23f376f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
65 changes: 59 additions & 6 deletions Sources/Controllers/Map/Widgets/OASunriseSunsetWidget.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@
#import "Localization.h"
#import "OsmAndApp.h"
#import "OsmAnd_Maps-Swift.h"
#import "OAResourcesUIHelper.h"

static const NSTimeInterval updateInterval = 60;
static const double locationChangeAccuracy = 0.0001;

@implementation OASunriseSunsetWidget
{
OsmAndAppInstance _app;
OAAppSettings *_settings;
OASunriseSunsetWidgetState *_state;
NSArray<NSString *> *_items;
CLLocationCoordinate2D _cachedCenterLatLon;
NSTimeInterval _timeToNextUpdate;
NSTimeInterval _cachedNextTime;
NSTimeInterval _lastUpdateTime;
BOOL _isForceUpdate;
BOOL _isLocationChanged;
}

- (instancetype)initWithState:(OASunriseSunsetWidgetState *)state
Expand All @@ -46,12 +56,17 @@ - (instancetype)initWithState:(OASunriseSunsetWidgetState *)state

[self setText:@"-" subtext:@""];
[self setIcon:[_state getWidgetIconName]];
_isForceUpdate = YES;
}
return self;
}

- (BOOL) updateInfo
{
[self updateCachedLocation];
if (![self isUpdateNeeded])
return NO;

if ([self isShowTimeLeft])
{
NSTimeInterval leftTime = [self getTimeLeft];
Expand All @@ -75,12 +90,34 @@ - (BOOL) updateInfo
{
[self setIcon:[_state getWidgetIconName]];
}


_isForceUpdate = NO;
_isLocationChanged = NO;
_lastUpdateTime = [[NSDate date] timeIntervalSince1970];
NSTimeInterval timeDifference = _cachedNextTime - _lastUpdateTime;
if (timeDifference > updateInterval)
_timeToNextUpdate = timeDifference - floor(timeDifference / updateInterval) * updateInterval;
else
_timeToNextUpdate = timeDifference;

return YES;
}

- (BOOL)isUpdateNeeded
{
if (_isForceUpdate || _isLocationChanged)
return YES;

NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];
if ([self isShowTimeLeft])
return (_lastUpdateTime + _timeToNextUpdate) <= currentTime;
else
return _cachedNextTime <= currentTime;
}

- (void) onWidgetClicked
{
_isForceUpdate = YES;
if ([self isShowTimeLeft])
[[self getPreference] set:EOASunriseSunsetNext];
else
Expand Down Expand Up @@ -319,20 +356,21 @@ - (NSTimeInterval)getNextTime
{
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;

NSCalendar *theCalendar = [NSCalendar currentCalendar];
nextTimeDate = [theCalendar dateByAddingComponents:dayComponent toDate:nextTimeDate options:0];
}
return nextTimeDate.timeIntervalSince1970;
_cachedNextTime = nextTimeDate.timeIntervalSince1970;
return _cachedNextTime;
}

return 0;
}

- (SunriseSunset *) createSunriseSunset:(NSDate *)date
{
CLLocation *location = OsmAndApp.instance.locationServices.lastKnownLocation;
double longitude = location.coordinate.longitude;
SunriseSunset *sunriseSunset = [[SunriseSunset alloc] initWithLatitude:location.coordinate.latitude longitude:longitude < 0 ? 360 + longitude : longitude dateInputIn:date tzIn:[NSTimeZone localTimeZone]];
double longitude = _cachedCenterLatLon.longitude;
SunriseSunset *sunriseSunset = [[SunriseSunset alloc] initWithLatitude:_cachedCenterLatLon.latitude longitude:longitude < 0 ? 360 + longitude : longitude dateInputIn:date tzIn:[NSTimeZone localTimeZone]];
return sunriseSunset;
}

Expand Down Expand Up @@ -369,4 +407,19 @@ - (NSString *) formatNextTime:(NSTimeInterval)nextTime
return [dateFormatter stringFromDate:nextDate];
}

- (void) updateCachedLocation
{
CLLocationCoordinate2D newCenterLatLon = [OAResourcesUIHelper getMapLocation];
if (![self isLocationsEqual:_cachedCenterLatLon with:newCenterLatLon])
{
_cachedCenterLatLon = newCenterLatLon;
_isLocationChanged = YES;
}
}

- (BOOL) isLocationsEqual:(CLLocationCoordinate2D)firstCoordinate with:(CLLocationCoordinate2D)secondCoordinate
{
return [OAMapUtils areLatLonEqual:firstCoordinate coordinate2:secondCoordinate precision:locationChangeAccuracy];
}

@end
1 change: 1 addition & 0 deletions Sources/Helpers/OAMapUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

+ (BOOL)areLocationEqual:(CLLocation *)l1 l2:(CLLocation *)l2;
+ (BOOL)areLocationEqual:(CLLocation *)l lat:(CGFloat)lat lon:(CGFloat)lon;
+ (BOOL)areLatLonEqual:(CLLocationCoordinate2D)l1 coordinate2:(CLLocationCoordinate2D)l2 precision:(double)precision;
+ (BOOL)areLatLonEqual:(CLLocationCoordinate2D)l1 l2:(CLLocationCoordinate2D)l2;
+ (BOOL)areLatLonEqual:(CLLocationCoordinate2D)l lat:(CGFloat)lat lon:(CGFloat)lon;
+ (BOOL)areLatLonEqual:(CGFloat)lat1 lon1:(CGFloat)lon1 lat2:(CGFloat)lat2 lon2:(CGFloat)lon2;
Expand Down
16 changes: 16 additions & 0 deletions Sources/Helpers/OAMapUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ + (BOOL)areLocationEqual:(CLLocation *)l lat:(CGFloat)lat lon:(CGFloat)lon
return l != nil && [self areLatLonEqual:l.coordinate.latitude lon1:l.coordinate.longitude lat2:lat lon2:lon];
}

+ (BOOL)areLatLonEqual:(CLLocationCoordinate2D)l1 coordinate2:(CLLocationCoordinate2D)l2 precision:(double)precision
{
if (!CLLocationCoordinate2DIsValid(l1) || !CLLocationCoordinate2DIsValid(l2))
return NO;

double lat1 = l1.latitude;
double lon1 = l1.longitude;
double lat2 = l2.latitude;
double lon2 = l2.longitude;

BOOL latEqual = (isnan(lat1) && isnan(lat2)) || fabs(lat1 - lat2) < precision;
BOOL lonEqual = (isnan(lon1) && isnan(lon2)) || fabs(lon1 - lon2) < precision;

return latEqual && lonEqual;
}

+ (BOOL)areLatLonEqual:(CLLocationCoordinate2D)l1 l2:(CLLocationCoordinate2D)l2
{
return (!CLLocationCoordinate2DIsValid(l1) && !CLLocationCoordinate2DIsValid(l2))
Expand Down

0 comments on commit 23f376f

Please sign in to comment.