You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

182 lines
6.4 KiB

//
// CLTimezoneCellView.m
// Clocker
//
// Created by Abhishek Banthia on 12/13/15.
//
//
#import "CLTimezoneCellView.h"
#import "PanelController.h"
#import "CommonStrings.h"
#import "CLTimezoneData.h"
#import "CLFloatingWindowController.h"
@implementation CLTimezoneCellView
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
// Drawing code here.
}
- (IBAction)labelDidChange:(NSTextField *)sender
{
NSTextField *customLabelCell = (NSTextField*) sender;
__block PanelController *panelController;
__block CLFloatingWindowController *floatingWindow;
NSNumber *displayMode = [[NSUserDefaults standardUserDefaults] objectForKey:CLShowAppInForeground];
[[[NSApplication sharedApplication] windows] enumerateObjectsUsingBlock:^(NSWindow * _Nonnull window, NSUInteger idx, BOOL * _Nonnull stop)
{
if (displayMode.integerValue == 0)
{
if ([window.windowController isMemberOfClass:[PanelController class]])
{
panelController = window.windowController;
}
}
else if (displayMode.integerValue == 1)
{
if ([window.windowController isMemberOfClass:[CLFloatingWindowController class]])
{
floatingWindow = window.windowController;
}
}
}];
NSString *originalValue = customLabelCell.stringValue;
NSString *customLabelValue = [originalValue stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
if ([[sender superview] isKindOfClass:[self class]])
{
CLTimezoneCellView *cellView = (CLTimezoneCellView *)[sender superview];
/*
Fix for http://crashes.to/s/f43ce0accd0
More stringent null check for array values.
*/
if (panelController.defaultPreferences == nil && floatingWindow.defaultPreferences == nil)
{
return;
}
NSData *dataObject = displayMode.integerValue == 0 ? panelController.defaultPreferences[cellView.rowNumber] : floatingWindow.defaultPreferences[cellView.rowNumber];
CLTimezoneData *timezoneObject = [CLTimezoneData getCustomObject:dataObject];
if (displayMode.integerValue == 0)
{
[panelController.defaultPreferences enumerateObjectsUsingBlock:^(id _Nonnull object, NSUInteger idx, BOOL * _Nonnull stop) {
CLTimezoneData *timeObject = [CLTimezoneData getCustomObject:object];
if ([timeObject.formattedAddress isEqualToString:customLabelValue]) {
timeObject.customLabel = CLEmptyString;
}
}];
}
else if (displayMode.integerValue == 1)
{
[floatingWindow.defaultPreferences enumerateObjectsUsingBlock:^(id _Nonnull object, NSUInteger idx, BOOL * _Nonnull stop) {
CLTimezoneData *timeObject = [CLTimezoneData getCustomObject:object];
if ([timeObject.formattedAddress isEqualToString:customLabelValue]) {
timeObject.customLabel = CLEmptyString;
}
}];
}
timezoneObject.customLabel = (customLabelValue.length > 0) ? customLabelValue : CLEmptyString;
if ([timezoneObject.isFavourite isEqualToNumber:@1])
{
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:timezoneObject];
[[NSUserDefaults standardUserDefaults] setObject:encodedObject
forKey:@"favouriteTimezone"];
}
NSData *newObject = [NSKeyedArchiver archivedDataWithRootObject:timezoneObject];
if (displayMode.integerValue == 0)
{
[panelController.defaultPreferences replaceObjectAtIndex:cellView.rowNumber withObject:newObject];
[[NSUserDefaults standardUserDefaults] setObject:panelController.defaultPreferences forKey:CLDefaultPreferenceKey];
[panelController updateDefaultPreferences];
[panelController.mainTableview reloadData];
}
else if(displayMode.integerValue == 1)
{
[floatingWindow.defaultPreferences replaceObjectAtIndex:cellView.rowNumber withObject:newObject];
[[NSUserDefaults standardUserDefaults] setObject:floatingWindow.defaultPreferences forKey:CLDefaultPreferenceKey];
[floatingWindow updateDefaultPreferences];
[floatingWindow.mainTableview reloadData];
}
[[NSNotificationCenter defaultCenter]
postNotificationName:CLCustomLabelChangedNotification
object:nil];
}
}
-(void)mouseDown:(NSEvent *)theEvent
{
[self.window endEditingFor:nil];
}
- (void)updateTextColorWithColor:(NSColor *)color andCell:(CLTimezoneCellView*)cell
{
cell.relativeDate.textColor = color;
cell.customName.textColor = color;
cell.time.textColor = color;
cell.sunriseSetTime.textColor = color;
}
- (void)setAppropriateFont
{
NSOperatingSystemVersion version = [[NSProcessInfo processInfo] operatingSystemVersion];
NSArray *availableFonts = [[NSFontManager sharedFontManager] availableFontFamilies];
if(version.minorVersion <= 10)
{
if ([availableFonts containsObject:@"Helvetica Neue"])
{
self.relativeDate.font = [NSFont fontWithName:@"HelveticaNeue-Bold" size:0];
self.customName.font = [NSFont fontWithName:@"HelveticaNeue-Thin" size:15];
self.time.font = [NSFont fontWithName:@"HelveticaNeue-Thin" size:22];
self.sunriseSetTime.font = [NSFont fontWithName:@"HelveticaNeue-Light" size:11];
}
}
}
- (void)setUpAutoLayoutWithCell
{
CGFloat width = [self.relativeDate.stringValue
sizeWithAttributes: @{NSFontAttributeName:self.relativeDate.font}].width;
[self.relativeDate.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint * _Nonnull constraint, NSUInteger idx, BOOL * _Nonnull stop) {
if (constraint.constant > 20)
{
constraint.constant = width+8;
}
}];
}
- (void)controlTextDidEndEditing:(NSNotification *)obj
{
CLFloatingWindowController *windowController = [CLFloatingWindowController sharedFloatingWindow];
[windowController.floatingWindowTimer start];
}
@end