// // LogisticsViewController.m // FirstLink // // Created by ascii on 15/5/29. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "LogisticsViewController.h" #import "LogisticsViewModel.h" #import #import "WebViewController.h" #import "FKOrderDetailProductCell.h" #import "LogisticsProgressCell.h" #import "LogisticsSuccessCell.h" #import "LogisticsQuestionCell.h" #import "LogisticsAnswerCell.h" #import "LogisticsTimeOutCell.h" #import "FKPackageIncompleteView.h" #import "FKTargetConfigUtil.h" @interface LogisticsViewController () @property (nonatomic, strong) FKCardSegmentView *cardSegmentView; @property (nonatomic, strong) FKPackageIncompleteView *packageIncompleteView; @property (nonatomic, strong) HMSegmentedControl *segmentedControl; @property (nonatomic, strong) LogisticsViewModel *viewModel; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, assign) BOOL isShowQuestion; @end static NSString * const FKOrderDetailProductCellIdentifier = @"FKOrderDetailProductCellIdentifier"; static NSString * const LogisticsProgressCellIdentifier = @"LogisticsProgressCellIdentifier"; static NSString * const LogisticsSuccessCellIdentifier = @"LogisticsSuccessCellIdentifier"; static NSString * const LogisticsQuestionCellIdentifier = @"LogisticsQuestionCellIdentifier"; static NSString * const LogisticsAnswerCellIdentifier = @"LogisticsAnswerCellIdentifier"; static NSString * const LogisticsTimeOutCellIdentifier = @"LogisticsTimeOutCellIdentifier"; @implementation LogisticsViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.isShowQuestion = YES; self.navigationItem.title = @"物流信息"; self.view.backgroundColor = UIColorFromRGB(0xf9f9f9); if (self.openURLPara) { self.waybillID = self.openURLPara[@"waybill_id"]; self.logisticsID = self.openURLPara[@"id"]; } [self.hudView show:YES]; [self loadNewRequest]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Request - (void)loadNewRequest { if (!self.waybillID || self.waybillID.length == 0){ [self finishLoadingData]; [self showStatusTipInView:self.tableView image:[UIImage imageNamed:@"StatusNoMessageIcon"] title:NoMessageTip]; return; } WeakSelf(weakSelf); [self.viewModel requestLogistics:self.waybillID logisticsID:self.logisticsID success:^(MSGHeader *header, NSDictionary *response) { [weakSelf.hudView hide:NO]; [weakSelf finishLoadingData]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { weakSelf.viewModel.logisticsStatus = [LogisticsViewModel parseLogisticsStatus:response]; NSArray *inPackageArray = [LogisticsViewModel parsePackages:response[@"data"][@"warehousing_package_list"]]; NSArray *outPackageArray = [LogisticsViewModel parsePackages:response[@"data"][@"send_package_list"]]; weakSelf.viewModel.inPackageArray = inPackageArray; weakSelf.viewModel.outPackageArray = outPackageArray; [weakSelf.view addSubview:weakSelf.packageIncompleteView]; [weakSelf.view addSubview:weakSelf.cardSegmentView]; [weakSelf.view addSubview:weakSelf.segmentedControl]; [weakSelf.view addSubview:weakSelf.tableView]; [weakSelf.view addSubview:weakSelf.statusView]; [weakSelf setupViews]; } else { [FLProgressHUDHelper showText:header.msg inView:weakSelf.view]; } } failure:^(MSGHeader *header, NSError *error) { [weakSelf.hudView hide:NO]; [weakSelf finishLoadingData]; [FLProgressHUDHelper showText:error.localizedDescription inView:weakSelf.view]; }]; } #pragma mark - Layout - (CGFloat)topEdgeOffset { return 0; } - (void)setupViews { self.packageIncompleteView.hidden = YES; if ([self.viewModel isPackageCompleted]) { [self.cardSegmentView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.view).offset(6); make.height.mas_equalTo(48); }]; } else { self.packageIncompleteView.hidden = NO; [self.packageIncompleteView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.view).offset(0); make.height.mas_equalTo(40); }]; [self.cardSegmentView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.packageIncompleteView.mas_bottom).offset(6); make.height.mas_equalTo(48); }]; } NSArray *packageArray = [self.viewModel selectPackageArray]; self.segmentedControl.hidden = YES; self.tableView.hidden = YES; self.statusView.hidden = YES; if (packageArray.count > 1) { self.segmentedControl.hidden = NO; [self.segmentedControl mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.top.equalTo(self.cardSegmentView.mas_bottom).offset([self topEdgeOffset]); make.height.mas_equalTo(44); }]; self.segmentedControl.sectionTitles = [self.viewModel sectionTitleArray]; self.segmentedControl.selectedSegmentIndex = [self.viewModel selectedIndex]; [self.segmentedControl setNeedsDisplay]; self.tableView.hidden = NO; [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.segmentedControl.mas_bottom); make.left.right.bottom.equalTo(self.view); }]; } else if (packageArray.count == 1) { self.tableView.hidden = NO; [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.view); make.top.equalTo(self.cardSegmentView.mas_bottom).offset([self topEdgeOffset]); }]; } else { self.statusView.hidden = NO; self.statusView.statusImageView.image = [UIImage imageNamed:@"StatusNoMessageIcon"]; self.statusView.statusLabel.text = NoMessageTip; self.statusView.backgroundColor = UIColorFromRGB(0xf4f4f4); [self.statusView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.equalTo(self.view); make.top.equalTo(self.cardSegmentView.mas_bottom).offset([self topEdgeOffset]); }]; } [self.tableView reloadData]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ #pragma mark - UITableViewDataSource, UITableViewDelegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.viewModel numberOfSectionsWithSegmentIndex:self.segmentedControl.selectedSegmentIndex]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.viewModel numberOfRowsInSection:section withSegmentIndex:self.segmentedControl.selectedSegmentIndex]; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger segmentIndex = self.segmentedControl.selectedSegmentIndex; kLogisticsCellType cellType = [self.viewModel cellTypeAtIndexPath:indexPath withSegmentIndex:segmentIndex]; switch (cellType) { case kLogisticsCellTypeProduct: { return (95 + 2); break; } case kLogisticsCellTypeProgress: case kLogisticsCellTypeSuccess: { LogisticsItem *item = [self.viewModel logisticsItemAtIndexPath:indexPath withSegmentIndex:segmentIndex]; return [LogisticsProgressCell cellHeightWith:item.desc]; break; } case kLogisticsCellTypeQuestionAnswer: { return [LogisticsAnswerCell height:[LogisticsViewController LOGISTICS_ANSWER_TEXT]]; break; } case kLogisticsCellTypeTimeOut: break; default: break; } return 44.0; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) { return CGFLOAT_MIN; } return 20; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell; NSInteger segmentIndex = self.segmentedControl.selectedSegmentIndex; kLogisticsCellType cellType = [self.viewModel cellTypeAtIndexPath:indexPath withSegmentIndex:segmentIndex]; switch (cellType) { case kLogisticsCellTypeProduct: { FKOrderDetailProductCell *productCell = [tableView dequeueReusableCellWithIdentifier:FKOrderDetailProductCellIdentifier]; FKOrderGoodItem *goodItem = [self.viewModel orderGoodItemAtIndexPath:indexPath withSegmentIndex:segmentIndex]; if (goodItem) { [productCell.productImgView sd_setImageWithURL:[NSURL URLWithString:goodItem.photoURL] placeholderImage:nil]; productCell.descripLabel.text = goodItem.title; productCell.priceLabel.text = [NSString stringWithFormat:@"¥%@", [FLStringHelper convertFenToYuan:goodItem.price]]; productCell.numberLabel.text = [NSString stringWithFormat:@"X%@", goodItem.quantity]; productCell.specLabel.text = goodItem.specName; } cell = productCell; break; } case kLogisticsCellTypeProgress: case kLogisticsCellTypeSuccess: { LogisticsItem *item = [self.viewModel logisticsItemAtIndexPath:indexPath withSegmentIndex:segmentIndex]; if (indexPath.row == 0) { LogisticsSuccessCell *detailCell = [tableView dequeueReusableCellWithIdentifier:LogisticsSuccessCellIdentifier]; detailCell.logisticsTextLabel.text = item.desc; NSString *createTime = [item.createTime stringByReplacingOccurrencesOfString:@"T" withString:@" "]; detailCell.logisticsTimeLabel.text = createTime; detailCell.horizontalLine.hidden = YES; cell = detailCell; } else { LogisticsProgressCell *detailCell = [tableView dequeueReusableCellWithIdentifier:LogisticsProgressCellIdentifier]; detailCell.logisticsTextLabel.text = item.desc; detailCell.logisticsTimeLabel.text = [item.createTime stringByReplacingOccurrencesOfString:@"T" withString:@" "]; detailCell.horizontalLine.hidden = NO; cell = detailCell; } break; } case kLogisticsCellTypeQuestionAnswer: { if (self.isShowQuestion) { LogisticsQuestionCell *questionCell = [tableView dequeueReusableCellWithIdentifier:LogisticsQuestionCellIdentifier]; [questionCell.iconButton addTarget:self action:@selector(clickAnswerOpenAction:) forControlEvents:UIControlEventTouchUpInside]; cell = questionCell; } else { LogisticsAnswerCell *answerCell = [tableView dequeueReusableCellWithIdentifier:LogisticsAnswerCellIdentifier]; [answerCell.closeButton addTarget:self action:@selector(clickAnswerCloseAction:) forControlEvents:UIControlEventTouchUpInside]; answerCell.contentLabel.text = [LogisticsViewController LOGISTICS_ANSWER_TEXT]; cell = answerCell; } break; } case kLogisticsCellTypeTimeOut: { LogisticsTimeOutCell *timeOutCell = [tableView dequeueReusableCellWithIdentifier:LogisticsTimeOutCellIdentifier]; [timeOutCell.timeOutBtn addTarget:self action:@selector(clickTimeOutButton:) forControlEvents:UIControlEventTouchUpInside]; cell = timeOutCell; break; } default: break; } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } #pragma mark - FKCardSegmentViewDelegate - (void)cardSegmentView:(FKCardSegmentView *)view didSelectWithVipIndex:(kCardSegmentIndex)index { if (index == kCardSegmentIndexLeft) { [self setButton:self.cardSegmentView.leftButton image:[UIImage imageNamed:@"vip_segment_left_select"]]; [self setButton:self.cardSegmentView.rightButton image:[UIImage imageNamed:@"vip_segment_unselect_gray"]]; } else if (index == kCardSegmentIndexRight) { [self setButton:self.cardSegmentView.leftButton image:[UIImage imageNamed:@"vip_segment_unselect_gray"]]; [self setButton:self.cardSegmentView.rightButton image:[UIImage imageNamed:@"vip_segment_right_select"]]; } self.viewModel.cardSegmentIndex = index; if (self.segmentedControl.hidden == NO) { [self.segmentedControl setSelectedSegmentIndex:0]; } [self setupViews]; } - (void)setButton:(UIButton *)button image:(UIImage *)image { [button setBackgroundImage:image forState:UIControlStateNormal]; [button setBackgroundImage:image forState:UIControlStateHighlighted]; } #pragma mark - Action - (IBAction)clickAnswerOpenAction:(id)sender { self.isShowQuestion = NO; [self.tableView reloadData]; } - (IBAction)clickAnswerCloseAction:(id)sender { self.isShowQuestion = YES; [self.tableView reloadData]; } - (IBAction)clickTimeOutButton:(id)sender { NSString *urlString = [NSString stringWithFormat:@"%@/support/timeout-pay.html", [[FKServerUtil sharedInstance] webServer]]; WebViewController *newViewController = [[WebViewController alloc] init]; newViewController.url = urlString; newViewController.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:newViewController animated:YES]; } #pragma mark - Property + (NSString *)LOGISTICS_ANSWER_TEXT { return [NSString stringWithFormat:@"我们会在最短时间内帮您在海外官网下单;\n下单成功后,官网约在3天内发货;\n%@直接提供海外直达中国的物流服务,全程可监控;\n清关后EMS直接收包投递;\n如出现清关缓慢等异常情况,我们将立即与您联系", [FKTargetConfigUtil appName]]; } - (FKPackageIncompleteView *)packageIncompleteView { if (!_packageIncompleteView) { _packageIncompleteView = [FKPackageIncompleteView new]; _packageIncompleteView.backgroundColor = UIColorFromRGB(0xe8d0a2); _packageIncompleteView.frame = CGRectMake(0, 0, UISCREENWIDTH, 40); } return _packageIncompleteView; } - (FKCardSegmentView *)cardSegmentView { if (!_cardSegmentView) { _cardSegmentView = [[FKCardSegmentView alloc] initWithFrame:CGRectMake(0, 6, UISCREENWIDTH, 48)]; _cardSegmentView.backgroundColor = UIColorFromRGB(0xf9f9f9); _cardSegmentView.superVipImgView.hidden = YES; _cardSegmentView.discountImgView.hidden = YES; _cardSegmentView.leftButtonLabel.text = @"国际物流"; _cardSegmentView.leftButtonLabel.font = [UIFont boldSystemFontOfSize:14]; _cardSegmentView.rightButtonLabel.text = @"国内物流"; _cardSegmentView.rightButtonLabel.font = [UIFont boldSystemFontOfSize:14]; _cardSegmentView.delegate = self; [self setButton:_cardSegmentView.leftButton image:[UIImage imageNamed:@"vip_segment_left_select"]]; [self setButton:_cardSegmentView.rightButton image:[UIImage imageNamed:@"vip_segment_unselect_gray"]]; } return _cardSegmentView; } - (HMSegmentedControl *)segmentedControl { if (!_segmentedControl) { _segmentedControl = [HMSegmentedControl new]; _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown; _segmentedControl.selectionIndicatorHeight = 2; _segmentedControl.segmentEdgeInset = UIEdgeInsetsMake(0, 15, -8, 15); _segmentedControl.selectionIndicatorEdgeInsets = UIEdgeInsetsMake(0, 15, -8, 15); _segmentedControl.selectionIndicatorColor = UIColorFromRGB(0xff6362); _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe; _segmentedControl.selectedTitleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:15]}; _segmentedControl.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor grayColor], NSFontAttributeName : [UIFont systemFontOfSize:15]}; _segmentedControl.type = HMSegmentedControlTypeText; _segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic; WeakSelf(weakSelf); _segmentedControl.indexChangeBlock = ^(NSInteger index) { [weakSelf.tableView reloadData]; }; } return _segmentedControl; } - (LogisticsViewModel *)viewModel { if (!_viewModel) { _viewModel = [[LogisticsViewModel alloc] init]; } return _viewModel; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = UIColorFromRGB(0xffffff); _tableView.dataSource = self; _tableView.delegate = self; [_tableView registerClass:[FKOrderDetailProductCell class] forCellReuseIdentifier:FKOrderDetailProductCellIdentifier]; [_tableView registerClass:[LogisticsProgressCell class] forCellReuseIdentifier:LogisticsProgressCellIdentifier]; [_tableView registerClass:[LogisticsSuccessCell class] forCellReuseIdentifier:LogisticsSuccessCellIdentifier]; [_tableView registerClass:[LogisticsQuestionCell class] forCellReuseIdentifier:LogisticsQuestionCellIdentifier]; [_tableView registerClass:[LogisticsAnswerCell class] forCellReuseIdentifier:LogisticsAnswerCellIdentifier]; [_tableView registerClass:[LogisticsTimeOutCell class] forCellReuseIdentifier:LogisticsTimeOutCellIdentifier]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _tableView; } @end