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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//
// ICRQuestionManager.m
// XFFruit
//
// 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 "ICRStoreResult.h"
#import "IBTAudioController.h"
#import "ICRQuestionBaseViewController.h"
#import "ICRQResultViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ICRQuestionManager () <CLLocationManagerDelegate>
@property (weak, nonatomic) UIViewController *m_baseRootViewCtrl;
@property (strong, nonatomic) NSArray *m_arrQuestions;
@property (strong, nonatomic) NSArray *m_arrAnswers;
@property (strong, nonatomic) NSArray *m_arrQuestViewCtrls;
@property (strong, nonatomic) NSDate *m_startTime;
@property (strong, nonatomic) CLLocationManager *m_locationManager;
@property (strong, nonatomic) NSString *m_nsLongtitude;
@property (strong, nonatomic) NSString *m_nsLatitude;
@property (strong, nonatomic) ICRStoreResult *m_result;
@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 - Life Cycle
- (instancetype)init {
self = [super init];
if (!self) {
return nil;
}
[[IBTAudioController sharedController] setup];
[self initLocaltionMgr];
return self;
}
#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;
}
__block BOOL bIsQComplete = NO;
__block BOOL bIsAComplete = NO;
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"];
NSLog(@"%@",sql);
return [db executeQuery:sql withArgumentsInArray:arrQuestIDs];
};
__weak typeof(self)weakSelf = self;
ICRDatabaseFetchResultsBlock fetchResultsBlk = ^(NSArray *fetchedObjects) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.m_arrQuestions = fetchedObjects;
bIsQComplete = YES;
if (bIsAComplete) {
if (_openQuestAction) {
_openQuestAction();
self.openQuestAction = NULL;
}
}
};
ICRDataBaseController *dbCtrl = [ICRDataBaseController sharedController];
[dbCtrl runFetchForClass:[ICRQuestion class]
fetchBlock:fetchBlk
fetchResultsBlock:fetchResultsBlk];
// StoreResult
ICRDatabaseFetchBlock srFetchBlk = ^FMResultSet *(FMDatabase *db) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
NSString * sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@ = ?", [ICRStoreResult TableName], @"uuid"];
return [db executeQuery:sql,strongSelf.m_patrolPlan.pID ];
};
ICRDatabaseFetchResultsBlock srFetchResultsBlk = ^(NSArray *fetchedObjects) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
strongSelf.m_result = [fetchedObjects firstObject];
NSMutableArray *mArr = [NSMutableArray array];
for (NSDictionary *dict in _m_result.answers) {
ICRAnswer *answer = [ICRAnswer DBObject];
[answer praseFromLocalDict:dict];
answer.bIsAnswered = YES;
[mArr safeAddObject:answer];
}
strongSelf.m_arrAnswers = [mArr count] > 0 ? mArr : nil;
bIsAComplete = YES;
if (bIsQComplete) {
if (_openQuestAction) {
_openQuestAction();
strongSelf.openQuestAction = NULL;
}
}
};
[dbCtrl runFetchForClass:[ICRStoreResult class]
fetchBlock:srFetchBlk
fetchResultsBlock:srFetchResultsBlk];
}
#pragma mark - Private Method
- (void)cleanQuestions {
self.m_imagePicker = nil;
self.m_arrQuestions = nil;
self.m_arrAnswers = nil;
self.m_arrQuestViewCtrls = nil;
self.m_baseRootViewCtrl = nil;
self.m_result = nil;
self.m_store = nil;
}
- (void)initLocaltionMgr {
if ([CLLocationManager locationServicesEnabled]) {
self.m_locationManager = [[CLLocationManager alloc] init];
_m_locationManager.delegate = self;
_m_locationManager.desiredAccuracy = kCLLocationAccuracyBest; //控制定位精度,越高耗电量越大。
_m_locationManager.distanceFilter = 100; //控制定位服务更新频率。单位是“米”
if ([_m_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_m_locationManager requestAlwaysAuthorization];
}
}
}
- (void)fetchLocation {
[_m_locationManager startUpdatingLocation];
}
#pragma mark - Public Method
- (ICRStoreResult *)getStoreResult {
if (!_m_result) {
_m_result=[[ICRStoreResult alloc]init];
_m_result.store = @{@"uuid":[[ICRUserUtil sharedInstance] currentStoreID]};
// _m_result.uuid = [[ICRUserUtil sharedInstance] mobileID];
_m_result.uuid =self.m_patrolPlan.pID;
_m_result.beginTime = [self.m_startTime httpParameterString];
_m_result.user=@{@"uuid":[[ICRUserUtil sharedInstance] userId],@"code":[[ICRUserUtil sharedInstance] userCode],@"name":[[ICRUserUtil sharedInstance] userName]};
}
ICRStoreResult *storeR = self.m_result;
storeR.endTime = [[NSDate date] httpParameterString];
storeR.uploadTime=[[NSDate date] httpParameterString] ;
storeR.longitude = _m_nsLongtitude ? : @"";
storeR.latitude = _m_nsLatitude ? : @"";
storeR.remark = @"";
NSMutableArray *mArr = [NSMutableArray array];
for (ICRQuestionBaseViewController *vc in _m_arrQuestViewCtrls) {
if (vc.m_answer.bIsAnswered) {
[mArr safeAddObject:[vc.m_answer dictForLocalSave]];
}
else {
break;
}
}
storeR.answers = mArr;
return storeR;
}
- (void)openQuestionVCFromViewControler:(UIViewController *)VC withPlan:(ICRPatrolPlan *)plan {
if (![VC isKindOfClass:[UIViewController class]] || !plan) {
return;
}
[self fetchLocation];
self.m_startTime = [NSDate date];
self.m_baseRootViewCtrl = VC;
__weak typeof(self)weakSelf = self;
void(^block)(void) = ^(void) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
NSMutableArray *arrViews = [NSMutableArray array];
NSUInteger uiCount = [_m_arrQuestions count];
NSUInteger uiIndex = 0;
NSUInteger uiAIndex = 0;
for (ICRQuestion *questE in strongSelf.m_arrQuestions) {
ICRAnswer *answerE = [strongSelf.m_arrAnswers safeObjectAtIndex:uiIndex];
ICRQuestionBaseViewController *qVC = [[ICRQuestionBaseViewController alloc] initWithQuestion:questE answer:answerE];
qVC.m_uiIndex = uiIndex;
qVC.title = [NSString stringWithFormat:@"巡店纪录(%@/%@)", @( uiIndex + 1 ), @( uiCount )];
[arrViews addObject:qVC];
uiIndex ++;
if (answerE) {
uiAIndex ++;
}
}
strongSelf.m_arrQuestViewCtrls = [arrViews count] > 0 ? arrViews : nil;
if (_m_arrQuestViewCtrls) {
IBTUINavigationController *navCtrl = [[IBTUINavigationController alloc] initWithRootViewController:[arrViews firstObject]];
NSArray *VCs = nil;
if (uiAIndex < [arrViews count]) {
VCs = [arrViews subarrayWithRange:NSMakeRange(0, uiAIndex + 1)];
}
else {
ICRQResultViewController *rVC = [[ICRQResultViewController alloc] initWithPatrolPlan:self.m_patrolPlan andStoreResult:self.m_result];
NSMutableArray *mArr = [NSMutableArray array];
[mArr addObjectsFromArray:arrViews];
[mArr addObject:rVC];
VCs = mArr;
}
navCtrl.viewControllers = VCs;
[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;
}
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *currentPosition = [locations lastObject];
self.m_nsLongtitude = [NSString stringWithFormat:@"%.3f",currentPosition.coordinate.longitude];
self.m_nsLatitude = [NSString stringWithFormat:@"%.3f",currentPosition.coordinate.latitude];
//_mylocationLongitude
[_m_locationManager stopUpdatingLocation];
}
@end