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.

190 lines
6.2 KiB

9 years ago
//
// CLTimezoneCellView.m
// Clocker
//
// Created by Abhishek Banthia on 12/13/15.
//
//
#import "CLTimezoneCellView.h"
#import "PanelController.h"
#import "CommonStrings.h"
9 years ago
#import "CLTimezoneData.h"
9 years ago
#import "CLFloatingWindowController.h"
9 years ago
@implementation CLTimezoneCellView
- (void)drawRect:(NSRect)dirtyRect
{
9 years ago
[super drawRect:dirtyRect];
9 years ago
9 years ago
// Drawing code here.
}
9 years ago
- (IBAction)labelDidChange:(NSTextField *)sender
9 years ago
{
NSTextField *customLabelCell = (NSTextField*) sender;
PanelController *panelController;
CLFloatingWindowController *floatingWindow;
9 years ago
NSNumber *displayMode = [[NSUserDefaults standardUserDefaults] objectForKey:CLShowAppInForeground];
if (displayMode.integerValue == 0)
9 years ago
{
panelController = [PanelController getPanelControllerInstance];
}
else if (displayMode.integerValue == 1)
{
floatingWindow = [CLFloatingWindowController sharedFloatingWindow];
}
9 years ago
NSString *originalValue = customLabelCell.stringValue;
NSString *customLabelValue = [originalValue stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
if ([sender.superview isKindOfClass:[self class]])
9 years ago
{
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;
}
9 years ago
NSData *dataObject = displayMode.integerValue == 0 ? panelController.defaultPreferences[cellView.rowNumber] : floatingWindow.defaultPreferences[cellView.rowNumber];
CLTimezoneData *timezoneObject = [CLTimezoneData getCustomObject:dataObject];
9 years ago
if (displayMode.integerValue == 0)
{
9 years ago
9 years ago
[panelController.defaultPreferences enumerateObjectsUsingBlock:^(id _Nonnull object, NSUInteger idx, BOOL * _Nonnull stop) {
CLTimezoneData *timeObject = [CLTimezoneData getCustomObject:object];
if ([timeObject.formattedAddress isEqualToString:customLabelValue]) {
[timeObject setLabelForTimezone:CLEmptyString];
9 years ago
}
}];
}
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 setLabelForTimezone:CLEmptyString];
9 years ago
}
}];
}
[timezoneObject setLabelForTimezone:customLabelValue];
if ([timezoneObject.isFavourite isEqualToNumber:@1])
{
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:timezoneObject];
[[NSUserDefaults standardUserDefaults] setObject:encodedObject
forKey:@"favouriteTimezone"];
}
NSData *newObject = [NSKeyedArchiver archivedDataWithRootObject:timezoneObject];
9 years ago
if (displayMode.integerValue == 0)
{
(panelController.defaultPreferences)[cellView.rowNumber] = newObject;
9 years ago
[[NSUserDefaults standardUserDefaults] setObject:panelController.defaultPreferences forKey:CLDefaultPreferenceKey];
[panelController updateDefaultPreferences];
[panelController updateTableContent];
9 years ago
}
else if(displayMode.integerValue == 1)
{
(floatingWindow.defaultPreferences)[cellView.rowNumber] = newObject;
9 years ago
[[NSUserDefaults standardUserDefaults] setObject:floatingWindow.defaultPreferences forKey:CLDefaultPreferenceKey];
[floatingWindow updateDefaultPreferences];
[floatingWindow updateTableContent];
9 years ago
}
[[NSNotificationCenter defaultCenter]
postNotificationName:CLCustomLabelChangedNotification
object:nil];
9 years ago
}
}
9 years ago
-(void)mouseDown:(NSEvent *)theEvent
{
[self.window endEditingFor:nil];
}
- (void)setTextColor:(NSColor *)color
9 years ago
{
self.relativeDate.textColor = color;
self.customName.textColor = color;
self.time.textColor = color;
self.sunriseSetTime.textColor = color;
9 years ago
}
- (void)setUpLayout
9 years ago
{
CGFloat width = [self.relativeDate.stringValue
sizeWithAttributes: @{NSFontAttributeName:self.relativeDate.font}].width;
CGFloat sunriseWidth = [self.sunriseSetTime.stringValue
sizeWithAttributes: @{NSFontAttributeName:self.sunriseSetTime.font}].width;
[self.relativeDate.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint * _Nonnull constraint, NSUInteger idx, BOOL * _Nonnull stop) {
9 years ago
if (constraint.constant > 20)
{
constraint.constant = width+8;
}
}];
[self.sunriseSetTime.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint * _Nonnull constraint, NSUInteger idx, BOOL * _Nonnull stop) {
if ([constraint.identifier isEqualToString:@"width"])
{
constraint.constant = sunriseWidth+3;
}
}];
[self.relativeDate setNeedsUpdateConstraints:YES];
[self.sunriseSetTime setNeedsUpdateConstraints:YES];
[self setUpTheme];
}
- (void)setUpTheme
{
NSNumber *theme = [[NSUserDefaults standardUserDefaults] objectForKey:CLThemeKey];
if (theme.integerValue == 1)
{
[self setTextColor:[NSColor whiteColor]];
[self.customName setDrawsBackground:YES];
(self.customName).backgroundColor = [NSColor blackColor];
}
else
{
[self setTextColor:[NSColor blackColor]];
[self.customName setDrawsBackground:NO];
}
9 years ago
}
9 years ago
- (void)controlTextDidEndEditing:(NSNotification *)obj
{
CLFloatingWindowController *windowController = [CLFloatingWindowController sharedFloatingWindow];
[windowController.floatingWindowTimer start];
}
@end