Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (BOOL)checkCanadian10:(NSDate *)date{
- BOOL inViolation = NO;
- if ([date isEqualToDate:[Utils getDateWithTime:@0 fromDate:[NSDate date]]] && [Utils getCurrentTime] < 23.59) {
- return inViolation;
- }
- NSDate *endDate = [Utils getDateWithTime:@23.59 fromDate:date];
- NSPredicate *bPredicate = [NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
- CycleStatus *status = evaluatedObject;
- if ([Utils date:[Utils convertDateFromString:status.time] isBetweenDate:date andDate:endDate] && (status.statusTypeId.integerValue == 4 || status.statusTypeId.integerValue == 5)) {
- return YES;
- }
- return NO;
- }];
- NSInteger cycle = [RulesSettings getSingleEntity].cycleId.integerValue;
- NSMutableArray *result = [NSMutableArray arrayWithArray:[self.cycles filteredArrayUsingPredicate:bPredicate]];
- if (!result.count && cycle != 4 && cycle != 5) {
- return inViolation;
- }
- int hour = 60*60;
- NSDate *dayStart = [Utils getDateWithTime:@0 fromDate:date];
- NSDate *dayEnd = [Utils getDateWithTime:@(24) fromDate:date];
- long dayEndTimeInSec = [dayEnd timeIntervalSince1970];
- NSArray *statuses = [self getStatusesFrom:dayStart to:dayEnd includeFake:true];
- float totallyOffSb = 0;
- for (int i = 0; i < statuses.count; i++) {
- LogStatus *status = statuses[i];
- NSInteger stType = status.statusTypeId.integerValue;
- float duration = 0;
- if (i == statuses.count-1) {//last status
- duration = dayEndTimeInSec - [status.startDate timeIntervalSince1970]+1;
- } else {//not the last
- LogStatus *nextStatus = statuses[i+1];
- duration = [nextStatus.startDate timeIntervalSince1970] - [status.startDate timeIntervalSince1970];
- }
- if((stType == 2 || stType == 3) && duration >= 0.5*hour){//off or sb
- totallyOffSb+=duration;
- }
- }
- if(totallyOffSb < 10*hour){
- inViolation = true;
- }
- return inViolation;
- }
- - (void)checkCyclesViolationsForDate:(NSDate *)date {
- NSMutableArray *allCycles = [NSMutableArray new];
- NSArray *cycles = [[DatabaseManager sharedInstance] getAllEntitiesWithoutSyncState:DELETE class:[CycleStatus class]];
- NSDate *currentDate = [[TimeManager sharedInstance] currentTimeWithCorrectTimeZone];
- for (int i = 0; i < cycles.count; i++) {
- CycleStatus *status = [cycles objectAtIndex:i];
- if ([[Utils convertDateFromString:status.time] compare:date]==NSOrderedDescending && [[Utils convertDateFromString:status.time] compare:currentDate]==NSOrderedAscending){
- [allCycles addObject:status];
- }
- }
- NSInteger oldCycle = 0;
- for (CycleStatus *status in allCycles) {
- if (oldCycle) {
- BOOL isViolation = [self checkCycleChangeDate:[Utils convertDateFromString:status.time] oldCycle:oldCycle newCycle:status.statusTypeId.integerValue];
- NSArray *violations = [[DatabaseManager sharedInstance] getViolationsForDate:[Utils getDateOnlyStringFromDate:[Utils convertDateFromString:status.time]]];
- for (Violation *violation in violations) {
- if (violation.type == CANADA_CYCLE) {
- [[DatabaseManager sharedInstance] deleteEntity:violation];
- }
- }
- if (isViolation) {
- Violation *violation = [Violation new];
- NSNumber *fakeId = [NSNumber numberWithInteger:MIN(-99, [[DatabaseManager sharedInstance] getLastFakeId]) - 1];
- violation.violationId = [NSNumber numberWithInteger:fakeId.integerValue*-1];
- [[DatabaseManager sharedInstance] setLastFakeId:[fakeId integerValue]];
- violation.date = [Utils getDateOnlyStringFromDate:[Utils convertDateFromString:status.time]];
- violation.type = CANADA_CYCLE;
- [[DatabaseManager sharedInstance] saveEntity:violation];
- }
- }
- oldCycle = status.statusTypeId.integerValue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement