RAOrderEditViewController.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // RAOrderEditViewController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/4.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAOrderEditViewController.h"
  9. #import "RAEditInputModel.h"
  10. #import "RAEditMultInputModel.h"
  11. #import "RAEditLabelModel.h"
  12. #import "RAEditPhotoModel.h"
  13. #import <MapKit/MapKit.h>
  14. @interface RAEditSectionModel : NSObject
  15. @property (nonatomic,strong) NSArray <RAEditBaseModel *> *items;
  16. @property (nonatomic,copy) NSString *title;
  17. @end
  18. @implementation RAEditSectionModel
  19. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  20. }
  21. - (void)setItems:(NSArray<RAEditBaseModel *> *)items {
  22. NSArray *tmpItems = items;
  23. NSMutableArray *itemArr = [NSMutableArray arrayWithCapacity:items.count];
  24. for (int i = 0; i < tmpItems.count; i++) {
  25. NSDictionary *item = [tmpItems objectAtIndex:i];
  26. RAEditType type = [[item objectForKey:@"type"] intValue];
  27. switch (type) {
  28. case RAEditTypeLabel: {
  29. RAEditLabelModel *model = [RAEditLabelModel new];
  30. [model setValuesForKeysWithDictionary:item];
  31. [itemArr addObject:model];
  32. }
  33. break;
  34. case RAEditTypeInput: {
  35. RAEditInputModel *model = [RAEditInputModel new];
  36. [model setValuesForKeysWithDictionary:item];
  37. [itemArr addObject:model];
  38. }
  39. break;
  40. case RAEditTypeMultInput: {
  41. RAEditMultInputModel *model = [RAEditMultInputModel new];
  42. [model setValuesForKeysWithDictionary:item];
  43. [itemArr addObject:model];
  44. }
  45. break;
  46. case RAEditTypePhoto: {
  47. RAEditPhotoModel *model = [RAEditPhotoModel new];
  48. [model setValuesForKeysWithDictionary:item];
  49. [itemArr addObject:model];
  50. }
  51. break;
  52. default:
  53. break;
  54. }
  55. }
  56. _items = itemArr;
  57. }
  58. - (NSInteger)itemCount {
  59. return self.items.count;
  60. }
  61. - (RAEditBaseModel *)itemModelForIndex:(NSInteger)index {
  62. return [self.items objectAtIndex:index];
  63. }
  64. @end
  65. #pragma mark - View Controller
  66. @interface RAOrderEditViewController () <CLLocationManagerDelegate>
  67. @property (nonatomic,strong) IBOutlet UITableView *orderEditTableView;
  68. @property (nonatomic,strong) NSMutableArray *sectionArray;
  69. @property (nonatomic,strong) CLLocationManager *locationManager;
  70. @property (nonatomic,strong) CLLocation *currentLocation;
  71. @end
  72. @implementation RAOrderEditViewController
  73. + (instancetype)viewControllerFromStoryboard {
  74. RAOrderEditViewController *editVC = [[UIStoryboard storyboardWithName:@"Edit" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  75. return editVC;
  76. }
  77. - (void)viewDidLoad {
  78. [super viewDidLoad];
  79. // Do any additional setup after loading the view.
  80. [self configureTable];
  81. [self configureNavigationBar];
  82. [self loadData];
  83. }
  84. - (void)viewWillAppear:(BOOL)animated {
  85. [super viewWillAppear:animated];
  86. [self startLocation];
  87. [self registKeyboardListener];
  88. }
  89. - (void)viewWillDisappear:(BOOL)animated {
  90. [super viewWillDisappear:animated];
  91. [self stopLocation];
  92. [self unregistKeyboardListener];
  93. }
  94. - (void)didReceiveMemoryWarning {
  95. [super didReceiveMemoryWarning];
  96. // Dispose of any resources that can be recreated.
  97. }
  98. #pragma mark - Configure
  99. - (void)startLocation {
  100. self.locationManager = [[CLLocationManager alloc] init];
  101. self.locationManager.delegate = self;
  102. [self.locationManager requestWhenInUseAuthorization];
  103. [self.locationManager startUpdatingLocation];
  104. self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;//设置定位精度
  105. self.locationManager.distanceFilter = 10;
  106. }
  107. - (void)stopLocation {
  108. [self.locationManager stopUpdatingLocation];
  109. }
  110. - (void)configureTable {
  111. self.orderEditTableView.tableFooterView = [UIView new];
  112. self.orderEditTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  113. }
  114. - (void)configureNavigationBar {
  115. UIBarButtonItem *updateItem = [[UIBarButtonItem alloc] initWithTitle:@"Update" style:UIBarButtonItemStylePlain target:self action:@selector(updateBtnClick:)];
  116. self.navigationItem.rightBarButtonItem = updateItem;
  117. }
  118. - (void)registKeyboardListener {
  119. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  120. }
  121. - (void)unregistKeyboardListener {
  122. [[NSNotificationCenter defaultCenter] removeObserver:self];
  123. }
  124. #pragma mark Getter
  125. - (NSMutableArray *)sectionArray {
  126. if (!_sectionArray) {
  127. _sectionArray = [NSMutableArray array];
  128. }
  129. return _sectionArray;
  130. }
  131. - (NSUInteger)editSectionCount {
  132. return self.sectionArray.count;
  133. }
  134. - (NSInteger)itemCountForSection:(NSInteger)section {
  135. return [[self.sectionArray objectAtIndex:section] itemCount];
  136. }
  137. - (RAEditBaseModel *)modelForIndexPath:(NSIndexPath *)indexPath {
  138. return [[self.sectionArray objectAtIndex:indexPath.section] itemModelForIndex:indexPath.row];
  139. }
  140. - (NSString *)titleForSection:(NSInteger)section {
  141. return [[self.sectionArray objectAtIndex:section] title];
  142. }
  143. - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell {
  144. return [self.orderEditTableView indexPathForCell:cell];
  145. }
  146. #pragma mark - Data
  147. - (void)loadData {
  148. // show progress
  149. NSString *orderID = self.orderID;
  150. NSInteger actionID = self.actionID;
  151. __weak typeof(self) weakSelf = self;
  152. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  153. NSDictionary *json = [RADataProvider requestUpdateOrder:orderID driverAction:actionID];
  154. dispatch_async(dispatch_get_main_queue(), ^{
  155. // dismiss progress
  156. if (weakSelf) {
  157. __strong typeof(weakSelf) strongSelf = weakSelf;
  158. int result = [[json objectForKey:@"result"] intValue];
  159. if (result == RESULT_TRUE) {
  160. NSArray *sectionArray = [json objectForKey:@"sections"];
  161. [strongSelf.sectionArray removeAllObjects];
  162. for (int i = 0; i < sectionArray.count; i++) {
  163. NSDictionary *section = [sectionArray objectAtIndex:i];
  164. RAEditSectionModel *model = [RAEditSectionModel new];
  165. [model setValuesForKeysWithDictionary:section];
  166. [strongSelf.sectionArray addObject:model];
  167. }
  168. [strongSelf.orderEditTableView reloadData];
  169. } else {
  170. // process error
  171. }
  172. }
  173. });
  174. });
  175. }
  176. #pragma mark - Tap Action
  177. - (IBAction)tapToResignFirstResponder:(UITapGestureRecognizer *)sender {
  178. [self.view endEditing:YES];
  179. }
  180. - (void)updateBtnClick:(UIBarButtonItem *)sender {
  181. }
  182. #pragma mark - Keyboard Listener
  183. - (void)keyboardWillChangeFrame:(NSNotification *)notification {
  184. CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  185. CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
  186. CGFloat keyboardHeight = screenHeight - CGRectGetMinY(end);
  187. UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
  188. self.orderEditTableView.contentInset = insets;
  189. if (self.editingIndexPath) {
  190. [self.orderEditTableView scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  191. }
  192. }
  193. #pragma mark - LocationManager Delegate
  194. - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
  195. if (locations.count) {
  196. self.currentLocation = [locations lastObject];
  197. }
  198. }
  199. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
  200. if (status == kCLAuthorizationStatusDenied) {
  201. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"app need location,would you like to open it" preferredStyle:UIAlertControllerStyleAlert];
  202. UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  203. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  204. if ([[UIApplication sharedApplication]canOpenURL:url]) {
  205. [[UIApplication sharedApplication]openURL:url options:@{} completionHandler:nil];
  206. }
  207. [self.navigationController popViewControllerAnimated:NO];
  208. }];
  209. UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  210. }];
  211. [alertVC addAction:noAction];
  212. [alertVC addAction:okAction];
  213. [self presentViewController:alertVC animated:YES completion:nil];
  214. }
  215. }
  216. @end