RAHomeViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. }
  84. - (void)dealloc {
  85. [[NSNotificationCenter defaultCenter] removeObserver:self];
  86. }
  87. - (void)didReceiveMemoryWarning {
  88. [super didReceiveMemoryWarning];
  89. // Dispose of any resources that can be recreated.
  90. }
  91. #pragma mark - Configure
  92. - (void)configureTable {
  93. if (@available(iOS 11, *)) {
  94. self.homeOrderTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  95. } else {
  96. self.automaticallyAdjustsScrollViewInsets = NO;
  97. }
  98. self.homeOrderTableView.tableFooterView = [UIView new];
  99. self.homeOrderTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  100. UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
  101. [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
  102. [self.homeOrderTableView addSubview:refresh];
  103. self.refreshControl = refresh;
  104. }
  105. - (void)configureNavigationBar {
  106. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"upload_list"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  107. style:UIBarButtonItemStylePlain
  108. target:self
  109. action:@selector(uploadListItemClick:)];
  110. UIBarButtonItem *logoutItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"logout"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  111. style:UIBarButtonItemStylePlain
  112. target:self
  113. action:@selector(logoutItemClick:)];
  114. self.navigationItem.rightBarButtonItems = @[logoutItem,uploadListItem];
  115. }
  116. - (void)registNotification {
  117. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationHandleOrder object:nil];
  118. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveReloadNotification:) name:RANotificationReloadHome object:nil];
  119. }
  120. #pragma mark - Action
  121. - (void)uploadListItemClick:(UIBarButtonItem *)sender {
  122. ApexDriverUploadListVC *vc = [ApexDriverUploadListVC viewControllerFromStoryboard];
  123. [self.navigationController pushViewController:vc animated:YES];
  124. }
  125. - (void)logoutItemClick:(UIBarButtonItem *)sender {
  126. // show progress
  127. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  128. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  129. [RADataProvider logout];
  130. dispatch_async(dispatch_get_main_queue(), ^{
  131. // dismiss progress
  132. [hud dismiss];
  133. [RASingleton.sharedInstance logout];
  134. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationLogout object:nil];
  135. });
  136. });
  137. }
  138. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  139. [self loadData];
  140. }
  141. #pragma mark - Getter
  142. - (NSMutableArray *)sectionArray {
  143. if (!_sectionArray) {
  144. _sectionArray = [NSMutableArray array];
  145. }
  146. return _sectionArray;
  147. }
  148. - (NSUInteger)orderSectionCount {
  149. return self.sectionArray.count;
  150. }
  151. - (NSUInteger)orderCountForSection:(NSInteger)section {
  152. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  153. return [sectionModel ordersCount];
  154. }
  155. - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath {
  156. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section];
  157. return [sectionModel orderModelForIndex:indexPath.row];
  158. }
  159. - (NSString *)titleForSection:(NSInteger)section {
  160. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  161. return sectionModel.title;
  162. }
  163. - (BOOL)hasMoreOrderForSection:(NSInteger)section {
  164. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  165. return [sectionModel hasMoreOrder];
  166. }
  167. #pragma mark - Data
  168. - (void)loadData {
  169. if (self.loading) {
  170. return;
  171. }
  172. self.loading = YES;
  173. // show progress
  174. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  175. __weak typeof(self) weakSelf = self;
  176. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  177. NSDictionary *json = [RADataProvider requestOrderList];
  178. NSLog(@"home load data");
  179. dispatch_async(dispatch_get_main_queue(), ^{
  180. // dismiss progress
  181. [hud dismiss];
  182. if (weakSelf.refreshControl.isRefreshing) {
  183. [weakSelf.refreshControl endRefreshing];
  184. }
  185. if (weakSelf) {
  186. __strong typeof(weakSelf) strongSelf = weakSelf;
  187. int result = [[json objectForKey:@"result"] intValue];
  188. if (result == RESULT_TRUE) {
  189. BOOL requiredLocation = [[json objectForKey:@"requiredLocation"] boolValue];
  190. [RASingleton sharedInstance].requiredLocation = requiredLocation;
  191. NSArray *sectionArray = [json objectForKey:@"sections"];
  192. NSMutableArray *tmpSectionArr = [NSMutableArray array];
  193. strongSelf.currentIndexPath = nil;
  194. for (int i = 0; i < sectionArray.count; i++) {
  195. NSDictionary *section = [sectionArray objectAtIndex:i];
  196. RAHomeSectionModel *sectionModel = [RAHomeSectionModel new];
  197. sectionModel.section = i;
  198. [sectionModel setValuesForKeysWithDictionary:section];
  199. [tmpSectionArr addObject:sectionModel];
  200. if (strongSelf.currentOrderID.length > 0) {
  201. NSInteger idx = [sectionModel orderModelIndexForID:self.currentOrderID];
  202. if (idx > -1) {
  203. strongSelf.currentIndexPath = [NSIndexPath indexPathForRow:idx inSection:i];
  204. }
  205. }
  206. }
  207. strongSelf.sectionArray = tmpSectionArr;
  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. [strongSelf.sectionArray removeAllObjects];
  221. strongSelf.homeOrderTableView.contentOffset = CGPointZero;
  222. [strongSelf.homeOrderTableView reloadData];
  223. // process error
  224. NSString *msg = [json objectForKey:@"err_msg"];
  225. // [strongSelf showAlert:msg];
  226. [strongSelf showAlertTilte:@"Warning" message:msg];
  227. }
  228. }
  229. self.loading = NO;
  230. });
  231. });
  232. }
  233. #pragma mark - Controller Action
  234. - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
  235. [self pushDetailViewControllerForOrderID:model.orderID type:model.status type2:model.order_type2];
  236. }
  237. - (void)pushDetailViewControllerForOrderID:(NSString *)orderID type:(NSInteger)type type2:(NSString *)type2 {
  238. if (!orderID) {
  239. return;
  240. }
  241. RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
  242. detailVC.orderID = orderID;
  243. detailVC.orderType = type;
  244. detailVC.orderType2 = type2;
  245. [self.navigationController pushViewController:detailVC animated:YES];
  246. }
  247. - (void)showMoreOrderForSection:(NSInteger)section {
  248. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  249. RAHomeMoreViewController *homeMoreVC = [RAHomeMoreViewController viewControllerFromStoryboard];
  250. homeMoreVC.title = sectionModel.title;
  251. homeMoreVC.type = sectionModel.type;
  252. [self.navigationController pushViewController:homeMoreVC animated:YES];
  253. }
  254. #pragma mark - Nofitication Selector
  255. - (void)receiveNewOrderNotification:(NSNotification *)notification {
  256. [self loadData];
  257. }
  258. - (void)receiveReloadNotification:(NSNotification *)notification {
  259. [self loadData];
  260. }
  261. - (void)receiveGoDetailNotification:(NSNotification *)notification {
  262. NSDictionary *userInfo = notification.object;
  263. if (userInfo) {
  264. NSString *orderID = [userInfo objectForKey:@"orderID"];
  265. NSInteger orderType = [[userInfo objectForKey:@"orderType"] integerValue];
  266. NSString *orderType2 = [userInfo objectForKey:@"orderType2"];
  267. if (orderID) {
  268. [self pushDetailViewControllerForOrderID:orderID type:orderType type2:orderType2];
  269. }
  270. }
  271. }
  272. @end