RAHomeViewController.m 27 KB

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