Browse Source

Sun time updates!

v1.2.1
Abhishek Banthia 9 years ago
parent
commit
af2fba1556
  1. BIN
      Clocker.xcodeproj/project.xcworkspace/xcuserdata/abhishekbanthia.xcuserdatad/UserInterfaceState.xcuserstate
  2. 4
      Clocker/Custom Table Cell Views/CLTimezoneCellView.m
  3. 2
      Clocker/PanelController.h
  4. 107
      Clocker/PanelController.m
  5. 96
      Clocker/Preferences/CLPreferencesViewController.m

BIN
Clocker.xcodeproj/project.xcworkspace/xcuserdata/abhishekbanthia.xcuserdatad/UserInterfaceState.xcuserstate generated

Binary file not shown.

4
Clocker/Custom Table Cell Views/CLTimezoneCellView.m

@ -38,8 +38,8 @@
if ([[sender superview] isKindOfClass:[self class]]) {
CLTimezoneCellView *cellView = (CLTimezoneCellView *)[sender superview];
NSDictionary *timezoneDictionary = panelController.defaultPreferences[cellView.rowNumber];
NSDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy];
NSMutableDictionary *timezoneDictionary = panelController.defaultPreferences[cellView.rowNumber];
NSMutableDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy];
(customLabelValue.length > 0) ? [mutableTimeZoneDict setValue:customLabelValue forKey:CLCustomLabel] : [mutableTimeZoneDict setValue:CLEmptyString forKey:CLCustomLabel] ;
[panelController.defaultPreferences replaceObjectAtIndex:cellView.rowNumber withObject:mutableTimeZoneDict];

2
Clocker/PanelController.h

@ -53,7 +53,7 @@
@property (nonatomic, strong) CLOneWindowController *oneWindow;
@property (nonatomic, strong) NSMutableArray *defaultPreferences;
@property (nonatomic, strong) NSMutableArray<NSMutableDictionary*> *defaultPreferences;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, assign) NSInteger futureSliderValue;
@property (nonatomic) BOOL hasActivePanel;

107
Clocker/PanelController.m

