// // P4UnreadItem.m // Perforce // // Created by Adam Czubernat on 15/11/2013. // Copyright (c) 2013 Perforce Software, Inc. All rights reserved. // #import "P4UnreadItem.h" @interface P4UnreadItem () - (id)initWithDictionary:(NSDictionary *)dictionary parentItem:(P4Item *)parent; @end @implementation P4UnreadItem #pragma mark - P4Item Override - (instancetype)initWithParent:(P4Item *)parentItem { if (self = [super initWithParent:parentItem]) if (!parentItem) { name = @"Unread"; localPath = @"unread://"; remotePath = nil; } return self; } - (NSArray *)actions { if (flags.directory) return nil; return [super actions]; } - (NSArray *)actionsForItems:(NSArray *)items { NSMutableArray *actions = (NSMutableArray *)[super actionsForItems:items]; [actions addObject:[P4ItemAction actionForItem:self object:[items valueForKey:@"localPath"] name:@"Mark selected as read" selector:@selector(markFilesAsRead:)]]; return actions; } - (void)loadPath:(NSString *)path { children = nil; flags.loading = YES; [[P4Workspace sharedInstance] listUnreadFiles:nil response:^(P4Operation *operation, NSArray *response) { if (operation.errors) { [self failWithError:operation.error]; return; } // Serialize children from response NSMutableArray *array = [NSMutableArray arrayWithCapacity:512]; for (NSDictionary *childDict in response) { P4UnreadItem *child = [[P4UnreadItem alloc] initWithDictionary:childDict parentItem:self]; [array addObject:child]; } // Sort files alphanumerically children = array; [self sortChildren]; [self finishLoading]; }]; } #pragma mark - Private - (id)initWithDictionary:(NSDictionary *)dictionary parentItem:(P4Item *)parentItem { if (self = [super initWithParent:parentItem]) { P4Workspace *p4 = [P4Workspace sharedInstance]; metadata = dictionary; remotePath = [dictionary objectForKey:@"depotFile"]; name = remotePath.lastPathComponent; localPath = [dictionary objectForKey:@"clientFile"]; if (!localPath || [localPath hasPrefix:@"//"]) { NSString *relativePath = [remotePath substringFromIndex:2]; localPath = [p4.root stringByAppendingPath:relativePath]; } // Set metadata status = [metadata objectForKey:@"action"]; NSString *user = [metadata objectForKey:@"otherOpen0"]; NSDictionary *info = [p4 userInfo:user]; statusOwner = [info objectForKey:@"Email"] ?: user; flags.tracked = [metadata objectForKey:@"isMapped"] != nil; [self refreshTags]; // Shelved and unread if (!remotePath) flags.shelved = [p4 shelvedForPath:remotePath].count > 0; flags.unread = YES; } return self; } #pragma mark P4Workspace Events - (void)file:(NSString *)filePath updated:(NSDictionary *)info { if (!self->children) return; // Not loaded P4Item *item = [self itemAtPath:filePath]; if (item) return; // Nothing changed // Update children by adding new item P4Item *newItem = [[P4UnreadItem alloc] initWithDictionary:info parentItem:self]; // Add item to parent NSMutableArray *array = [NSMutableArray arrayWithArray:self->children]; [array addObject:newItem]; // Sort files alphanumerically self->children = array; [self sortChildren]; [self finishLoading]; } - (void)file:(NSString *)filePath revertedAction:(NSDictionary *)info { if (!self->children) return; // Not loaded P4Item *item = [self itemAtPath:filePath]; if (!item) return; // Nothing changed NSMutableArray *array = [NSMutableArray arrayWithArray:self->children]; [array removeObject:item]; self->children = array; [self finishLoading]; [item invalidate]; } @end