| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- //
- // 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 <UserNotifications/UserNotifications.h>
- @interface RAHomeSectionModel : NSObject
- @property (nonatomic,assign) RAOrderStatus type;
- @property (nonatomic,strong) NSArray <RAHomeOrderModel *> *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 <RAHomeSectionModel *> *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 type:self.gotoDetailType type2:self.gotoDetailType2];
- 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:@"upload_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 {
-
- if (self.loading) {
- return;
- }
- self.loading = YES;
-
- // 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"];
-
- NSMutableArray *tmpSectionArr = [NSMutableArray array];
- 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];
- [tmpSectionArr addObject:sectionModel];
- if (strongSelf.currentOrderID.length > 0) {
- NSInteger idx = [sectionModel orderModelIndexForID:self.currentOrderID];
- if (idx > -1) {
- strongSelf.currentIndexPath = [NSIndexPath indexPathForRow:idx inSection:i];
- }
- }
- }
- strongSelf.sectionArray = tmpSectionArr;
- [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 {
-
- [strongSelf.sectionArray removeAllObjects];
- strongSelf.homeOrderTableView.contentOffset = CGPointZero;
- [strongSelf.homeOrderTableView reloadData];
-
- // process error
- NSString *msg = [json objectForKey:@"err_msg"];
- // [strongSelf showAlert:msg];
- [strongSelf showAlertTilte:@"Warning" message:msg];
- }
- }
-
- self.loading = NO;
- });
-
- });
- }
- #pragma mark - Controller Action
- - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
-
- [self pushDetailViewControllerForOrderID:model.orderID type:model.status type2:model.order_type2];
- }
- - (void)pushDetailViewControllerForOrderID:(NSString *)orderID type:(NSInteger)type type2:(NSString *)type2 {
-
- if (!orderID) {
- return;
- }
- RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
- detailVC.orderID = orderID;
- detailVC.orderType = type;
- detailVC.orderType2 = type2;
- [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"];
- NSInteger orderType = [[userInfo objectForKey:@"orderType"] integerValue];
- NSString *orderType2 = [userInfo objectForKey:@"orderType2"];
- if (orderID) {
- [self pushDetailViewControllerForOrderID:orderID type:orderType type2:orderType2];
- }
- }
- }
- @end
|