RAHomeViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. // [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"%s",__func__]];
  118. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationHandleOrder object:nil];
  119. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveReloadNotification:) name:RANotificationReloadHome object:nil];
  120. }
  121. #pragma mark - Action
  122. - (void)uploadListItemClick:(UIBarButtonItem *)sender {
  123. ApexDriverUploadListVC *vc = [ApexDriverUploadListVC viewControllerFromStoryboard];
  124. [self.navigationController pushViewController:vc animated:YES];
  125. }
  126. - (void)logoutItemClick:(UIBarButtonItem *)sender {
  127. // show progress
  128. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  129. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  130. [RADataProvider logout];
  131. dispatch_async(dispatch_get_main_queue(), ^{
  132. // dismiss progress
  133. [hud dismiss];
  134. [RASingleton.sharedInstance logout];
  135. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationLogout object:nil];
  136. });
  137. });
  138. }
  139. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  140. [self loadData];
  141. }
  142. #pragma mark - Getter
  143. - (NSMutableArray *)sectionArray {
  144. if (!_sectionArray) {
  145. _sectionArray = [NSMutableArray array];
  146. }
  147. return _sectionArray;
  148. }
  149. - (NSUInteger)orderSectionCount {
  150. return self.sectionArray.count;
  151. }
  152. - (NSUInteger)orderCountForSection:(NSInteger)section {
  153. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  154. return [sectionModel ordersCount];
  155. }
  156. - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath {
  157. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section];
  158. return [sectionModel orderModelForIndex:indexPath.row];
  159. }
  160. - (NSString *)titleForSection:(NSInteger)section {
  161. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  162. return sectionModel.title;
  163. }
  164. - (BOOL)hasMoreOrderForSection:(NSInteger)section {
  165. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  166. return [sectionModel hasMoreOrder];
  167. }
  168. #pragma mark - Data
  169. - (void)loadData {
  170. if (self.loading) {
  171. return;
  172. }
  173. self.loading = YES;
  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. NSMutableArray *tmpSectionArr = [NSMutableArray array];
  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. [tmpSectionArr 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.sectionArray = tmpSectionArr;
  209. [strongSelf.homeOrderTableView reloadData];
  210. if (strongSelf.currentIndexPath) {
  211. // [strongSelf.homeOrderTableView scrollToRowAtIndexPath:self.currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
  212. [strongSelf.homeOrderTableView selectRowAtIndexPath:self.currentIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  213. } else {
  214. }
  215. //
  216. // [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
  217. // UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  218. // [center removeAllDeliveredNotifications];
  219. // [center removeAllPendingNotificationRequests];
  220. } else {
  221. [strongSelf.sectionArray removeAllObjects];
  222. strongSelf.homeOrderTableView.contentOffset = CGPointZero;
  223. [strongSelf.homeOrderTableView reloadData];
  224. // process error
  225. NSString *msg = [json objectForKey:@"err_msg"];
  226. // [strongSelf showAlert:msg];
  227. [strongSelf showAlertTilte:@"Warning" message:msg];
  228. }
  229. }
  230. self.loading = NO;
  231. });
  232. });
  233. }
  234. #pragma mark - Controller Action
  235. - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
  236. [self pushDetailViewControllerForOrderID:model.orderID type:model.status type2:model.order_type2];
  237. }
  238. - (void)pushDetailViewControllerForOrderID:(NSString *)orderID type:(NSInteger)type type2:(NSString *)type2 {
  239. if (!orderID) {
  240. return;
  241. }
  242. RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
  243. detailVC.orderID = orderID;
  244. detailVC.orderType = type;
  245. detailVC.orderType2 = type2;
  246. [self.navigationController pushViewController:detailVC animated:YES];
  247. }
  248. - (void)showMoreOrderForSection:(NSInteger)section {
  249. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  250. RAHomeMoreViewController *homeMoreVC = [RAHomeMoreViewController viewControllerFromStoryboard];
  251. homeMoreVC.title = sectionModel.title;
  252. homeMoreVC.type = sectionModel.type;
  253. [self.navigationController pushViewController:homeMoreVC animated:YES];
  254. }
  255. #pragma mark - Nofitication Selector
  256. - (void)receiveNewOrderNotification:(NSNotification *)notification {
  257. [self loadData];
  258. }
  259. - (void)receiveReloadNotification:(NSNotification *)notification {
  260. [self loadData];
  261. }
  262. - (void)receiveGoDetailNotification:(NSNotification *)notification {
  263. NSDictionary *userInfo = notification.object;
  264. if (userInfo) {
  265. NSString *orderID = [userInfo objectForKey:@"orderID"];
  266. NSInteger orderType = [[userInfo objectForKey:@"orderType"] integerValue];
  267. NSString *orderType2 = [userInfo objectForKey:@"orderType2"];
  268. if (orderID) {
  269. [self pushDetailViewControllerForOrderID:orderID type:orderType type2:orderType2];
  270. }
  271. }
  272. }
  273. @end