Advertisement
Guest User

Untitled

a guest
May 27th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (BOOL)checkCanadian10:(NSDate *)date{
  2.     BOOL inViolation = NO;
  3.     if ([date isEqualToDate:[Utils getDateWithTime:@0 fromDate:[NSDate date]]] && [Utils getCurrentTime] < 23.59)  {
  4.         return inViolation;
  5.     }
  6.     NSDate *endDate = [Utils getDateWithTime:@23.59 fromDate:date];
  7.    
  8.     NSPredicate *bPredicate = [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  9.         CycleStatus *status = evaluatedObject;
  10.         if ([Utils date:[Utils convertDateFromString:status.time] isBetweenDate:date andDate:endDate] && (status.statusTypeId.integerValue == 4 || status.statusTypeId.integerValue == 5)) {
  11.             return YES;
  12.         }
  13.         return NO;
  14.     }];
  15.     NSInteger cycle = [RulesSettings getSingleEntity].cycleId.integerValue;
  16.     NSMutableArray *result = [NSMutableArray arrayWithArray:[self.cycles filteredArrayUsingPredicate:bPredicate]];
  17.     if (!result.count && cycle != 4 && cycle != 5) {
  18.         return inViolation;
  19.     }
  20.     int hour = 60*60;
  21.     NSDate *dayStart = [Utils getDateWithTime:@0 fromDate:date];
  22.     NSDate *dayEnd = [Utils getDateWithTime:@(24) fromDate:date];
  23.     long dayEndTimeInSec = [dayEnd timeIntervalSince1970];
  24.     NSArray *statuses = [self getStatusesFrom:dayStart to:dayEnd includeFake:true];
  25.     float totallyOffSb = 0;
  26.     for (int i = 0; i < statuses.count; i++) {
  27.         LogStatus *status = statuses[i];
  28.         NSInteger stType = status.statusTypeId.integerValue;
  29.         float duration = 0;
  30.         if (i == statuses.count-1) {//last status
  31.             duration = dayEndTimeInSec - [status.startDate timeIntervalSince1970]+1;
  32.         } else {//not the last
  33.             LogStatus *nextStatus = statuses[i+1];
  34.             duration = [nextStatus.startDate timeIntervalSince1970] - [status.startDate timeIntervalSince1970];
  35.         }
  36.         if((stType == 2 || stType == 3) && duration >= 0.5*hour){//off or sb
  37.             totallyOffSb+=duration;
  38.         }
  39.     }
  40.    
  41.     if(totallyOffSb < 10*hour){
  42.         inViolation = true;
  43.     }
  44.     return inViolation;
  45. }
  46.  
  47. - (void)checkCyclesViolationsForDate:(NSDate *)date {
  48.     NSMutableArray *allCycles = [NSMutableArray new];
  49.     NSArray *cycles = [[DatabaseManager sharedInstance] getAllEntitiesWithoutSyncState:DELETE class:[CycleStatus class]];
  50.     NSDate *currentDate = [[TimeManager sharedInstance] currentTimeWithCorrectTimeZone];
  51.     for (int i = 0; i < cycles.count; i++) {
  52.         CycleStatus *status = [cycles objectAtIndex:i];
  53.         if ([[Utils convertDateFromString:status.time] compare:date]==NSOrderedDescending && [[Utils convertDateFromString:status.time] compare:currentDate]==NSOrderedAscending){
  54.             [allCycles addObject:status];
  55.         }
  56.     }
  57.     NSInteger oldCycle = 0;
  58.     for (CycleStatus *status in allCycles) {
  59.         if (oldCycle) {
  60.             BOOL isViolation = [self checkCycleChangeDate:[Utils convertDateFromString:status.time] oldCycle:oldCycle newCycle:status.statusTypeId.integerValue];
  61.            
  62.             NSArray *violations = [[DatabaseManager sharedInstance] getViolationsForDate:[Utils getDateOnlyStringFromDate:[Utils convertDateFromString:status.time]]];
  63.             for (Violation *violation in violations) {
  64.                 if (violation.type == CANADA_CYCLE) {
  65.                     [[DatabaseManager sharedInstance] deleteEntity:violation];
  66.                 }
  67.             }
  68.             if (isViolation) {
  69.                 Violation *violation = [Violation new];
  70.                 NSNumber *fakeId = [NSNumber numberWithInteger:MIN(-99, [[DatabaseManager sharedInstance] getLastFakeId]) - 1];
  71.                 violation.violationId = [NSNumber numberWithInteger:fakeId.integerValue*-1];
  72.                 [[DatabaseManager sharedInstance] setLastFakeId:[fakeId integerValue]];
  73.                 violation.date = [Utils getDateOnlyStringFromDate:[Utils convertDateFromString:status.time]];
  74.                 violation.type = CANADA_CYCLE;
  75.                 [[DatabaseManager sharedInstance] saveEntity:violation];
  76.             }
  77.         }
  78.         oldCycle = status.statusTypeId.integerValue;
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement