RAHomeViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. #import "RAMessageViewController.h"
  18. #import "RASettingViewController.h"
  19. @implementation RAHomeSectionModel
  20. - (instancetype)init {
  21. if (self = [super init]) {
  22. }
  23. return self;
  24. }
  25. - (void)setOrders:(NSArray *)orders {
  26. NSMutableArray *orderArr = [NSMutableArray array];
  27. for (int i = 0; i < orders.count; i++) {
  28. NSDictionary *order = [orders objectAtIndex:i];
  29. RAHomeOrderModel *orderModel = [RAHomeOrderModel new];
  30. [orderModel setValuesForKeysWithDictionary:order];
  31. [orderArr addObject:orderModel];
  32. }
  33. _orders = orderArr;
  34. }
  35. - (void)setBackendFlagCount:(NSInteger)backendFlagCount {
  36. _backendFlagCount = backendFlagCount;
  37. if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) {
  38. [self.delegate refreshUI];
  39. }
  40. }
  41. - (RAHomeOrderModel *)orderModelForIndex:(NSInteger)index {
  42. return [self.orders objectAtIndex:index];
  43. }
  44. - (NSInteger)ordersCount {
  45. return self.orders.count;
  46. }
  47. - (NSInteger)orderModelIndexForID:(NSString *)orderID {
  48. __block NSInteger curIndex = -1;;
  49. [self.orders enumerateObjectsUsingBlock:^(RAHomeOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  50. if ([obj.orderID isEqualToString:orderID]) {
  51. curIndex = idx;
  52. return ;
  53. }
  54. }];
  55. return curIndex;
  56. }
  57. - (BOOL)hasMoreOrder {
  58. return self.totalCount > self.ordersCount;
  59. }
  60. @end
  61. #pragma mark - View Controller
  62. @interface RAHomeViewController () <RAHomeHeaderDelegate>
  63. @property (nonatomic,strong) NSMutableArray <RAHomeSectionModel *> *sectionArray;
  64. @property (nonatomic,strong) NSIndexPath *currentIndexPath;
  65. @property (nonatomic,strong) UIRefreshControl *refreshControl;
  66. @end
  67. @implementation RAHomeViewController
  68. + (instancetype)viewControllerFromStoryboard {
  69. RAHomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  70. return homeVC;
  71. }
  72. - (void)viewDidLoad {
  73. [super viewDidLoad];
  74. // Do any additional setup after loading the view.
  75. [self configureNavigationBar];
  76. [self configureTable];
  77. [self registNotification];
  78. [self loadData];
  79. }
  80. - (void)viewWillAppear:(BOOL)animated {
  81. [super viewWillAppear:animated];
  82. }
  83. - (void)viewDidAppear:(BOOL)animated {
  84. [super viewDidAppear:animated];
  85. if (self.reloadFlag) {
  86. [self loadData];
  87. self.reloadFlag = !self.reloadFlag;
  88. }
  89. }
  90. - (void)dealloc {
  91. [[NSNotificationCenter defaultCenter] removeObserver:self];
  92. }
  93. - (void)didReceiveMemoryWarning {
  94. [super didReceiveMemoryWarning];
  95. // Dispose of any resources that can be recreated.
  96. }
  97. #pragma mark - Configure
  98. - (void)configureTable {
  99. if (@available(iOS 11, *)) {
  100. self.homeOrderTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  101. } else {
  102. self.automaticallyAdjustsScrollViewInsets = NO;
  103. }
  104. self.homeOrderTableView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
  105. self.homeOrderTableView.tableFooterView = [UIView new];
  106. self.homeOrderTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  107. UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
  108. [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
  109. [self.homeOrderTableView addSubview:refresh];
  110. self.refreshControl = refresh;
  111. // empty
  112. __weak typeof(self) weakSelf = self;
  113. self.homeOrderTableView.emptyView = [RAEmptyView emptyViewWithTapBlk:^(id sender) {
  114. [weakSelf loadData];
  115. }];
  116. // Header
  117. self.headerView = [RAHomeHeaderView homeHeader];
  118. self.headerView.delegate = self;
  119. self.homeOrderTableView.tableHeaderView = self.headerView;
  120. }
  121. - (void)configureNavigationBar {
  122. UIImage *logo = [[UIImage imageNamed:@"apexlogo-2"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
  123. UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithImage:logo landscapeImagePhone:logo style:UIBarButtonItemStylePlain target:nil action:nil];
  124. // logoItem.enabled = NO;
  125. self.navigationItem.leftBarButtonItem = logoItem;
  126. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"upload_list"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  127. style:UIBarButtonItemStylePlain
  128. target:self
  129. action:@selector(uploadListItemClick:)];
  130. self.navigationItem.rightBarButtonItems = @[uploadListItem];
  131. // UIBarButtonItem *logoutItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"logout"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
  132. // style:UIBarButtonItemStylePlain
  133. // target:self
  134. // action:@selector(logoutItemClick:)];
  135. //
  136. // self.navigationItem.rightBarButtonItems = @[logoutItem,uploadListItem];
  137. self.title = RASingleton.sharedInstance.user;
  138. }
  139. - (void)registNotification {
  140. // [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"%s",__func__]];
  141. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationHandleOrder object:nil];
  142. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveReloadNotification:) name:RANotificationReloadHome object:nil];
  143. }
  144. #pragma mark - Action
  145. - (void)uploadListItemClick:(UIBarButtonItem *)sender {
  146. ApexDriverUploadListVC *vc = [ApexDriverUploadListVC viewControllerFromStoryboard];
  147. [self.navigationController pushViewController:vc animated:YES];
  148. }
  149. - (void)logoutItemClick:(UIBarButtonItem *)sender {
  150. // show progress
  151. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  152. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  153. [RADataProvider logout];
  154. dispatch_async(dispatch_get_main_queue(), ^{
  155. // dismiss progress
  156. [hud dismiss];
  157. [RASingleton.sharedInstance logout];
  158. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationLogout object:nil];
  159. });
  160. });
  161. }
  162. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  163. [self loadData];
  164. }
  165. #pragma mark - Setter
  166. - (void)setHeaderView:(RAHomeHeaderView *)headerView {
  167. _headerView = headerView;
  168. }
  169. #pragma mark - Getter
  170. - (NSMutableArray *)sectionArray {
  171. if (!_sectionArray) {
  172. _sectionArray = [NSMutableArray array];
  173. }
  174. return _sectionArray;
  175. }
  176. - (RAHomeSectionModel *)modelForSection:(NSInteger)section {
  177. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  178. return sectionModel;
  179. }
  180. - (NSUInteger)orderSectionCount {
  181. return self.sectionArray.count;
  182. }
  183. - (NSUInteger)orderCountForSection:(NSInteger)section {
  184. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  185. return [sectionModel ordersCount];
  186. }
  187. - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath {
  188. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section];
  189. return [sectionModel orderModelForIndex:indexPath.row];
  190. }
  191. - (NSString *)titleForSection:(NSInteger)section {
  192. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  193. return sectionModel.title;
  194. }
  195. - (BOOL)hasMoreOrderForSection:(NSInteger)section {
  196. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  197. return [sectionModel hasMoreOrder];
  198. }
  199. - (NSInteger)backendCountForSection:(NSInteger)section {
  200. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  201. return sectionModel.backendFlagCount;
  202. }
  203. #pragma mark - Set/Update
  204. - (void)setBackendFlagCount:(NSInteger)count forSection:(NSInteger)section {
  205. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  206. sectionModel.backendFlagCount = count;
  207. }
  208. - (void)decreaseBackendFlagCountForSection:(NSInteger)section {
  209. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  210. NSInteger count = sectionModel.backendFlagCount;
  211. sectionModel.backendFlagCount = --count;
  212. }
  213. - (void)setHeaderDelegate:(id<RAHomeSectionModelDelegate>)delegate forSection:(NSInteger)section {
  214. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  215. sectionModel.delegate = delegate;
  216. }
  217. #pragma mark - Data
  218. - (void)loadData {
  219. if (self.loading) {
  220. return;
  221. }
  222. self.loading = YES;
  223. [self.homeOrderTableView hideEmpty];
  224. // show progress
  225. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  226. __weak typeof(self) weakSelf = self;
  227. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  228. NSDictionary *json = [RADataProvider requestOrderList];
  229. NSLog(@"home load data");
  230. dispatch_async(dispatch_get_main_queue(), ^{
  231. // dismiss progress
  232. [hud dismiss];
  233. if (weakSelf.refreshControl.isRefreshing) {
  234. [weakSelf.refreshControl endRefreshing];
  235. }
  236. if (weakSelf) {
  237. __strong typeof(weakSelf) strongSelf = weakSelf;
  238. // header
  239. BOOL driverAvailable = [[json objectForKey:@"driver_available"] boolValue];
  240. strongSelf.headerView.availabel = driverAvailable;
  241. NSInteger newCount = [[json objectForKey:@"new_count"] integerValue];
  242. NSInteger processingCount = [[json objectForKey:@"processing_count"] integerValue];
  243. NSInteger finishCount = [[json objectForKey:@"finish_count"] integerValue];
  244. strongSelf.headerView.NewOrderCount = newCount;
  245. strongSelf.headerView.ProcessingOrderCount = processingCount;
  246. strongSelf.headerView.FinishOrderCount = finishCount;
  247. int result = [[json objectForKey:@"result"] intValue];
  248. if (result == RESULT_TRUE) {
  249. BOOL requiredLocation = [[json objectForKey:@"requiredLocation"] boolValue];
  250. [RASingleton sharedInstance].requiredLocation = requiredLocation;
  251. NSArray *sectionArray = [json objectForKey:@"sections"];
  252. NSMutableArray *tmpSectionArr = [NSMutableArray array];
  253. strongSelf.currentIndexPath = nil;
  254. for (int i = 0; i < sectionArray.count; i++) {
  255. NSDictionary *section = [sectionArray objectAtIndex:i];
  256. RAHomeSectionModel *sectionModel = [RAHomeSectionModel new];
  257. sectionModel.section = i;
  258. [sectionModel setValuesForKeysWithDictionary:section];
  259. [tmpSectionArr addObject:sectionModel];
  260. if (strongSelf.currentOrderID.length > 0) {
  261. NSInteger idx = [sectionModel orderModelIndexForID:self.currentOrderID];
  262. if (idx > -1) {
  263. strongSelf.currentIndexPath = [NSIndexPath indexPathForRow:idx inSection:i];
  264. }
  265. }
  266. }
  267. strongSelf.sectionArray = tmpSectionArr;
  268. [strongSelf.homeOrderTableView reloadData];
  269. if (strongSelf.currentIndexPath) {
  270. // [strongSelf.homeOrderTableView scrollToRowAtIndexPath:self.currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
  271. [strongSelf.homeOrderTableView selectRowAtIndexPath:self.currentIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  272. } else {
  273. }
  274. } else {
  275. [strongSelf.sectionArray removeAllObjects];
  276. strongSelf.homeOrderTableView.contentOffset = CGPointZero;
  277. [strongSelf.homeOrderTableView reloadData];
  278. // process error
  279. NSString *msg = [json objectForKey:@"err_msg"];
  280. // [strongSelf showAlert:msg];
  281. [strongSelf showAlertTilte:@"Warning" message:msg];
  282. }
  283. }
  284. weakSelf.loading = NO;
  285. if (weakSelf.orderSectionCount == 0) {
  286. [weakSelf.homeOrderTableView showEmpty];
  287. } else {
  288. [weakSelf.homeOrderTableView hideEmpty];
  289. }
  290. });
  291. });
  292. }
  293. - (void)updateDriverAvailable:(BOOL)available {
  294. // show progress
  295. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  296. __weak typeof(self) weakSelf = self;
  297. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  298. NSDictionary *json = [RADataProvider updateDriverAvailable:available];
  299. dispatch_async(dispatch_get_main_queue(), ^{
  300. [hud dismiss];
  301. int result = [[json objectForKey:@"result"] intValue];
  302. if (result == RESULT_TRUE) {
  303. weakSelf.headerView.availabel = available;
  304. } else {
  305. NSString *msg = [json objectForKey:@"err_msg"];
  306. [weakSelf showAlertTilte:@"Warning" message:msg];
  307. }
  308. });
  309. });
  310. }
  311. #pragma mark - Controller Action
  312. - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
  313. [self pushDetailViewControllerForOrderID:model.orderID type:model.status type2:model.order_type2 statusNo:model.status_no];
  314. }
  315. - (void)pushDetailViewControllerForOrderID:(NSString *)orderID type:(NSInteger)type type2:(NSString *)type2 statusNo:(NSString *)statusNo {
  316. if (!orderID) {
  317. return;
  318. }
  319. RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
  320. detailVC.orderID = orderID;
  321. detailVC.orderType = type;
  322. detailVC.orderType2 = type2;
  323. detailVC.status_no = statusNo;
  324. [self.navigationController pushViewController:detailVC animated:YES];
  325. }
  326. - (void)showMoreOrderForSection:(NSInteger)section {
  327. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  328. RAHomeMoreViewController *homeMoreVC = [RAHomeMoreViewController viewControllerFromStoryboard];
  329. homeMoreVC.title = sectionModel.title;
  330. homeMoreVC.type = sectionModel.type;
  331. __weak typeof(self) weakSelf = self;
  332. homeMoreVC.backendFlagOrderClickBlk = ^{
  333. weakSelf.reloadFlag = YES;
  334. };
  335. [self.navigationController pushViewController:homeMoreVC animated:YES];
  336. }
  337. #pragma mark - Nofitication Selector
  338. - (void)receiveNewOrderNotification:(NSNotification *)notification {
  339. [self loadData];
  340. }
  341. - (void)receiveReloadNotification:(NSNotification *)notification {
  342. [self loadData];
  343. }
  344. #pragma mark - Header Delegate
  345. - (void)signoutClick:(UIButton *)sender {
  346. [self logoutItemClick:nil];
  347. }
  348. - (void)settingClick:(UIButton *)sender {
  349. RASettingViewController *settingVC = [RASettingViewController viewControllerFromStoryboard];
  350. [self.navigationController pushViewController:settingVC animated:YES];
  351. }
  352. - (void)availableClick:(UIButton *)sender {
  353. NSString *msg = [NSString stringWithFormat:@"are you sure to change status to %@",self.headerView.availabel ? @"Unavailable" : @"Available"];
  354. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:msg preferredStyle:UIAlertControllerStyleAlert];
  355. UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  356. [self updateDriverAvailable:!self.headerView.availabel];
  357. }];
  358. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  359. }];
  360. [alertVC addAction:cancelAction];
  361. [alertVC addAction:yesAction];
  362. [self presentViewController:alertVC animated:YES completion:nil];
  363. }
  364. - (void)messageClick:(UIButton *)sender {
  365. RAMessageViewController *messageVC = [RAMessageViewController viewControllerFromStoryboard];
  366. [self.navigationController pushViewController:messageVC animated:YES];
  367. }
  368. @end