| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- //
- // HomeViewController.m
- // Apex Mobile
- //
- // Created by Ray on 25/01/2018.
- // Copyright © 2018 United Software Applications, Inc. All rights reserved.
- //
- #import "HomeViewController.h"
- #import "ShipingStatusCell.h"
- #import "ShipSearchController.h"
- #import "ApexMobileDB.h"
- #import "DetailTabBarController.h"
- #import "RAUtils.h"
- #import "KPICell.h"
- #import "KPITableCell.h"
- #import "AMResultViewController.h"
- #import "AMShipMap.h"
- #import "ApexResultViewController.h"
- #define SHIP_CELL_IDENTIFIER @"ShippingStatusCell"
- #define KPI_CELL_IDENTIFIER @"KPITableCell"
- typedef enum {
- HomeModeRecent = 0,
- HomeModeKPI = 1
- } HomeMode;
- @interface HomeViewController () <UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,KPIDelegate,ShipCellDelegate,AMShipMapDelegate>
- @property (strong, nonatomic) IBOutlet UISegmentedControl *segmentControl;
- @property (nonatomic,strong) IBOutlet UIView *ShipContainer;
- @property (nonatomic,strong) IBOutlet UIActivityIndicatorView *ShipLoading;
- @property (nonatomic,strong) IBOutlet UIButton *ShipNoDataBtn;
- @property (strong, nonatomic) IBOutlet UITableView *shipTableView;
- @property (strong, nonatomic) IBOutlet UISearchBar *shipSearchBar;
- @property (nonatomic,strong) NSMutableArray *shipArray;
- @property (nonatomic,strong) UIRefreshControl *refreshControl;
- @property (nonatomic,strong) IBOutlet UIView *KPIContainer;
- @property (nonatomic,strong) IBOutlet UIActivityIndicatorView *KPILoading;
- @property (nonatomic,strong) IBOutlet UIButton *KPINoDataBtn;
- @property (nonatomic,strong) IBOutlet UITableView *KPITableView;
- @property (nonatomic,strong) UIRefreshControl *KPIRefresh;
- @property (nonatomic,strong) NSMutableArray *KPIArray;
- @property (nonatomic,strong) NSArray *monthArray;
- @property (nonatomic,assign) HomeMode mode;///<
- @property (nonatomic,assign) NSInteger currentKPI;
- @property (nonatomic,strong) UIAlertController *progressDialog;
- @property (nonatomic,assign) BOOL recentRefresh;
- @property (nonatomic,assign) BOOL kpiRefresh;
- @property (nonatomic,strong) IBOutlet AMShipMap *mapView;
- @property (nonatomic,strong) NSIndexPath *iconSelectedIndexPath;
- @end
- @implementation HomeViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- if (@available(iOS 11,*)) {
- self.shipTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- } else {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
- self.currentKPI = 1;
- self.segmentControl.tintColor = APGRAYCOLOR;
- self.recentRefresh = NO;
- self.kpiRefresh = NO;
-
- [self registNotification];
- [self configureTableView];
- self.shipSearchBar.delegate =self;
-
- [self configureMap];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logout) name:APLogoutNotification object:nil];
-
- // self.loadIndicator.layer.cornerRadius = 5;
- // self.loadIndicator.layer.masksToBounds = YES;
- // self.loadIndicator.layer.borderColor = [UIColor colorWithRed:169 / 255.0 green:169 / 255.0 blue:169 / 255.0 alpha:1.0].CGColor;
- // self.loadIndicator.layer.borderWidth = 0.3f;
- // self.loadIndicator.color = [UIColor colorWithRed:43 / 255.0 green:43 / 255.0 blue:43 / 255.0 alpha:1.0];
- // self.loadIndicator.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:0.9];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [self clearNavigationbar];
- [self configureNavigationBar];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
-
- if (self.mode == HomeModeRecent) {
- if (self.shipArray.count == 0 || self.recentRefresh) {
- [self loadData];
- }
- [self.mapView shipMapWillAppear];
-
- } else if (self.mode == HomeModeKPI) {
- if (self.KPIArray.count == 0 || self.kpiRefresh) {
- [self loadKPIData];
- } else {
- NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
- self.parentViewController.title = str;
- }
- }
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- self.parentViewController.title = nil;
- [self.mapView shipMapWillDisappear];
- }
- - (void)clearNavigationbar {
- self.tabBarController.navigationItem.leftBarButtonItem = nil;
- self.tabBarController.navigationItem.leftBarButtonItems = nil;
- self.tabBarController.navigationItem.titleView = nil;
- self.tabBarController.navigationItem.title = nil;
- self.tabBarController.navigationItem.rightBarButtonItem = nil;
- self.tabBarController.navigationItem.rightBarButtonItems = nil;
- }
- - (void)configureMap {
-
- self.mapView.showCurrent = YES;
- self.mapView.showPoe = YES;
- self.mapView.showPol = YES;
- self.mapView.showPod = YES;
- self.mapView.showPor = YES;
- self.mapView.showOrigin = YES;
- self.mapView.showDestination = YES;
-
- self.mapView.delegate = self;
- }
- - (void)configureNavigationBar {
-
- if (self.navigationController && !self.navigationController.isNavigationBarHidden) {
-
- UIImage *logo = [[UIImage imageNamed:@"apexlogo-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- if (appDelegate.personalmode) {
- logo = [[UIImage imageNamed:@"apexlogo-2p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- }
- UIBarButtonItem *logoItem = [[UIBarButtonItem alloc] initWithImage:logo landscapeImagePhone:logo style:UIBarButtonItemStylePlain target:self action:@selector(switchMode:)];
- logoItem.enabled = true;
- self.tabBarController.navigationItem.leftBarButtonItem = logoItem;
-
- UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"bar_search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(searchItemClick:)];
- self.tabBarController.navigationItem.rightBarButtonItem = searchItem;
- }
- }
- - (void)configureTableView {
-
- CGFloat w = CGRectGetWidth(self.shipTableView.bounds);
- self.shipTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, CGFLOAT_MIN)];
- self.shipTableView.tableFooterView = [UIView new];
-
- [self.shipTableView registerNib:[UINib nibWithNibName:@"ShippingStatusCell" bundle:nil] forCellReuseIdentifier:SHIP_CELL_IDENTIFIER];
- [self.shipTableView registerNib:[UINib nibWithNibName:@"KPITableCell" bundle:nil] forCellReuseIdentifier:KPI_CELL_IDENTIFIER];
-
-
- UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
- refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"loading data..."];
- [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
- [self.shipTableView addSubview:refresh];
- self.refreshControl = refresh;
-
-
- // KPI
-
- self.KPITableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, CGFLOAT_MIN)];
- self.KPITableView.tableFooterView = [UIView new];
-
- [self.KPITableView registerNib:[UINib nibWithNibName:@"KPITableCell" bundle:nil] forCellReuseIdentifier:KPI_CELL_IDENTIFIER];
-
-
- UIRefreshControl *KPIRefresh = [[UIRefreshControl alloc] init];
- KPIRefresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"loading data..."];
- [KPIRefresh addTarget:self action:@selector(KPIRefreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
- [self.KPITableView addSubview:KPIRefresh];
- self.KPIRefresh = KPIRefresh;
- }
- - (void)registNotification {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handlePersonModeChangeNotification:) name:APPersonModeChangeNotification object:nil];
- }
- - (void)logout {
- [self.shipArray removeAllObjects];
- [self.shipTableView reloadData];
-
- self.currentKPI = 1;
- self.monthArray = nil;
- [self.KPIArray removeAllObjects];
- [self.KPITableView reloadData];
- }
- - (void)handlePersonModeChangeNotification:(NSNotification *)notification {
- [self reloadContent];
- }
- - (void)postPersonModeChangeNotification {
- [[NSNotificationCenter defaultCenter] postNotificationName:APPersonModeChangeNotification object:nil];
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark - Private
- - (UIAlertController *)progressDialog {
- if (!_progressDialog) {
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle: nil
- message: @"loading..."
- preferredStyle: UIAlertControllerStyleAlert];
-
-
-
- UIViewController *customVC = [[UIViewController alloc] init];
-
-
- UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
- // spinner.color = [UIColor redColor];
- [spinner startAnimating];
- [customVC.view addSubview:spinner];
-
- [customVC.view addConstraint:[NSLayoutConstraint
- constraintWithItem: spinner
- attribute:NSLayoutAttributeCenterX
- relatedBy:NSLayoutRelationEqual
- toItem:customVC.view
- attribute:NSLayoutAttributeCenterX
- multiplier:1.0f
- constant:0.0f]];
-
- [customVC.view addConstraint:[NSLayoutConstraint
- constraintWithItem: spinner
- attribute:NSLayoutAttributeCenterY
- relatedBy:NSLayoutRelationEqual
- toItem:customVC.view
- attribute:NSLayoutAttributeCenterY
- multiplier:1.0f
- constant:0.0f]];
-
-
- [alertController setValue:customVC forKey:@"contentViewController"];
- _progressDialog = alertController;
- }
- return _progressDialog;
- }
- - (void)showProgressDialog {
- [self presentViewController:self.progressDialog animated:true completion:nil];
- NSLog(@"-dialog show %@",self.progressDialog);
- }
- // 2019.1.4 增加参数completion
- // 解决加载完成后在dismiss过程中弹出对话框询问上传Cache时警告:while a presentation is in progress!
- - (void)dismissProgressDialog:(void(^)(void))completion {
- [self.progressDialog dismissViewControllerAnimated:YES completion:completion];
- NSLog(@"-dialog dismiss %@",self.progressDialog);
- }
- #pragma mark - Check Database
- - (void)checkDatabase {
-
- AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- if (appDelegate.shouldCheckCache) {
- appDelegate.shouldCheckCache = NO;
-
- if ([ApexMobileDB isCachedDataForUser:appDelegate.user]) {
-
- __weak typeof(self) weakSelf = self;
- // alert
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"there is some cached data need to upload to service, do you allow us to upload it? if you reject we would clean it" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *rejectAction = [UIAlertAction actionWithTitle:@"Reject" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- [ApexMobileDB cleanCacheDataForUser:appDelegate.user];
- });
- }];
- UIAlertAction *allowAction = [UIAlertAction actionWithTitle:@"Allow" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- [weakSelf uploadCache];
- }];
-
- [alertVC addAction:rejectAction];
- [alertVC addAction:allowAction];
-
- [self presentViewController:alertVC animated:YES completion:nil];
- }
- }
- }
- - (void)uploadCache {
-
- AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- [self showProgressDialog];
-
- __weak typeof(self) weakSelf = self;
- // upload/handle data
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
-
- NSString *sql = [ApexMobileDB prepareUploadSQLOfUser:appDelegate.user withDiviceID:RAUtils.deviceID];
-
- // upload
- NSDictionary *json = [RANetwork uploadUserSql:sql];
-
- // handle result
- int result = [json[@"result"] intValue];
- dispatch_async(dispatch_get_main_queue(), ^{
-
- [weakSelf dismissProgressDialog:^{
-
- if (result == RESULT_TRUE) {
-
- [RAUtils message_alert:nil title:@"upload success" controller:self];
-
- } else {
-
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Warning" message:@"upload local cached data failed,do you want to rery or cancel? if you choose cancel, then we will clean the cache" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- [ApexMobileDB cleanCacheDataForUser:appDelegate.user];
- });
- }];
-
- UIAlertAction *retryAction = [UIAlertAction actionWithTitle:@"Retry" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- [weakSelf uploadCache];
- }];
-
- [alertVC addAction:cancelAction];
- [alertVC addAction:retryAction];
-
- [self presentViewController:alertVC animated:YES completion:nil];
- }
-
- }]; // dismiss
-
-
- });// main queue
-
- }); // global queue
-
- }
- #pragma mark - Load Data
- - (NSMutableArray *)shipArray {
- if (!_shipArray) {
- _shipArray = [NSMutableArray array];
- }
- return _shipArray;
- }
- - (NSMutableArray *)KPIArray {
- if (!_KPIArray) {
- _KPIArray = [NSMutableArray array];
- }
- return _KPIArray;
- }
- - (void)reloadContent {
- NSLog(@"Home Person Mode Change");
-
- self.recentRefresh = YES;
- self.kpiRefresh = YES;
- if (((UITabBarController *)self.parentViewController).selectedIndex == 0) {
- if (self.mode == HomeModeRecent) {
- [self loadData];
- } else {
- [self loadKPIData];
- }
- }
-
- }
- - (void)loadData {
- // NSString *path = [[NSBundle mainBundle] pathForResource:@"fake_container_list.json" ofType:nil];
- // NSData *data = [[NSData alloc] initWithContentsOfFile:path];
- //
- // NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
-
- // 避免重复刷新
- if (self.ShipLoading.isAnimating) {
- return;
- }
-
- [self.ShipLoading startAnimating];
-
- [self showProgressDialog];
-
- __weak typeof(self) weakSelf = self;
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
-
- NSDictionary *json = [RANetwork requestHome];
-
- // NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"fake_home.json" ofType:nil]];
- // NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
-
- dispatch_async(dispatch_get_main_queue(), ^{
-
- if (self.refreshControl.isRefreshing) {
- [self.refreshControl endRefreshing];
- }
-
- [self.ShipLoading stopAnimating];
-
- int result = [[json objectForKey:@"result"] intValue];
-
- [weakSelf dismissProgressDialog:^{
-
- if (result == RESULT_TRUE) {
-
- [self.shipArray removeAllObjects];
- [self.shipArray addObjectsFromArray:[json objectForKey:@"container_list"]];
- self.iconSelectedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
- [self showMapAnnotaionWithIndexPath:self.iconSelectedIndexPath];
- [self.shipTableView reloadData];
-
- // [self checkDatabase];
-
- } else {
- NSString *msg = [json objectForKey:@"err_msg"];
- [RAUtils message_alert:msg title:@"Warning" controller:weakSelf];
- }
-
- self.ShipNoDataBtn.hidden = self.shipArray.count > 0;
- if (self.shipArray.count) {
- [self.shipTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
- }
- weakSelf.recentRefresh = NO;
-
- }];// dismiss
- }); // main queue
-
- }); // global queue
- }
- - (void)loadKPIData {
-
- if (self.KPILoading.isAnimating) {
- return;
- }
-
- [self.KPILoading startAnimating];
- [self showProgressDialog];
-
- __weak typeof(self) weakSelf = self;
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
-
- NSDictionary *json = [RANetwork requestKPI];
-
- dispatch_async(dispatch_get_main_queue(), ^{
-
- if (weakSelf.KPIRefresh.isRefreshing) {
- [weakSelf.KPIRefresh endRefreshing];
- }
-
- [weakSelf.KPILoading stopAnimating];
-
- int result = [[json objectForKey:@"result"] intValue];
- [weakSelf dismissProgressDialog:^{
-
- if (result == RESULT_TRUE) {
- // self.currentKPI = 1;
- weakSelf.monthArray = [json objectForKey:@"month"];
-
- [weakSelf.KPIArray removeAllObjects];
- NSArray *kpiArr = [json objectForKey:@"KPI"];
-
- // [self.KPIArray addObject:[[kpiArr firstObject] mutableCopy]];
- for (int i = 0; i < kpiArr.count; i++) {
- NSMutableDictionary *kpiModel = [[kpiArr objectAtIndex:i] mutableCopy];
- NSMutableArray *itemMutArr = [NSMutableArray array];
- NSArray *itemArr = [kpiModel objectForKey:@"arr_val"];
- for (int j = 0; j < itemArr.count; j++) {
- NSMutableDictionary *item = [[itemArr objectAtIndex:j] mutableCopy];
- [itemMutArr addObject:item];
- }
- [kpiModel setObject:itemMutArr forKey:@"arr_val"];
-
- // [self.KPIArray addObjectsFromArray:kpiArr];
- [weakSelf.KPIArray addObject:kpiModel];
- }
- // [self.KPIArray addObject:[[kpiArr lastObject] mutableCopy]];
-
-
- [weakSelf.KPITableView reloadData];
-
- if (((UITabBarController *)weakSelf.parentViewController).selectedIndex == 0) {
- NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
- weakSelf.parentViewController.title = str;
- }
-
- } else {
- NSString *msg = [json objectForKey:@"err_msg"];
- [RAUtils message_alert:msg title:@"Warning" controller:weakSelf];
- }
- weakSelf.KPINoDataBtn.hidden = self.KPIArray.count > 0;
- weakSelf.kpiRefresh = NO;
- }];
-
- });
-
- });
-
- }
- #pragma mark - Actinon
- - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
- [self loadData];
- }
- - (void)KPIRefreshControlValueChanged:(UIRefreshControl *)refresh {
- [self loadKPIData];
- }
- - (void)searchItemClick:(id)sender {
-
- ShipSearchController *searchVC = [ShipSearchController build];
-
- [self.navigationController pushViewController:searchVC animated:YES];
-
- }
- - (IBAction)modeSegmentClick:(UISegmentedControl *)sender {
-
- if (sender.selectedSegmentIndex == 1) {
- self.mode = HomeModeKPI;
- self.ShipContainer.hidden = YES;
- self.KPIContainer.hidden = NO;
-
- if (self.KPIArray.count == 0 || self.kpiRefresh) {
- [self loadKPIData];
- } else {
- NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
- self.parentViewController.title = str;
- }
-
- [self.mapView shipMapWillDisappear];
-
- } else {
- self.mode = HomeModeRecent;
- self.KPIContainer.hidden = YES;
- self.ShipContainer.hidden = NO;
- self.parentViewController.title = nil;
- if (self.shipArray.count == 0 || self.recentRefresh) {
- [self loadData];
- }
-
- [self.mapView shipMapWillAppear];
- }
-
-
- [self.shipTableView reloadData];
- }
- - (void)switchMode:(id)sender {
-
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
-
- appDelegate.personalmode = ! appDelegate.personalmode;
-
- NSString* msg = @"";
- if(appDelegate.personalmode)
- {
- msg=@"Personal Mode On.";
- self.tabBarController.navigationItem.leftBarButtonItem.image = [[UIImage imageNamed:@"apexlogo-2p"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- }
- else
- {
- msg=@"Personal Mode Off.";
- self.tabBarController.navigationItem.leftBarButtonItem.image = [[UIImage imageNamed:@"apexlogo-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- }
-
-
- [self postPersonModeChangeNotification];
- // [RAUtils alert_view:msg title:@"Personal Mode"];
- }
- - (IBAction)ShipNoDataBtnClick:(UIButton *)sender {
- [self loadData];
- }
- - (IBAction)KPINoDataBtnClick:(UIButton *)sender {
- [self loadKPIData];
- }
- #pragma mark - TableView DataSource && Delegate
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- if (self.shipTableView == tableView) {
- return self.shipArray.count;
- } else {
- return 1;
- }
-
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return 1;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- if (self.shipTableView == tableView) {
- ShipingStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:SHIP_CELL_IDENTIFIER forIndexPath:indexPath];
-
- NSDictionary *item = [self.shipArray objectAtIndex:indexPath.section];
-
- NSString *title = [item objectForKey:@"title"];
- NSString *icon = [item objectForKey:@"icon"];
- NSString *desc = [item objectForKey:@"description"];
- NSString *detail = [item objectForKey:@"detail"];
- NSString *date = [item objectForKey:@"date"];
- NSString *port = [item objectForKey:@"port"];
- NSInteger stage = [[item objectForKey:@"transport_stage"] integerValue];
-
- [[[[[[[[cell setTitle:title] setDescription:desc] setDetail:detail] setPort:port] setTime:date] setIcon:icon] setTransportStage:stage] setDelegate:self];
-
- if (self.iconSelectedIndexPath && self.iconSelectedIndexPath.section == indexPath.section) {
- [self setSelectedIndexPath:self.iconSelectedIndexPath shipCell:cell];
- }
-
- return cell;
- } else {
-
- KPITableCell *kpiCell = [tableView dequeueReusableCellWithIdentifier:KPI_CELL_IDENTIFIER forIndexPath:indexPath];
- kpiCell.KPICollectionView.dataSource = self;
- kpiCell.KPICollectionView.delegate = self;
- [kpiCell.KPICollectionView reloadData];
- if (self.KPIArray.count) {
- [kpiCell.KPICollectionView setContentOffset:CGPointMake(self.currentKPI * kpiCell.KPICollectionView.bounds.size.width, 0) animated:NO];
- }
- return kpiCell;
- }
-
- }
- //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
- // CGFloat w = CGRectGetWidth(tableView.bounds);
- //
- // UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, 30)];
- // header.backgroundColor = [UIColor lightGrayColor];
- //
- // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, w - 10 * 2, 20)];
- // label.textColor = [UIColor whiteColor];
- // label.text = @"Recently";
- //
- // [header addSubview:label];
- // return header;
- //}
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
- if (self.shipTableView != tableView) {
- return 0.f;
- }
- return 10;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
- return 0.001;
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
- if (self.shipTableView != tableView) {
- return tableView.bounds.size.height;
- }
- return 110;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
-
- if (self.shipTableView != tableView) {
- return;
- }
- [self setSelectedIndexPath:indexPath shipCell:nil];
- [self showMapAnnotaionWithIndexPath:indexPath];
-
- NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
- // [params setObject:@"dumb" forKey:@"id"];
- NSString* h_field;
- //if(self.radioContainer.selected==true)
- //{
- // [params setObject:@"1" forKey:@"criterion_type"];
- // h_field = @"ctnr";
- //}
- //else
- {
- [params setObject:@"0" forKey:@"criterion_type"];
- h_field = @"h_bol";
- }
-
- NSDictionary *item = [self.shipArray objectAtIndex:indexPath.section];
- NSString * cargo_criterion = item[@"hbol"];
- NSString *serial_no = item[@"serial_no"];
- if(cargo_criterion==nil)
- cargo_criterion=@"";
- [params setObject:cargo_criterion forKey:@"cargo_criterion"];
- if (serial_no != nil) {
- [params setObject:serial_no forKey:@"serial_no"];
- [params setObject:serial_no forKey:@"id"];
- }
- [ApexMobileDB savehistory:h_field value:cargo_criterion];
- DetailTabBarController *detailViewController=[[DetailTabBarController alloc] init:@"Cargo Tracking" actions:[NSArray arrayWithObjects:@"Tracking",nil] params:params];
- [self.navigationController pushViewController:detailViewController animated:YES];
-
- }
- //#pragma mark - SearchBar Delegate
- //
- //- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
- //
- // ShipSearchController *searchVC = [ShipSearchController build];
- //
- // UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchVC];
- //
- // [self presentViewController:nav animated:YES completion:nil];
- //
- // return NO;
- //}
- #pragma mark - CollectionView DataSource & Delegate
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 1;
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- if (self.KPIArray && self.KPIArray.count > 0) {
- return self.KPIArray.count + 2;
- }
- return 0;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- KPICell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"KPICell" forIndexPath:indexPath];
-
- NSInteger index = indexPath.item;
- if (indexPath.item == 0) {
- index = self.KPIArray.count - 1;
- } else if (indexPath.item == self.KPIArray.count + 1) {
- index = 0;
- } else {
- index = indexPath.item - 1;
- }
-
- NSMutableDictionary *item = [self.KPIArray objectAtIndex:index];
- [item setObject:self.monthArray forKey:@"month"];
- [cell setInfoDic:item];
- cell.delegate = self;
- return cell;
- }
- #pragma mark - KPICell Delegate
- - (void)KPICell:(KPICell *)cell repeatClickLegendAtIndex:(NSInteger)index withItem:(NSDictionary *)item {
- NSLog(@"double clicked %ld",index);
- /**
- s_kpi = true;
- name = kpi name
- sector = sector title
- module_name = module_name
- */
- NSArray * itemsArr = item[@"arr_val"];
- NSMutableDictionary* legend = [itemsArr objectAtIndex:index];
-
- NSString *kpi_name = [item objectForKey:@"name"];
- NSString *sector = [legend objectForKey:@"full_title"];
- NSString *module_name = [item objectForKey:@"module_name"];
- if (kpi_name == nil) {
- kpi_name = @"";
- }
- if (sector == nil) {
- sector = @"";
- }
- if (module_name == nil) {
- module_name = @"";
- }
-
-
- NSMutableDictionary *params = [@{
- @"is_kpi" : @(YES),
- @"name" : kpi_name,
- @"sector": sector,
- @"module_name" : module_name
- } mutableCopy];
-
- // AMResultViewController *resultVC = [[AMResultViewController alloc] initWithNibName:@"Result" bundle:nil];
- // resultVC.params = params;
- // [self.navigationController pushViewController:resultVC animated:YES];
-
- ApexResultViewController *vc = [ApexResultViewController resultViewController:params];
- [self.navigationController pushViewController:vc animated:YES];
- }
- #pragma mark - FlowLayout Delegate
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- return collectionView.bounds.size;
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
- return 0.0f;
- }
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
- return 0.0f;
- }
- #pragma mark - ScrollViewDelegate
- - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
- if (scrollView != self.shipTableView && [scrollView isKindOfClass:[UICollectionView class]]) {
- UICollectionView *collectionView = (UICollectionView *)scrollView;
- CGFloat w = scrollView.bounds.size.width;
- CGFloat offsetX = scrollView.contentOffset.x;
- float f = floorf(offsetX / w);
- NSInteger index = floorf(f + 0.5);
- NSInteger cur = index;
- if (index == 0) {
- cur = self.KPIArray.count;
- [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:cur inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
- } else if (index == self.KPIArray.count + 1) {
- cur = 1;
- [collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:cur inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
- } else {
- cur = index;
- }
- }
- }
- - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
- if (scrollView != self.shipTableView && [scrollView isKindOfClass:[UICollectionView class]]) {
- // UICollectionView *collectionView = (UICollectionView *)scrollView;
- CGFloat w = scrollView.bounds.size.width;
- CGFloat offsetX = scrollView.contentOffset.x;
- float f = floorf(offsetX / w);
- NSInteger index = floorf(f + 0.5);
- NSInteger cur = index;
- if (index == 0) {
- cur = self.KPIArray.count;
- } else if (index == self.KPIArray.count + 1) {
- cur = 1;
- } else {
- cur = index;
- }
- self.currentKPI = cur;
- if (self.currentKPI > 0) {
- NSString *str = [NSString stringWithFormat:@"%ld / %ld",self.currentKPI,self.KPIArray.count];
- self.parentViewController.title = str;
- } else {
- self.currentKPI = 1;
- }
- }
- }
- #pragma mark - Map
- - (NSDictionary *)fakeLoaction {
- NSArray *arr = [ApexMobileDB get_Location];
-
- NSMutableDictionary *returnDic = [NSMutableDictionary dictionary];
-
- for (int i = 0; i < arr.count ; i ++) {
-
- NSDictionary* location = arr[i];
- NSString* area = [location valueForKey:@"area"];
- NSString* company = [location valueForKey:@"company"];
- // NSString* city = [location valueForKey:@"city"];
- NSString *longitude = [location valueForKey:@"longitude"];
- NSString *latitude = [location valueForKey:@"latitude"];
-
- NSDictionary *dic = @{
- @"addr" : area,
- @"name" : company,
- @"lon" : longitude,
- @"lat" : latitude
- };
- if (i == 0) {
- [returnDic setObject:dic forKey:@"pol"];
- } else if (i == 1) {
- [returnDic setObject:dic forKey:@"pod"];
- } else if (i == 2) {
- [returnDic setObject:dic forKey:@"poe"];
- } else if (i == 3) {
- [returnDic setObject:dic forKey:@"por"];
- } else if (i == 4) {
- [returnDic setObject:dic forKey:@"origin"];
- } else if (i == 5) {
- [returnDic setObject:dic forKey:@"destination"];
- } else if (i == 6) {
- [returnDic setObject:dic forKey:@"current"];
- } else {
- break;
- }
-
- }
-
- return returnDic;
- }
- - (void)setSelectedIndexPath:(NSIndexPath *)indexPath shipCell:(ShipingStatusCell *)shipCell {
- if (indexPath == nil) {
- return;
- }
- if (self.iconSelectedIndexPath) {
- ShipingStatusCell *cell = [self.shipTableView cellForRowAtIndexPath:self.iconSelectedIndexPath];
- [cell setIconSelect:NO];
- }
- self.iconSelectedIndexPath = indexPath;
- ShipingStatusCell *cell = [self.shipTableView cellForRowAtIndexPath:indexPath];
- if (cell == nil) { // 滑动过程中,dequeue方法中设置选中
- cell = shipCell;
- }
- [self.shipTableView selectRowAtIndexPath:self.iconSelectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
- [cell setIconSelect:YES];
- }
- - (void)showMapAnnotaionWithIndexPath:(NSIndexPath *)indexPath {
-
- if(self.shipArray.count==0)
- return;
- NSDictionary *item = [self.shipArray objectAtIndex:indexPath.section];
- NSDictionary *locationInfo = [item objectForKey:@"locations"];
- NSString* port = @"";
- if([item[@"location_eta"] boolValue])
- port=@"pod";
- else
- if([item[@"location_etd"] boolValue])
- port=@"pol";
- [self.mapView showShipAnnotaion:locationInfo backupLocation:port];
-
- }
- #pragma mark - ShipCell Delegate
- - (void)shipCellDidTapIcon:(ShipingStatusCell *)cell {
- NSIndexPath *indexPath = [self.shipTableView indexPathForCell:cell];
- [self setSelectedIndexPath:indexPath shipCell:cell];
- [self showMapAnnotaionWithIndexPath:indexPath];
- }
- #pragma mark - ShipMap Delegate
- - (void)shipMap:(AMShipMap *)shipMap tryToZoomIn:(BOOL)zoomIn {
- if (zoomIn) {
-
- NSArray *constraints = self.ShipContainer.constraints;
- NSLayoutConstraint *h_ratio_constraint = nil;
-
- for (NSLayoutConstraint *constraint in constraints) {
- if (constraint.identifier && [constraint.identifier isEqualToString:@"map_height_ratio"]) {
- h_ratio_constraint = constraint;
- }
- }
-
- if (h_ratio_constraint) {
- [self.ShipContainer removeConstraint:h_ratio_constraint];
- }
-
- NSLayoutConstraint *table_h_constraint = [NSLayoutConstraint constraintWithItem:self.shipTableView
- attribute:NSLayoutAttributeHeight
- relatedBy:NSLayoutRelationEqual
- toItem:nil
- attribute:NSLayoutAttributeNotAnAttribute
- multiplier:0
- constant:120.0f];
- table_h_constraint.identifier = @"ship_table_height";
- [self.ShipContainer addConstraint:table_h_constraint];
-
- } else {
- NSArray *constraints = self.ShipContainer.constraints;
- NSLayoutConstraint *h_constraint = nil;
-
- for (NSLayoutConstraint *constraint in constraints) {
- if (constraint.identifier && [constraint.identifier isEqualToString:@"ship_table_height"]) {
- h_constraint = constraint;
- }
- }
-
- if (h_constraint) {
- [self.ShipContainer removeConstraint:h_constraint];
- }
-
- float h_ratio = 0.5;
- NSLayoutConstraint *h_ratio_constraint = [NSLayoutConstraint constraintWithItem:shipMap
- attribute:NSLayoutAttributeHeight
- relatedBy:NSLayoutRelationEqual
- toItem:self.shipTableView
- attribute:NSLayoutAttributeHeight
- multiplier:h_ratio
- constant:0];
- h_ratio_constraint.identifier = @"map_height_ratio";
-
- [self.ShipContainer addConstraint:h_ratio_constraint];
- }
-
-
- [self.ShipContainer layoutIfNeeded];
- }
- @end
|