| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- //
- // 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"
- @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 ()
- @property (nonatomic,strong) IBOutlet UITableView *orderEditTableView;
- @property (nonatomic,strong) NSMutableArray *sectionArray;
- @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 loadData];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [self registKeyboardListener];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- [self unregistKeyboardListener];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)configureTable {
-
- self.orderEditTableView.tableFooterView = [UIView new];
- self.orderEditTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
- }
- - (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
-
- 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
-
- 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];
-
- }
- #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];
- }
- }
- @end
|