RAHomeViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. @end
  37. @interface RAHomeViewController ()
  38. @property (nonatomic,strong) NSMutableArray <RAHomeSectionModel *> *sectionArray;
  39. @end
  40. @implementation RAHomeViewController
  41. + (instancetype)viewControllerFromStoryboard {
  42. RAHomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  43. return homeVC;
  44. }
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. // Do any additional setup after loading the view.
  48. [self configureNavigationBar];
  49. [self configureTable];
  50. [self loadData];
  51. }
  52. - (void)didReceiveMemoryWarning {
  53. [super didReceiveMemoryWarning];
  54. // Dispose of any resources that can be recreated.
  55. }
  56. #pragma mark - Configure
  57. - (void)configureTable {
  58. self.homeOrderTableView.tableFooterView = [UIView new];
  59. self.homeOrderTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  60. }
  61. - (void)configureNavigationBar {
  62. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithTitle:@"Upload List" style:UIBarButtonItemStylePlain target:self action:@selector(uploadListItemClick:)];
  63. self.navigationItem.rightBarButtonItem = uploadListItem;
  64. }
  65. #pragma mark - Action
  66. - (void)uploadListItemClick:(UIBarButtonItem *)sender {
  67. ApexDriverUploadListVC *vc = [ApexDriverUploadListVC viewControllerFromStoryboard];
  68. [self.navigationController pushViewController:vc animated:YES];
  69. }
  70. #pragma mark - Getter
  71. - (NSMutableArray *)sectionArray {
  72. if (!_sectionArray) {
  73. _sectionArray = [NSMutableArray array];
  74. }
  75. return _sectionArray;
  76. }
  77. - (NSUInteger)orderSectionCount {
  78. return self.sectionArray.count;
  79. }
  80. - (NSUInteger)orderCountForSection:(NSInteger)section {
  81. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  82. return [sectionModel ordersCount];
  83. }
  84. - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath {
  85. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section];
  86. return [sectionModel orderModelForIndex:indexPath.row];
  87. }
  88. - (NSString *)titleForSection:(NSInteger)section {
  89. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  90. return sectionModel.title;
  91. }
  92. #pragma mark - Data
  93. - (void)loadData {
  94. // show progress
  95. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  96. __weak typeof(self) weakSelf = self;
  97. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  98. NSDictionary *json = [RADataProvider requestOrderList];
  99. dispatch_async(dispatch_get_main_queue(), ^{
  100. // dismiss progress
  101. [hud dismiss];
  102. if (weakSelf) {
  103. __strong typeof(weakSelf) strongSelf = weakSelf;
  104. int result = [[json objectForKey:@"result"] intValue];
  105. if (result == RESULT_TRUE) {
  106. NSArray *sectionArray = [json objectForKey:@"sections"];
  107. [strongSelf.sectionArray removeAllObjects];
  108. for (int i = 0; i < sectionArray.count; i++) {
  109. NSDictionary *section = [sectionArray objectAtIndex:i];
  110. RAHomeSectionModel *sectionModel = [RAHomeSectionModel new];
  111. [sectionModel setValuesForKeysWithDictionary:section];
  112. [strongSelf.sectionArray addObject:sectionModel];
  113. }
  114. [strongSelf.homeOrderTableView reloadData];
  115. } else {
  116. // process error
  117. }
  118. }
  119. });
  120. });
  121. }
  122. #pragma mark - Controller Action
  123. - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
  124. RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
  125. detailVC.orderID = model.orderID;
  126. [self.navigationController pushViewController:detailVC animated:YES];
  127. }
  128. @end