InspectNotUploadViewController.m 19 KB
Newer Older
1
//
admin's avatar
admin committed
2
//  InspectNotUploadViewController.m
3 4
//  redstar
//
admin's avatar
admin committed
5
//  Created by admin on 15/11/24.
6 7 8
//  Copyright © 2015年 ZWF. All rights reserved.
//

admin's avatar
admin committed
9 10
#import "InspectNotUploadViewController.h"
#import "InspectNotUpLoadCell.h"
admin's avatar
admin committed
11
#import "CheckPicViewController.h"
admin's avatar
admin committed
12
#import "InspectDetailFooterView.h"
admin's avatar
admin committed
13 14
#import <MBProgressHUD.h>
#import "CommonFunc.h"
15
#import "HttpClient.h"
admin's avatar
admin committed
16

admin's avatar
admin committed
17 18
#import "CheckPicViewController.h"

admin's avatar
admin committed
19 20
#import "InspectTaskViewController.h"

21 22 23 24
#import "InspectAddCell.h"
#import "InspectPicAddCell.h"
#import "InpectPictureCell.h"

admin's avatar
admin committed
25 26
#define kPhotoTag 2221113

admin's avatar
admin committed
27
#define kInspectNotUploadCell @"InspectNotUploadCell"
28

29
@interface InspectNotUploadViewController () <UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, UIImagePickerControllerDelegate , UIScrollViewDelegate, UIAlertViewDelegate, UITextViewDelegate>
30 31 32

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) InspectDetailFooterView *footerView;
admin's avatar
admin committed
33
@property (nonatomic, strong) NSMutableArray *imageNameArray;
admin's avatar
admin committed
34 35
@property (nonatomic, strong) NSLayoutConstraint *tableBottom;
@property (nonatomic, strong) NSLayoutConstraint *tableTop;
36
@property (nonatomic, assign) int rowNumber;
admin's avatar
admin committed
37

38 39
@end

admin's avatar
admin committed
40 41
@implementation InspectNotUploadViewController

admin's avatar
admin committed
42 43


admin's avatar
admin committed
44 45
- (void)viewDidLoad
{
46 47
    [super viewDidLoad];
    
admin's avatar
admin committed
48
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
49
    [customLab setTextColor:[UIColor whiteColor]];
admin's avatar
admin committed
50 51
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setText:[NSString stringWithFormat:@"检查点--%@(%@/%d)", self.taskModel.category, self.taskModel.lineNo, self.questionCount]];
52 53 54
    customLab.font = [UIFont boldSystemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
55 56 57 58 59 60 61 62
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    [backBtn setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
admin's avatar
admin committed
63
    [self.tableView registerClass:[InspectNotUpLoadCell class] forCellReuseIdentifier:kInspectNotUploadCell];
64
    self.tableView.tableFooterView = self.footerView;
admin's avatar
admin committed
65 66 67
    [self.footerView.reportBtn addTarget:self action:@selector(uploadTaskInfo:) forControlEvents:UIControlEventTouchUpInside];
    
    self.imageNameArray = [NSMutableArray array];
68 69
    _rowNumber = 2;
    self.view.backgroundColor = [UIColor colorWithRed:239 / 255.0 green:239 / 255.0 blue:239 / 255.0 alpha:239 / 255.0];
admin's avatar
admin committed
70

71 72 73
}


74
#pragma mark - private Methods
admin's avatar
admin committed
75 76 77

- (void)uploadTaskInfo:(UIButton *)sender
{
admin's avatar
admin committed
78 79 80 81 82 83 84
    int impose = _taskModel.attachmentNumber;
    if ((int)_imageNameArray.count < impose) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"要求图片%d张才能上传!", impose] delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        return;
    }
    
85 86 87
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"你确定要上传检查点的相关信息吗?"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag = 533292;
    [alert show];
admin's avatar
admin committed
88 89
}

90 91 92 93 94
// 返回上一页面
- (void)doBack:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
admin's avatar
admin committed
95

