#import "ThingsAppSectionHandler.h" NS_ASSUME_NONNULL_BEGIN @implementation ThingsAppSectionHandler - (BOOL)objectHasChildren:(QSObject *)object { return YES; } - (void)setQuickIconForObject:(QSObject *)object { NSImage *baseImage = [QSResourceManager imageNamed:@"com.culturedcode.ThingsMac"]; ThingsAppSectionKey identifier = [object identifier]; NSImage *imageWithBadge = nil; if (identifier == ThingsAppSectionKeyInbox) { NSColor *color = [NSColor colorWithRed:0.318 green:0.667 blue:0.949 alpha:1.0]; imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"tray.circle" ofColor:color]; } if (identifier == ThingsAppSectionKeyToday) { NSColor *color = [NSColor colorWithRed:0.973 green:0.839 blue:0.282 alpha:1.0]; imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"star.circle" ofColor:color]; } if (identifier == ThingsAppSectionKeyAnytime) { NSColor *color = [NSColor colorWithRed:0.353 green:0.651 blue:0.616 alpha:1.0]; imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"diamond.circle" ofColor:color]; } if (identifier == ThingsAppSectionKeySomeday) { NSColor *color = [NSColor colorWithRed:0.792 green:0.749 blue:0.525 alpha:1.0]; imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"archivebox.circle" ofColor:color]; } if (identifier == ThingsAppSectionKeyUpcoming) { NSColor *color = [NSColor colorWithRed:0.902 green:0.224 blue:0.349 alpha:1.0]; imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"calendar.circle" ofColor:color]; } if (imageWithBadge) { [object setIcon:imageWithBadge]; } else { [object setIcon:baseImage]; } } - (BOOL)loadChildrenForObject:(QSObject *)object { NSDictionary *contents = [object objectForType:kAppSectionType]; if (!contents) { NSLog(@"Malformed Things Task: No content."); return NO; } NSString *path = [contents objectForKey:@"path"]; if (!path) { NSLog(@"Malformed Things Task: No path."); return NO; } ThingsAppSectionKey appSection = [object identifier]; if (!appSection) { NSLog(@"Malformed Things App Section: No identifier."); return NO; } NSMutableArray *children = [NSMutableArray array]; [children addObjectsFromArray:[self tasksForAppSection:appSection usingDatabaseAt:path ]]; [children addObjectsFromArray:[self projectsForAppSection:appSection usingDatabaseAt:path ]]; [object setChildren:children]; return YES; } - (NSArray *)projectsForAppSection:(ThingsAppSectionKey)appSection usingDatabaseAt:(NSString *)path { NSString *conditions = [self conditionsFor:appSection]; NSString *query = [NSString stringWithFormat: @"SELECT " "task.uuid AS uuid, " "task.creationDate AS creationDate, " "task.status AS status, " "task.title AS title, " "task.notes AS notes, " "task.area AS area " "FROM TMTask as task " "LEFT JOIN TMTask AS project " "ON task.project = project.uuid " "LEFT JOIN TMTask AS heading " "ON task.heading = heading.uuid " "LEFT JOIN TMTask AS headingProject " "ON heading.project = headingProject.uuid " "WHERE task.type = 1 " "AND task.trashed = 0 " "AND task.status = 0 " "%@" "GROUP BY task.uuid " "ORDER BY title;", conditions]; NSArray *results = [SQLHelper executeSql:query onFile:path]; NSMutableArray *objects = [NSMutableArray array]; [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger i, BOOL *stop) { NSLog(@"WE PROJECTIN!"); [objects addObject:[ObjectHelper projectFromSQLResult:dictionary usingPath:path ]]; }]; return objects; } - (NSArray *)tasksForAppSection:(ThingsAppSectionKey)appSection usingDatabaseAt:(NSString *)path { NSString *conditions = [self conditionsFor:appSection]; NSString *query = [NSString stringWithFormat: @"SELECT " "task.uuid AS uuid, " "task.creationDate AS creationDate, " "task.status AS status, " "task.title AS title, " "task.notes AS notes, " "task.area AS area " "FROM TMTask as task " "LEFT JOIN TMTask AS project " "ON task.project = project.uuid " "LEFT JOIN TMTask AS heading " "ON task.heading = heading.uuid " "LEFT JOIN TMTask AS headingProject " "ON heading.project = headingProject.uuid " "WHERE task.type = 0 " "AND task.trashed = 0 " "AND task.status = 0 " "%@" "GROUP BY task.uuid " "ORDER BY title;", conditions]; NSArray *results = [SQLHelper executeSql:query onFile:path]; NSMutableArray *objects = [NSMutableArray array]; [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger i, BOOL *stop) { [objects addObject:[ObjectHelper taskFromSQLResult:dictionary usingPath:path ]]; }]; return objects; } - (NSString *)conditionsFor:(ThingsAppSectionKey)appSection { if (appSection == ThingsAppSectionKeyInbox) { return @"AND task.startDate IS NULL " "AND task.area IS NULL " "AND task.project IS NULL " "AND task.heading IS NULL "; } if (appSection == ThingsAppSectionKeyToday) { return @"AND task.todayIndexReferenceDate IS NOT NULL " "AND task.todayIndex <= 0 "; } if (appSection == ThingsAppSectionKeyUpcoming) { return @"AND task.todayIndexReferenceDate IS NOT NULL " "AND task.todayIndex > 0 "; } if (appSection == ThingsAppSectionKeySomeday) { return @"AND task.start=2 " "AND task.startDate IS NULL " "AND task.rt1_instanceCreationStartDate IS NULL "; } if (appSection == ThingsAppSectionKeyAnytime) { return @"AND ( " "( " "task.todayIndexReferenceDate IS NOT NULL " "AND task.todayIndex <= 0 " ") " "OR ( " "task.startDate IS NULL AND " "task.start=1 AND " "task.rt1_instanceCreationStartDate IS NULL " "AND ( " "task.area IS NOT NULL " "OR task.project IS NOT NULL " "OR task.heading IS NOT NULL " ") " "AND project.todayIndexReferenceDate IS NULL " "AND (project.start=1 OR headingProject.start IS NULL) " "AND heading.todayIndexReferenceDate IS NULL " "AND (heading.start=1 OR headingProject.start IS NULL) " "AND headingProject.todayIndexReferenceDate IS NULL " "AND (headingProject.start=1 OR headingProject.start IS NULL) " ") " ") "; } } @end NS_ASSUME_NONNULL_END