HomeViewController.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. //
  2. // HomeViewController.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 25/01/2018.
  6. // Copyright © 2018 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "HomeViewController.h"
  9. #import "ShipingStatusCell.h"
  10. #import "ShipSearchController.h"
  11. #import "ApexMobileDB.h"
  12. #import "DetailTabBarController.h"
  13. #import "RAUtils.h"
  14. #import "KPICell.h"
  15. #import "KPITableCell.h"
  16. #import "AMResultViewController.h"
  17. #import <MapKit/MapKit.h>
  18. #define SHIP_CELL_IDENTIFIER @"ShippingStatusCell"
  19. #define KPI_CELL_IDENTIFIER @"KPITableCell"
  20. typedef enum {
  21. HomeModeRecent = 0,
  22. HomeModeKPI = 1
  23. } HomeMode;
  24. @interface HomeViewController () <UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,KPIDelegate>
  25. @property (strong, nonatomic) IBOutlet UISegmentedControl *segmentControl;
  26. @property (nonatomic,strong) IBOutlet UIView *ShipContainer;
  27. @property (nonatomic,strong) IBOutlet UIActivityIndicatorView *ShipLoading;
  28. @property (nonatomic,strong) IBOutlet UIButton *ShipNoDataBtn;
  29. @property (strong, nonatomic) IBOutlet UITableView *shipTableView;
  30. @property (strong, nonatomic) IBOutlet UISearchBar *shipSearchBar;
  31. @property (nonatomic,strong) NSMutableArray *shipArray;
  32. @property (nonatomic,strong) UIRefreshControl *refreshControl;
  33. @property (nonatomic,strong) IBOutlet UIView *KPIContainer;
  34. @property (nonatomic,strong) IBOutlet UIActivityIndicatorView *KPILoading;
  35. @property (nonatomic,strong) IBOutlet UIButton *KPINoDataBtn;
  36. @property (nonatomic,strong) IBOutlet UITableView *KPITableView;
  37. @property (nonatomic,strong) UIRefreshControl *KPIRefresh;
  38. @property (nonatomic,strong) NSMutableArray *KPIArray;
  39. @property (nonatomic,strong) NSArray *monthArray;
  40. @property (nonatomic,assign) HomeMode mode;///<
  41. @property (nonatomic,assign) NSInteger currentKPI;
  42. @property (nonatomic,strong) UIAlertController *progressDialog;
  43. @property (nonatomic,assign) BOOL recentRefresh;
  44. @property (nonatomic,assign) BOOL kpiRefresh;
  45. @property (nonatomic,strong) IBOutlet MKMapView *mapView;
  46. @end
  47. @implementation HomeViewController
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. // Do any additional setup after loading the view.
  51. if (@available(iOS 11,*)) {
  52. self.shipTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  53. } else {
  54. self.automaticallyAdjustsScrollViewInsets = NO;
  55. }
  56. self.currentKPI = 1;
  57. self.segmentControl.tintColor = APGRAYCOLOR;
  58. self.recentRefresh = NO;
  59. self.kpiRefresh = NO;
  60. [self registNotification];
  61. [self configureTableView];
  62. self.shipSearchBar.delegate =self;
  63. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logout) name:APLogoutNotification object:nil];
  64. // self.loadIndicator.layer.cornerRadius = 5;
  65. // self.loadIndicator.layer.masksToBounds = YES;
  66. // self.loadIndicator.layer.borderColor = [UIColor colorWithRed:169 / 255.0 green:169 / 255.0 blue:169 / 255.0 alpha:1.0].CGColor;
  67. // self.loadIndicator.layer.borderWidth = 0.3f;
  68. // self.loadIndicator.color = [UIColor colorWithRed:43 / 255.0 green:43 / 255.0 blue:43 / 255.0 alpha:1.0];
  69. // self.loadIndicator.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:0.9];
  70. }
  71. - (void)didReceiveMemoryWarning {
  72. [super didReceiveMemoryWarning];
  73. // Dispose of any resources that can be recreated.
  74. }
  75. - (void)viewWillAppear:(BOOL)animated {
  76. [super viewWillAppear:animated];
  77. [self clearNavigationbar];
  78. [self configureNavigationBar];
  79. if (self.mode == HomeModeRecent) {
  80. if (self.shipArray.count == 0 || self.recentRefresh) {
  81. [self loadData];
  82. }
  83. } else if (self.mode == HomeModeKPI) {
  84. if (self.KPIArray.count == 0 || self.kpiRefresh) {
  85. [self loadKPIData];
  86. } else {
  87. NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
  88. self.parentViewController.title = str;
  89. }
  90. }
  91. }
  92. - (void)viewWillDisappear:(BOOL)animated {
  93. [super viewWillDisappear:animated];
  94. self.parentViewController.title = nil;
  95. }
  96. - (void)clearNavigationbar {
  97. self.tabBarController.navigationItem.leftBarButtonItem = nil;
  98. self.tabBarController.navigationItem.leftBarButtonItems = nil;
  99. self.tabBarController.navigationItem.titleView = nil;
  100. self.tabBarController.navigationItem.title = nil;
  101. self.tabBarController.navigationItem.rightBarButtonItem = nil;
  102. self.tabBarController.navigationItem.rightBarButtonItems = nil;
  103. }
  104. - (void)configureNavigationBar {
  105. if (self.navigationController && !self.navigationController.isNavigationBarHidden) {
  106. UIImage *logo = [[UIImage imageNamed:@"apexlogo-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  107. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  108. if (appDelegate.personalmode) {
  109. logo = [[UIImage imageNamed:@"apexlogo-2p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  110. }
  111. UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithImage:logo landscapeImagePhone:logo style:UIBarButtonItemStylePlain target:self action:@selector(switchMode:)];
  112. logoItem.enabled = true;
  113. self.tabBarController.navigationItem.leftBarButtonItem = logoItem;
  114. UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"bar_search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(searchItemClick:)];
  115. self.tabBarController.navigationItem.rightBarButtonItem = searchItem;
  116. }
  117. }
  118. - (void)configureTableView {
  119. CGFloat w = CGRectGetWidth(self.shipTableView.bounds);
  120. self.shipTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, CGFLOAT_MIN)];
  121. self.shipTableView.tableFooterView = [UIView new];
  122. [self.shipTableView registerNib:[UINib nibWithNibName:@"ShippingStatusCell" bundle:nil] forCellReuseIdentifier:SHIP_CELL_IDENTIFIER];
  123. [self.shipTableView registerNib:[UINib nibWithNibName:@"KPITableCell" bundle:nil] forCellReuseIdentifier:KPI_CELL_IDENTIFIER];
  124. UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
  125. refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"loading data..."];
  126. [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
  127. [self.shipTableView addSubview:refresh];
  128. self.refreshControl = refresh;
  129. // KPI
  130. self.KPITableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, CGFLOAT_MIN)];
  131. self.KPITableView.tableFooterView = [UIView new];
  132. [self.KPITableView registerNib:[UINib nibWithNibName:@"KPITableCell" bundle:nil] forCellReuseIdentifier:KPI_CELL_IDENTIFIER];
  133. UIRefreshControl *KPIRefresh = [[UIRefreshControl alloc] init];
  134. KPIRefresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"loading data..."];
  135. [KPIRefresh addTarget:self action:@selector(KPIRefreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
  136. [self.KPITableView addSubview:KPIRefresh];
  137. self.KPIRefresh = KPIRefresh;
  138. }
  139. - (void)registNotification {
  140. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePersonModeChangeNotification:) name:APPersonModeChangeNotification object:nil];
  141. }
  142. - (void)logout {
  143. [self.shipArray removeAllObjects];
  144. [self.shipTableView reloadData];
  145. self.currentKPI = 1;
  146. self.monthArray = nil;
  147. [self.KPIArray removeAllObjects];
  148. [self.KPITableView reloadData];
  149. }
  150. - (void)handlePersonModeChangeNotification:(NSNotification *)notification {
  151. [self reloadContent];
  152. }
  153. - (void)postPersonModeChangeNotification {
  154. [[NSNotificationCenter defaultCenter] postNotificationName:APPersonModeChangeNotification object:nil];
  155. }
  156. - (void)dealloc {
  157. [[NSNotificationCenter defaultCenter] removeObserver:self];
  158. }
  159. #pragma mark - Private
  160. - (UIAlertController *)progressDialog {
  161. if (!_progressDialog) {
  162. UIAlertController *alertController = [UIAlertController alertControllerWithTitle: nil
  163. message: @"loading..."
  164. preferredStyle: UIAlertControllerStyleAlert];
  165. UIViewController *customVC = [[UIViewController alloc] init];
  166. UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  167. // spinner.color = [UIColor redColor];
  168. [spinner startAnimating];
  169. [customVC.view addSubview:spinner];
  170. [customVC.view addConstraint:[NSLayoutConstraint
  171. constraintWithItem: spinner
  172. attribute:NSLayoutAttributeCenterX
  173. relatedBy:NSLayoutRelationEqual
  174. toItem:customVC.view
  175. attribute:NSLayoutAttributeCenterX
  176. multiplier:1.0f
  177. constant:0.0f]];
  178. [customVC.view addConstraint:[NSLayoutConstraint
  179. constraintWithItem: spinner
  180. attribute:NSLayoutAttributeCenterY
  181. relatedBy:NSLayoutRelationEqual
  182. toItem:customVC.view
  183. attribute:NSLayoutAttributeCenterY
  184. multiplier:1.0f
  185. constant:0.0f]];
  186. [alertController setValue:customVC forKey:@"contentViewController"];
  187. _progressDialog = alertController;
  188. }
  189. return _progressDialog;
  190. }
  191. - (void)showProgressDialog {
  192. [self presentViewController:self.progressDialog animated:true completion:nil];
  193. }
  194. - (void)dismissProgressDialog {
  195. [self.progressDialog dismissViewControllerAnimated:YES completion:nil];
  196. }
  197. #pragma mark - Load Data
  198. - (NSMutableArray *)shipArray {
  199. if (!_shipArray) {
  200. _shipArray = [NSMutableArray array];
  201. }
  202. return _shipArray;
  203. }
  204. - (NSMutableArray *)KPIArray {
  205. if (!_KPIArray) {
  206. _KPIArray = [NSMutableArray array];
  207. }
  208. return _KPIArray;
  209. }
  210. - (void)reloadContent {
  211. NSLog(@"Home Person Mode Change");
  212. self.recentRefresh = YES;
  213. self.kpiRefresh = YES;
  214. if (((UITabBarController *)self.parentViewController).selectedIndex == 0) {
  215. if (self.mode == HomeModeRecent) {
  216. [self loadData];
  217. } else {
  218. [self loadKPIData];
  219. }
  220. }
  221. }
  222. - (void)loadData {
  223. // NSString *path = [[NSBundle mainBundle] pathForResource:@"fake_container_list.json" ofType:nil];
  224. // NSData *data = [[NSData alloc] initWithContentsOfFile:path];
  225. //
  226. // NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
  227. // 避免重复刷新
  228. if (self.ShipLoading.isAnimating) {
  229. return;
  230. }
  231. [self.ShipLoading startAnimating];
  232. [self showProgressDialog];
  233. __weak typeof(self) weakSelf = self;
  234. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  235. NSDictionary *json = [RANetwork requestHome];
  236. // NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fake_home.json" ofType:nil]];
  237. // NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
  238. dispatch_async(dispatch_get_main_queue(), ^{
  239. if (self.refreshControl.isRefreshing) {
  240. [self.refreshControl endRefreshing];
  241. }
  242. [self.ShipLoading stopAnimating];
  243. [weakSelf dismissProgressDialog];
  244. int result = [[json objectForKey:@"result"] intValue];
  245. if (result == RESULT_TRUE) {
  246. [self.shipArray removeAllObjects];
  247. [self.shipArray addObjectsFromArray:[json objectForKey:@"container_list"]];
  248. [self.shipTableView reloadData];
  249. } else {
  250. NSString *msg = [json objectForKey:@"err_msg"];
  251. [RAUtils message_alert:msg title:@"Warning" controller:weakSelf];
  252. }
  253. self.ShipNoDataBtn.hidden = self.shipArray.count > 0;
  254. if (self.shipArray.count) {
  255. [self.shipTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
  256. }
  257. weakSelf.recentRefresh = NO;
  258. });
  259. });
  260. }
  261. - (void)loadKPIData {
  262. if (self.KPILoading.isAnimating) {
  263. return;
  264. }
  265. [self.KPILoading startAnimating];
  266. [self showProgressDialog];
  267. __weak typeof(self) weakSelf = self;
  268. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  269. NSDictionary *json = [RANetwork requestKPI];
  270. dispatch_async(dispatch_get_main_queue(), ^{
  271. if (weakSelf.KPIRefresh.isRefreshing) {
  272. [weakSelf.KPIRefresh endRefreshing];
  273. }
  274. [weakSelf.KPILoading stopAnimating];
  275. [weakSelf dismissProgressDialog];
  276. int result = [[json objectForKey:@"result"] intValue];
  277. if (result == RESULT_TRUE) {
  278. // self.currentKPI = 1;
  279. weakSelf.monthArray = [json objectForKey:@"month"];
  280. [weakSelf.KPIArray removeAllObjects];
  281. NSArray *kpiArr = [json objectForKey:@"KPI"];
  282. // [self.KPIArray addObject:[[kpiArr firstObject] mutableCopy]];
  283. for (int i = 0; i < kpiArr.count; i++) {
  284. NSMutableDictionary *kpiModel = [[kpiArr objectAtIndex:i] mutableCopy];
  285. NSMutableArray *itemMutArr = [NSMutableArray array];
  286. NSArray *itemArr = [kpiModel objectForKey:@"arr_val"];
  287. for (int j = 0; j < itemArr.count; j++) {
  288. NSMutableDictionary *item = [[itemArr objectAtIndex:j] mutableCopy];
  289. [itemMutArr addObject:item];
  290. }
  291. [kpiModel setObject:itemMutArr forKey:@"arr_val"];
  292. // [self.KPIArray addObjectsFromArray:kpiArr];
  293. [weakSelf.KPIArray addObject:kpiModel];
  294. }
  295. // [self.KPIArray addObject:[[kpiArr lastObject] mutableCopy]];
  296. [weakSelf.KPITableView reloadData];
  297. if (((UITabBarController *)weakSelf.parentViewController).selectedIndex == 0) {
  298. NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
  299. weakSelf.parentViewController.title = str;
  300. }
  301. } else {
  302. NSString *msg = [json objectForKey:@"err_msg"];
  303. [RAUtils message_alert:msg title:@"Warning" controller:weakSelf];
  304. }
  305. weakSelf.KPINoDataBtn.hidden = self.KPIArray.count > 0;
  306. weakSelf.kpiRefresh = NO;
  307. });
  308. });
  309. }
  310. #pragma mark - Actinon
  311. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  312. [self loadData];
  313. }
  314. - (void)KPIRefreshControlValueChanged:(UIRefreshControl *)refresh {
  315. [self loadKPIData];
  316. }
  317. - (void)searchItemClick:(id)sender {
  318. ShipSearchController *searchVC = [ShipSearchController build];
  319. [self.navigationController pushViewController:searchVC animated:YES];
  320. }
  321. - (IBAction)modeSegmentClick:(UISegmentedControl *)sender {
  322. if (sender.selectedSegmentIndex == 1) {
  323. self.mode = HomeModeKPI;
  324. self.ShipContainer.hidden = YES;
  325. self.KPIContainer.hidden = NO;
  326. if (self.KPIArray.count == 0 || self.kpiRefresh) {
  327. [self loadKPIData];
  328. } else {
  329. NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
  330. self.parentViewController.title = str;
  331. }
  332. } else {
  333. self.mode = HomeModeRecent;
  334. self.KPIContainer.hidden = YES;
  335. self.ShipContainer.hidden = NO;
  336. self.parentViewController.title = nil;
  337. if (self.shipArray.count == 0 || self.recentRefresh) {
  338. [self loadData];
  339. }
  340. }
  341. [self.shipTableView reloadData];
  342. }
  343. - (void)switchMode:(id)sender {
  344. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  345. appDelegate.personalmode = ! appDelegate.personalmode;
  346. NSString* msg = @"";
  347. if(appDelegate.personalmode)
  348. {
  349. msg=@"Personal Mode On.";
  350. self.tabBarController.navigationItem.leftBarButtonItem.image = [[UIImage imageNamed:@"apexlogo-2p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  351. }
  352. else
  353. {
  354. msg=@"Personal Mode Off.";
  355. self.tabBarController.navigationItem.leftBarButtonItem.image = [[UIImage imageNamed:@"apexlogo-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  356. }
  357. [self postPersonModeChangeNotification];
  358. // [RAUtils alert_view:msg title:@"Personal Mode"];
  359. }
  360. - (IBAction)ShipNoDataBtnClick:(UIButton *)sender {
  361. [self loadData];
  362. }
  363. - (IBAction)KPINoDataBtnClick:(UIButton *)sender {
  364. [self loadKPIData];
  365. }
  366. #pragma mark - TableView DataSource && Delegate
  367. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  368. if (self.shipTableView == tableView) {
  369. return self.shipArray.count;
  370. } else {
  371. return 1;
  372. }
  373. }
  374. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  375. return 1;
  376. }
  377. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  378. if (self.shipTableView == tableView) {
  379. ShipingStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:SHIP_CELL_IDENTIFIER forIndexPath:indexPath];
  380. NSDictionary *item = [self.shipArray objectAtIndex:indexPath.section];
  381. NSString *title = [item objectForKey:@"title"];
  382. NSString *icon = [item objectForKey:@"icon"];
  383. NSString *desc = [item objectForKey:@"description"];
  384. NSString *detail = [item objectForKey:@"detail"];
  385. NSString *date = [item objectForKey:@"date"];
  386. NSString *port = [item objectForKey:@"port"];
  387. NSInteger stage = [[item objectForKey:@"transport_stage"] integerValue];
  388. [[[[[[[cell setTitle:title] setDescription:desc] setDetail:detail] setPort:port] setTime:date] setIcon:icon] setTransportStage:stage];
  389. return cell;
  390. } else {
  391. KPITableCell *kpiCell = [tableView dequeueReusableCellWithIdentifier:KPI_CELL_IDENTIFIER forIndexPath:indexPath];
  392. kpiCell.KPICollectionView.dataSource = self;
  393. kpiCell.KPICollectionView.delegate = self;
  394. [kpiCell.KPICollectionView reloadData];
  395. if (self.KPIArray.count) {
  396. [kpiCell.KPICollectionView setContentOffset:CGPointMake(self.currentKPI * kpiCell.KPICollectionView.bounds.size.width, 0) animated:NO];
  397. }
  398. return kpiCell;
  399. }
  400. }
  401. //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  402. // CGFloat w = CGRectGetWidth(tableView.bounds);
  403. //
  404. // UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, 30)];
  405. // header.backgroundColor = [UIColor lightGrayColor];
  406. //
  407. // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, w - 10 * 2, 20)];
  408. // label.textColor = [UIColor whiteColor];
  409. // label.text = @"Recently";
  410. //
  411. // [header addSubview:label];
  412. // return header;
  413. //}
  414. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  415. if (self.shipTableView != tableView) {
  416. return 0.f;
  417. }
  418. return 10;
  419. }
  420. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  421. return 0.001;
  422. }
  423. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  424. if (self.shipTableView != tableView) {
  425. return tableView.bounds.size.height;
  426. }
  427. return 110;
  428. }
  429. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  430. if (self.shipTableView != tableView) {
  431. return;
  432. }
  433. NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
  434. // [params setObject:@"dumb" forKey:@"id"];
  435. NSString* h_field;
  436. //if(self.radioContainer.selected==true)
  437. //{
  438. // [params setObject:@"1" forKey:@"criterion_type"];
  439. // h_field = @"ctnr";
  440. //}
  441. //else
  442. {
  443. [params setObject:@"0" forKey:@"criterion_type"];
  444. h_field = @"h_bol";
  445. }
  446. NSDictionary *item = [self.shipArray objectAtIndex:indexPath.section];
  447. NSString * cargo_criterion = item[@"hbol"];
  448. NSString *serial_no = item[@"serial_no"];
  449. if(cargo_criterion==nil)
  450. cargo_criterion=@"";
  451. [params setObject:cargo_criterion forKey:@"cargo_criterion"];
  452. if (serial_no != nil) {
  453. [params setObject:serial_no forKey:@"serial_no"];
  454. [params setObject:serial_no forKey:@"id"];
  455. }
  456. [ApexMobileDB savehistory:h_field value:cargo_criterion];
  457. DetailTabBarController *detailViewController=[[DetailTabBarController alloc] init:@"Cargo Tracking" actions:[NSArray arrayWithObjects:@"Tracking",nil] params:params];
  458. [self.navigationController pushViewController:detailViewController animated:YES];
  459. }
  460. //#pragma mark - SearchBar Delegate
  461. //
  462. //- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
  463. //
  464. // ShipSearchController *searchVC = [ShipSearchController build];
  465. //
  466. // UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchVC];
  467. //
  468. // [self presentViewController:nav animated:YES completion:nil];
  469. //
  470. // return NO;
  471. //}
  472. #pragma mark - CollectionView DataSource & Delegate
  473. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  474. return 1;
  475. }
  476. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  477. if (self.KPIArray && self.KPIArray.count > 0) {
  478. return self.KPIArray.count + 2;
  479. }
  480. return 0;
  481. }
  482. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  483. KPICell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KPICell" forIndexPath:indexPath];
  484. NSInteger index = indexPath.item;
  485. if (indexPath.item == 0) {
  486. index = self.KPIArray.count - 1;
  487. } else if (indexPath.item == self.KPIArray.count + 1) {
  488. index = 0;
  489. } else {
  490. index = indexPath.item - 1;
  491. }
  492. NSMutableDictionary *item = [self.KPIArray objectAtIndex:index];
  493. [item setObject:self.monthArray forKey:@"month"];
  494. [cell setInfoDic:item];
  495. cell.delegate = self;
  496. return cell;
  497. }
  498. #pragma mark - KPICell Delegate
  499. - (void)KPICell:(KPICell *)cell repeatClickLegendAtIndex:(NSInteger)index withItem:(NSDictionary *)item {
  500. NSLog(@"double clicked %ld",index);
  501. /**
  502. s_kpi = true;
  503. name = kpi name
  504. sector = sector title
  505. module_name = module_name
  506. */
  507. NSArray * itemsArr = item[@"arr_val"];
  508. NSMutableDictionary* legend = [itemsArr objectAtIndex:index];
  509. NSString *kpi_name = [item objectForKey:@"name"];
  510. NSString *sector = [legend objectForKey:@"full_title"];
  511. NSString *module_name = [item objectForKey:@"module_name"];
  512. if (kpi_name == nil) {
  513. kpi_name = @"";
  514. }
  515. if (sector == nil) {
  516. sector = @"";
  517. }
  518. if (module_name == nil) {
  519. module_name = @"";
  520. }
  521. NSMutableDictionary *params = [@{
  522. @"is_kpi" : @(YES),
  523. @"name" : kpi_name,
  524. @"sector": sector,
  525. @"module_name" : module_name
  526. } mutableCopy];
  527. AMResultViewController *resultVC = [[AMResultViewController alloc] initWithNibName:@"Result" bundle:nil];
  528. resultVC.params = params;
  529. [self.navigationController pushViewController:resultVC animated:YES];
  530. }
  531. #pragma mark - FlowLayout Delegate
  532. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  533. return collectionView.bounds.size;
  534. }
  535. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  536. return 0.0f;
  537. }
  538. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  539. return 0.0f;
  540. }
  541. #pragma mark - ScrollViewDelegate
  542. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  543. if (scrollView != self.shipTableView && [scrollView isKindOfClass:[UICollectionView class]]) {
  544. UICollectionView *collectionView = (UICollectionView *)scrollView;
  545. CGFloat w = scrollView.bounds.size.width;
  546. CGFloat offsetX = scrollView.contentOffset.x;
  547. float f = floorf(offsetX / w);
  548. NSInteger index = floorf(f + 0.5);
  549. NSInteger cur = index;
  550. if (index == 0) {
  551. cur = self.KPIArray.count;
  552. [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:cur inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  553. } else if (index == self.KPIArray.count + 1) {
  554. cur = 1;
  555. [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:cur inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
  556. } else {
  557. cur = index;
  558. }
  559. }
  560. }
  561. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  562. if (scrollView != self.shipTableView && [scrollView isKindOfClass:[UICollectionView class]]) {
  563. // UICollectionView *collectionView = (UICollectionView *)scrollView;
  564. CGFloat w = scrollView.bounds.size.width;
  565. CGFloat offsetX = scrollView.contentOffset.x;
  566. float f = floorf(offsetX / w);
  567. NSInteger index = floorf(f + 0.5);
  568. NSInteger cur = index;
  569. if (index == 0) {
  570. cur = self.KPIArray.count;
  571. } else if (index == self.KPIArray.count + 1) {
  572. cur = 1;
  573. } else {
  574. cur = index;
  575. }
  576. self.currentKPI = cur;
  577. if (self.currentKPI > 0) {
  578. NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
  579. self.parentViewController.title = str;
  580. } else {
  581. self.currentKPI = 1;
  582. }
  583. }
  584. }
  585. @end