admin's avatar
admin committed
96 97 98 99 100 101 102 103 104 105
- (void)showPicture:(UIButton *)sender
{
    CheckPicViewController *checkVC = [[CheckPicViewController alloc] init];
    NSString *imageName = _imageNameArray[sender.tag - kPhotoTag];
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
    checkVC.checkImage = image;
    [self.navigationController pushViewController:checkVC animated:YES];
}

106 107

- (void)createImagePicker:(UIButton *)sender
108
{
109 110
    if (sender.tag == 2201) {
        _rowNumber++;
admin's avatar
admin committed
111 112 113
    }
    
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
114
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"找不到相机" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
admin's avatar
admin committed
115 116 117 118 119 120 121 122 123
        [alert show];
    } else {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        picker.videoQuality = UIImagePickerControllerQualityTypeLow;
        [self presentViewController:picker animated:YES completion:nil];
    }
124 125
}

126 127 128 129 130 131




#pragma mark - UIImagePickerController 代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
admin's avatar
admin committed
132
{
133 134 135 136 137 138 139 140
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self saveImage:image];

    // 退出图片选择控制器
    [picker dismissViewControllerAnimated:YES completion:^{
        [self.tableView reloadData];
    }];

admin's avatar
admin committed
141 142
}

admin's avatar
admin committed
143 144 145 146
#pragma mark - 保存图片至沙盒
- (void)saveImage:(UIImage *)currentImage {
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    
147
    NSString *imageName = [NSString stringWithFormat:@"imageName%d", (int)_imageNameArray.count];
admin's avatar
admin committed
148
    [_imageNameArray addObject:imageName];
149

admin's avatar
admin committed
150 151 152 153 154 155 156
    
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}

157 158 159
#pragma mark - TableView Delegate/DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
160
    return _rowNumber;
161 162 163 164 165
}

// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
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
    if (indexPath.row == 0) {
        InspectNotUpLoadCell *cell=[tableView dequeueReusableCellWithIdentifier:kInspectNotUploadCell];
        if (!cell) {
            cell = [[InspectNotUpLoadCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kInspectNotUploadCell];
        }
        cell.titleLabel.text = [NSString stringWithFormat:@"%@", _taskModel.title];
        cell.titleDetailLabel.text = [NSString stringWithFormat:@"要求图片%d张", _taskModel.attachmentNumber];
        cell.introLabel.text = @"参考说明";
        cell.introDetailLabel.text = [NSString stringWithFormat:@"%@", _taskModel.reference];
        cell.pictureLabel.text = @"现场照片";
        
        NSString *refrer = [NSString stringWithFormat:@"%@", _taskModel.reference];
        if (refrer == nil || refrer == NULL || [refrer isEqual:[NSNull null]] || refrer == Nil || [refrer isEqualToString:@"(null)"]) {
            cell.introDetailLabel.text = @"";
        } else {
            NSString *str = [NSString stringWithFormat:@"<style> html{ font-size: 15px; color: #444444 } </style>%@", _taskModel.reference];
            NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSForegroundColorAttributeName:kLightBlack,NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} documentAttributes:nil error:nil];
            
            cell.introDetailLabel.attributedText = attrStr;
        }
        return cell;
    } else if (indexPath.row == _rowNumber - 1) {
        if ((_imageNameArray.count + 1) % 2 == 0) {
            NSString *identifier = [NSString stringWithFormat:@"InspectaddPicCell"];
            InspectPicAddCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[InspectPicAddCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            }
            NSString *imageName = _imageNameArray[_imageNameArray.count - 1];
            NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
            UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
admin's avatar
admin committed
197 198 199
            UIImage *image00 = [self cutImage:image];

            [cell.photoButton1 setImage:image00 forState:UIControlStateNormal];
admin's avatar
admin committed
200 201 202
            [cell.photoButton1 addTarget:self action:@selector(showPicture:) forControlEvents:UIControlEventTouchUpInside];
            cell.photoButton1.tag = kPhotoTag + _imageNameArray.count - 1;

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
            
            cell.photoButton2.tag = 2201;
            [cell.photoButton2 addTarget:self action:@selector(createImagePicker:) forControlEvents:UIControlEventTouchUpInside];
            
            return cell;
        } else {
            
            NSString *identifier = [NSString stringWithFormat:@"InspectaddCell"];
            InspectAddCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
            if (!cell) {
                cell = [[InspectAddCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            }
            cell.photoButton.tag = 2200;
            [cell.photoButton addTarget:self action:@selector(createImagePicker:) forControlEvents:UIControlEventTouchUpInside];
            
            return cell;
        }
        
        
admin's avatar
admin committed
222 223
    } else {
        
224 225 226 227 228 229 230 231
        NSString *identifier = [NSString stringWithFormat:@"InspectPictureCell"];
        InpectPictureCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[InpectPictureCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        }
        NSString *imageName0 = _imageNameArray[(indexPath.row - 1) * 2];
        NSString *fullPath0 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName0];
        UIImage *image0 = [UIImage imageWithContentsOfFile:fullPath0];
admin's avatar
admin committed
232 233
        UIImage *image00 = [self cutImage:image0];
        [cell.photoButton1 setImage:image00 forState:UIControlStateNormal];
234 235 236 237
        
        NSString *imageName1 = _imageNameArray[(indexPath.row - 1) * 2 + 1];
        NSString *fullPath1 = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName1];
        UIImage *image1 = [UIImage imageWithContentsOfFile:fullPath1];
admin's avatar
admin committed
238 239 240
        UIImage *image11 = [self cutImage:image1];

        [cell.photoButton2 setImage:image11 forState:UIControlStateNormal];
admin's avatar
admin committed
241 242 243 244 245 246 247 248
        
        [cell.photoButton1 addTarget:self action:@selector(showPicture:) forControlEvents:UIControlEventTouchUpInside];
        cell.photoButton1.tag = kPhotoTag + (indexPath.row - 1) * 2;

        [cell.photoButton2 addTarget:self action:@selector(showPicture:) forControlEvents:UIControlEventTouchUpInside];
        cell.photoButton2.tag = kPhotoTag + (indexPath.row - 1) * 2 + 1;


249
        return cell;
admin's avatar
admin committed
250
    }
251 252
}

admin's avatar
admin committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
//裁剪图片
- (UIImage *)cutImage:(UIImage*)image
{
    //压缩图片
    CGSize newSize;
    CGImageRef imageRef = nil;
    
    if ((image.size.width / image.size.height) < (((kScreenWidth - 20 * 3) / 2) / 90)) {
        newSize.width = image.size.width;
        newSize.height = image.size.width * 90 / ((kScreenWidth - 20 * 3) / 2);
        
        imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, fabs(image.size.height - newSize.height) / 2, newSize.width, newSize.height));
        
    } else {
        newSize.height = image.size.height;
        newSize.width = image.size.height * ((kScreenWidth - 20 * 3) / 2) / 90;
        
        imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(fabs(image.size.width - newSize.width) / 2, 0, newSize.width, newSize.height));
        
    }
    
    return [UIImage imageWithCGImage:imageRef];
}

277 278 279 280 281 282 283 284 285 286 287 288

// section高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

admin's avatar
admin committed
289 290 291
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    if (alertView.tag == 39429234) {
        if (buttonIndex == 0) {
            [self.navigationController popViewControllerAnimated:YES];
            NSNotificationCenter *notification = [NSNotificationCenter defaultCenter];
            [notification postNotificationName:kRefreshInspectPointNotification object:self];
        }
    } else if (alertView.tag == 533292) {
        if (buttonIndex == 0) {
            return;
        } else {
            NSMutableArray *imageArray = [NSMutableArray array];
            for (int i = 0; i < _imageNameArray.count; i++) {
                NSString *imageName = _imageNameArray[i];
                NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
                UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
                NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
                NSString *imageBase64 = [imageData base64EncodedStringWithOptions:0];
                NSMutableDictionary *dict = [NSMutableDictionary dictionary];
                [dict setObject:imageBase64 forKey:@"fileContent"];
                [dict setObject:imageName forKey:@"fileName"];
                [dict setObject:@(i) forKey:@"index"];
                [imageArray addObject:dict];
            }
            
            NSDate *date = [NSDate date];
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"YYYY-MM-dd+hh:mm:ss"];
            NSString *dateString = [dateFormatter stringFromDate:date];
            
            NSString *operId = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_code"];
            NSString *operName = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_name"];
            
            NSString *url = [NSString stringWithFormat:@"%@%@%@?time=%@&operId=%@&operName=%@",kRedStarURL, kUpLoadInspectResult, self.praiseUuid, dateString, operId, operName];
            url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
            NSLog(@"上传检查点url = %@", url);
            HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
            [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            NSDictionary *parameters = @{@"questionUuid":self.questionUuid,
                                         @"storeUuid":self.store_uuid,
                                         @"remark":self.footerView.contentTextView.text,
                                         @"attachments":imageArray
                                         };
            [httpClient uploadInspectResultWithParameters:parameters completion:^(id response, NSError *error) {
                
                NSLog(@"上传检查点  respose  =%@  error = %@", response, error);
                if (response[@"success"]) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"检查点上报成功!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                    alert.delegate = self;
                    alert.tag = 39429234;
                    [alert show];
                }
                [MBProgressHUD hideHUDForView:self.view animated:YES];
                
            }];
        }
admin's avatar
admin committed
348 349 350 351
    }
}


352 353 354 355 356 357 358 359 360 361

#pragma mark - lazy loading
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
admin's avatar
admin committed
362
        _tableView.estimatedRowHeight = 300.0;
363
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
364 365
        [self.view addSubview:_tableView];
        
admin's avatar
admin committed
366 367
        self.tableTop = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        [self.view addConstraint:_tableTop];
368 369 370 371 372 373 374
        
        NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self.view addConstraint:tableLeft];
        
        NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.view addConstraint:tableRight];
        
admin's avatar
admin committed
375 376
        self.tableBottom = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self.view addConstraint:_tableBottom];
377 378 379 380 381 382 383
    }
    return _tableView;
}

- (InspectDetailFooterView *)footerView
{
    if (!_footerView) {
384
        _footerView = [[InspectDetailFooterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 180)];
admin's avatar
admin committed
385
        _footerView.contentTextView.delegate = self;
386 387
        _footerView.backgroundColor = [UIColor colorWithRed:239 / 255.0 green:239 / 255.0 blue:239 / 255.0 alpha:239 / 255.0];

388 389 390 391
    }
    return _footerView;
}

admin's avatar
admin committed
392

admin's avatar
admin committed
393
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
admin's avatar
admin committed
394 395 396
    if (![_footerView.contentTextView isExclusiveTouch]) {
        [self.footerView.contentTextView resignFirstResponder];
    }
admin's avatar
admin committed
397
}
admin's avatar
admin committed
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    self.tableTop.constant = -216;
    self.tableBottom.constant = -216;
    [UIView animateWithDuration:0.3 animations:^{
        [self.tableView layoutIfNeeded];
    }];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    self.tableTop.constant = 0;
    self.tableBottom.constant = 0;
    [UIView animateWithDuration:0.3 animations:^{
        [self.tableView layoutIfNeeded];
    }];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (![text isEqualToString:@""]) {
        self.footerView.placeholderLabel2.hidden = YES;
    }
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1) {
        self.footerView.placeholderLabel2.hidden = NO;
    }
    
    if ([text isEqualToString:@"\n"]) {
        [self.footerView.contentTextView resignFirstResponder];
        return NO;
    }
    
430 431 432 433 434 435
    if (range.location>=200) {
        return  NO;
    }
    else {
        return YES;
    }
admin's avatar
admin committed
436 437 438 439
    
    return YES;
}

admin's avatar
admin committed
440 441 442 443 444 445 446 447 448 449
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

450
@end