RAHomeViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #import "RAHomeMoreViewController.h"
  14. #import <UserNotifications/UserNotifications.h>
  15. @interface RAHomeSectionModel : NSObject
  16. @property (nonatomic,assign) RAOrderStatus type;
  17. @property (nonatomic,strong) NSArray <RAHomeOrderModel *> *orders;
  18. @property (nonatomic,copy) NSString *title;
  19. @property (nonatomic,readonly) NSInteger ordersCount;
  20. @property (nonatomic,assign) NSInteger section;
  21. @property (nonatomic,assign) NSInteger totalCount;
  22. @end
  23. @implementation RAHomeSectionModel
  24. - (instancetype)init {
  25. if (self = [super init]) {
  26. }
  27. return self;
  28. }
  29. - (void)setOrders:(NSArray *)orders {
  30. NSMutableArray *orderArr = [NSMutableArray array];
  31. for (int i = 0; i < orders.count; i++) {
  32. NSDictionary *order = [orders objectAtIndex:i];
  33. RAHomeOrderModel *orderModel = [RAHomeOrderModel new];
  34. [orderModel setValuesForKeysWithDictionary:order];
  35. [orderArr addObject:orderModel];
  36. }
  37. _orders = orderArr;
  38. }
  39. - (RAHomeOrderModel *)orderModelForIndex:(NSInteger)index {
  40. return [self.orders objectAtIndex:index];
  41. }
  42. - (NSInteger)ordersCount {
  43. return self.orders.count;
  44. }
  45. - (NSInteger)orderModelIndexForID:(NSString *)orderID {
  46. __block NSInteger curIndex = -1;;
  47. [self.orders enumerateObjectsUsingBlock:^(RAHomeOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  48. if ([obj.orderID isEqualToString:orderID]) {
  49. curIndex = idx;
  50. return ;
  51. }
  52. }];
  53. return curIndex;
  54. }
  55. - (BOOL)hasMoreOrder {
  56. return self.totalCount > self.ordersCount;
  57. }
  58. @end
  59. #pragma mark - View Controller
  60. @interface RAHomeViewController ()
  61. @property (nonatomic,strong) NSMutableArray <RAHomeSectionModel *> *sectionArray;
  62. @property (nonatomic,strong) NSIndexPath *currentIndexPath;
  63. @property (nonatomic,strong) UIRefreshControl *refreshControl;
  64. @end
  65. @implementation RAHomeViewController
  66. + (instancetype)viewControllerFromStoryboard {
  67. RAHomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  68. return homeVC;
  69. }
  70. - (void)viewDidLoad {
  71. [super viewDidLoad];
  72. // Do any additional setup after loading the view.
  73. [self configureNavigationBar];
  74. [self configureTable];
  75. [self registNotification];
  76. [self loadData];
  77. }
  78. - (void)viewWillAppear:(BOOL)animated {
  79. [super viewWillAppear:animated];
  80. }
  81. - (void)viewDidAppear:(BOOL)animated {
  82. [super viewDidAppear:animated];
  83. if (self.gotoDetailID) {
  84. [self pushDetailViewControllerForOrderID:self.gotoDetailID];
  85. self.gotoDetailID = nil;
  86. }
  87. }
  88. - (void)dealloc {
  89. [[NSNotificationCenter defaultCenter] removeObserver:self];
  90. }
  91. - (void)didReceiveMemoryWarning {
  92. [super didReceiveMemoryWarning];
  93. // Dispose of any resources that can be recreated.
  94. }
  95. #pragma mark - Configure
  96. - (void)configureTable {
  97. if (@available(iOS 11, *)) {
  98. self.homeOrderTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  99. } else {
  100. self.automaticallyAdjustsScrollViewInsets = NO;
  101. }
  102. self.homeOrderTableView.tableFooterView = [UIView new];
  103. self.homeOrderTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  104. UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
  105. [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
  106. [self.homeOrderTableView addSubview:refresh];
  107. self.refreshControl = refresh;
  108. }
  109. - (void)configureNavigationBar {
  110. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"list"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  111. style:UIBarButtonItemStylePlain
  112. target:self
  113. action:@selector(uploadListItemClick:)];
  114. UIBarButtonItem *logoutItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"logout"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  115. style:UIBarButtonItemStylePlain
  116. target:self
  117. action:@selector(logoutItemClick:)];
  118. self.navigationItem.rightBarButtonItems = @[logoutItem,uploadListItem];
  119. }
  120. - (void)registNotification {
  121. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationNewOrder object:nil];
  122. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveReloadNotification:) name:RANotificationReloadHome object:nil];
  123. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveGoDetailNotification:) name:RANotificationGoDetail object:nil];
  124. }
  125. #pragma mark - Action
  126. - (void)uploadListItemClick:(UIBarButtonItem *)sender {
  127. ApexDriverUploadListVC *vc = [ApexDriverUploadListVC viewControllerFromStoryboard];
  128. [self.navigationController pushViewController:vc animated:YES];
  129. }
  130. - (void)logoutItemClick:(UIBarButtonItem *)sender {
  131. // show progress
  132. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  133. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  134. [RADataProvider logout];
  135. dispatch_async(dispatch_get_main_queue(), ^{
  136. // dismiss progress
  137. [hud dismiss];
  138. [RASingleton.sharedInstance logout];
  139. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationLogout object:nil];
  140. });
  141. });
  142. }
  143. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  144. [self loadData];
  145. }
  146. #pragma mark - Getter
  147. - (NSMutableArray *)sectionArray {
  148. if (!_sectionArray) {
  149. _sectionArray = [NSMutableArray array];
  150. }
  151. return _sectionArray;
  152. }
  153. - (NSUInteger)orderSectionCount {
  154. return self.sectionArray.count;
  155. }
  156. - (NSUInteger)orderCountForSection:(NSInteger)section {
  157. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  158. return [sectionModel ordersCount];
  159. }
  160. - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath {
  161. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section];
  162. return [sectionModel orderModelForIndex:indexPath.row];
  163. }
  164. - (NSString *)titleForSection:(NSInteger)section {
  165. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  166. return sectionModel.title;
  167. }
  168. - (BOOL)hasMoreOrderForSection:(NSInteger)section {
  169. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  170. return [sectionModel hasMoreOrder];
  171. }
  172. #pragma mark - Data
  173. - (void)loadData {
  174. // show progress
  175. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  176. __weak typeof(self) weakSelf = self;
  177. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  178. NSDictionary *json = [RADataProvider requestOrderList];
  179. NSLog(@"home load data");
  180. dispatch_async(dispatch_get_main_queue(), ^{
  181. // dismiss progress
  182. [hud dismiss];
  183. if (weakSelf.refreshControl.isRefreshing) {
  184. [weakSelf.refreshControl endRefreshing];
  185. }
  186. if (weakSelf) {
  187. __strong typeof(weakSelf) strongSelf = weakSelf;
  188. int result = [[json objectForKey:@"result"] intValue];
  189. if (result == RESULT_TRUE) {
  190. BOOL requiredLocation = [[json objectForKey:@"requiredLocation"] boolValue];
  191. [RASingleton sharedInstance].requiredLocation = requiredLocation;
  192. NSArray *sectionArray = [json objectForKey:@"sections"];
  193. [strongSelf.sectionArray removeAllObjects];
  194. strongSelf.currentIndexPath = nil;
  195. for (int i = 0; i < sectionArray.count; i++) {
  196. NSDictionary *section = [sectionArray objectAtIndex:i];
  197. RAHomeSectionModel *sectionModel = [RAHomeSectionModel new];
  198. sectionModel.section = i;
  199. [sectionModel setValuesForKeysWithDictionary:section];
  200. [strongSelf.sectionArray addObject:sectionModel];
  201. if (strongSelf.currentOrderID.length > 0) {
  202. NSInteger idx = [sectionModel orderModelIndexForID:self.currentOrderID];
  203. if (idx > -1) {
  204. strongSelf.currentIndexPath = [NSIndexPath indexPathForRow:idx inSection:i];
  205. }
  206. }
  207. }
  208. [strongSelf.homeOrderTableView reloadData];
  209. if (strongSelf.currentIndexPath) {
  210. // [strongSelf.homeOrderTableView scrollToRowAtIndexPath:self.currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
  211. [strongSelf.homeOrderTableView selectRowAtIndexPath:self.currentIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  212. } else {
  213. }
  214. //
  215. // [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  216. // UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  217. // [center removeAllDeliveredNotifications];
  218. // [center removeAllPendingNotificationRequests];
  219. } else {
  220. // process error
  221. NSString *msg = [json objectForKey:@"err_msg"];
  222. [strongSelf showAlertTilte:@"Warning" message:msg];
  223. }
  224. }
  225. });
  226. });
  227. }
  228. #pragma mark - Controller Action
  229. - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
  230. [self pushDetailViewControllerForOrderID:model.orderID];
  231. }
  232. - (void)pushDetailViewControllerForOrderID:(NSString *)orderID {
  233. if (!orderID) {
  234. return;
  235. }
  236. RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
  237. detailVC.orderID = orderID;
  238. [self.navigationController pushViewController:detailVC animated:YES];
  239. }
  240. - (void)showMoreOrderForSection:(NSInteger)section {
  241. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  242. RAHomeMoreViewController *homeMoreVC = [RAHomeMoreViewController viewControllerFromStoryboard];
  243. homeMoreVC.title = sectionModel.title;
  244. homeMoreVC.type = sectionModel.type;
  245. [self.navigationController pushViewController:homeMoreVC animated:YES];
  246. }
  247. #pragma mark - Nofitication Selector
  248. - (void)receiveNewOrderNotification:(NSNotification *)notification {
  249. [self loadData];
  250. }
  251. - (void)receiveReloadNotification:(NSNotification *)notification {
  252. [self loadData];
  253. }
  254. - (void)receiveGoDetailNotification:(NSNotification *)notification {
  255. NSDictionary *userInfo = notification.object;
  256. if (userInfo) {
  257. NSString *orderID = [userInfo objectForKey:@"orderID"];
  258. if (orderID) {
  259. [self pushDetailViewControllerForOrderID:orderID];
  260. }
  261. }
  262. }
  263. @end