RAHomeViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "RAOrderDetailViewController.h"
  10. #import "RAProgressHUD.h"
  11. #import "ApexDriverUploadListVC.h"
  12. #import "RAHomeMoreViewController.h"
  13. #import <UserNotifications/UserNotifications.h>
  14. #import "UIScrollView+Empty.h"
  15. #import "RAEmptyView.h"
  16. #import "RAHomeHeaderView.h"
  17. @implementation RAHomeSectionModel
  18. - (instancetype)init {
  19. if (self = [super init]) {
  20. }
  21. return self;
  22. }
  23. - (void)setOrders:(NSArray *)orders {
  24. NSMutableArray *orderArr = [NSMutableArray array];
  25. for (int i = 0; i < orders.count; i++) {
  26. NSDictionary *order = [orders objectAtIndex:i];
  27. RAHomeOrderModel *orderModel = [RAHomeOrderModel new];
  28. [orderModel setValuesForKeysWithDictionary:order];
  29. [orderArr addObject:orderModel];
  30. }
  31. _orders = orderArr;
  32. }
  33. - (void)setBackendFlagCount:(NSInteger)backendFlagCount {
  34. _backendFlagCount = backendFlagCount;
  35. if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) {
  36. [self.delegate refreshUI];
  37. }
  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.reloadFlag) {
  84. [self loadData];
  85. self.reloadFlag = !self.reloadFlag;
  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. // empty
  109. __weak typeof(self) weakSelf = self;
  110. self.homeOrderTableView.emptyView = [RAEmptyView emptyViewWithTapBlk:^(id sender) {
  111. [weakSelf loadData];
  112. }];
  113. // Header
  114. RAHomeHeaderView *header = [RAHomeHeaderView homeHeader];
  115. self.homeOrderTableView.tableHeaderView = header;
  116. }
  117. - (void)configureNavigationBar {
  118. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"upload_list"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  119. style:UIBarButtonItemStylePlain
  120. target:self
  121. action:@selector(uploadListItemClick:)];
  122. UIBarButtonItem *logoutItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"logout"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  123. style:UIBarButtonItemStylePlain
  124. target:self
  125. action:@selector(logoutItemClick:)];
  126. self.navigationItem.rightBarButtonItems = @[logoutItem,uploadListItem];
  127. }
  128. - (void)registNotification {
  129. // [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"%s",__func__]];
  130. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationHandleOrder object:nil];
  131. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveReloadNotification:) name:RANotificationReloadHome object:nil];
  132. }
  133. #pragma mark - Action
  134. - (void)uploadListItemClick:(UIBarButtonItem *)sender {
  135. ApexDriverUploadListVC *vc = [ApexDriverUploadListVC viewControllerFromStoryboard];
  136. [self.navigationController pushViewController:vc animated:YES];
  137. }
  138. - (void)logoutItemClick:(UIBarButtonItem *)sender {
  139. // show progress
  140. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  141. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  142. [RADataProvider logout];
  143. dispatch_async(dispatch_get_main_queue(), ^{
  144. // dismiss progress
  145. [hud dismiss];
  146. [RASingleton.sharedInstance logout];
  147. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationLogout object:nil];
  148. });
  149. });
  150. }
  151. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  152. [self loadData];
  153. }
  154. #pragma mark - Getter
  155. - (NSMutableArray *)sectionArray {
  156. if (!_sectionArray) {
  157. _sectionArray = [NSMutableArray array];
  158. }
  159. return _sectionArray;
  160. }
  161. - (RAHomeSectionModel *)modelForSection:(NSInteger)section {
  162. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  163. return sectionModel;
  164. }
  165. - (NSUInteger)orderSectionCount {
  166. return self.sectionArray.count;
  167. }
  168. - (NSUInteger)orderCountForSection:(NSInteger)section {
  169. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  170. return [sectionModel ordersCount];
  171. }
  172. - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath {
  173. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section];
  174. return [sectionModel orderModelForIndex:indexPath.row];
  175. }
  176. - (NSString *)titleForSection:(NSInteger)section {
  177. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  178. return sectionModel.title;
  179. }
  180. - (BOOL)hasMoreOrderForSection:(NSInteger)section {
  181. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  182. return [sectionModel hasMoreOrder];
  183. }
  184. - (NSInteger)backendCountForSection:(NSInteger)section {
  185. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  186. return sectionModel.backendFlagCount;
  187. }
  188. #pragma mark - Set/Update
  189. - (void)setBackendFlagCount:(NSInteger)count forSection:(NSInteger)section {
  190. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  191. sectionModel.backendFlagCount = count;
  192. }
  193. - (void)decreaseBackendFlagCountForSection:(NSInteger)section {
  194. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  195. NSInteger count = sectionModel.backendFlagCount;
  196. sectionModel.backendFlagCount = --count;
  197. }
  198. - (void)setHeaderDelegate:(id<RAHomeSectionModelDelegate>)delegate forSection:(NSInteger)section {
  199. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  200. sectionModel.delegate = delegate;
  201. }
  202. #pragma mark - Data
  203. - (void)loadData {
  204. if (self.loading) {
  205. return;
  206. }
  207. self.loading = YES;
  208. [self.homeOrderTableView hideEmpty];
  209. // show progress
  210. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  211. __weak typeof(self) weakSelf = self;
  212. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  213. NSDictionary *json = [RADataProvider requestOrderList];
  214. NSLog(@"home load data");
  215. dispatch_async(dispatch_get_main_queue(), ^{
  216. // dismiss progress
  217. [hud dismiss];
  218. if (weakSelf.refreshControl.isRefreshing) {
  219. [weakSelf.refreshControl endRefreshing];
  220. }
  221. if (weakSelf) {
  222. __strong typeof(weakSelf) strongSelf = weakSelf;
  223. int result = [[json objectForKey:@"result"] intValue];
  224. if (result == RESULT_TRUE) {
  225. BOOL requiredLocation = [[json objectForKey:@"requiredLocation"] boolValue];
  226. [RASingleton sharedInstance].requiredLocation = requiredLocation;
  227. NSArray *sectionArray = [json objectForKey:@"sections"];
  228. NSMutableArray *tmpSectionArr = [NSMutableArray array];
  229. strongSelf.currentIndexPath = nil;
  230. for (int i = 0; i < sectionArray.count; i++) {
  231. NSDictionary *section = [sectionArray objectAtIndex:i];
  232. RAHomeSectionModel *sectionModel = [RAHomeSectionModel new];
  233. sectionModel.section = i;
  234. [sectionModel setValuesForKeysWithDictionary:section];
  235. [tmpSectionArr addObject:sectionModel];
  236. if (strongSelf.currentOrderID.length > 0) {
  237. NSInteger idx = [sectionModel orderModelIndexForID:self.currentOrderID];
  238. if (idx > -1) {
  239. strongSelf.currentIndexPath = [NSIndexPath indexPathForRow:idx inSection:i];
  240. }
  241. }
  242. }
  243. strongSelf.sectionArray = tmpSectionArr;
  244. [strongSelf.homeOrderTableView reloadData];
  245. if (strongSelf.currentIndexPath) {
  246. // [strongSelf.homeOrderTableView scrollToRowAtIndexPath:self.currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
  247. [strongSelf.homeOrderTableView selectRowAtIndexPath:self.currentIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  248. } else {
  249. }
  250. } else {
  251. [strongSelf.sectionArray removeAllObjects];
  252. strongSelf.homeOrderTableView.contentOffset = CGPointZero;
  253. [strongSelf.homeOrderTableView reloadData];
  254. // process error
  255. NSString *msg = [json objectForKey:@"err_msg"];
  256. // [strongSelf showAlert:msg];
  257. [strongSelf showAlertTilte:@"Warning" message:msg];
  258. }
  259. }
  260. weakSelf.loading = NO;
  261. if (weakSelf.orderSectionCount == 0) {
  262. [weakSelf.homeOrderTableView showEmpty];
  263. } else {
  264. [weakSelf.homeOrderTableView hideEmpty];
  265. }
  266. });
  267. });
  268. }
  269. #pragma mark - Controller Action
  270. - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
  271. [self pushDetailViewControllerForOrderID:model.orderID type:model.status type2:model.order_type2];
  272. }
  273. - (void)pushDetailViewControllerForOrderID:(NSString *)orderID type:(NSInteger)type type2:(NSString *)type2 {
  274. if (!orderID) {
  275. return;
  276. }
  277. RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
  278. detailVC.orderID = orderID;
  279. detailVC.orderType = type;
  280. detailVC.orderType2 = type2;
  281. [self.navigationController pushViewController:detailVC animated:YES];
  282. }
  283. - (void)showMoreOrderForSection:(NSInteger)section {
  284. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  285. RAHomeMoreViewController *homeMoreVC = [RAHomeMoreViewController viewControllerFromStoryboard];
  286. homeMoreVC.title = sectionModel.title;
  287. homeMoreVC.type = sectionModel.type;
  288. __weak typeof(self) weakSelf = self;
  289. homeMoreVC.backendFlagOrderClickBlk = ^{
  290. weakSelf.reloadFlag = YES;
  291. };
  292. [self.navigationController pushViewController:homeMoreVC animated:YES];
  293. }
  294. #pragma mark - Nofitication Selector
  295. - (void)receiveNewOrderNotification:(NSNotification *)notification {
  296. [self loadData];
  297. }
  298. - (void)receiveReloadNotification:(NSNotification *)notification {
  299. [self loadData];
  300. }
  301. - (void)receiveGoDetailNotification:(NSNotification *)notification {
  302. NSDictionary *userInfo = notification.object;
  303. if (userInfo) {
  304. NSString *orderID = [userInfo objectForKey:@"orderID"];
  305. NSInteger orderType = [[userInfo objectForKey:@"orderType"] integerValue];
  306. NSString *orderType2 = [userInfo objectForKey:@"orderType2"];
  307. if (orderID) {
  308. [self pushDetailViewControllerForOrderID:orderID type:orderType type2:orderType2];
  309. }
  310. }
  311. }
  312. @end