RAHomeViewController.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // RAHomeViewController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/1.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAHomeViewController.h"
  9. #import "RAHomeOrderModel.h"
  10. #import "RAOrderDetailViewController.h"
  11. #import "RAProgressHUD.h"
  12. #import "ApexDriverUploadListVC.h"
  13. @interface RAHomeSectionModel : NSObject
  14. @property (nonatomic,assign) RAOrderStatus type;
  15. @property (nonatomic,strong) NSArray <RAHomeOrderModel *> *orders;
  16. @property (nonatomic,copy) NSString *title;
  17. @property (nonatomic,readonly) NSInteger ordersCount;
  18. @end
  19. @implementation RAHomeSectionModel
  20. - (void)setOrders:(NSArray *)orders {
  21. NSMutableArray *orderArr = [NSMutableArray array];
  22. for (int i = 0; i < orders.count; i++) {
  23. NSDictionary *order = [orders objectAtIndex:i];
  24. RAHomeOrderModel *orderModel = [RAHomeOrderModel new];
  25. [orderModel setValuesForKeysWithDictionary:order];
  26. [orderArr addObject:orderModel];
  27. }
  28. _orders = orderArr;
  29. }
  30. - (RAHomeOrderModel *)orderModelForIndex:(NSInteger)index {
  31. return [self.orders objectAtIndex:index];
  32. }
  33. - (NSInteger)ordersCount {
  34. return self.orders.count;
  35. }
  36. - (NSInteger)orderModelIndexForID:(NSString *)orderID {
  37. __block NSInteger curIndex = -1;;
  38. [self.orders enumerateObjectsUsingBlock:^(RAHomeOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  39. if ([obj.orderID isEqualToString:orderID]) {
  40. curIndex = idx;
  41. return ;
  42. }
  43. }];
  44. return curIndex;
  45. }
  46. @end
  47. #pragma mark - View Controller
  48. @interface RAHomeViewController ()
  49. @property (nonatomic,strong) NSMutableArray <RAHomeSectionModel *> *sectionArray;
  50. @property (nonatomic,strong) NSIndexPath *currentIndexPath;
  51. @end
  52. @implementation RAHomeViewController
  53. + (instancetype)viewControllerFromStoryboard {
  54. RAHomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  55. return homeVC;
  56. }
  57. - (void)viewDidLoad {
  58. [super viewDidLoad];
  59. // Do any additional setup after loading the view.
  60. [self configureNavigationBar];
  61. [self configureTable];
  62. [self registNotification];
  63. [self loadData];
  64. }
  65. - (void)viewWillAppear:(BOOL)animated {
  66. [super viewWillAppear:animated];
  67. }
  68. - (void)dealloc {
  69. [[NSNotificationCenter defaultCenter] removeObserver:self];
  70. }
  71. - (void)didReceiveMemoryWarning {
  72. [super didReceiveMemoryWarning];
  73. // Dispose of any resources that can be recreated.
  74. }
  75. #pragma mark - Configure
  76. - (void)configureTable {
  77. self.homeOrderTableView.tableFooterView = [UIView new];
  78. self.homeOrderTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  79. }
  80. - (void)configureNavigationBar {
  81. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithTitle:@"Upload List" style:UIBarButtonItemStylePlain target:self action:@selector(uploadListItemClick:)];
  82. self.navigationItem.rightBarButtonItem = uploadListItem;
  83. }
  84. - (void)registNotification {
  85. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationNewOrder object:nil];
  86. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationReloadHome object:nil];
  87. }
  88. #pragma mark - Action
  89. - (void)uploadListItemClick:(UIBarButtonItem *)sender {
  90. ApexDriverUploadListVC *vc = [ApexDriverUploadListVC viewControllerFromStoryboard];
  91. [self.navigationController pushViewController:vc animated:YES];
  92. }
  93. #pragma mark - Getter
  94. - (NSMutableArray *)sectionArray {
  95. if (!_sectionArray) {
  96. _sectionArray = [NSMutableArray array];
  97. }
  98. return _sectionArray;
  99. }
  100. - (NSUInteger)orderSectionCount {
  101. return self.sectionArray.count;
  102. }
  103. - (NSUInteger)orderCountForSection:(NSInteger)section {
  104. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  105. return [sectionModel ordersCount];
  106. }
  107. - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath {
  108. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section];
  109. return [sectionModel orderModelForIndex:indexPath.row];
  110. }
  111. - (NSString *)titleForSection:(NSInteger)section {
  112. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  113. return sectionModel.title;
  114. }
  115. #pragma mark - Data
  116. - (void)loadData {
  117. // show progress
  118. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  119. __weak typeof(self) weakSelf = self;
  120. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  121. NSDictionary *json = [RADataProvider requestOrderList];
  122. NSLog(@"home load data");
  123. dispatch_async(dispatch_get_main_queue(), ^{
  124. // dismiss progress
  125. [hud dismiss];
  126. if (weakSelf) {
  127. __strong typeof(weakSelf) strongSelf = weakSelf;
  128. int result = [[json objectForKey:@"result"] intValue];
  129. if (result == RESULT_TRUE) {
  130. NSArray *sectionArray = [json objectForKey:@"sections"];
  131. [strongSelf.sectionArray removeAllObjects];
  132. for (int i = 0; i < sectionArray.count; i++) {
  133. NSDictionary *section = [sectionArray objectAtIndex:i];
  134. RAHomeSectionModel *sectionModel = [RAHomeSectionModel new];
  135. [sectionModel setValuesForKeysWithDictionary:section];
  136. [strongSelf.sectionArray addObject:sectionModel];
  137. if (self.currentOrderID.length > 0) {
  138. NSInteger idx = [sectionModel orderModelIndexForID:self.currentOrderID];
  139. if (idx > -1) {
  140. self.currentIndexPath = [NSIndexPath indexPathForRow:idx inSection:i];
  141. }
  142. }
  143. }
  144. [strongSelf.homeOrderTableView reloadData];
  145. } else {
  146. // process error
  147. }
  148. }
  149. });
  150. });
  151. }
  152. #pragma mark - Controller Action
  153. - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
  154. RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
  155. detailVC.orderID = model.orderID;
  156. [self.navigationController pushViewController:detailVC animated:YES];
  157. }
  158. #pragma mark - Nofitication Selector
  159. - (void)receiveNewOrderNotification:(NSNotification *)notification {
  160. [self loadData];
  161. }
  162. - (void)receiveReloadNotification:(NSNotification *)notification {
  163. [self loadData];
  164. }
  165. @end