#import "ThingsTagHandler.h" NS_ASSUME_NONNULL_BEGIN @implementation ThingsTagHandler - (BOOL)objectHasChildren:(QSObject *)object { return YES; } - (void)setQuickIconForObject:(QSObject *)object { NSImage *baseImage = [QSResourceManager imageNamed:@"com.culturedcode.ThingsMac"]; NSColor *color = [NSColor colorWithRed:0.263 green:0.502 blue:0.376 alpha:1.0]; NSImage *imageWithBadge = [baseImage imageOfSize:NSMakeSize(512, 512) withSystemBadge:@"tag.circle" ofColor:color]; if (imageWithBadge) { [object setIcon:imageWithBadge]; } else { [object setIcon:baseImage]; } } - (BOOL)loadChildrenForObject:(QSObject *)object { NSDictionary *contents = [object objectForType:kTagType]; 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; } NSString *uuid = [contents objectForKey:@"uuid"]; if (!uuid) { NSLog(@"Malformed Things Task: No uuid."); return NO; } NSMutableArray *children = [NSMutableArray array]; [children addObjectsFromArray:[self areasForTagIdentifiedBy:uuid usingDatabaseAt:path ]]; [children addObjectsFromArray:[self projectsForTagIdentifiedBy:uuid usingDatabaseAt:path ]]; [children addObjectsFromArray:[self tasksForTagIdentifiedBy:uuid usingDatabaseAt:path ]]; [object setChildren:children]; return YES; } - (NSArray *)projectsForTagIdentifiedBy:(NSString *)uuid usingDatabaseAt:(NSString *)path { NSString *query = [NSString stringWithFormat: @"SELECT " "project.uuid as uuid, " "project.creationDate as creationDate, " "project.status as status, " "project.title as title, " "project.notes as notes, " "project.area as area " "FROM TMTag AS tag " "LEFT JOIN TMTaskTag as junction " "ON junction.tags = tag.uuid " "LEFT JOIN TMTask as project " "ON junction.tasks = project.uuid " "WHERE tag.uuid = '%@' " "AND project.uuid IS NOT NULL " "AND project.type = 1 " "AND project.status = 0 " "AND project.trashed = 0 " "ORDER BY project.title;", uuid]; NSArray *results = [SQLHelper executeSql:query onFile:path]; NSMutableArray *objects = [NSMutableArray array]; [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger i, BOOL *stop) { [objects addObject:[ObjectHelper projectFromSQLResult:dictionary usingPath:path ]]; }]; return objects; } - (NSArray *)tasksForTagIdentifiedBy:(NSString *)uuid usingDatabaseAt:(NSString *)path { 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 TMTag AS tag " "LEFT JOIN TMTaskTag as junction " "ON junction.tags = tag.uuid " "LEFT JOIN TMTask as task " "ON junction.tasks = task.uuid " "WHERE tag.uuid = '%@' " "AND task.uuid IS NOT NULL " "AND task.type = 0 " "AND task.status = 0 " "AND task.trashed = 0 " "ORDER BY task.title;", uuid]; 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; } - (NSArray *)areasForTagIdentifiedBy:(NSString *)uuid usingDatabaseAt:(NSString *)path { NSString *query = [NSString stringWithFormat: @"SELECT " "area.uuid as uuid, " "area.title as title " "FROM TMTag AS tag " "LEFT JOIN TMAreaTag as junction " "ON junction.tags = tag.uuid " "LEFT JOIN TMArea as area " "ON junction.areas = area.uuid " "WHERE tag.uuid = '%@' " "AND area.uuid IS NOT NULL " "ORDER BY area.title;", uuid]; NSArray *results = [SQLHelper executeSql:query onFile:path]; NSMutableArray *objects = [NSMutableArray array]; [results enumerateObjectsUsingBlock:^(NSDictionary *dictionary, NSUInteger i, BOOL *stop) { [objects addObject:[ObjectHelper areaFromSQLResult:dictionary usingPath:path ]]; }]; return objects; } @end NS_ASSUME_NONNULL_END