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.

155 lines
5.4 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 {
[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;
__block PanelController *panelController;
9 years ago
__block CLFloatingWindowController *floatingWindow;
NSNumber *displayMode = [[NSUserDefaults standardUserDefaults] objectForKey:CLShowAppInForeground];
9 years ago
9 years ago
[[[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)
9 years ago
{
9 years ago
if ([window.windowController isMemberOfClass:[CLFloatingWindowController class]])
{
floatingWindow = window.windowController;
}
9 years ago
}
9 years ago
}];
9 years ago
NSString *originalValue = customLabelCell.stringValue;
NSString *customLabelValue = [originalValue stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
9 years ago
if ([[sender superview] isKindOfClass:[self class]])
{
CLTimezoneCellView *cellView = (CLTimezoneCellView *)[sender superview];
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.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];
9 years ago
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];
9 years ago
}
}
9 years ago
-(void)mouseDown:(NSEvent *)theEvent
{
[self.window endEditingFor:nil];
}
9 years ago
- (void)updateTextColorWithColor:(NSColor *)color andCell:(CLTimezoneCellView*)cell
{
cell.relativeDate.textColor = color;
cell.customName.textColor = color;
cell.time.textColor = color;
9 years ago
}
- (void)setUpAutoLayoutWithCell:(CLTimezoneCellView *)cell
{
CGFloat width = [cell.relativeDate.stringValue
sizeWithAttributes: @{NSFontAttributeName:cell.relativeDate.font}].width;
[cell.relativeDate.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint * _Nonnull constraint, NSUInteger idx, BOOL * _Nonnull stop) {
9 years ago
if (constraint.constant > 20)
{
constraint.constant = width+8;
}
}];
9 years ago
}
9 years ago
- (void)controlTextDidEndEditing:(NSNotification *)obj
{
CLFloatingWindowController *windowController = [CLFloatingWindowController sharedFloatingWindow];
[windowController.floatingWindowTimer start];
}
@end