RAHomeViewController.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. #import <CoreLocation/CoreLocation.h>
  20. //#import "RABadgeButton.h"
  21. @implementation RAHomeSectionModel
  22. - (instancetype)init {
  23. if (self = [super init]) {
  24. }
  25. return self;
  26. }
  27. - (void)setOrders:(NSArray *)orders {
  28. NSMutableArray *orderArr = [NSMutableArray array];
  29. for (int i = 0; i < orders.count; i++) {
  30. NSDictionary *order = [orders objectAtIndex:i];
  31. RAHomeOrderModel *orderModel = [RAHomeOrderModel new];
  32. [orderModel setValuesForKeysWithDictionary:order];
  33. [orderArr addObject:orderModel];
  34. }
  35. _orders = orderArr;
  36. }
  37. - (void)setBackendFlagCount:(NSInteger)backendFlagCount {
  38. _backendFlagCount = backendFlagCount;
  39. if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) {
  40. [self.delegate refreshUI];
  41. }
  42. }
  43. - (RAHomeOrderModel *)orderModelForIndex:(NSInteger)index {
  44. return [self.orders objectAtIndex:index];
  45. }
  46. - (NSInteger)ordersCount {
  47. return self.orders.count;
  48. }
  49. - (NSInteger)orderModelIndexForID:(NSString *)orderID {
  50. __block NSInteger curIndex = -1;;
  51. [self.orders enumerateObjectsUsingBlock:^(RAHomeOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  52. if ([obj.orderID isEqualToString:orderID]) {
  53. curIndex = idx;
  54. return ;
  55. }
  56. }];
  57. return curIndex;
  58. }
  59. - (BOOL)hasMoreOrder {
  60. return self.totalCount > self.ordersCount;
  61. }
  62. - (RAHomeOrderModel *)modelForOrder:(NSString *)orderId {
  63. if (!orderId) {
  64. return nil;
  65. }
  66. __block RAHomeOrderModel *model = nil;
  67. [self.orders enumerateObjectsUsingBlock:^(RAHomeOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  68. if ([obj.orderID isEqualToString:orderId]) {
  69. model = obj;
  70. }
  71. }];
  72. return model;
  73. }
  74. @end
  75. #pragma mark - View Controller
  76. @interface RAHomeViewController () <RAHomeHeaderDelegate>
  77. @property (nonatomic,strong) NSMutableArray <RAHomeSectionModel *> *sectionArray;
  78. @property (nonatomic,strong) NSIndexPath *currentIndexPath;
  79. @property (nonatomic,strong) UIRefreshControl *refreshControl;
  80. @property (nonatomic,assign) NSUInteger messageCount;///<未读消息数量
  81. @end
  82. @implementation RAHomeViewController
  83. + (instancetype)viewControllerFromStoryboard {
  84. RAHomeViewController *homeVC = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  85. return homeVC;
  86. }
  87. - (void)viewDidLoad {
  88. [super viewDidLoad];
  89. // Do any additional setup after loading the view.
  90. self.label_cache.text = NSLocalizedString(@"cache content", nil);
  91. [self.label_cache sizeToFit];
  92. self.label_cache.layer.borderColor=ApexDriverGrayColor.CGColor;
  93. self.label_cache.layer.borderWidth = 1.0f;
  94. self.label_cache.layer.cornerRadius = 5.0f;
  95. self.label_cache.layer.masksToBounds = YES;
  96. [self checkPermission];
  97. [self configureNavigationBar];
  98. [self configureTable];
  99. [self registNotification];
  100. [self loadData];
  101. }
  102. - (void)viewWillAppear:(BOOL)animated {
  103. [super viewWillAppear:animated];
  104. }
  105. - (void)viewDidAppear:(BOOL)animated {
  106. [super viewDidAppear:animated];
  107. if (self.reloadFlag) {
  108. [self loadData];
  109. self.reloadFlag = !self.reloadFlag;
  110. }
  111. }
  112. - (void)dealloc {
  113. [[NSNotificationCenter defaultCenter] removeObserver:self];
  114. }
  115. - (void)didReceiveMemoryWarning {
  116. [super didReceiveMemoryWarning];
  117. // Dispose of any resources that can be recreated.
  118. }
  119. #pragma mark - Configure
  120. - (void)configureTable {
  121. if (@available(iOS 11, *)) {
  122. self.homeOrderTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  123. }
  124. // else {
  125. // self.automaticallyAdjustsScrollViewInsets = NO;
  126. // }
  127. self.homeOrderTableView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
  128. self.homeOrderTableView.tableFooterView = [UIView new];
  129. self.homeOrderTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  130. UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
  131. [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
  132. [self.homeOrderTableView addSubview:refresh];
  133. self.refreshControl = refresh;
  134. // self.homeOrderTableView.emptyDataViewManualsize = true;
  135. // empty
  136. // self.homeOrderTableView setempty
  137. // __weak typeof(self) weakSelf = self;
  138. // self.homeOrderTableView.emptyView = [RAEmptyView emptyViewWithTapBlk:^(id sender) {
  139. // [weakSelf loadData];
  140. // }];
  141. // Header
  142. self.headerView = [RAHomeHeaderView homeHeader];
  143. self.headerView.delegate = self;
  144. self.homeOrderTableView.tableHeaderView = self.headerView;
  145. // self.emptyReloadview =[[UIView alloc] initWithFrame:CGRectMake(200, 200, 300, 300)];
  146. //// self.emptyReloadview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  147. //// [self.homeOrderTableView addSubview:self.emptyContentview];
  148. // [self.homeOrderTableView insertSubview:self.emptyReloadview atIndex:0];
  149. //
  150. // self.emptyReloadview.backgroundColor = [UIColor redColor];
  151. // self.emptyReloadview.hidden = NO;
  152. // [self.homeOrderTableView bringSubviewToFront:self.emptyReloadview];
  153. __weak typeof(self) weakSelf = self;
  154. RAEmptyView* ev = [RAEmptyView emptyViewWithTapBlk:^(id sender) {
  155. [weakSelf loadData];
  156. }];
  157. self.Reloadview = ev;
  158. [self.homeOrderTableView addSubview:ev];
  159. // CGRect tvrect = self.homeOrderTableView.frame;
  160. // CGRect hvrect = self.homeOrderTableView.tableHeaderView.frame;
  161. ev.hidden = YES;
  162. // ev.translatesAutoresizingMaskIntoConstraints = false;
  163. // NSLayoutConstraint *centerYConstraint =
  164. // [NSLayoutConstraint constraintWithItem:ev
  165. // attribute:NSLayoutAttributeCenterY
  166. // relatedBy:NSLayoutRelationEqual
  167. // toItem:self.homeOrderTableView
  168. // attribute:NSLayoutAttributeCenterY
  169. // multiplier:1.0
  170. // constant:0.0];
  171. // [self.homeOrderTableView addConstraint:centerYConstraint];
  172. //
  173. ev.center = self.homeOrderTableView.center;
  174. }
  175. - (void)updateViewConstraints
  176. {
  177. [super updateViewConstraints];
  178. //
  179. // //约束条件
  180. // //redview在垂直方向上距离边界为50
  181. // UIView* dv= self.emptyReloadview;
  182. // dv.translatesAutoresizingMaskIntoConstraints=NO;
  183. // NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:dv attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.homeOrderTableView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
  184. // NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:dv attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.homeOrderTableView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
  185. //
  186. // NSLayoutConstraint *constraint3 = [NSLayoutConstraint constraintWithItem:dv attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.homeOrderTableView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
  187. //
  188. // NSLayoutConstraint *constraint4 = [NSLayoutConstraint constraintWithItem:dv attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.homeOrderTableView attribute:NSLayoutAttributeRight multiplier:1 constant:0];
  189. //
  190. // [self.homeOrderTableView addConstraint:constraint1];
  191. // [self.homeOrderTableView addConstraint:constraint2];
  192. // [self.homeOrderTableView addConstraint:constraint3];
  193. // [self.homeOrderTableView addConstraint:constraint4];
  194. }
  195. - (void)configureNavigationBar {
  196. UIImage *logo = [[UIImage imageNamed:@"apex_logo"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  197. // UIImage *logo = [[UIImage imageNamed:@"apexlogo-2"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
  198. UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithImage:logo landscapeImagePhone:logo style:UIBarButtonItemStylePlain target:nil action:nil];
  199. // logoItem.enabled = NO;
  200. self.navigationItem.leftBarButtonItem = logoItem;
  201. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"upload_list"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  202. style:UIBarButtonItemStylePlain
  203. target:self
  204. action:@selector(uploadListItemClick:)];
  205. self.navigationItem.rightBarButtonItems = @[uploadListItem];
  206. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationRequestUploadCount object:nil];
  207. self.title = [NSString stringWithFormat:@"%@ %@",RASingleton.sharedInstance.user,RASingleton.sharedInstance.firstName];
  208. }
  209. - (void)registNotification {
  210. // [RASingleton.sharedInstance writeLog:[NSString stringWithFormat:@"%s",__func__]];
  211. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNewOrderNotification:) name:RANotificationHandleOrder object:nil];
  212. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveReloadNotification:) name:RANotificationReloadHome object:nil];
  213. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveCheckDetailNofitication:) name:RANotificationCheckDetail object:nil];
  214. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveUploadFinishNotification:) name:RANotificationChangeUpload object:nil];
  215. }
  216. - (void)checkPermission {
  217. CLAuthorizationStatus locationAuthoriztionAstatus = [CLLocationManager authorizationStatus];
  218. [[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
  219. UNAuthorizationStatus notificationAuthorizationStatus = settings.authorizationStatus;
  220. BOOL enableLocation = locationAuthoriztionAstatus != kCLAuthorizationStatusRestricted && locationAuthoriztionAstatus != kCLAuthorizationStatusDenied;
  221. BOOL enableRemoteNotification = notificationAuthorizationStatus != UNAuthorizationStatusDenied;
  222. if (!enableLocation | !enableRemoteNotification) {
  223. NSMutableString *msg = [NSMutableString stringWithString:NSLocalizedString(@"you should open", nil)];
  224. if (!enableLocation) {
  225. [msg appendString:NSLocalizedString(@" location permissions", nil)];
  226. }
  227. if (!enableRemoteNotification) {
  228. if (!enableLocation) {
  229. [msg appendString:NSLocalizedString(@" and notification permissions", nil)];
  230. } else {
  231. [msg appendString:NSLocalizedString(@" notification permissions", nil)];
  232. }
  233. }
  234. dispatch_async(dispatch_get_main_queue(), ^{
  235. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil) message:msg preferredStyle:UIAlertControllerStyleAlert];
  236. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  237. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  238. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  239. // [[UIApplication sharedApplication] openURL:url];
  240. [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly : @NO} completionHandler:nil];
  241. }
  242. }];
  243. [alertVC addAction:okAction];
  244. [self presentViewController:alertVC animated:YES completion:nil];
  245. });
  246. }
  247. }];
  248. }
  249. #pragma mark - Private
  250. - (void)_checkOrder:(NSString *)orderId {
  251. if (orderId) {
  252. for (RAHomeSectionModel *section in self.sectionArray) {
  253. RAHomeOrderModel *model = [section modelForOrder:orderId];
  254. dispatch_async(dispatch_get_main_queue(), ^{
  255. if (model && model.backendFlag) {
  256. if (section.backendFlagCount > 0) {
  257. section.backendFlagCount = section.backendFlagCount - 1;
  258. }
  259. model.backendFlag = NO;
  260. self.messageCount = self.messageCount - 1;
  261. }
  262. });
  263. }
  264. }
  265. }
  266. - (void)_updateUploadItem:(UIBarButtonItem *)item withUploadCount:(NSUInteger)count {
  267. item.tintColor = ApexDriverWhiteColor;
  268. // if (item) {
  269. //
  270. // if (count > 0) {
  271. // item.tintColor = UIColorFromRGB(0x299D4D);
  272. // } else {
  273. // item.tintColor = ApexDriverWhiteColor;
  274. // }
  275. // }
  276. }
  277. #pragma mark - Action
  278. - (void)uploadListItemClick:(UIBarButtonItem *)sender {
  279. ApexDriverUploadListVC *vc = [ApexDriverUploadListVC viewControllerFromStoryboard];
  280. [self.navigationController pushViewController:vc animated:YES];
  281. }
  282. - (void)logoutItemClick:(UIBarButtonItem *)sender {
  283. // show progress
  284. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  285. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  286. [RADataProvider logout];
  287. dispatch_async(dispatch_get_main_queue(), ^{
  288. // dismiss progress
  289. [hud dismiss];
  290. [RASingleton.sharedInstance logout];
  291. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationLogout object:nil];
  292. });
  293. });
  294. }
  295. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  296. self.currentIndexPath = nil;
  297. [self loadData];
  298. }
  299. #pragma mark - Setter
  300. - (void)setHeaderView:(RAHomeHeaderView *)headerView {
  301. _headerView = headerView;
  302. }
  303. - (void)setMessageCount:(NSUInteger)messageCount {
  304. _messageCount = messageCount;
  305. dispatch_async(dispatch_get_main_queue(), ^{
  306. self.headerView.existNewMessage = messageCount > 0;
  307. });
  308. }
  309. #pragma mark - Getter
  310. - (NSMutableArray *)sectionArray {
  311. if (!_sectionArray) {
  312. _sectionArray = [NSMutableArray array];
  313. }
  314. return _sectionArray;
  315. }
  316. - (RAHomeSectionModel *)modelForSection:(NSInteger)section {
  317. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  318. return sectionModel;
  319. }
  320. - (NSUInteger)orderSectionCount {
  321. return self.sectionArray.count;
  322. }
  323. - (NSUInteger)orderCountForSection:(NSInteger)section {
  324. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  325. return [sectionModel ordersCount];
  326. }
  327. - (RAHomeOrderModel *)orderModelForIndexPath:(NSIndexPath *)indexPath {
  328. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:indexPath.section];
  329. return [sectionModel orderModelForIndex:indexPath.row];
  330. }
  331. - (NSString *)titleForSection:(NSInteger)section {
  332. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  333. return sectionModel.title;
  334. }
  335. - (BOOL)hasMoreOrderForSection:(NSInteger)section {
  336. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  337. return [sectionModel hasMoreOrder];
  338. }
  339. - (NSInteger)backendCountForSection:(NSInteger)section {
  340. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  341. return sectionModel.backendFlagCount;
  342. }
  343. #pragma mark - Set/Update
  344. - (void)setBackendFlagCount:(NSInteger)count forSection:(NSInteger)section {
  345. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  346. sectionModel.backendFlagCount = count;
  347. }
  348. - (void)decreaseBackendFlagCountForSection:(NSInteger)section {
  349. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  350. NSInteger count = sectionModel.backendFlagCount;
  351. sectionModel.backendFlagCount = --count;
  352. }
  353. - (void)setHeaderDelegate:(id<RAHomeSectionModelDelegate>)delegate forSection:(NSInteger)section {
  354. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  355. sectionModel.delegate = delegate;
  356. }
  357. #pragma mark - Data
  358. - (void)loadData {
  359. self.label_cache.hidden=true;
  360. if (self.loading) {
  361. return;
  362. }
  363. self.loading = YES;
  364. // [self.homeOrderTableView hideEmpty];
  365. self.Reloadview.hidden = true;
  366. // show progress
  367. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  368. __weak typeof(self) weakSelf = self;
  369. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  370. NSDictionary *json = [RADataProvider requestOrderList];
  371. dispatch_async(dispatch_get_main_queue(), ^{
  372. // dismiss progress
  373. [hud dismiss];
  374. if (weakSelf.refreshControl.isRefreshing) {
  375. [weakSelf.refreshControl endRefreshing];
  376. }
  377. if (weakSelf) {
  378. __strong typeof(weakSelf) strongSelf = weakSelf;
  379. // header
  380. BOOL driverAvailable = [[json objectForKey:@"driver_available"] boolValue];
  381. strongSelf.headerView.availabel = driverAvailable;
  382. NSInteger newCount = [[json objectForKey:@"new_count"] integerValue];
  383. NSInteger processingCount = [[json objectForKey:@"processing_count"] integerValue];
  384. NSInteger finishCount = [[json objectForKey:@"finish_count"] integerValue];
  385. NSInteger messageCount = [[json objectForKey:@"message_count"] integerValue];
  386. strongSelf.headerView.NewOrderCount = newCount;
  387. strongSelf.headerView.ProcessingOrderCount = processingCount;
  388. strongSelf.headerView.FinishOrderCount = finishCount;
  389. strongSelf.messageCount = messageCount;
  390. int result = [[json objectForKey:@"result"] intValue];
  391. if (result == RESULT_TRUE) {
  392. int badge = [json[@"badge"] intValue];
  393. [UIApplication sharedApplication].applicationIconBadgeNumber=badge;
  394. BOOL requiredLocation = [[json objectForKey:@"requiredLocation"] boolValue];
  395. [RASingleton sharedInstance].requiredBackgroundLocation = requiredLocation;
  396. NSArray *sectionArray = [json objectForKey:@"sections"];
  397. NSMutableArray *tmpSectionArr = [NSMutableArray array];
  398. strongSelf.currentIndexPath = nil;
  399. for (int i = 0; i < sectionArray.count; i++) {
  400. NSDictionary *section = [sectionArray objectAtIndex:i];
  401. RAHomeSectionModel *sectionModel = [RAHomeSectionModel new];
  402. sectionModel.section = i;
  403. [sectionModel setValuesForKeysWithDictionary:section];
  404. [tmpSectionArr addObject:sectionModel];
  405. if (strongSelf.currentOrderID.length > 0) {
  406. NSInteger idx = [sectionModel orderModelIndexForID:self.currentOrderID];
  407. if (idx > -1) {
  408. strongSelf.currentIndexPath = [NSIndexPath indexPathForRow:idx inSection:i];
  409. }
  410. }
  411. }
  412. strongSelf.sectionArray = tmpSectionArr;
  413. [strongSelf.homeOrderTableView reloadData];
  414. if (strongSelf.currentIndexPath) {
  415. // [strongSelf.homeOrderTableView scrollToRowAtIndexPath:self.currentIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
  416. [strongSelf.homeOrderTableView selectRowAtIndexPath:strongSelf.currentIndexPath animated:NO scrollPosition:UITableViewScrollPositionMiddle];
  417. } else {
  418. }
  419. if([json[@"iscache"] boolValue]==true)
  420. {
  421. self.label_cache.hidden=false;
  422. }
  423. } else {
  424. [strongSelf.sectionArray removeAllObjects];
  425. strongSelf.homeOrderTableView.contentOffset = CGPointZero;
  426. [strongSelf.homeOrderTableView reloadData];
  427. // process error
  428. NSString *msg = [json objectForKey:@"err_msg"];
  429. // [strongSelf showAlert:msg];
  430. [strongSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg];
  431. }
  432. }
  433. weakSelf.loading = NO;
  434. if (weakSelf.orderSectionCount == 0) {
  435. // [weakSelf.homeOrderTableView showEmpty];
  436. self.Reloadview.hidden = false;
  437. } else {
  438. // [weakSelf.homeOrderTableView hideEmpty];
  439. self.Reloadview.hidden = true;
  440. }
  441. });
  442. });
  443. }
  444. - (void)updateDriverAvailable:(BOOL)available {
  445. // show progress
  446. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  447. __weak typeof(self) weakSelf = self;
  448. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  449. NSDictionary *json = [RADataProvider updateDriverAvailable:available];
  450. dispatch_async(dispatch_get_main_queue(), ^{
  451. [hud dismiss];
  452. int result = [[json objectForKey:@"result"] intValue];
  453. if (result == RESULT_TRUE) {
  454. weakSelf.headerView.availabel = available;
  455. } else {
  456. NSString *msg = [json objectForKey:@"err_msg"];
  457. [weakSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg];
  458. }
  459. });
  460. });
  461. }
  462. #pragma mark - Controller Action
  463. - (void)pushDetailViewControllerForModel:(RAHomeOrderModel *)model {
  464. [self pushDetailViewControllerForOrderID:model.orderID type:model.status type2:model.order_type2 statusNo:model.status_no];
  465. }
  466. - (void)pushDetailViewControllerForOrderID:(NSString *)orderID type:(NSInteger)type type2:(NSString *)type2 statusNo:(NSString *)statusNo {
  467. if (!orderID) {
  468. return;
  469. }
  470. RAOrderDetailViewController *detailVC = [RAOrderDetailViewController viewControllerFromStoryboard];
  471. detailVC.orderID = orderID;
  472. detailVC.orderType = type;
  473. detailVC.orderType2 = type2;
  474. detailVC.status_no = statusNo;
  475. [self.navigationController pushViewController:detailVC animated:YES];
  476. }
  477. - (void)showMoreControllerWithTitle:(NSString *)title Type:(RAOrderStatus)type {
  478. RAHomeMoreViewController *homeMoreVC = [RAHomeMoreViewController viewControllerFromStoryboard];
  479. homeMoreVC.title = title;
  480. homeMoreVC.type = type;
  481. __weak typeof(self) weakSelf = self;
  482. homeMoreVC.backendFlagOrderClickBlk = ^{
  483. weakSelf.reloadFlag = YES;
  484. };
  485. [self.navigationController pushViewController:homeMoreVC animated:YES];
  486. }
  487. - (void)showMoreOrderForSection:(NSInteger)section {
  488. RAHomeSectionModel *sectionModel = [self.sectionArray objectAtIndex:section];
  489. [self showMoreControllerWithTitle:sectionModel.title Type:sectionModel.type];
  490. }
  491. #pragma mark - Nofitication Selector
  492. - (void)receiveNewOrderNotification:(NSNotification *)notification {
  493. [self loadData];
  494. }
  495. - (void)receiveReloadNotification:(NSNotification *)notification {
  496. [self loadData];
  497. }
  498. - (void)receiveCheckDetailNofitication:(NSNotification *)notification {
  499. NSDictionary *userInfo = [notification userInfo];
  500. if (userInfo) {
  501. NSString *orderId = [userInfo objectForKey:@"order_id"];
  502. if (orderId) {
  503. [self _checkOrder:orderId];
  504. }
  505. }
  506. }
  507. - (void)receiveUploadFinishNotification:(NSNotification *)notification {
  508. NSUInteger count = [notification.userInfo[@"upload_count"] integerValue];
  509. UIBarButtonItem *item = self.navigationItem.rightBarButtonItem;
  510. [self _updateUploadItem:item withUploadCount:count];
  511. }
  512. #pragma mark - Header Delegate
  513. - (void)signoutClick:(UIButton *)sender {
  514. [self logoutItemClick:nil];
  515. }
  516. - (void)settingClick:(UIButton *)sender {
  517. dispatch_async(dispatch_get_main_queue(), ^{
  518. RASettingViewController *settingVC = [RASettingViewController viewControllerFromStoryboard];
  519. [self.navigationController pushViewController:settingVC animated:YES];
  520. });
  521. }
  522. - (void)availableClick:(UIButton *)sender {
  523. NSString *msg = [NSString localizedStringWithFormat:NSLocalizedString(@"are you sure to change status to %@", nil),self.headerView.availabel ? NSLocalizedString(@"Unavailable", nil) : NSLocalizedString(@"Available", nil)];
  524. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil) message:msg preferredStyle:UIAlertControllerStyleAlert];
  525. UIAlertAction *yesAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"YES", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  526. [self updateDriverAvailable:!self.headerView.availabel];
  527. }];
  528. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  529. }];
  530. [alertVC addAction:cancelAction];
  531. [alertVC addAction:yesAction];
  532. [self presentViewController:alertVC animated:YES completion:nil];
  533. }
  534. - (void)messageClick:(UIButton *)sender {
  535. RAMessageViewController *messageVC = [RAMessageViewController viewControllerFromStoryboard];
  536. [self.navigationController pushViewController:messageVC animated:YES];
  537. }
  538. - (void)newOrderClick:(id)sender {
  539. [self showMoreControllerWithTitle:NSLocalizedString(@"New Order", nil) Type:RAOrderStatusNew];
  540. }
  541. - (void)processingOrderClick:(id)sender {
  542. [self showMoreControllerWithTitle:NSLocalizedString(@"Processing Order", nil) Type:RAOrderStatusProcessing];
  543. }
  544. - (void)finishedOrderClick:(id)sender {
  545. [self showMoreControllerWithTitle:NSLocalizedString(@"Finished Order", nil) Type:RAOrderStatusFinish];
  546. }
  547. @end