@ -294,7 +294,9 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
-(NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if (self.showReviewCell && row == self.defaultPreferences.count) {
CLRatingCellView *cellView = [self.mainTableview makeViewWithIdentifier:CLRatingCellViewIdentifier owner:self];
CLRatingCellView *cellView = [self.mainTableview
makeViewWithIdentifier:CLRatingCellViewIdentifier
owner:self];
return cellView;
}
@ -317,7 +319,7 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
self.window.alphaValue = 1;
}
cell.relativeDate.stringValue = [self getDateForTimeZone:self.defaultPreferences[row][CLTimezoneID]];
cell.relativeDate.stringValue = [self getDateForTimeZone:self.defaultPreferences[row]];
cell.time.stringValue = [self getTimeForTimeZone:self.defaultPreferences[row][CLTimezoneID]];
@ -326,11 +328,10 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
cell.customName.stringValue = [self formatStringShouldContainCity:YES
withTimezoneName:self.defaultPreferences[row]];
NSNumber *displaySuntimings = [[NSUserDefaults standardUserDefaults] objectForKey:CLDisplaySunTimingKey];
if ([displaySuntimings isEqualToNumber:[NSNumber numberWithInteger:0]]) {
cell.sunTime.stringValue = [self getFormattedSunriseOrSunsetTime:self.defaultPreferences[row] andSunImage:cell];
if ([displaySuntimings isEqualToNumber:[NSNumber numberWithInteger:0]] && self.defaultPreferences[row][@"sunriseTime"] && self.defaultPreferences[row][@"sunsetTime"]) {
cell.sunTime.stringValue = [self getFormattedSunriseOrSunsetTime:self.
defaultPreferences[row] andSunImage:cell];
}
else
{
@ -340,7 +341,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
[cell setUpAutoLayoutWithCell:cell];
return cell;
}
@ -348,11 +348,12 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
#pragma mark Datasource formatting
#pragma mark -
- (NSString *)formatStringShouldContainCity:(BOOL)value withTimezoneName:(NSDictionary *)timeZoneDictionary
- (NSString *)formatStringShouldContainCity:(BOOL)value withTimezoneName:(NSMutableDictionary *)timeZoneDictionary
{
if (timeZoneDictionary[CLCustomLabel]) {
NSString *customLabel = timeZoneDictionary[CLCustomLabel];
if (customLabel.length > 0) {
if (customLabel.length > 0)
{
return customLabel;
}
}
@ -379,7 +380,7 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
}
- (NSString *)getFormattedSunriseOrSunsetTime:(NSDictionary *)originalTime andSunImage:(CLTimezoneCellView *)cell
- (NSString *)getFormattedSunriseOrSunsetTime:(NSMutableDictionary *)originalTime andSunImage:(CLTimezoneCellView *)cell
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm";
@ -392,7 +393,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
NSString *newDate = [dateFormatter stringFromDate:[NSDate date]];
NSDateFormatter *dateConversion = [[NSDateFormatter alloc] init];
dateConversion.timeZone = [NSTimeZone timeZoneWithName:originalTime[CLTimezoneID]];
dateConversion.dateStyle = kCFDateFormatterShortStyle;
@ -401,7 +401,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
NSString *theme = [[NSUserDefaults standardUserDefaults] objectForKey:CLThemeKey];
if ([sunTime laterDate:[dateConversion dateFromString:newDate]] == sunTime)
{
cell.sunImage.image = theme.length > 0 && [theme isEqualToString:@"Default"] ?
@ -449,9 +448,8 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
}
- (NSString *)compareSystemDate:(NSString *)systemDate toTimezoneDate:(NSString *)date
- (NSString *)compareSystemDate:(NSString *)systemDate toTimezoneDate:(NSString *)date andDictionary:(NSMutableDictionary *)dictionary
{
NSLog(@"Timezone date:%@ and System Date:%@", date, systemDate);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy"
options:0
@ -470,6 +468,32 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSInteger weekday = [calendar component:NSCalendarUnitWeekday fromDate:localDate];
NSInteger index = [self.defaultPreferences indexOfObject:dictionary];
if ([dictionary[@"nextUpdate"] isKindOfClass:[NSString class]])
{
NSUInteger units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:units fromDate:timezoneDate];
comps.day = comps.day + 1;
NSDate *tomorrowMidnight = [[NSCalendar currentCalendar] dateFromComponents:comps];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] initWithDictionary:dictionary copyItems:YES];
[newDict setObject:tomorrowMidnight forKey:@"nextUpdate"];
index = [self.defaultPreferences indexOfObject:dictionary];
[self.defaultPreferences replaceObjectAtIndex:[self.defaultPreferences indexOfObject:dictionary] withObject:newDict];
[[NSUserDefaults standardUserDefaults] setObject:self.defaultPreferences forKey:CLDefaultPreferenceKey];
}
else if ([dictionary[@"nextUpdate"] isKindOfClass:[NSDate class]] &&
[dictionary[@"nextUpdate"] isEarlierThanOrEqualTo:timezoneDate])
{
[self getTimeZoneForLatitude:dictionary[@"latitude"]
andLongitude:dictionary[@"longitude"]
andDictionary:dictionary];
}
NSInteger daysApart = [timezoneDate daysFrom:localDate];
if (daysApart == 0) {
@ -489,7 +513,7 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
}
}
- (NSString *)getDateForTimeZone:(NSString *)timezoneID
- (NSString *)getDateForTimeZone:(NSMutableDictionary *)dictionary
{
NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *newDate = [currentCalendar dateByAddingUnit:NSCalendarUnitHour
@ -500,11 +524,12 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
dateFormatter.timeStyle = kCFDateFormatterNoStyle;
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:timezoneID];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:dictionary[CLTimezoneID]];
NSNumber *relativeDayPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLRelativeDateKey];
if (relativeDayPreference.integerValue == 0) {
return [self compareSystemDate:[self getLocalCurrentDate] toTimezoneDate:[dateFormatter stringFromDate:newDate]];;
return [self compareSystemDate:[self getLocalCurrentDate]
toTimezoneDate:[dateFormatter stringFromDate:newDate] andDictionary:dictionary] ;;
}
else
{
@ -536,8 +561,8 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
{
if ([object isKindOfClass:[NSString class]])
{
NSDictionary *timezoneDictionary = self.defaultPreferences[row];
NSDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy];
NSMutableDictionary *timezoneDictionary = self.defaultPreferences[row];
NSMutableDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy];
[mutableTimeZoneDict setValue:object forKey:CLCustomLabel];
[self.defaultPreferences replaceObjectAtIndex:row withObject:mutableTimeZoneDict];
[[NSUserDefaults standardUserDefaults] setObject:self.defaultPreferences forKey:CLDefaultPreferenceKey];
@ -607,8 +632,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
toDate:[NSDate date]
options:kNilOptions];
self.dateFormatter.dateStyle = kCFDateFormatterNoStyle;
self.dateFormatter.timeStyle = kCFDateFormatterShortStyle;
@ -694,7 +717,7 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
}
}
- (void)getTimeZoneForLatitude:(NSString *)latitude andLongitude:(NSString *)longitude
- (void)getTimeZoneForLatitude:(NSString *)latitude andLongitude:(NSString *)longitude andDictionary:(NSMutableDictionary *)dictionary
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
@ -725,7 +748,8 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (httpResp.statusCode == 200) {
if (httpResp.statusCode == 200)
{
dispatch_async(dispatch_get_main_queue(), ^{
@ -739,22 +763,25 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
return;
}
if ([json[@"status"][@"message"] isEqualToString:@"the hourly limit of 2000 credits for abhishaker17 has been exceeded. Please throttle your requests or use the commercial service."])
{
return;
}
// NSString *filteredAddress = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName];
// NSRange range = [filteredAddress rangeOfString:@","];
// if (range.location != NSNotFound)
// {
// filteredAddress = [[self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName ] substringWithRange:NSMakeRange(0, range.location)];
// }
// NSDictionary *newTimezone = @{CLTimezoneID: json[@"timezoneId"],
// @"sunriseTime" : json[@"sunrise"],
// @"sunsetTime": json[@"sunset"],
// CLCustomLabel : @"",
// CLTimezoneName : filteredAddress,
// CLPlaceIdentifier : self.filteredArray[self.availableTimezoneTableView.selectedRow][CLPlaceIdentifier],
// @"latitude" : latitude,
// @"longitude" : longitude};
NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithDictionary:dictionary copyItems:YES];
[newDictionary setObject:json[@"sunrise"] forKey:@"sunriseTime"];
[newDictionary setObject:json[@"sunset"] forKey:@"sunsetTime"];
NSUInteger units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSDateComponents *comps = [[NSCalendar currentCalendar] components:units fromDate:newDictionary[@"nextUpdate"]];
comps.day = comps.day + 1;
NSDate *tomorrowMidnight = [[NSCalendar currentCalendar] dateFromComponents:comps];
[newDictionary setObject:tomorrowMidnight forKey:@"nextUpdate"];
NSArray *defaultPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLDefaultPreferenceKey];
@ -764,10 +791,12 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
}
NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:defaultPreference];
// [newArray addObject:newTimezone];
[newArray replaceObjectAtIndex:[self.defaultPreferences indexOfObject:dictionary] withObject:newDictionary];
[[NSUserDefaults standardUserDefaults] setObject:newArray forKey:CLDefaultPreferenceKey];
[self.mainTableview reloadData];
});
}
}
@ -783,6 +812,4 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
}
}
@end

