Create a gist now

Instantly share code, notes, and snippets.

@khanlou /SlicePhotos.m Secret
Created Jan 27, 2018

What would you like to do?
- (NSArray<PhotoSection *> *)fetchGroupedPhotos {
NSArray<LocalPhoto *> *photos = [self fetchPhotos];
NSMutableArray<PhotoSection *> *sections = [NSMutableArray new];
NSMutableArray<LocalPhoto *> *photosForCurrentSection = [NSMutableArray new];
for (LocalPhoto *photo in photos) {
if (photosForCurrentSection.count == 0) {
[photosForCurrentSection addObject:photo];
continue;
}
if ([photosForCurrentSection.lastObject.date timeIntervalSinceDate:photo.date] > 60*60) {
PhotoSection *section = [[PhotoSection alloc] init];
section.header = [self.dateFormatter stringFromDate:photosForCurrentSection.firstObject.date];
section.photos = photosForCurrentSection;
[sections addObject:section];
photosForCurrentSection = [NSMutableArray new];
[photosForCurrentSection addObject:photo];
} else {
[photosForCurrentSection addObject:photo];
}
}
if (photosForCurrentSection.count != 0) {
PhotoSection *section = [[PhotoSection alloc] init];
section.header = [self.dateFormatter stringFromDate:photosForCurrentSection.firstObject.date];
section.photos = photosForCurrentSection;
[sections addObject:section];
}
return sections;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment