| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- //
- // RAOrderEditViewController.m
- // Apex And Drivers
- //
- // Created by Jack on 2018/6/4.
- // Copyright © 2018年 USAI. All rights reserved.
- //
- #import "RAOrderEditViewController.h"
- #import "RAEditInputModel.h"
- #import "RAEditMultInputModel.h"
- #import "RAEditLabelModel.h"
- #import "RAEditPhotoModel.h"
- #import "RAProgressHUD.h"
- #import "ZipArchive.h"
- #import "AppDelegate.h"
- #import <CoreLocation/CoreLocation.h>
- @interface RAEditSectionModel : NSObject
- @property (nonatomic,strong) NSArray <RAEditBaseModel *> *items;
- @property (nonatomic,copy) NSString *title;
- @end
- @implementation RAEditSectionModel
- - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
-
- }
- - (void)setItems:(NSArray<RAEditBaseModel *> *)items {
-
- NSArray *tmpItems = items;
- NSMutableArray *itemArr = [NSMutableArray arrayWithCapacity:items.count];
- for (int i = 0; i < tmpItems.count; i++) {
- NSDictionary *item = [tmpItems objectAtIndex:i];
- RAEditType type = [[item objectForKey:@"type"] intValue];
- switch (type) {
- case RAEditTypeLabel: {
-
- RAEditLabelModel *model = [RAEditLabelModel new];
- [model setValuesForKeysWithDictionary:item];
- [itemArr addObject:model];
- }
- break;
- case RAEditTypeInput: {
-
- RAEditInputModel *model = [RAEditInputModel new];
- [model setValuesForKeysWithDictionary:item];
- [itemArr addObject:model];
- }
- break;
- case RAEditTypeMultInput: {
-
- RAEditMultInputModel *model = [RAEditMultInputModel new];
- [model setValuesForKeysWithDictionary:item];
- [itemArr addObject:model];
- }
- break;
- case RAEditTypePhoto: {
-
- RAEditPhotoModel *model = [RAEditPhotoModel new];
- [model setValuesForKeysWithDictionary:item];
- [itemArr addObject:model];
- }
- break;
-
- default:
- break;
- }
- }
- _items = itemArr;
- }
- - (NSInteger)itemCount {
- return self.items.count;
- }
- - (RAEditBaseModel *)itemModelForIndex:(NSInteger)index {
- return [self.items objectAtIndex:index];
- }
- @end
- #pragma mark - View Controller
- @interface RAOrderEditViewController () <CLLocationManagerDelegate>
- @property (nonatomic,strong) IBOutlet UITableView *orderEditTableView;
- @property (nonatomic,strong) NSMutableArray *sectionArray;
- @property (nonatomic,strong) CLLocationManager *locationManager;
- @property (nonatomic,strong) CLLocation *currentLocation;
- @end
- @implementation RAOrderEditViewController
- + (instancetype)viewControllerFromStoryboard {
- RAOrderEditViewController *editVC = [[UIStoryboard storyboardWithName:@"Edit" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
- return editVC;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self configureTable];
- [self configureNavigationBar];
- [self loadData];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [self startLocation];
- [self registKeyboardListener];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- [self stopLocation];
- [self unregistKeyboardListener];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Configure
- - (void)startLocation {
- self.locationManager = [[CLLocationManager alloc] init];
- self.locationManager.delegate = self;
- self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//设置定位精度
- [self.locationManager requestWhenInUseAuthorization];
- [self.locationManager startUpdatingLocation];
- }
- - (void)stopLocation {
- [self.locationManager stopUpdatingLocation];
- }
- - (void)configureTable {
-
- self.orderEditTableView.tableFooterView = [UIView new];
- self.orderEditTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
- }
- - (void)configureNavigationBar {
-
- UIBarButtonItem *updateItem = [[UIBarButtonItem alloc] initWithTitle:@"Update" style:UIBarButtonItemStylePlain target:self action:@selector(updateBtnClick:)];
- self.navigationItem.rightBarButtonItem = updateItem;
- }
- - (void)registKeyboardListener {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
- }
- - (void)unregistKeyboardListener {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark Getter
- - (NSMutableArray *)sectionArray {
- if (!_sectionArray) {
- _sectionArray = [NSMutableArray array];
- }
- return _sectionArray;
- }
- - (NSUInteger)editSectionCount {
-
- return self.sectionArray.count;
- }
- - (NSInteger)itemCountForSection:(NSInteger)section {
- return [[self.sectionArray objectAtIndex:section] itemCount];
- }
- - (RAEditBaseModel *)modelForIndexPath:(NSIndexPath *)indexPath {
- return [[self.sectionArray objectAtIndex:indexPath.section] itemModelForIndex:indexPath.row];
- }
- - (NSString *)titleForSection:(NSInteger)section {
- return [[self.sectionArray objectAtIndex:section] title];
- }
- - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell {
- return [self.orderEditTableView indexPathForCell:cell];
- }
- #pragma mark - Data
- - (void)loadData {
-
- // show progress
- RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
-
- NSString *orderID = self.orderID;
- NSInteger actionID = self.actionID;
- __weak typeof(self) weakSelf = self;
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
-
- NSDictionary *json = [RADataProvider requestUpdateOrder:orderID driverAction:actionID];
-
- dispatch_async(dispatch_get_main_queue(), ^{
-
- // dismiss progress
- [hud dismiss];
-
- if (weakSelf) {
- __strong typeof(weakSelf) strongSelf = weakSelf;
-
- int result = [[json objectForKey:@"result"] intValue];
- if (result == RESULT_TRUE) {
-
- NSArray *sectionArray = [json objectForKey:@"sections"];
- [strongSelf.sectionArray removeAllObjects];
- for (int i = 0; i < sectionArray.count; i++) {
- NSDictionary *section = [sectionArray objectAtIndex:i];
- RAEditSectionModel *model = [RAEditSectionModel new];
- [model setValuesForKeysWithDictionary:section];
- [strongSelf.sectionArray addObject:model];
- }
-
- [strongSelf.orderEditTableView reloadData];
-
- } else {
- // process error
-
- }
- }
-
- });
-
- });
- }
- #pragma mark - Tap Action
- - (IBAction)tapToResignFirstResponder:(UITapGestureRecognizer *)sender {
-
- [self.view endEditing:YES];
- }
- - (void)updateBtnClick:(UIBarButtonItem *)sender {
-
- RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
-
- NSMutableDictionary *task = [self preparePackage];
-
- AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- [appDelegate.uploadManager addTask:task];
-
- __weak typeof(self) weakSelf = self;
- [hud dismiss:^{
-
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Message" message:@"you can find the update progress in upload list" preferredStyle:UIAlertControllerStyleAlert];
-
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationReloadHome object:nil];
- [weakSelf.navigationController popToRootViewControllerAnimated:YES];
- }];
-
- [alertVC addAction:okAction];
-
- [weakSelf presentViewController:alertVC animated:YES completion:nil];
- }];
-
-
-
- }
- #pragma mark - Keyboard Listener
- - (void)keyboardWillChangeFrame:(NSNotification *)notification {
-
- CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
- CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
- CGFloat keyboardHeight = screenHeight - CGRectGetMinY(end);
-
- UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
- self.orderEditTableView.contentInset = insets;
-
- if (self.editingIndexPath) {
- [self.orderEditTableView scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
- }
- }
- #pragma mark - LocationManager Delegate
- - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
- if (locations.count) {
- self.currentLocation = [locations lastObject];
- }
- }
- - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
-
- if (status == kCLAuthorizationStatusDenied) {
-
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"app need location,would you like to open it" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
- if ([[UIApplication sharedApplication]canOpenURL:url]) {
- [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
- }
- [self.navigationController popViewControllerAnimated:NO];
- }];
- UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- }];
-
- [alertVC addAction:noAction];
- [alertVC addAction:okAction];
-
- [self presentViewController:alertVC animated:YES completion:nil];
- }
- }
- #pragma mark - Package Update Data
- - (NSString *)createPhotoDir {
-
- NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
- NSString *photoDir = [cacheDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%ld_%@",self.orderID,self.actionID,[NSUUID UUID].UUIDString]];
-
- NSError *error;
- // BOOL dirExist = YES;
- // for (int i = 0; i < INT_MAX; i++) {
- // if (i != 0) {
- // photoDir = [photoDir stringByAppendingString:[NSString stringWithFormat:@"(%d)",i]];
- // }
- // if (![[NSFileManager defaultManager] fileExistsAtPath:photoDir]) {
- // dirExist = NO;
- // break;
- // }
- // }
- [[NSFileManager defaultManager] createDirectoryAtPath:photoDir withIntermediateDirectories:YES attributes:nil error:&error];
- if (error) {
- NSLog(@"create dir %@ failed %@",photoDir,error.localizedDescription);
- return nil;
- }
-
- return photoDir;
- }
- - (NSMutableDictionary *)preparePackage {
-
- NSMutableDictionary *params = [NSMutableDictionary dictionary];
- NSString *photoDir = [self createPhotoDir];
-
- [params setObject:self.orderID forKey:@"orderID"];
- [params setObject:@(self.actionID) forKey:@"actionID"];
-
- NSString* encryptu=[AESCrypt AES128Encrypt:[RASingleton sharedInstance].user key:@"usai"];
- NSString* encryptp=[AESCrypt AES128Encrypt:[RASingleton sharedInstance].password key:@"usai"];
- [params setObject:encryptu forKey:@"user"];
- [params setObject:encryptp forKey:@"password"];
- [params setObject:@"iOS" forKey:@"platform"];
-
- if (self.currentLocation) {
- [params setObject:[NSString stringWithFormat:@"%f,%f",self.currentLocation.coordinate.latitude,self.currentLocation.coordinate.longitude] forKey:@"location"];
- }
-
- NSInteger photoCount = 0;
- for (RAEditSectionModel *section in self.sectionArray) {
- for (int i = 0; i < [section itemCount]; i++) {
- RAEditBaseModel *model = [section itemModelForIndex:i];
- switch (model.type) {
- case RAEditTypeLabel: {
-
- }
- break;
- case RAEditTypeInput: {
-
- RAEditInputModel *inputModel = (RAEditInputModel *)model;
- if (inputModel.key && inputModel.value.length > 0) {
- [params setObject:inputModel.value forKey:inputModel.key];
- }
- }
- break;
- case RAEditTypeMultInput: {
-
- RAEditMultInputModel *multInputModel = (RAEditMultInputModel *)model;
- if (multInputModel.key && multInputModel.value.length > 0) {
- [params setObject:multInputModel.value forKey:multInputModel.key];
- }
- }
- break;
- case RAEditTypePhoto: {
- if (photoDir) {
- RAEditPhotoModel *photoModel = (RAEditPhotoModel *)model;
- if (photoModel.photo) {
- NSString *photoPath = [photoDir stringByAppendingPathComponent:photoModel.photoName];
- NSData *imgData = UIImageJPEGRepresentation(photoModel.photo, 1.0f);
- if (imgData) {
- [imgData writeToFile:photoPath atomically:NO];
- [params setObject:photoModel.photoName forKey:photoModel.key];
- photoCount++;
- }
- }
-
- }
-
-
- }
- break;
-
- default:
- break;
- }
- }
- }
-
- NSMutableDictionary *task = [@{
- @"order" : self.orderID,
- @"action" : self.actionTitle,
- @"url" : URL_HOST,
- @"noFile" : @(YES)
- } mutableCopy];
-
- if (photoCount > 0) {
-
- // 压缩文件
- NSString *zipPath = [photoDir stringByAppendingPathExtension:@".zip"];
- ZipArchive *zip = [[ZipArchive alloc] init];
- [zip CreateZipFile2:zipPath];
- NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:photoDir];
- for (NSString *subPath in subPaths) {
- NSString *fullPath = [photoDir stringByAppendingPathComponent:subPath];
- BOOL isDir;
- if([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir]) {
- if (!isDir) {
- [zip addFileToZip:fullPath newname:subPath];
- }
- }
- }
- [zip CloseZipFile2];
-
- [[NSFileManager defaultManager] removeItemAtPath:photoDir error:nil];
- NSString *md5 = [RAUtils md5WithFile:zipPath];
-
- [params setObject:md5 forKey:@"md5"];
- [task setObject:@"file" forKey:zipPath.lastPathComponent];
- }
-
- [task setObject:params forKey:@"params"];
-
- return task;
- }
- @end
|