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.
 
 
 
 
 

174 lines
6.2 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)setUpAutoLayoutWithCell
{
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) {
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];
}
- (void)controlTextDidEndEditing:(NSNotification *)obj
{
CLFloatingWindowController *windowController = [CLFloatingWindowController sharedFloatingWindow];
[windowController.floatingWindowTimer start];
}
@end