RAHomeViewController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. @interface RAHomeSectionModel : NSObject
  13. @property (nonatomic,assign) RAOrderStatus type;
  14. @property (nonatomic,strong) NSArray <RAHomeOrderModel *> *orders;
  15. @property (nonatomic,copy) NSString *title;
  16. @property (nonatomic,readonly) NSInteger ordersCount;
  17. @end
  18. @implementation RAHomeSectionModel
  19. - (void)setOrders:(NSArray *)orders {
  20. NSMutableArray *orderArr = [NSMutableArray array];
  21. for (int i = 0; i < orders.count; i++) {
  22. NSDictionary *order = [orders objectAtIndex:i];
  23. RAHomeOrderModel *orderModel = [RAHomeOrderModel new];
  24. [orderModel setValuesForKeysWithDictionary:order];
  25. [orderArr addObject:orderModel];
  26. }
  27. _orders = orderArr;
  28. }
  29. - (RAHomeOrderModel *)orderModelForIndex:(NSInteger)index {
  30. return [self.orders objectAtIndex:index];
  31. }
  32. - (NSInteger)ordersCount {
  33. return self.orders.count;
  34. }
  35. @end
  36. @interface RAHomeViewController ()
  37. @property (nonatomic,strong) NSMutableArray <RAHomeSectionModel *> *sectionArray;
  38. @end
  39. @implementation RAHomeViewController
  40. + (instancetype)viewControllerFromStoryboard {
  41. RAHomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  42. return homeVC;
  43. }
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. // Do any additional setup after loading the view.
  47. [self configureTable];
  48. [self loadData];
  49. }
  50. - (void)didReceiveMemoryWarning {
  51. [super didReceiveMemoryWarning];
  52. // Dispose of any resources that can be recreated.
  53. }
  54. - (void)configureTable {
  55. self.homeOrderTableView.tableFooterView = [UIView new];
  56. self.homeOrderTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  57. }
  58. #pragma mark Getter
  59. - (NSMutableArray *)sectionArray {
  60. if (!_sectionArray) {
  61. _sectionArray = [NSMutableArray array];
  62. }
  63. return _sectionArray;
  64. }
  65. - (NSUInteger)orderSectionCount {
  66. return self.sectionArray.count;
  67. }
  68. - (NSUInteger)orderCountForSection:(NSInteger)section {
  69. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  70. return [sectionModel ordersCount];
  71. }
  72. - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath {
  73. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section];
  74. return [sectionModel orderModelForIndex:indexPath.row];
  75. }
  76. - (NSString *)titleForSection:(NSInteger)section {
  77. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  78. return sectionModel.title;
  79. }
  80. #pragma mark - Data
  81. - (void)loadData {
  82. // show progress
  83. // RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  84. __weak typeof(self) weakSelf = self;
  85. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  86. NSDictionary *json = [RADataProvider requestOrderList];
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. // dismiss progress
  89. if (weakSelf) {
  90. __strong typeof(weakSelf) strongSelf = weakSelf;
  91. int result = [[json objectForKey:@"result"] intValue];
  92. if (result == RESULT_TRUE) {
  93. NSArray *sectionArray = [json objectForKey:@"sections"];
  94. [strongSelf.sectionArray removeAllObjects];
  95. for (int i = 0; i < sectionArray.count; i++) {
  96. NSDictionary *section = [sectionArray objectAtIndex:i];
  97. RAHomeSectionModel *sectionModel = [RAHomeSectionModel new];
  98. [sectionModel setValuesForKeysWithDictionary:section];
  99. [strongSelf.sectionArray addObject:sectionModel];
  100. }
  101. [strongSelf.homeOrderTableView reloadData];
  102. } else {
  103. // process error
  104. }
  105. }
  106. });
  107. });
  108. }
  109. #pragma mark - Controller Action
  110. - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
  111. RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
  112. detailVC.orderID = model.orderID;
  113. [self.navigationController pushViewController:detailVC animated:YES];
  114. }
  115. @end