- (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