RAOrderEditViewController.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. @interface RAEditSectionModel : NSObject
  14. @property (nonatomic,strong) NSArray <RAEditBaseModel *> *items;
  15. @property (nonatomic,copy) NSString *title;
  16. @end
  17. @implementation RAEditSectionModel
  18. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  19. }
  20. - (void)setItems:(NSArray<RAEditBaseModel *> *)items {
  21. NSArray *tmpItems = items;
  22. NSMutableArray *itemArr = [NSMutableArray arrayWithCapacity:items.count];
  23. for (int i = 0; i < tmpItems.count; i++) {
  24. NSDictionary *item = [tmpItems objectAtIndex:i];
  25. RAEditType type = [[item objectForKey:@"type"] intValue];
  26. switch (type) {
  27. case RAEditTypeLabel: {
  28. RAEditLabelModel *model = [RAEditLabelModel new];
  29. [model setValuesForKeysWithDictionary:item];
  30. [itemArr addObject:model];
  31. }
  32. break;
  33. case RAEditTypeInput: {
  34. RAEditInputModel *model = [RAEditInputModel new];
  35. [model setValuesForKeysWithDictionary:item];
  36. [itemArr addObject:model];
  37. }
  38. break;
  39. case RAEditTypeMultInput: {
  40. RAEditMultInputModel *model = [RAEditMultInputModel new];
  41. [model setValuesForKeysWithDictionary:item];
  42. [itemArr addObject:model];
  43. }
  44. break;
  45. case RAEditTypePhoto: {
  46. RAEditPhotoModel *model = [RAEditPhotoModel new];
  47. [model setValuesForKeysWithDictionary:item];
  48. [itemArr addObject:model];
  49. }
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. _items = itemArr;
  56. }
  57. - (NSInteger)itemCount {
  58. return self.items.count;
  59. }
  60. - (RAEditBaseModel *)itemModelForIndex:(NSInteger)index {
  61. return [self.items objectAtIndex:index];
  62. }
  63. @end
  64. #pragma mark - View Controller
  65. @interface RAOrderEditViewController ()
  66. @property (nonatomic,strong) IBOutlet UITableView *orderEditTableView;
  67. @property (nonatomic,strong) NSMutableArray *sectionArray;
  68. @end
  69. @implementation RAOrderEditViewController
  70. + (instancetype)viewControllerFromStoryboard {
  71. RAOrderEditViewController *editVC = [[UIStoryboard storyboardWithName:@"Edit" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  72. return editVC;
  73. }
  74. - (void)viewDidLoad {
  75. [super viewDidLoad];
  76. // Do any additional setup after loading the view.
  77. [self configureTable];
  78. [self loadData];
  79. }
  80. - (void)viewWillAppear:(BOOL)animated {
  81. [super viewWillAppear:animated];
  82. [self registKeyboardListener];
  83. }
  84. - (void)viewWillDisappear:(BOOL)animated {
  85. [super viewWillDisappear:animated];
  86. [self unregistKeyboardListener];
  87. }
  88. - (void)didReceiveMemoryWarning {
  89. [super didReceiveMemoryWarning];
  90. // Dispose of any resources that can be recreated.
  91. }
  92. - (void)configureTable {
  93. self.orderEditTableView.tableFooterView = [UIView new];
  94. self.orderEditTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  95. }
  96. - (void)registKeyboardListener {
  97. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  98. }
  99. - (void)unregistKeyboardListener {
  100. [[NSNotificationCenter defaultCenter] removeObserver:self];
  101. }
  102. #pragma mark Getter
  103. - (NSMutableArray *)sectionArray {
  104. if (!_sectionArray) {
  105. _sectionArray = [NSMutableArray array];
  106. }
  107. return _sectionArray;
  108. }
  109. - (NSUInteger)editSectionCount {
  110. return self.sectionArray.count;
  111. }
  112. - (NSInteger)itemCountForSection:(NSInteger)section {
  113. return [[self.sectionArray objectAtIndex:section] itemCount];
  114. }
  115. - (RAEditBaseModel *)modelForIndexPath:(NSIndexPath *)indexPath {
  116. return [[self.sectionArray objectAtIndex:indexPath.section] itemModelForIndex:indexPath.row];
  117. }
  118. - (NSString *)titleForSection:(NSInteger)section {
  119. return [[self.sectionArray objectAtIndex:section] title];
  120. }
  121. - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell {
  122. return [self.orderEditTableView indexPathForCell:cell];
  123. }
  124. #pragma mark - Data
  125. - (void)loadData {
  126. // show progress
  127. NSString *orderID = self.orderID;
  128. NSInteger actionID = self.actionID;
  129. __weak typeof(self) weakSelf = self;
  130. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  131. NSDictionary *json = [RADataProvider requestUpdateOrder:orderID driverAction:actionID];
  132. dispatch_async(dispatch_get_main_queue(), ^{
  133. // dismiss progress
  134. if (weakSelf) {
  135. __strong typeof(weakSelf) strongSelf = weakSelf;
  136. int result = [[json objectForKey:@"result"] intValue];
  137. if (result == RESULT_TRUE) {
  138. NSArray *sectionArray = [json objectForKey:@"sections"];
  139. [strongSelf.sectionArray removeAllObjects];
  140. for (int i = 0; i < sectionArray.count; i++) {
  141. NSDictionary *section = [sectionArray objectAtIndex:i];
  142. RAEditSectionModel *model = [RAEditSectionModel new];
  143. [model setValuesForKeysWithDictionary:section];
  144. [strongSelf.sectionArray addObject:model];
  145. }
  146. [strongSelf.orderEditTableView reloadData];
  147. } else {
  148. // process error
  149. }
  150. }
  151. });
  152. });
  153. }
  154. #pragma mark - Tap Action
  155. - (IBAction)tapToResignFirstResponder:(UITapGestureRecognizer *)sender {
  156. [self.view endEditing:YES];
  157. }
  158. #pragma mark - Keyboard Listener
  159. - (void)keyboardWillChangeFrame:(NSNotification *)notification {
  160. CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  161. CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
  162. CGFloat keyboardHeight = screenHeight - CGRectGetMinY(end);
  163. UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
  164. self.orderEditTableView.contentInset = insets;
  165. if (self.editingIndexPath) {
  166. [self.orderEditTableView scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  167. }
  168. }
  169. @end