-
-
Save khanlou/0d776b1bb83e7c850a8e6afca24334ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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