Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created January 27, 2018 15:49
Show Gist options
  • Save khanlou/0d776b1bb83e7c850a8e6afca24334ae to your computer and use it in GitHub Desktop.
Save khanlou/0d776b1bb83e7c850a8e6afca24334ae to your computer and use it in GitHub Desktop.
- (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