// // RAHomeViewController.m // Apex And Drivers // // Created by Jack on 2018/6/1. // Copyright © 2018年 USAI. All rights reserved. // #import "RAHomeViewController.h" #import "RAHomeOrderModel.h" #import "RAOrderDetailViewController.h" #import "RAProgressHUD.h" #import "ApexDriverUploadListVC.h" #import "RAHomeMoreViewController.h" #import @interface RAHomeSectionModel : NSObject @property (nonatomic,assign) RAOrderStatus type; @property (nonatomic,strong) NSArray *orders; @property (nonatomic,copy) NSString *title; @property (nonatomic,readonly) NSInteger ordersCount; @property (nonatomic,assign) NSInteger section; @property (nonatomic,assign) NSInteger totalCount; @end @implementation RAHomeSectionModel - (instancetype)init { if (self = [super init]) { } return self; } - (void)setOrders:(NSArray *)orders { NSMutableArray *orderArr = [NSMutableArray array]; for (int i = 0; i < orders.count; i++) { NSDictionary *order = [orders objectAtIndex:i]; RAHomeOrderModel *orderModel = [RAHomeOrderModel new]; [orderModel setValuesForKeysWithDictionary:order]; [orderArr addObject:orderModel]; } _orders = orderArr; } - (RAHomeOrderModel *)orderModelForIndex:(NSInteger)index { return [self.orders objectAtIndex:index]; } - (NSInteger)ordersCount { return self.orders.count; } - (NSInteger)orderModelIndexForID:(NSString *)orderID { __block NSInteger curIndex = -1;; [self.orders enumerateObjectsUsingBlock:^(RAHomeOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { if ([obj.orderID isEqualToString:orderID]) { curIndex = idx; return ; } }]; return curIndex; } - (BOOL)hasMoreOrder { return self.totalCount > self.ordersCount; } @end #pragma mark - View Controller @interface RAHomeViewController () @property (nonatomic,strong) NSMutableArray *sectionArray; @property (nonatomic,strong) NSIndexPath *currentIndexPath; @property (nonatomic,strong) UIRefreshControl *refreshControl; @end @implementation RAHomeViewController + (instancetype)viewControllerFromStoryboard { RAHomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]]; return homeVC; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self configureNavigationBar]; [self configureTable]; [self registNotification]; [self loadData]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (self.gotoDetailID) { [self pushDetailViewControllerForOrderID:self.gotoDetailID]; self.gotoDetailID = nil; } } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Configure - (void)configureTable { if (@available(iOS 11, *)) { self.homeOrderTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } self.homeOrderTableView.tableFooterView = [UIView new]; self.homeOrderTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0); UIRefreshControl *refresh = [[UIRefreshControl alloc] init]; [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged]; [self.homeOrderTableView addSubview:refresh]; self.refreshControl = refresh; } - (void)configureNavigationBar { UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"list"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(uploadListItemClick:)]; UIBarButtonItem *logoutItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"logout"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(logoutItemClick:)]; self.navigationItem.rightBarButtonItems = @[logoutItem,uploadListItem]; } - (void)registNotification { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationNewOrder object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveReloadNotification:) name:RANotificationReloadHome object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveGoDetailNotification:) name:RANotificationGoDetail object:nil]; } #pragma mark - Action - (void)uploadListItemClick:(UIBarButtonItem *)sender { ApexDriverUploadListVC *vc = [ApexDriverUploadListVC viewControllerFromStoryboard]; [self.navigationController pushViewController:vc animated:YES]; } - (void)logoutItemClick:(UIBarButtonItem *)sender { // show progress RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ [RADataProvider logout]; dispatch_async(dispatch_get_main_queue(), ^{ // dismiss progress [hud dismiss]; [RASingleton.sharedInstance logout]; [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationLogout object:nil]; }); }); } - (void)refreshControlValueChanged:(UIRefreshControl *)refresh { [self loadData]; } #pragma mark - Getter - (NSMutableArray *)sectionArray { if (!_sectionArray) { _sectionArray = [NSMutableArray array]; } return _sectionArray; } - (NSUInteger)orderSectionCount { return self.sectionArray.count; } - (NSUInteger)orderCountForSection:(NSInteger)section { RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section]; return [sectionModel ordersCount]; } - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath { RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section]; return [sectionModel orderModelForIndex:indexPath.row]; } - (NSString *)titleForSection:(NSInteger)section { RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section]; return sectionModel.title; } - (BOOL)hasMoreOrderForSection:(NSInteger)section { RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section]; return [sectionModel hasMoreOrder]; } #pragma mark - Data - (void)loadData { // show progress RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view]; __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSDictionary *json = [RADataProvider requestOrderList]; NSLog(@"home load data"); dispatch_async(dispatch_get_main_queue(), ^{ // dismiss progress [hud dismiss]; if (weakSelf.refreshControl.isRefreshing) { [weakSelf.refreshControl endRefreshing]; } if (weakSelf) { __strong typeof(weakSelf) strongSelf = weakSelf; int result = [[json objectForKey:@"result"] intValue]; if (result == RESULT_TRUE) { BOOL requiredLocation = [[json objectForKey:@"requiredLocation"] boolValue]; [RASingleton sharedInstance].requiredLocation = requiredLocation; NSArray *sectionArray = [json objectForKey:@"sections"]; [strongSelf.sectionArray removeAllObjects]; strongSelf.currentIndexPath = nil; for (int i = 0; i < sectionArray.count; i++) { NSDictionary *section = [sectionArray objectAtIndex:i]; RAHomeSectionModel *sectionModel = [RAHomeSectionModel new]; sectionModel.section = i; [sectionModel setValuesForKeysWithDictionary:section]; [strongSelf.sectionArray addObject:sectionModel]; if (strongSelf.currentOrderID.length > 0) { NSInteger idx = [sectionModel orderModelIndexForID:self.currentOrderID]; if (idx > -1) { strongSelf.currentIndexPath = [NSIndexPath indexPathForRow:idx inSection:i]; } } } [strongSelf.homeOrderTableView reloadData]; if (strongSelf.currentIndexPath) { // [strongSelf.homeOrderTableView scrollToRowAtIndexPath:self.currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO]; [strongSelf.homeOrderTableView selectRowAtIndexPath:self.currentIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle]; } else { } // // [UIApplication sharedApplication].applicationIconBadgeNumber = 0; // UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; // [center removeAllDeliveredNotifications]; // [center removeAllPendingNotificationRequests]; } else { // process error NSString *msg = [json objectForKey:@"err_msg"]; [strongSelf showAlertTilte:@"Warning" message:msg]; } } }); }); } #pragma mark - Controller Action - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model { [self pushDetailViewControllerForOrderID:model.orderID]; } - (void)pushDetailViewControllerForOrderID:(NSString *)orderID { if (!orderID) { return; } RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard]; detailVC.orderID = orderID; [self.navigationController pushViewController:detailVC animated:YES]; } - (void)showMoreOrderForSection:(NSInteger)section { RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section]; RAHomeMoreViewController *homeMoreVC = [RAHomeMoreViewController viewControllerFromStoryboard]; homeMoreVC.title = sectionModel.title; homeMoreVC.type = sectionModel.type; [self.navigationController pushViewController:homeMoreVC animated:YES]; } #pragma mark - Nofitication Selector - (void)receiveNewOrderNotification:(NSNotification *)notification { [self loadData]; } - (void)receiveReloadNotification:(NSNotification *)notification { [self loadData]; } - (void)receiveGoDetailNotification:(NSNotification *)notification { NSDictionary *userInfo = notification.object; if (userInfo) { NSString *orderID = [userInfo objectForKey:@"orderID"]; if (orderID) { [self pushDetailViewControllerForOrderID:orderID]; } } } @end