96
Clocker/Preferences/CLPreferencesViewController.m

@ -93,7 +93,8 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
if (tableView == self.timezoneTableView) {
if (tableView == self.timezoneTableView)
{
return self.selectedTimeZones.count;
}
else
@ -140,8 +141,8 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
NSString *customLabelValue = [originalValue stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
NSDictionary *timezoneDictionary = self.selectedTimeZones[row];
NSDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy];
NSMutableDictionary *timezoneDictionary = self.selectedTimeZones[row];
NSMutableDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy];
customLabelValue.length > 0 ? [mutableTimeZoneDict setValue:customLabelValue forKey:CLCustomLabel] : [mutableTimeZoneDict setValue:CLEmptyString forKey:CLCustomLabel];
[self.selectedTimeZones replaceObjectAtIndex:row withObject:mutableTimeZoneDict];
[[NSUserDefaults standardUserDefaults] setObject:self.selectedTimeZones forKey:CLDefaultPreferenceKey];
@ -152,8 +153,7 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
- (IBAction)addTimeZone:(id)sender
{
[self.view.window beginSheet:self.timezonePanel completionHandler:^(NSModalResponse returnCode) {
}];
[self.view.window beginSheet:self.timezonePanel completionHandler:nil];
}
- (IBAction)addToFavorites:(id)sender
@ -163,7 +163,10 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
if (self.availableTimezoneTableView.selectedRow == -1)
{
self.messageLabel.stringValue = CLNoTimezoneSelectedErrorMessage;
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(clearLabel) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:5 target:self
selector:@selector(clearLabel)
userInfo:nil
repeats:NO];
self.activityInProgress = NO;
return;
}
@ -173,12 +176,15 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
if (self.selectedTimeZones.count >= 10)
{
self.messageLabel.stringValue = CLMaxTimezonesErrorMessage;
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(clearLabel) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:5 target:self
selector:@selector(clearLabel)
userInfo:nil
repeats:NO];
self.activityInProgress = NO;
return;
}
for (NSDictionary *timezoneDictionary in self.selectedTimeZones)
for (NSMutableDictionary *timezoneDictionary in self.selectedTimeZones)
{
NSString *name = timezoneDictionary[CLPlaceIdentifier];
NSString *selectedPlaceID = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLPlaceIdentifier];
@ -188,7 +194,10 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
[name isEqualToString:selectedPlaceID])
{
self.messageLabel.stringValue = CLTimezoneAlreadySelectedError;
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(clearLabel) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:@selector(clearLabel) userInfo:nil
repeats:NO];
self.activityInProgress = NO;
return;
}
@ -206,15 +215,15 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
feedbackObject[CLParseTimezoneNameProperty] = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName];
[feedbackObject saveEventually];
}
- (IBAction)closePanel:(id)sender {
- (IBAction)closePanel:(id)sender
{
self.filteredArray = [NSMutableArray array];
self.placeholderLabel.placeholderString = CLEmptyString;
[self.availableTimezoneTableView reloadData];
self.searchField.stringValue = CLEmptyString;
self.searchField.placeholderString = @"Enter a city, state or country name";
[self.timezonePanel close];
self.activityInProgress = NO;
}
@ -250,22 +259,6 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
[self refreshMainTableview];
}
-(void)keyDown:(NSEvent *)theEvent
{
[super keyDown:theEvent];
if (theEvent.keyCode == 53) {
[self.timezonePanel close];
}
}
-(void)keyUp:(NSEvent *)theEvent
{
if (theEvent.keyCode == 53) {
[self.timezonePanel close];
}
}
- (IBAction)filterArray:(id)sender
{
[self clearLabel];
@ -375,6 +368,11 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
[self.dataTask cancel];
}
if (self.availableTimezoneTableView.isHidden)
{
self.availableTimezoneTableView.hidden = NO;
}
self.placeholderLabel.hidden = NO;
Reachability *reachability = [Reachability reachabilityForInternetConnection];
@ -424,9 +422,8 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
JSONObjectWithData:data
options:kNilOptions
error:nil];
if ([json[@"status"] isEqualToString:@"ZERO_RESULTS"]) {
self.placeholderLabel.placeholderString = @"No results found! 😔";
self.placeholderLabel.placeholderString = @"No results! 😔 Try entering the exact name.";
self.activityInProgress = NO;
return;
}
@ -523,11 +520,21 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
options:kNilOptions
error:nil];
if (json.count == 0) {
self.activityInProgress = NO;
self.placeholderLabel.placeholderString = @"No results found! ! 😔 Try Again?";
return;
}
if ([json[@"status"][@"message"] isEqualToString:@"the hourly limit of 2000 credits for abhishaker17 has been exceeded. Please throttle your requests or use the commercial service."])
{
self.activityInProgress = NO;
self.placeholderLabel.placeholderString = @"API limit reached. Try again in an hour.?";
self.searchField.placeholderString = @"We rely on free APIs which have limits.";
return;
}
NSString *filteredAddress = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName];
NSRange range = [filteredAddress rangeOfString:@","];
@ -536,14 +543,23 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
filteredAddress = [[self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName ] substringWithRange:NSMakeRange(0, range.location)];
}
NSDictionary *newTimezone = @{CLTimezoneID: json[@"timezoneId"],
@"sunriseTime" : json[@"sunrise"],
@"sunsetTime": json[@"sunset"],
CLCustomLabel : @"",
CLTimezoneName : filteredAddress,
CLPlaceIdentifier : self.filteredArray[self.availableTimezoneTableView.selectedRow][CLPlaceIdentifier],
@"latitude" : latitude,
@"longitude" : longitude};
NSMutableDictionary *newTimezone = [NSMutableDictionary dictionary];
if (json[@"sunrise"]) {
[newTimezone setObject:json[@"sunrise"] forKey:@"sunriseTime"];
}
if (json[@"sunset"]) {
[newTimezone setObject:json[@"sunset"] forKey:@"sunsetTime"];
}
[newTimezone setObject:json[@"timezoneId"] forKey:CLTimezoneID];
[newTimezone setObject:filteredAddress forKey:CLTimezoneName];
[newTimezone setObject:self.filteredArray[self.availableTimezoneTableView.selectedRow][CLPlaceIdentifier] forKey:CLPlaceIdentifier];
[newTimezone setObject:latitude forKey:@"latitude"];
[newTimezone setObject:longitude forKey:@"longitude"];
[newTimezone setObject:CLEmptyString forKey:@"nextUpdate"];
[newTimezone setObject:CLEmptyString forKey:CLCustomLabel];
NSArray *defaultPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLDefaultPreferenceKey];
@ -567,12 +583,12 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
[self.timezonePanel close];
self.availableTimezoneTableView.hidden = NO;
self.placeholderLabel.placeholderString = CLEmptyString;
self.searchField.placeholderString = @"Enter a city, state or country name";
self.availableTimezoneTableView.hidden = NO;
self.activityInProgress = NO;
});

Loading…
Cancel
Save