Skip to content

Instantly share code, notes, and snippets.

@OdNairy
Created October 22, 2014 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OdNairy/1c745c782df9a9428574 to your computer and use it in GitHub Desktop.
Save OdNairy/1c745c782df9a9428574 to your computer and use it in GitHub Desktop.
- (NSArray *)timelineRecordsFromEvents:(NSArray *)events {
WOTimelineRecord *lastRecord = [self.items lastObject];
WOTimeLineSessionGroup *lastSessionGroup = (lastRecord.type == WOTimelineRecordTypeGroupedAuthEvents) ? lastRecord.item : nil;
NSMutableArray *newRecords = [NSMutableArray new];
NSMutableArray *sessionEvents = [NSMutableArray new];
for (int i = 0; i < [events count]; i++) {
WOTimeLineEvent *event = events[i];
if (event.type == WOEventTypeSession) {
[sessionEvents addObject:event];
WOTimeLineSessionGroupType groupType = WOTimeLineSessionGroupTypeSingle;
//grouping auth events
for (int j = i + 1; j < [events count]; j++) {
event = events[j];
if (event.type == WOEventTypeSession) {
WOTimelineAuthEvent *prevEvent = [sessionEvents lastObject];
[sessionEvents addObject:event];
BOOL shouldGroupAsSingle = [self shouldGroupAsSingeWihtAuthEvent1:prevEvent authEvent2:(WOTimelineAuthEvent *)event];
if (!shouldGroupAsSingle && groupType == WOTimeLineSessionGroupTypeSingle) {
groupType = WOTimeLineSessionGroupTypeMulti;
}
if (j + 1 == [events count]) {
i = j + 1;
if (lastSessionGroup) {
[lastSessionGroup addEvents:sessionEvents];
} else {
[newRecords addObject:[WOTimelineRecord authEventRecordWithEvents:sessionEvents groupType:groupType]];
}
break;
}
}
else {
if (!lastSessionGroup) {
[newRecords addObject:[WOTimelineRecord authEventRecordWithEvents:sessionEvents groupType:groupType]];
}
else {
[lastSessionGroup addEvents:sessionEvents];
lastSessionGroup = nil;
}
[sessionEvents removeAllObjects];
i = j;
break;
}
}
}
if (i + 1 >= [events count] ) {
break;
}
WOTimelineRecord *record = [WOTimelineRecord new];
record.item = event;
record.type = WOTimelineRecordTypeEvent;
[newRecords addObject:record];
}
return newRecords;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment