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. 112
      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]]) { if ([[sender superview] isKindOfClass:[self class]]) {
CLTimezoneCellView *cellView = (CLTimezoneCellView *)[sender superview]; CLTimezoneCellView *cellView = (CLTimezoneCellView *)[sender superview];
NSDictionary *timezoneDictionary = panelController.defaultPreferences[cellView.rowNumber]; NSMutableDictionary *timezoneDictionary = panelController.defaultPreferences[cellView.rowNumber];
NSDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy]; NSMutableDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy];
(customLabelValue.length > 0) ? [mutableTimeZoneDict setValue:customLabelValue forKey:CLCustomLabel] : [mutableTimeZoneDict setValue:CLEmptyString forKey:CLCustomLabel] ; (customLabelValue.length > 0) ? [mutableTimeZoneDict setValue:customLabelValue forKey:CLCustomLabel] : [mutableTimeZoneDict setValue:CLEmptyString forKey:CLCustomLabel] ;
[panelController.defaultPreferences replaceObjectAtIndex:cellView.rowNumber withObject:mutableTimeZoneDict]; [panelController.defaultPreferences replaceObjectAtIndex:cellView.rowNumber withObject:mutableTimeZoneDict];

2
Clocker/PanelController.h

@ -53,7 +53,7 @@
@property (nonatomic, strong) CLOneWindowController *oneWindow; @property (nonatomic, strong) CLOneWindowController *oneWindow;
@property (nonatomic, strong) NSMutableArray *defaultPreferences; @property (nonatomic, strong) NSMutableArray<NSMutableDictionary*> *defaultPreferences;
@property (nonatomic, strong) NSDateFormatter *dateFormatter; @property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, assign) NSInteger futureSliderValue; @property (nonatomic, assign) NSInteger futureSliderValue;
@property (nonatomic) BOOL hasActivePanel; @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 -(NSView*)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{ {
if (self.showReviewCell && row == self.defaultPreferences.count) { 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; return cellView;
} }
@ -317,7 +319,7 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
self.window.alphaValue = 1; 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]]; cell.time.stringValue = [self getTimeForTimeZone:self.defaultPreferences[row][CLTimezoneID]];
@ -326,11 +328,10 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
cell.customName.stringValue = [self formatStringShouldContainCity:YES cell.customName.stringValue = [self formatStringShouldContainCity:YES
withTimezoneName:self.defaultPreferences[row]]; withTimezoneName:self.defaultPreferences[row]];
NSNumber *displaySuntimings = [[NSUserDefaults standardUserDefaults] objectForKey:CLDisplaySunTimingKey]; NSNumber *displaySuntimings = [[NSUserDefaults standardUserDefaults] objectForKey:CLDisplaySunTimingKey];
if ([displaySuntimings isEqualToNumber:[NSNumber numberWithInteger:0]]) { if ([displaySuntimings isEqualToNumber:[NSNumber numberWithInteger:0]] && self.defaultPreferences[row][@"sunriseTime"] && self.defaultPreferences[row][@"sunsetTime"]) {
cell.sunTime.stringValue = [self getFormattedSunriseOrSunsetTime:self.defaultPreferences[row] andSunImage:cell]; cell.sunTime.stringValue = [self getFormattedSunriseOrSunsetTime:self.
defaultPreferences[row] andSunImage:cell];
} }
else else
{ {
@ -340,7 +341,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
[cell setUpAutoLayoutWithCell:cell]; [cell setUpAutoLayoutWithCell:cell];
return cell; return cell;
} }
@ -348,11 +348,12 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
#pragma mark Datasource formatting #pragma mark Datasource formatting
#pragma mark - #pragma mark -
- (NSString *)formatStringShouldContainCity:(BOOL)value withTimezoneName:(NSDictionary *)timeZoneDictionary - (NSString *)formatStringShouldContainCity:(BOOL)value withTimezoneName:(NSMutableDictionary *)timeZoneDictionary
{ {
if (timeZoneDictionary[CLCustomLabel]) { if (timeZoneDictionary[CLCustomLabel]) {
NSString *customLabel = timeZoneDictionary[CLCustomLabel]; NSString *customLabel = timeZoneDictionary[CLCustomLabel];
if (customLabel.length > 0) { if (customLabel.length > 0)
{
return customLabel; 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]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm"; formatter.dateFormat = @"yyyy-MM-dd HH:mm";
@ -392,7 +393,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm"; dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm";
NSString *newDate = [dateFormatter stringFromDate:[NSDate date]]; NSString *newDate = [dateFormatter stringFromDate:[NSDate date]];
NSDateFormatter *dateConversion = [[NSDateFormatter alloc] init]; NSDateFormatter *dateConversion = [[NSDateFormatter alloc] init];
dateConversion.timeZone = [NSTimeZone timeZoneWithName:originalTime[CLTimezoneID]]; dateConversion.timeZone = [NSTimeZone timeZoneWithName:originalTime[CLTimezoneID]];
dateConversion.dateStyle = kCFDateFormatterShortStyle; dateConversion.dateStyle = kCFDateFormatterShortStyle;
@ -401,7 +401,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
NSString *theme = [[NSUserDefaults standardUserDefaults] objectForKey:CLThemeKey]; NSString *theme = [[NSUserDefaults standardUserDefaults] objectForKey:CLThemeKey];
if ([sunTime laterDate:[dateConversion dateFromString:newDate]] == sunTime) if ([sunTime laterDate:[dateConversion dateFromString:newDate]] == sunTime)
{ {
cell.sunImage.image = theme.length > 0 && [theme isEqualToString:@"Default"] ? 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]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy" formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy"
options:0 options:0
@ -470,6 +468,32 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSInteger weekday = [calendar component:NSCalendarUnitWeekday fromDate:localDate]; 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]; NSInteger daysApart = [timezoneDate daysFrom:localDate];
if (daysApart == 0) { if (daysApart == 0) {
@ -489,7 +513,7 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
} }
} }
- (NSString *)getDateForTimeZone:(NSString *)timezoneID - (NSString *)getDateForTimeZone:(NSMutableDictionary *)dictionary
{ {
NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar]; NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar];
NSDate *newDate = [currentCalendar dateByAddingUnit:NSCalendarUnitHour NSDate *newDate = [currentCalendar dateByAddingUnit:NSCalendarUnitHour
@ -500,11 +524,12 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
dateFormatter.dateStyle = kCFDateFormatterShortStyle; dateFormatter.dateStyle = kCFDateFormatterShortStyle;
dateFormatter.timeStyle = kCFDateFormatterNoStyle; dateFormatter.timeStyle = kCFDateFormatterNoStyle;
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:timezoneID]; dateFormatter.timeZone = [NSTimeZone timeZoneWithName:dictionary[CLTimezoneID]];
NSNumber *relativeDayPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLRelativeDateKey]; NSNumber *relativeDayPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLRelativeDateKey];
if (relativeDayPreference.integerValue == 0) { 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 else
{ {
@ -536,8 +561,8 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
{ {
if ([object isKindOfClass:[NSString class]]) if ([object isKindOfClass:[NSString class]])
{ {
NSDictionary *timezoneDictionary = self.defaultPreferences[row]; NSMutableDictionary *timezoneDictionary = self.defaultPreferences[row];
NSDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy]; NSMutableDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy];
[mutableTimeZoneDict setValue:object forKey:CLCustomLabel]; [mutableTimeZoneDict setValue:object forKey:CLCustomLabel];
[self.defaultPreferences replaceObjectAtIndex:row withObject:mutableTimeZoneDict]; [self.defaultPreferences replaceObjectAtIndex:row withObject:mutableTimeZoneDict];
[[NSUserDefaults standardUserDefaults] setObject:self.defaultPreferences forKey:CLDefaultPreferenceKey]; [[NSUserDefaults standardUserDefaults] setObject:self.defaultPreferences forKey:CLDefaultPreferenceKey];
@ -607,8 +632,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
toDate:[NSDate date] toDate:[NSDate date]
options:kNilOptions]; options:kNilOptions];
self.dateFormatter.dateStyle = kCFDateFormatterNoStyle; self.dateFormatter.dateStyle = kCFDateFormatterNoStyle;
self.dateFormatter.timeStyle = kCFDateFormatterShortStyle; 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]; Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus]; NetworkStatus networkStatus = [reachability currentReachabilityStatus];
@ -725,7 +748,8 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) { if (!error) {
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response; NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (httpResp.statusCode == 200) { if (httpResp.statusCode == 200)
{
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
@ -739,22 +763,25 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
return; 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"], NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithDictionary:dictionary copyItems:YES];
// @"sunriseTime" : json[@"sunrise"],
// @"sunsetTime": json[@"sunset"], [newDictionary setObject:json[@"sunrise"] forKey:@"sunriseTime"];
// CLCustomLabel : @"", [newDictionary setObject:json[@"sunset"] forKey:@"sunsetTime"];
// CLTimezoneName : filteredAddress,
// CLPlaceIdentifier : self.filteredArray[self.availableTimezoneTableView.selectedRow][CLPlaceIdentifier], NSUInteger units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
// @"latitude" : latitude, NSDateComponents *comps = [[NSCalendar currentCalendar] components:units fromDate:newDictionary[@"nextUpdate"]];
// @"longitude" : longitude}; comps.day = comps.day + 1;
NSDate *tomorrowMidnight = [[NSCalendar currentCalendar] dateFromComponents:comps];
[newDictionary setObject:tomorrowMidnight forKey:@"nextUpdate"];
NSArray *defaultPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLDefaultPreferenceKey]; NSArray *defaultPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLDefaultPreferenceKey];
@ -764,10 +791,12 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
} }
NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:defaultPreference]; NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:defaultPreference];
// [newArray addObject:newTimezone]; [newArray replaceObjectAtIndex:[self.defaultPreferences indexOfObject:dictionary] withObject:newDictionary];
[[NSUserDefaults standardUserDefaults] setObject:newArray forKey:CLDefaultPreferenceKey]; [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:CLDefaultPreferenceKey];
[self.mainTableview reloadData];
}); });
} }
} }
@ -783,6 +812,4 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell";
} }
} }
@end @end

112
Clocker/Preferences/CLPreferencesViewController.m

@ -93,7 +93,8 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView -(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{ {
if (tableView == self.timezoneTableView) { if (tableView == self.timezoneTableView)
{
return self.selectedTimeZones.count; return self.selectedTimeZones.count;
} }
else else
@ -140,8 +141,8 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
NSString *customLabelValue = [originalValue stringByTrimmingCharactersInSet: NSString *customLabelValue = [originalValue stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]]; [NSCharacterSet whitespaceCharacterSet]];
NSDictionary *timezoneDictionary = self.selectedTimeZones[row]; NSMutableDictionary *timezoneDictionary = self.selectedTimeZones[row];
NSDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy]; NSMutableDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy];
customLabelValue.length > 0 ? [mutableTimeZoneDict setValue:customLabelValue forKey:CLCustomLabel] : [mutableTimeZoneDict setValue:CLEmptyString forKey:CLCustomLabel]; customLabelValue.length > 0 ? [mutableTimeZoneDict setValue:customLabelValue forKey:CLCustomLabel] : [mutableTimeZoneDict setValue:CLEmptyString forKey:CLCustomLabel];
[self.selectedTimeZones replaceObjectAtIndex:row withObject:mutableTimeZoneDict]; [self.selectedTimeZones replaceObjectAtIndex:row withObject:mutableTimeZoneDict];
[[NSUserDefaults standardUserDefaults] setObject:self.selectedTimeZones forKey:CLDefaultPreferenceKey]; [[NSUserDefaults standardUserDefaults] setObject:self.selectedTimeZones forKey:CLDefaultPreferenceKey];
@ -152,8 +153,7 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
- (IBAction)addTimeZone:(id)sender - (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 - (IBAction)addToFavorites:(id)sender
@ -163,7 +163,10 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
if (self.availableTimezoneTableView.selectedRow == -1) if (self.availableTimezoneTableView.selectedRow == -1)
{ {
self.messageLabel.stringValue = CLNoTimezoneSelectedErrorMessage; 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; self.activityInProgress = NO;
return; return;
} }
@ -173,12 +176,15 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
if (self.selectedTimeZones.count >= 10) if (self.selectedTimeZones.count >= 10)
{ {
self.messageLabel.stringValue = CLMaxTimezonesErrorMessage; 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; self.activityInProgress = NO;
return; return;
} }
for (NSDictionary *timezoneDictionary in self.selectedTimeZones) for (NSMutableDictionary *timezoneDictionary in self.selectedTimeZones)
{ {
NSString *name = timezoneDictionary[CLPlaceIdentifier]; NSString *name = timezoneDictionary[CLPlaceIdentifier];
NSString *selectedPlaceID = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLPlaceIdentifier]; NSString *selectedPlaceID = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLPlaceIdentifier];
@ -188,7 +194,10 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
[name isEqualToString:selectedPlaceID]) [name isEqualToString:selectedPlaceID])
{ {
self.messageLabel.stringValue = CLTimezoneAlreadySelectedError; 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; self.activityInProgress = NO;
return; return;
} }
@ -205,18 +214,18 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
PFObject *feedbackObject = [PFObject objectWithClassName:CLParseTimezoneSelectionClassIdentifier]; PFObject *feedbackObject = [PFObject objectWithClassName:CLParseTimezoneSelectionClassIdentifier];
feedbackObject[CLParseTimezoneNameProperty] = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName]; feedbackObject[CLParseTimezoneNameProperty] = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName];
[feedbackObject saveEventually]; [feedbackObject saveEventually];
} }
- (IBAction)closePanel:(id)sender { - (IBAction)closePanel:(id)sender
{
self.filteredArray = [NSMutableArray array]; self.filteredArray = [NSMutableArray array];
self.placeholderLabel.placeholderString = CLEmptyString; self.placeholderLabel.placeholderString = CLEmptyString;
[self.availableTimezoneTableView reloadData]; [self.availableTimezoneTableView reloadData];
self.searchField.stringValue = CLEmptyString; self.searchField.stringValue = CLEmptyString;
[self.timezonePanel close]; self.searchField.placeholderString = @"Enter a city, state or country name";
self.activityInProgress = NO; [self.timezonePanel close];
self.activityInProgress = NO;
} }
- (void)clearLabel - (void)clearLabel
@ -250,22 +259,6 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
[self refreshMainTableview]; [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 - (IBAction)filterArray:(id)sender
{ {
[self clearLabel]; [self clearLabel];
@ -375,7 +368,12 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
[self.dataTask cancel]; [self.dataTask cancel];
} }
self.placeholderLabel.hidden = NO; if (self.availableTimezoneTableView.isHidden)
{
self.availableTimezoneTableView.hidden = NO;
}
self.placeholderLabel.hidden = NO;
Reachability *reachability = [Reachability reachabilityForInternetConnection]; Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus]; NetworkStatus networkStatus = [reachability currentReachabilityStatus];
@ -424,9 +422,8 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
JSONObjectWithData:data JSONObjectWithData:data
options:kNilOptions options:kNilOptions
error:nil]; error:nil];
if ([json[@"status"] isEqualToString:@"ZERO_RESULTS"]) { 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; self.activityInProgress = NO;
return; return;
} }
@ -448,7 +445,7 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
} }
self.activityInProgress = NO; self.activityInProgress = NO;
[self.availableTimezoneTableView reloadData]; [self.availableTimezoneTableView reloadData];
}); });
@ -523,11 +520,21 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
options:kNilOptions options:kNilOptions
error:nil]; error:nil];
if (json.count == 0) { if (json.count == 0) {
self.activityInProgress = NO;
self.placeholderLabel.placeholderString = @"No results found! ! 😔 Try Again?"; self.placeholderLabel.placeholderString = @"No results found! ! 😔 Try Again?";
return; 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]; NSString *filteredAddress = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName];
NSRange range = [filteredAddress rangeOfString:@","]; 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)]; filteredAddress = [[self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName ] substringWithRange:NSMakeRange(0, range.location)];
} }
NSDictionary *newTimezone = @{CLTimezoneID: json[@"timezoneId"], NSMutableDictionary *newTimezone = [NSMutableDictionary dictionary];
@"sunriseTime" : json[@"sunrise"], if (json[@"sunrise"]) {
@"sunsetTime": json[@"sunset"], [newTimezone setObject:json[@"sunrise"] forKey:@"sunriseTime"];
CLCustomLabel : @"", }
CLTimezoneName : filteredAddress, if (json[@"sunset"]) {
CLPlaceIdentifier : self.filteredArray[self.availableTimezoneTableView.selectedRow][CLPlaceIdentifier], [newTimezone setObject:json[@"sunset"] forKey:@"sunsetTime"];
@"latitude" : latitude, }
@"longitude" : longitude};
[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]; NSArray *defaultPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLDefaultPreferenceKey];
@ -567,12 +583,12 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?";
[self.timezonePanel close]; [self.timezonePanel close];
self.availableTimezoneTableView.hidden = NO;
self.placeholderLabel.placeholderString = CLEmptyString; self.placeholderLabel.placeholderString = CLEmptyString;
self.searchField.placeholderString = @"Enter a city, state or country name"; self.searchField.placeholderString = @"Enter a city, state or country name";
self.availableTimezoneTableView.hidden = NO;
self.activityInProgress = NO; self.activityInProgress = NO;
}); });

Loading…
Cancel
Save