diff --git a/Clocker.xcodeproj/project.xcworkspace/xcuserdata/abhishekbanthia.xcuserdatad/UserInterfaceState.xcuserstate b/Clocker.xcodeproj/project.xcworkspace/xcuserdata/abhishekbanthia.xcuserdatad/UserInterfaceState.xcuserstate index 94e38a2..5cbc018 100644 Binary files a/Clocker.xcodeproj/project.xcworkspace/xcuserdata/abhishekbanthia.xcuserdatad/UserInterfaceState.xcuserstate and b/Clocker.xcodeproj/project.xcworkspace/xcuserdata/abhishekbanthia.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Clocker/App Feedback/CLAppFeedbackWindow.xib b/Clocker/App Feedback/CLAppFeedbackWindow.xib index 5fed062..d3c5f0c 100644 --- a/Clocker/App Feedback/CLAppFeedbackWindow.xib +++ b/Clocker/App Feedback/CLAppFeedbackWindow.xib @@ -1,6 +1,7 @@ + diff --git a/Clocker/ApplicationDelegate.m b/Clocker/ApplicationDelegate.m index 41fd6fa..c9e0968 100755 --- a/Clocker/ApplicationDelegate.m +++ b/Clocker/ApplicationDelegate.m @@ -77,16 +77,10 @@ void *kContextActivePanel = &kContextActivePanel; - (void)applicationDidFinishLaunching:(NSNotification *)notification { - NSNumber *opened = [[NSUserDefaults standardUserDefaults] objectForKey:@"noOfTimes"]; - if (opened == nil) - { - [[NSUserDefaults standardUserDefaults] setObject:[NSMutableArray array] - forKey:CLDefaultPreferenceKey]; - NSInteger noOfTimes = opened.integerValue + 1; - NSNumber *noOfTime = [NSNumber numberWithInteger:noOfTimes]; - [[NSUserDefaults standardUserDefaults] setObject:noOfTime forKey:@"noOfTimes"];; - - } + + [[NSUserDefaults standardUserDefaults] setObject:[NSMutableArray array] + forKey:CLDefaultPreferenceKey]; + NSString *defaultTheme = [[NSUserDefaults standardUserDefaults] objectForKey:CLThemeKey]; if (defaultTheme == nil) { diff --git a/Clocker/Custom Table Cell Views/CLTimezoneCellView.m b/Clocker/Custom Table Cell Views/CLTimezoneCellView.m index 405cd0a..10f3dbc 100644 --- a/Clocker/Custom Table Cell Views/CLTimezoneCellView.m +++ b/Clocker/Custom Table Cell Views/CLTimezoneCellView.m @@ -9,6 +9,7 @@ #import "CLTimezoneCellView.h" #import "PanelController.h" #import "CommonStrings.h" +#import "CLTimezoneData.h" @implementation CLTimezoneCellView @@ -38,16 +39,16 @@ if ([[sender superview] isKindOfClass:[self class]]) { CLTimezoneCellView *cellView = (CLTimezoneCellView *)[sender superview]; - NSMutableDictionary *timezoneDictionary = panelController.defaultPreferences[cellView.rowNumber]; - NSMutableDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy]; + CLTimezoneData *dataObject = panelController.defaultPreferences[cellView.rowNumber]; + CLTimezoneData *newDataObject = [dataObject mutableCopy]; - for (NSDictionary *dictionary in panelController.defaultPreferences) { - if ([dictionary[CLTimezoneName] isEqualToString:customLabelValue]) { + for (CLTimezoneData *object in panelController.defaultPreferences) { + if ([object.formattedAddress isEqualToString:customLabelValue]) { return; } } - (customLabelValue.length > 0) ? [mutableTimeZoneDict setValue:customLabelValue forKey:CLCustomLabel] : [mutableTimeZoneDict setValue:CLEmptyString forKey:CLCustomLabel] ; - [panelController.defaultPreferences replaceObjectAtIndex:cellView.rowNumber withObject:mutableTimeZoneDict]; + newDataObject.customLabel = (customLabelValue.length > 0) ? customLabelValue : CLEmptyString; + [panelController.defaultPreferences replaceObjectAtIndex:cellView.rowNumber withObject:newDataObject]; [[NSUserDefaults standardUserDefaults] setObject:panelController.defaultPreferences forKey:CLDefaultPreferenceKey]; [panelController updateDefaultPreferences]; diff --git a/Clocker/Model/CLTimezoneData.h b/Clocker/Model/CLTimezoneData.h index ba2d786..cec6ff3 100644 --- a/Clocker/Model/CLTimezoneData.h +++ b/Clocker/Model/CLTimezoneData.h @@ -18,7 +18,7 @@ @property (strong, nonatomic) NSString *timezoneID; @property (strong, nonatomic) NSString *latitude; @property (strong, nonatomic) NSString *longitude; - +@property (strong, nonatomic) NSDate *nextUpdate; -(instancetype)initWithDictionary:(NSDictionary *)dictionary; - (BOOL)saveObjectToPreferences:(CLTimezoneData *)object; diff --git a/Clocker/Model/CLTimezoneData.m b/Clocker/Model/CLTimezoneData.m index f7becf5..0478795 100644 --- a/Clocker/Model/CLTimezoneData.m +++ b/Clocker/Model/CLTimezoneData.m @@ -8,6 +8,10 @@ #import "CLTimezoneData.h" #import "CommonStrings.h" +#import "DateTools.h" +#import "CLAPI.h" +#import "CLTimezoneCellView.h" +#import "PanelController.h" @implementation CLTimezoneData @@ -59,7 +63,7 @@ [coder encodeObject:self.sunriseTime forKey:@"sunriseTime"]; [coder encodeObject:self.sunsetTime forKey:@"sunsetTime"]; [coder encodeObject:self.timezoneID forKey:@"timezoneID"]; - + [coder encodeObject:self.nextUpdate forKey:@"nextUpdate"]; } @@ -73,6 +77,7 @@ self.sunsetTime = [coder decodeObjectForKey:@"sunsetTime"]; self.sunriseTime = [coder decodeObjectForKey:@"sunriseTime"]; self.timezoneID = [coder decodeObjectForKey:@"timezoneID"]; + self.nextUpdate = [coder decodeObjectForKey:@"nextUpdate"]; } return self; @@ -80,7 +85,7 @@ -(NSString *)description { - return [NSString stringWithFormat:@"TimezoneID:%@\nFormatted Address:%@\nCustom Label:%@\nLatitude:%@\nLongitude:%@\nSunrise:%@\nSunset:%@\nPlaceID:%@", self.timezoneID, + return [NSString stringWithFormat:@"TimezoneID: %@\nFormatted Address: %@\nCustom Label: %@\nLatitude: %@\nLongitude:%@\nSunrise: %@\nSunset: %@\nPlaceID: %@", self.timezoneID, self.formattedAddress, self.customLabel, self.latitude, @@ -90,4 +95,342 @@ self.place_id]; } +- (NSString *)formatStringShouldContainCity:(BOOL)value withTimezoneName:(NSMutableDictionary *)timeZoneDictionary +{ + if (timeZoneDictionary[CLCustomLabel]) { + NSString *customLabel = timeZoneDictionary[CLCustomLabel]; + if (customLabel.length > 0) + { + return customLabel; + } + } + + if ([timeZoneDictionary[CLTimezoneName] length] > 0) + { + return timeZoneDictionary[CLTimezoneName]; + } + else if (timeZoneDictionary[CLTimezoneID]) + { + NSString *timezoneID = timeZoneDictionary[CLTimezoneID]; + + NSRange range = [timezoneID rangeOfString:@"/"]; + if (range.location != NSNotFound) + { + timezoneID = [timezoneID substringWithRange:NSMakeRange(range.location+1, timezoneID.length-1 - range.location)]; + } + return timezoneID; + } + else + { + return @"Error"; + } + +} + +- (NSString *)getFormattedSunriseOrSunsetTime:(NSMutableDictionary *)originalTime + andSunImage:(CLTimezoneCellView *)cell +{ + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + formatter.dateFormat = @"yyyy-MM-dd HH:mm"; + NSDate *sunTime = [formatter dateFromString:originalTime[@"sunriseTime"]]; + + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + dateFormatter.timeZone = [NSTimeZone timeZoneWithName:originalTime[CLTimezoneID]]; + dateFormatter.dateStyle = kCFDateFormatterShortStyle; + dateFormatter.timeStyle = kCFDateFormatterShortStyle; + 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; + dateConversion.timeStyle = kCFDateFormatterShortStyle; + dateConversion.dateFormat = @"yyyy-MM-dd HH:mm"; + + NSString *theme = [[NSUserDefaults standardUserDefaults] objectForKey:CLThemeKey]; + + if ([sunTime laterDate:[dateConversion dateFromString:newDate]] == sunTime) + { + cell.sunImage.image = theme.length > 0 && [theme isEqualToString:@"Default"] ? + [NSImage imageNamed:@"Sunrise"] : [NSImage imageNamed:@"White Sunrise"]; + return [originalTime[@"sunriseTime"] substringFromIndex:11]; + } + else + { + cell.sunImage.image = theme.length > 0 && [theme isEqualToString:@"Default"] ? + [NSImage imageNamed:@"Sunset"] : [NSImage imageNamed:@"White Sunset"]; + return [originalTime[@"sunsetTime"] substringFromIndex:11]; + } +} + +- (NSString *)getTimeForTimeZone:(NSString *)timezoneID withFutureSliderValue:(NSInteger)futureSliderValue +{ + NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar]; + NSDate *newDate = [currentCalendar dateByAddingUnit:NSCalendarUnitHour + value:futureSliderValue + toDate:[NSDate date] + options:kNilOptions]; + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + dateFormatter.dateStyle = kCFDateFormatterNoStyle; + + NSNumber *is24HourFormatSelected = [[NSUserDefaults standardUserDefaults] objectForKey:CL24hourFormatSelectedKey]; + + is24HourFormatSelected.boolValue ? [dateFormatter setDateFormat:@"HH:mm"] : [dateFormatter setDateFormat:@"hh:mm a"]; + + dateFormatter.timeZone = [NSTimeZone timeZoneWithName:timezoneID]; + //In the format 22:10 + + return [dateFormatter stringFromDate:newDate]; +} + +- (NSString *)getLocalCurrentDate +{ + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + dateFormatter.dateStyle = kCFDateFormatterShortStyle; + dateFormatter.timeStyle = kCFDateFormatterNoStyle; + dateFormatter.timeZone = [NSTimeZone systemTimeZone]; + + return [NSDateFormatter localizedStringFromDate:[NSDate date] + dateStyle:NSDateFormatterShortStyle + timeStyle:NSDateFormatterNoStyle]; + +} + +- (NSString *)compareSystemDate:(NSString *)systemDate toTimezoneDate:(NSString *)date andDataObject:(CLTimezoneData *)dataObject +{ + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy" + options:0 + locale:[NSLocale currentLocale]]; + + NSDate *localDate = [formatter dateFromString:systemDate]; + NSDate *timezoneDate = [formatter dateFromString:date]; + + if (localDate == nil || timezoneDate == nil) { + return @"Today"; + } + + // Specify which units we would like to use + NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; + NSInteger weekday = [calendar component:NSCalendarUnitWeekday fromDate:localDate]; + + if ([dataObject.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]; + + CLTimezoneData *newDataObject = [[CLTimezoneData alloc] init]; + newDataObject.timezoneID = dataObject.timezoneID; + newDataObject.formattedAddress = dataObject.formattedAddress; + newDataObject.latitude = dataObject.latitude; + newDataObject.longitude = dataObject.longitude; + newDataObject.sunriseTime = dataObject.sunriseTime; + newDataObject.sunsetTime = dataObject.sunsetTime; + newDataObject.customLabel = dataObject.customLabel; + newDataObject.place_id = dataObject.place_id; + newDataObject.nextUpdate = tomorrowMidnight; + + + + PanelController *panelController; + + for (NSWindow *window in [[NSApplication sharedApplication] windows]) + { + if ([window.windowController isMemberOfClass:[PanelController class]]) + { + panelController = window.windowController; + } + } + + + + [panelController.defaultPreferences replaceObjectAtIndex:[panelController.defaultPreferences indexOfObject:dataObject] withObject:newDataObject]; + [[NSUserDefaults standardUserDefaults] setObject:panelController.defaultPreferences forKey:CLDefaultPreferenceKey]; + } + else if ([dataObject.nextUpdate isKindOfClass:[NSDate class]] && + [dataObject.nextUpdate isEarlierThanOrEqualTo:timezoneDate]) + { + [self getTimeZoneForLatitude:dataObject.latitude + andLongitude:dataObject.longitude + andDataObject:dataObject]; + } + + NSInteger daysApart = [timezoneDate daysFrom:localDate]; + + if (daysApart == 0) { + return @"Today"; + } + else if (daysApart == -1) + { + return @"Yesterday"; + } + else if (daysApart == 1) + { + return @"Tomorrow"; + } + else + { + return [self getWeekdayFromInteger:weekday+2]; + } +} + +- (NSString *)getDateForTimeZone:(CLTimezoneData *)dataObject withFutureSliderValue:(NSInteger)futureSliderValue +{ + NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar]; + NSDate *newDate = [currentCalendar dateByAddingUnit:NSCalendarUnitHour + value:futureSliderValue + toDate:[NSDate date] + options:kNilOptions]; + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + dateFormatter.dateStyle = kCFDateFormatterShortStyle; + dateFormatter.timeStyle = kCFDateFormatterNoStyle; + + dateFormatter.timeZone = [NSTimeZone timeZoneWithName:dataObject.timezoneID]; + + NSNumber *relativeDayPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLRelativeDateKey]; + if (relativeDayPreference.integerValue == 0) { + return [self compareSystemDate:[self getLocalCurrentDate] + toTimezoneDate:[dateFormatter stringFromDate:newDate] + andDataObject:dataObject]; + } + else + { + NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; + formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy" options:0 locale:[NSLocale currentLocale]]; + + NSDate *convertedDate = [formatter dateFromString:[dateFormatter stringFromDate:newDate]]; + + NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; + NSInteger weekday = [calendar component:NSCalendarUnitWeekday fromDate:convertedDate]; + return [self getWeekdayFromInteger:weekday]; + } +} + +- (void)getTimeZoneForLatitude:(NSString *)latitude andLongitude:(NSString *)longitude andDataObject:(CLTimezoneData *)dataObject +{ + + if (![CLAPI isUserConnectedToInternet]) + { + //Could not fetch data + return; + } + + NSString *urlString = [NSString stringWithFormat:@"http://api.geonames.org/timezoneJSON?lat=%@&lng=%@&username=abhishaker17", latitude, longitude]; + + + [CLAPI dataTaskWithServicePath:urlString + bySender:self + withCompletionBlock:^(NSError *error, NSDictionary *json) { + dispatch_async(dispatch_get_main_queue(), ^{ + + if (json.count == 0) { + //No results found + 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; + } + + CLTimezoneData *newDataObject = [dataObject mutableCopy]; + + if (json[@"sunrise"] && json[@"sunset"]) { + newDataObject.sunriseTime = json[@"sunrise"]; + newDataObject.sunsetTime = json[@"sunset"]; + } + + NSUInteger units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; + NSDateComponents *comps = [[NSCalendar currentCalendar] components:units fromDate:newDataObject.nextUpdate]; + comps.day = comps.day + 1; + NSDate *tomorrowMidnight = [[NSCalendar currentCalendar] dateFromComponents:comps]; + + dataObject.nextUpdate = tomorrowMidnight; + + + NSArray *defaultPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLDefaultPreferenceKey]; + + if (defaultPreference == nil) + { + defaultPreference = [[NSMutableArray alloc] init]; + } + + + PanelController *panelController; + + for (NSWindow *window in [[NSApplication sharedApplication] windows]) + { + if ([window.windowController isMemberOfClass:[PanelController class]]) + { + panelController = window.windowController; + } + } + + + NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:defaultPreference]; + + for (NSMutableDictionary *timeDictionary in panelController.defaultPreferences) { + if ([dataObject.place_id isEqualToString:timeDictionary[CLPlaceIdentifier]]) { + [newArray replaceObjectAtIndex:[panelController.defaultPreferences indexOfObject:dataObject] withObject:newDataObject]; + } + } + + [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:CLDefaultPreferenceKey]; + + [panelController.mainTableview reloadData]; + + }); + + + }]; + +} + +- (NSString *)getWeekdayFromInteger:(NSInteger)weekdayInteger +{ + if (weekdayInteger > 7) { + weekdayInteger = weekdayInteger - 7; + } + + switch (weekdayInteger) { + case 1: + return @"Sunday"; + break; + + case 2: + return @"Monday"; + break; + + case 3: + return @"Tuesday"; + break; + + case 4: + return @"Wednesday"; + break; + + case 5: + return @"Thursday"; + break; + + case 6: + return @"Friday"; + break; + + case 7: + return @"Saturday"; + break; + + default: + return @"Error"; + break; + } +} + + + @end diff --git a/Clocker/PanelController.h b/Clocker/PanelController.h index ea4d025..ace0cde 100755 --- a/Clocker/PanelController.h +++ b/Clocker/PanelController.h @@ -31,6 +31,7 @@ #import "CLOneWindowController.h" @class PanelController; +@class CLTimezoneData; @protocol PanelControllerDelegate @@ -53,7 +54,7 @@ @property (nonatomic, strong) CLOneWindowController *oneWindow; -@property (nonatomic, strong) NSMutableArray *defaultPreferences; +@property (nonatomic, strong) NSMutableArray *defaultPreferences; @property (nonatomic, strong) NSDateFormatter *dateFormatter; @property (nonatomic, assign) NSInteger futureSliderValue; @property (nonatomic) BOOL hasActivePanel; diff --git a/Clocker/PanelController.m b/Clocker/PanelController.m index b21ff1d..ad51939 100755 --- a/Clocker/PanelController.m +++ b/Clocker/PanelController.m @@ -35,6 +35,7 @@ #import "CommonStrings.h" #import "CLTimezoneCellView.h" #import "DateTools.h" +#import "CLTimezoneData.h" #define OPEN_DURATION .15 #define CLOSE_DURATION .1 @@ -327,17 +328,12 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; cell.relativeDate.stringValue = [self getDateForTimeZone:self.defaultPreferences[row]]; - cell.time.stringValue = [self getTimeForTimeZone:self.defaultPreferences[row][CLTimezoneID]]; cell.rowNumber = row; cell.customName.stringValue = [self formatStringShouldContainCity:YES - withTimezoneName:self.defaultPreferences[row]]; NSNumber *displaySuntimings = [[NSUserDefaults standardUserDefaults] objectForKey:CLDisplaySunTimingKey]; - 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 { @@ -368,23 +364,16 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; #pragma mark Datasource formatting #pragma mark - -- (NSString *)formatStringShouldContainCity:(BOOL)value withTimezoneName:(NSMutableDictionary *)timeZoneDictionary { - if (timeZoneDictionary[CLCustomLabel]) { - NSString *customLabel = timeZoneDictionary[CLCustomLabel]; if (customLabel.length > 0) { return customLabel; } } - if ([timeZoneDictionary[CLTimezoneName] length] > 0) { - return timeZoneDictionary[CLTimezoneName]; } - else if (timeZoneDictionary[CLTimezoneID]) { - NSString *timezoneID = timeZoneDictionary[CLTimezoneID]; NSRange range = [timezoneID rangeOfString:@"/"]; if (range.location != NSNotFound) @@ -400,22 +389,18 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; } -- (NSString *)getFormattedSunriseOrSunsetTime:(NSMutableDictionary *)originalTime andSunImage:(CLTimezoneCellView *)cell { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyy-MM-dd HH:mm"; - NSDate *sunTime = [formatter dateFromString:originalTime[@"sunriseTime"]]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; - dateFormatter.timeZone = [NSTimeZone timeZoneWithName:originalTime[CLTimezoneID]]; dateFormatter.dateStyle = kCFDateFormatterShortStyle; dateFormatter.timeStyle = kCFDateFormatterShortStyle; 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; dateConversion.timeStyle = kCFDateFormatterShortStyle; dateConversion.dateFormat = @"yyyy-MM-dd HH:mm"; @@ -426,13 +411,11 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; { cell.sunImage.image = theme.length > 0 && [theme isEqualToString:@"Default"] ? [NSImage imageNamed:@"Sunrise"] : [NSImage imageNamed:@"White Sunrise"]; - return [originalTime[@"sunriseTime"] substringFromIndex:11]; } else { cell.sunImage.image = theme.length > 0 && [theme isEqualToString:@"Default"] ? [NSImage imageNamed:@"Sunset"] : [NSImage imageNamed:@"White Sunset"]; - return [originalTime[@"sunsetTime"] substringFromIndex:11]; } } @@ -469,7 +452,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; } -- (NSString *)compareSystemDate:(NSString *)systemDate toTimezoneDate:(NSString *)date andDictionary:(NSMutableDictionary *)dictionary { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy" @@ -489,7 +471,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; NSInteger weekday = [calendar component:NSCalendarUnitWeekday fromDate:localDate]; - if ([dictionary[@"nextUpdate"] isKindOfClass:[NSString class]]) { NSUInteger units = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; @@ -497,19 +478,10 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; comps.day = comps.day + 1; NSDate *tomorrowMidnight = [[NSCalendar currentCalendar] dateFromComponents:comps]; - NSMutableDictionary *newDict = [[NSMutableDictionary alloc] initWithDictionary:dictionary copyItems:YES]; - [newDict setObject:tomorrowMidnight forKey:@"nextUpdate"]; - - [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]; @@ -531,7 +503,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; } } -- (NSString *)getDateForTimeZone:(NSMutableDictionary *)dictionary { NSCalendar *currentCalendar = [NSCalendar autoupdatingCurrentCalendar]; NSDate *newDate = [currentCalendar dateByAddingUnit:NSCalendarUnitHour @@ -542,12 +513,10 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; dateFormatter.dateStyle = kCFDateFormatterShortStyle; dateFormatter.timeStyle = kCFDateFormatterNoStyle; - 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] andDictionary:dictionary] ;; } else { @@ -579,16 +548,11 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; { if ([object isKindOfClass:[NSString class]]) { - NSMutableDictionary *timezoneDictionary = self.defaultPreferences[row]; - if ([self.defaultPreferences[row][CLTimezoneName] isEqualToString:object]) { return; } - NSMutableDictionary *mutableTimeZoneDict = [timezoneDictionary mutableCopy]; - [mutableTimeZoneDict setValue:object forKey:CLCustomLabel]; - [self.defaultPreferences replaceObjectAtIndex:row withObject:mutableTimeZoneDict]; [[NSUserDefaults standardUserDefaults] setObject:self.defaultPreferences forKey:CLDefaultPreferenceKey]; [self.mainTableview reloadData]; } @@ -757,7 +721,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; } } -- (void)getTimeZoneForLatitude:(NSString *)latitude andLongitude:(NSString *)longitude andDictionary:(NSMutableDictionary *)dictionary { Reachability *reachability = [Reachability reachabilityForInternetConnection]; NetworkStatus networkStatus = [reachability currentReachabilityStatus]; @@ -811,21 +774,12 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; - NSMutableDictionary *newDictionary = [[NSMutableDictionary alloc] initWithDictionary:dictionary - copyItems:YES]; - if (json[@"sunrise"] && json[@"sunset"]) { - [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]; @@ -837,8 +791,6 @@ NSString *const CLTimezoneCellViewIdentifier = @"timeZoneCell"; NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:defaultPreference]; for (NSMutableDictionary *timeDictionary in self.defaultPreferences) { - if ([dictionary[CLPlaceIdentifier] isEqualToString:timeDictionary[CLPlaceIdentifier]]) { - [newArray replaceObjectAtIndex:[self.defaultPreferences indexOfObject:timeDictionary] withObject:newDictionary]; } } diff --git a/Clocker/Preferences/CLPreferencesViewController.m b/Clocker/Preferences/CLPreferencesViewController.m index 2ce7dc9..3ad28e3 100644 --- a/Clocker/Preferences/CLPreferencesViewController.m +++ b/Clocker/Preferences/CLPreferencesViewController.m @@ -108,6 +108,15 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?"; - (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row { + CLTimezoneData *dataSource; + + if (self.filteredArray.count > 0) + { + dataSource = self.filteredArray[row]; + } + + + if ([[tableColumn identifier] isEqualToString:CLPreferencesTimezoneNameIdentifier]) { if ([self.selectedTimeZones[row][CLTimezoneName] length] > 0) { @@ -119,7 +128,7 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?"; { if (row < self.filteredArray.count) { - return [self.filteredArray[row] objectForKey:CLTimezoneName]; + return dataSource.formattedAddress; } return nil; @@ -183,10 +192,14 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?"; return; } + CLTimezoneData *dataObject = self.filteredArray[self.availableTimezoneTableView.selectedRow]; + for (NSMutableDictionary *timezoneDictionary in self.selectedTimeZones) { + + NSString *name = timezoneDictionary[CLPlaceIdentifier]; - NSString *selectedPlaceID = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLPlaceIdentifier]; + NSString *selectedPlaceID = dataObject.place_id; if (self.searchField.stringValue.length > 0) { if ([name isKindOfClass:[NSString class]] && @@ -205,12 +218,12 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?"; self.searchField.stringValue = CLEmptyString; - [self getTimeZoneForLatitude:[self.filteredArray[self.availableTimezoneTableView - .selectedRow] objectForKey:@"latitude"]andLongitude:[self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:@"longitude"]]; + [self getTimeZoneForLatitude:dataObject.latitude andLongitude:dataObject.longitude]; + /* PFObject *feedbackObject = [PFObject objectWithClassName:CLParseTimezoneSelectionClassIdentifier]; feedbackObject[CLParseTimezoneNameProperty] = [self.filteredArray[self.availableTimezoneTableView.selectedRow] objectForKey:CLTimezoneName]; - [feedbackObject saveEventually]; + [feedbackObject saveEventually];*/ } @@ -428,9 +441,7 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?"; CLTimezoneData *newObject = [[CLTimezoneData alloc] initWithDictionary:totalPackage]; - NSLog(@"%@", newObject.description); - - [self.filteredArray addObject:totalPackage]; + [self.filteredArray addObject:newObject]; } self.activityInProgress = NO; @@ -486,12 +497,14 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?"; return; } + CLTimezoneData *dataObject = self.filteredArray[self.availableTimezoneTableView.selectedRow]; + + NSString *filteredAddress = dataObject.formattedAddress; - 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)]; + filteredAddress = [dataObject.formattedAddress substringWithRange:NSMakeRange(0, range.location)]; } NSMutableDictionary *newTimezone = [NSMutableDictionary dictionary]; @@ -506,12 +519,14 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?"; [newTimezone setObject:filteredAddress forKey:CLTimezoneName]; - [newTimezone setObject:self.filteredArray[self.availableTimezoneTableView.selectedRow][CLPlaceIdentifier] forKey:CLPlaceIdentifier]; + [newTimezone setObject:dataObject.place_id forKey:CLPlaceIdentifier]; [newTimezone setObject:latitude forKey:@"latitude"]; [newTimezone setObject:longitude forKey:@"longitude"]; [newTimezone setObject:CLEmptyString forKey:@"nextUpdate"]; [newTimezone setObject:CLEmptyString forKey:CLCustomLabel]; + CLTimezoneData *timezoneObject = [[CLTimezoneData alloc] initWithDictionary:newTimezone]; + NSArray *defaultPreference = [[NSUserDefaults standardUserDefaults] objectForKey:CLDefaultPreferenceKey]; if (defaultPreference == nil) @@ -519,8 +534,9 @@ NSString *const CLTryAgainMessage = @"Try again, maybe?"; defaultPreference = [[NSMutableArray alloc] init]; } + NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:timezoneObject]; NSMutableArray *newArray = [[NSMutableArray alloc] initWithArray:defaultPreference]; - [newArray addObject:newTimezone]; + [newArray addObject:encodedObject]; [[NSUserDefaults standardUserDefaults] setObject:newArray forKey:CLDefaultPreferenceKey];