ICRQuestionManager.m 4.4 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
//
//  ICRQuestionManager.m
//  Cruiser
//
//  Created by Xummer on 15/6/2.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ICRQuestionManager.h"
#import "IBTImagePicker.h"
#import "ICRPatrolPlan.h"
#import "ICRQuestion.h"

#import "ICRQuestionBaseViewController.h"

@interface ICRQuestionManager ()
@property (weak, nonatomic) UIViewController *m_baseRootViewCtrl;
@property (strong, nonatomic) NSArray *m_arrQuestions;
@property (strong, nonatomic) NSArray *m_arrQuestViewCtrls;

@property (copy, nonatomic) void(^openQuestAction)();

@end

@implementation ICRQuestionManager

#pragma mark - Class Method
+ (instancetype)sharedManager {
    static ICRQuestionManager *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[[self class] alloc] init];
    });
    
    return _sharedManager;
}

#pragma mark - Getter
- (IBTImagePicker *)m_imagePicker {
    if (!_m_imagePicker) {
        self.m_imagePicker = [[IBTImagePicker alloc] init];
    }
    
    return _m_imagePicker;
}

#pragma mark - Setter
- (void)setM_bIsHelpViewAttachHide:(BOOL)bIsHelpViewAttachHide {
    _m_bIsHelpViewAttachHide = bIsHelpViewAttachHide;
    
    for (ICRQuestionBaseViewController *qVC in _m_arrQuestViewCtrls) {
        [qVC.m_helperView updateViewRect:bIsHelpViewAttachHide];
    }
}

- (void)setM_patrolPlan:(ICRPatrolPlan *)patrolPlan {
    if ([_m_patrolPlan isEqual:patrolPlan]) {
        if (_openQuestAction) {
            _openQuestAction();
            self.openQuestAction = NULL;
        }
        return;
    }
    
    _m_patrolPlan = patrolPlan;
    
    if (!_m_patrolPlan) {
        [self cleanQuestions];
        return;
    }
    
    NSUInteger uiQuestionsCount = [_m_patrolPlan.questions count];
    
    NSArray *arrQuestIDs = [_m_patrolPlan.questions valueForKeyPath:@"uuid"];
    
    ICRDatabaseFetchBlock fetchBlk = ^FMResultSet *(FMDatabase *db) {
        NSString * sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ IN %@ ORDER BY %@ ASC", [ICRQuestion TableName], @"uuid", [IBTModel ValuePlaceholdersWithCount:uiQuestionsCount], @"lineNo"];
        return [db executeQuery:sql withArgumentsInArray:arrQuestIDs];
    };
    
    __weak typeof(self)weakSelf = self;
    ICRDatabaseFetchResultsBlock fetchResultsBlk = ^(NSArray *fetchedObjects) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        
        strongSelf.m_arrQuestions = fetchedObjects;
        if (_openQuestAction) {
            _openQuestAction();
            self.openQuestAction = NULL;
        }
    };
    
    ICRDataBaseController *dbCtrl = [ICRDataBaseController sharedController];
    [dbCtrl runFetchForClass:[ICRQuestion class]
                  fetchBlock:fetchBlk
           fetchResultsBlock:fetchResultsBlk];
}

#pragma mark - Private Method
- (void)cleanQuestions {
    self.m_imagePicker = nil;
    self.m_arrQuestions = nil;
    self.m_arrQuestViewCtrls = nil;
    self.m_baseRootViewCtrl = nil;
}

#pragma mark - Public Method
- (void)openQuestionVCFromViewControler:(UIViewController *)VC withPlan:(ICRPatrolPlan *)plan  {
    if (![VC isKindOfClass:[UIViewController class]] || !plan) {
        return;
    }
    self.m_baseRootViewCtrl = VC;
    void(^block)(void) = ^(void) {
        NSMutableArray *arrViews = [NSMutableArray array];
        NSUInteger uiCount = [_m_arrQuestions count];
        NSUInteger uiIndex = 0;
        for (ICRQuestion *questE in self.m_arrQuestions) {
            ICRQuestionBaseViewController *qVC = [[ICRQuestionBaseViewController alloc] initWithQuestion:questE];
            qVC.m_uiIndex = uiIndex;
            qVC.title = [NSString stringWithFormat:@"巡店纪录(%@/%@)", @( uiIndex + 1 ), @( uiCount )];
            [arrViews addObject:qVC];
            uiIndex ++;
        }
        self.m_arrQuestViewCtrls = [arrViews count] > 0 ? arrViews : nil;
        
        if (_m_arrQuestViewCtrls) {
            IBTUINavigationController *navCtrl = [[IBTUINavigationController alloc] initWithRootViewController:[arrViews firstObject]];
            navCtrl.viewControllers = arrViews;
            [VC presentViewController:navCtrl animated:YES completion:NULL];
        }
    };
    self.openQuestAction = block;
    self.m_patrolPlan = plan;
}

- (UIViewController *)questionViewControlAtIndex:(NSUInteger)uiIndex {
    if (uiIndex < [_m_arrQuestViewCtrls count]) {
        return _m_arrQuestViewCtrls[ uiIndex ];
    }
    else {
        return nil;
    }
}

@end