#pragma mark -Life Cycle
-(void)dealloc
{
[self removeObserver];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = self.segmentedView;
[self initDefault];
[self initSubView];
[self addObServer];
[self loadData]; // 发起网络请求
[self.goodsTable reloadData];
}
- (void)initDefault
{
_isOnSaleStatus = YES;
_isHaveActivityStatus = NO;
_currentAddressInfo = [[SNCPSelectedAddressDTO alloc] init];
_isShowSalesDetaillStatus = YES;
_countNumber = 1;
_areaCode = [SNSPLoginInfo shareLoginInfo].currentRole.baseRoleInfo.areaCode;
_cityCode = [SNSPLoginInfo shareLoginInfo].currentRole.baseRoleInfo.cityCode;
_isAlertSpecificationView = NO;
}
- (void)initSubView
{
[self.view addSubview:self.goodsTable];
self.goodsTable.tableHeaderView = self.headerView;
[self.view addSubview:self.detailWebView];
[self.detailWebView addSubview:self.webViewHeadView];
[self.view addSubview:self.orderActionButton];
[self.view bringSubviewToFront:self.orderActionButton];
self.segmentedView.selectedSegmentIndex = 0;
[self.segmentedView sendActionsForControlEvents:UIControlEventValueChanged];
}
#pragma mark - NetWork
- (void)refreshData
{
[self loadData];
}
//FIXME:发起网络请求
- (void)loadData
{
}
#pragma mark -UITableViewDataSource、UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
SNGoodsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:kGoodsDetailCell];
cell.cmmdtyCode = self.commodityCode;
cell.countNumber = self.countNumber;
[cell.enterGoodsDetailButton addTarget:self action:@selector(jumpGoodsDetail) forControlEvents:UIControlEventTouchUpInside];
cell.titleLabel.text = self.goodsInfo.cmmdtyName;
if (!self.isOnSaleStatus) {
[cell isOnSaleWithBool:NO];
}
else
{
PriceDetailDto *price = self.priceInfo.price;
NSString *priceStr = price.price == nil ? @"":price.price;
NSString *refPriceStr = price.refPrice == nil ? @"":price.refPrice;
cell.redPriceLabel.text = [NSString stringWithFormat:@"¥%@",priceStr];
cell.grayPriceLabel.text = [NSString stringWithFormat:@"¥%@",refPriceStr];
[cell isShowActivityViewWithBool:self.isHaveActivityStatus activity:self.activityInfo];
}
[cell hiddenTestTicketView];
return cell;
}
else if (indexPath.row == 1)
{
SNGoodsFeedbackRateCell *cell = [tableView dequeueReusableCellWithIdentifier:kGoodsFeedbackCell];
NSString *rate =self.feedbackInfo.reviewInfo.goodRate;
if (!rate) {
cell.rateString = @"";
}
else
{
cell.rateString = [NSString stringWithFormat:@"%@%%",rate];
}
return cell;
}
else if (indexPath.row == 2)
{
SNGoodsSelectedCell *cell = [tableView dequeueReusableCellWithIdentifier:kGoodsSelectedCell];
cell.vauleLabel.text = self.specificationValueString;
return cell;
}
else if (indexPath.row == 3)
{
SNGoodsAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:kGoodsAddressCell];
NSString *cityName = self.currentAddressInfo.currentCity.name == nil ? @"" : self.currentAddressInfo.currentCity.name;
NSString *areaName = self.currentAddressInfo.currentCounty.name == nil ? @"" : self.currentAddressInfo.currentCounty.name;
NSString *townName = self.currentAddressInfo.currentTown.name == nil ? @"" : self.currentAddressInfo.currentTown.name;
NSString *nearestTime = self.deliverAgingInfo.nearestCuttime == nil ? @"" : self.deliverAgingInfo.nearestCuttime;
NSString *earliestArriveDate = self.deliverAgingInfo.earliestArriveDate == nil ? @"" : self.deliverAgingInfo.earliestArriveDate;
cell.vauleLabel.text = [NSString stringWithFormat:@"%@%@%@",cityName,areaName,townName];
if (!self.deliverAgingInfo.earliestArriveDate && !self.deliverAgingInfo.earliestArriveDate) {
cell.detailLabel.text = @"";
}
else
{
cell.detailLabel.text = [NSString stringWithFormat:@" %@之前下单,预计%@ 送达",nearestTime,earliestArriveDate];
}
return cell;
}
else if (indexPath.row == 4)
{
//如果不可售,则隐藏促销cell
if (!self.isOnSaleStatus || !self.isShowSalesDetaillStatus)
{
SNGoodsProductCell *cell = [tableView dequeueReusableCellWithIdentifier:kGoodsProductCell];
return cell;
}
else
{
SNGoodsSalesCell *cell = [tableView dequeueReusableCellWithIdentifier:kGoodsSalesCell];
NSArray *sales = self.saleListArray;
[cell updateTextWithArray:sales];
return cell;
}
}
else if (indexPath.row == 5)
{
if (!self.isOnSaleStatus || !self.isShowSalesDetaillStatus)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellId];
cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
return cell;
}
else
{
SNGoodsProductCell *cell = [tableView dequeueReusableCellWithIdentifier:kGoodsProductCell];
return cell;
}
}
else
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellId];
cell.backgroundColor = [UIColor greenColor];
}
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0)
{
if (!self.isHaveActivityStatus || !self.isOnSaleStatus) {
return 130;
}
return kGoodsDetailCellHeight;
}
else if (indexPath.row == 1)
{
return kGoodsFeedbackCellHeight;
}
else if (indexPath.row == 2)
{
return kGoodsSelectedCellHeight;
}
else if (indexPath.row == 3)
{
return kGoodsAddressCellHeight;
}
else if (indexPath.row == 4)
{
if (!self.isOnSaleStatus)
{
return kGoodsProductCellHeight;
}
return kGoodsSalesCellHeight;
}
else if (indexPath.row == 5)
{
if (!self.isOnSaleStatus || !self.isShowSalesDetaillStatus) {
return kGoodsSalesCellHeight;
}
return kGoodsProductCellHeight;
}
else
{
return kGoodsDefaultCellHeight;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
if (indexPath.row == 2) {
NSLog(@"选择商品颜色数量");
self.isAlertSpecificationView = YES;
[self showGoodsSpecificationView];
}
else if (indexPath.row == 3)
{
NSLog(@"选择配送地址");
SNCPAddressPickerView *addressPickerView = [[SNCPAddressPickerView alloc] init];
addressPickerView.addressInfo = self.currentAddressInfo;
__weak typeof(self) weakSelf = self;
[addressPickerView showViewWithSelectedBlock:^(SNCPSelectedAddressDTO *addressInfo) {
weakSelf.currentAddressInfo = addressInfo;
weakSelf.cityCode = weakSelf.currentAddressInfo.currentCity.code;
weakSelf.areaCode = weakSelf.currentAddressInfo.currentTown.code;
// 重新获取价格
[weakSelf fetchCommodityOffLinePrice];
[weakSelf.goodsTable reloadData];
}];
}
else if (indexPath.row == 4)
{
if (self.isOnSaleStatus && !self.isHaveActivityStatus)
{
NSLog(@"选择促销");
[self showGoodsSalesDetailView];
}
else
{
NSLog(@"选择商品参数");
[self showGoodsProductView];
[self.productView reloadDataWithArray:self.productArray];
}
}
else if (indexPath.row == 5)
{
if (self.isOnSaleStatus)
{
NSLog(@"选择商品参数");
[self showGoodsProductView];
[self.productView reloadDataWithArray:self.productArray];
}
}
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
CGFloat offsetY = scrollView.contentOffset.y;
if([scrollView isKindOfClass:[UITableView class]]) // tableView界面上的滚动
{
// 触发翻页的理想值:tableView整体的高度减去屏幕自身的高度
CGFloat valueNum = _goodsTable.contentSize.height - self.view.frame.size.height;
if ((offsetY - valueNum) > 40)
{
// 进入图文详情的动画
self.segmentedView.selectedSegmentIndex = 1;
[self.segmentedView sendActionsForControlEvents:UIControlEventValueChanged];
}
}
else // webView页面上的滚动
{
if(offsetY < 0 && -offsetY > 40)
{
// 返回基本详情界面的动画
self.segmentedView.selectedSegmentIndex = 0;
[self.segmentedView sendActionsForControlEvents:UIControlEventValueChanged];
}
}
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
for (int i = 0; i < 20; i++) {
NSString *str = [NSString stringWithFormat:@"document.getElementByTagName('img')[%d].style.width='100%%'",i];
[self.detailWebView stringByEvaluatingJavaScriptFromString:str];
}
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isDescendantOfView:self.productView] || [touch.view isDescendantOfView:self.salesView] || [touch.view isDescendantOfView:self.specificationView]) {
return NO;
}
return YES;
}
#pragma mark - KVO
- (void)addObServer
{
[_detailWebView.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)removeObserver
{
[_detailWebView.scrollView removeObserver:self forKeyPath:@"contentOffset"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if(object == _detailWebView.scrollView && [keyPath isEqualToString:@"contentOffset"])
{
[self headLabAnimation:[change[@"new"] CGPointValue].y];
}else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
#pragma mark -Event
- (void)change:(UISegmentedControl *)sender
{
if (sender.selectedSegmentIndex == 0 ) {
NSLog(@"商品");
[self backToFirstPageAnimation];
}else{
NSLog(@"详情页");
[self goToDetailAnimation];
}
}
#pragma mark - Private Method
- (void)endRefresh
{
if ([self.goodsTable.header isRefreshing]) {
[self.goodsTable.header endRefreshing];
}
}
// 进入详情的动画
- (void)goToDetailAnimation
{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
_detailWebView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
_goodsTable.frame = CGRectMake(0, -self.view.frame.size.height , self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
}];
}
// 返回第一个界面的动画
- (void)backToFirstPageAnimation
{
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionLayoutSubviews animations:^{
_goodsTable.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.bounds.size.height);
_detailWebView.frame = CGRectMake(0, _goodsTable.contentSize.height, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
}];
}
// 头部提示文本动画
- (void)headLabAnimation:(CGFloat)offsetY
{
_webViewHeadView.alpha = -offsetY/60;
_webViewHeadView.center = CGPointMake(self.view.frame.size.width/2, -offsetY/2.f);
// 图标翻转,表示已超过临界值,松手就会返回上页
if(-offsetY > 40){
_webViewHeadView.textColor = [UIColor grayColor];
_webViewHeadView.text = @"释放,返回商品页";
// _webViewHeadView.text = @"";
}else{
_webViewHeadView.textColor = [UIColor grayColor];
_webViewHeadView.text = @"上拉,进入详情";
}
}
- (void)removeFromScreen
{
if (self.productView) {
[self.productView removeFromSuperview];
}
if (self.salesView) {
[self.salesView hiddenPresentListView];
[self.salesView removeFromSuperview];
}
if (self.isAlertSpecificationView) {
[self removeSpecificationView];
}
[self.backView removeFromSuperview];
}
- (void)removeSpecificationView
{
if (self.specificationView) {
NSString *selectedStr = self.specificationView.selectedValue == nil ? @"" :self.specificationView.selectedValue;
self.specificationValueString = [NSString stringWithFormat:@"%@ %ld件",selectedStr,self.specificationView.countNumber];
NSLog(@"selectedValue is %@ ,number is %ld ,cmmdtyCode is %@",self.specificationView.selectedValue,self.specificationView.countNumber,self.specificationView.cmmdtyCode);
[self.specificationView removeFromSuperview];
self.isAlertSpecificationView = NO;
[self.goodsTable reloadData];
//FIXME:选择颜色和内存后,重新根据商品code刷新名称和价格
if (self.specificationView.cmmdtyCode) {
self.commodityCode =self.specificationView.cmmdtyCode;
self.countNumber =self.specificationView.countNumber;
[self freshGoodsDetail];
}
}
[self.backView removeFromSuperview];
}
// 选择参数后,刷新商品的名称价格
- (void)freshGoodsDetail
{
[self fetchGoodsBaseInfo];
[self fetchCommodityOffLinePrice];
}
- (void)showGoodsProductView
{
[self.backView addSubview:self.productView];
[[UIApplication sharedApplication].keyWindow addSubview:self.backView];
[[UIApplication sharedApplication].keyWindow addSubview:self.productView];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self.productView];
}
- (void)showGoodsSalesDetailView
{
[self.backView addSubview:self.salesView];
self.salesView.stdLocatCode = self.stdLocatCode;
self.salesView.commodityCode = self.commodityCode;
self.salesView.cityCode = self.cityCode;
self.salesView.countNumber = self.countNumber;
[self.salesView reloadDataWithArray:self.saleListArray];
[[UIApplication sharedApplication].keyWindow addSubview:self.backView];
[[UIApplication sharedApplication].keyWindow addSubview:self.salesView];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self.salesView];
}
- (void)showGoodsSpecificationView
{
[self.backView addSubview:self.specificationView];
self.specificationView.cityCode = self.cityCode;
self.specificationView.areaCode = self.areaCode;
self.specificationView.codeArray = self.specificationCodeArray;
self.specificationView.countNumber = self.countNumber;
[self.specificationView reloadDataWithArray:self.specificationArray];
[[UIApplication sharedApplication].keyWindow addSubview:self.backView];
[[UIApplication sharedApplication].keyWindow addSubview:self.specificationView];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self.specificationView];
}
#pragma mark - Getter And Setter
- (UITableView *)goodsTable
{
if (!_goodsTable) {
_goodsTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - orderButtonHeight) style:UITableViewStylePlain];
_goodsTable.separatorStyle = UITableViewCellSeparatorStyleNone;
_goodsTable.dataSource = self;
_goodsTable.delegate = self;
UILabel *footerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 60)];
footerView.text = @"继续拖动,查看图文详情";
// footerView.text = @"";
footerView.textColor = [UIColor grayColor];
footerView.textAlignment = NSTextAlignmentCenter;
footerView.font = [UIFont systemFontOfSize:13.f];
footerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
_goodsTable.tableFooterView = footerView;
[_goodsTable registerClass:[SNGoodsDetailCell class] forCellReuseIdentifier:kGoodsDetailCell];
[_goodsTable registerClass:[SNGoodsFeedbackRateCell class] forCellReuseIdentifier:kGoodsFeedbackCell];
[_goodsTable registerClass:[SNGoodsSelectedCell class] forCellReuseIdentifier:kGoodsSelectedCell];
[_goodsTable registerClass:[SNGoodsAddressCell class] forCellReuseIdentifier:kGoodsAddressCell];
[_goodsTable registerClass:[SNGoodsSalesCell class] forCellReuseIdentifier:kGoodsSalesCell];
[_goodsTable registerClass:[SNGoodsProductCell class] forCellReuseIdentifier:kGoodsProductCell];
_goodsTable.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
}
return _goodsTable;
}
-(SNGoodsHeaderView *)headerView
{
if (!_headerView) {
_headerView = [[SNGoodsHeaderView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 150)];
}
return _headerView;
}
- (UIWebView *)detailWebView
{
if (!_detailWebView) {
_detailWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, _goodsTable.contentSize.height, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
_detailWebView.delegate = self;
_detailWebView.scrollView.delegate = self;
// _detailWebView.scalesPageToFit = YES;
}
return _detailWebView;
}
- (UILabel *)webViewHeadView
{
if (!_webViewHeadView) {
_webViewHeadView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 40.f)];
_webViewHeadView.text = @"上拉,返回详情";
_webViewHeadView.text = @"";
_webViewHeadView.textAlignment = NSTextAlignmentCenter;
_webViewHeadView.font = [UIFont systemFontOfSize:13.f];
_webViewHeadView.textColor = [UIColor grayColor];
}
return _webViewHeadView;
}
- (UISegmentedControl *)segmentedView
{
if (!_segmentedView) {
_segmentedView = [[UISegmentedControl alloc] initWithItems:@[@"商品",@"详情"]];
_segmentedView.backgroundColor = [UIColor clearColor];
_segmentedView.layer.masksToBounds = YES;
_segmentedView.layer.cornerRadius = 5;
_segmentedView.layer.borderWidth = 1.5;
_segmentedView.layer.borderColor = [UIColor whiteColor].CGColor;
_segmentedView.frame = CGRectMake(0, 0, 140, 30);
[_segmentedView addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
_segmentedView.selectedSegmentIndex = 0;
_segmentedView.tintColor = [UIColor whiteColor];
[_segmentedView setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"15b374"],
NSFontAttributeName:[UIFont systemFontOfSize:13]
} forState:UIControlStateSelected];
[_segmentedView setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:[UIFont systemFontOfSize:13]
} forState:UIControlStateNormal];
}
return _segmentedView;
}
- (UIButton *)orderActionButton
{
if (!_orderActionButton) {
_orderActionButton = [UIButton buttonWithType:UIButtonTypeCustom];
_orderActionButton.frame = CGRectMake(0, CGRectGetHeight(self.view.frame) - orderButtonHeight, CGRectGetWidth(self.view.frame), orderButtonHeight);
_orderActionButton.backgroundColor = [UIColor colorWithHexString:@"15b374"];
[_orderActionButton setTitle:@"立即开单" forState:UIControlStateNormal];
[_orderActionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_orderActionButton addTarget:self action:@selector(orderAction) forControlEvents:UIControlEventTouchUpInside];
// [_orderActionButton setBackgroundImage:[UIImage patternImageWithColor:[UIColor colorWithHexString:@"15b374"]] forState:UIControlStateSelected];
[_orderActionButton setBackgroundImage:[UIImage patternImageWithColor:[UIColor colorWithHexString:@"cccccc"]] forState:UIControlStateDisabled];
}
return _orderActionButton;
}
-(SNGoodsProductView *)productView
{
if (!_productView) {
_productView = [[SNGoodsProductView alloc] initWithFrame:CGRectMake(0, self.backView.height - kGoodsProductViewHeight, self.view.width, kGoodsProductViewHeight)];
__weak typeof(self) weakSelf = self;
_productView.exitProduct = ^{
[weakSelf removeFromScreen];
};
}
return _productView;
}
-(SNGoodsSalesDetailView *)salesView
{
if (!_salesView) {
_salesView = [[SNGoodsSalesDetailView alloc] initWithFrame:CGRectMake(0, self.backView.height - kGoodsSalesDetailViewHeight, self.view.width, kGoodsSalesDetailViewHeight)];
__weak typeof(self) weakSelf = self;
_salesView.exitBlock = ^{
[weakSelf removeFromScreen];
};
}
return _salesView;
}
-(SNGoodsSpecificationView *)specificationView
{
if (!_specificationView) {
__weak typeof(self) weakSelf = self;
_specificationView = [[SNGoodsSpecificationView alloc] initWithFrame:CGRectMake(0, self.backView.height - kGoodsSpecificationViewHeight, self.view.width, kGoodsSpecificationViewHeight)];
_specificationView.specExitBlock = ^{
[weakSelf removeSpecificationView];
};
_specificationView.specOrderBlock = ^{
[weakSelf removeSpecificationView];
[weakSelf orderAction];
};
}
return _specificationView;
}
-(UIView *)backView
{
if (!_backView) {
_backView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_backView.backgroundColor = [UIColor colorWithHexString:@"000000"];
_backView.alpha = 0.7;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeFromScreen)];
tap.delegate = self;
[_backView addGestureRecognizer:tap];
}
return _backView;
}
- (NSMutableArray *)dataSource
{
if (!_dataSource) {
_dataSource = [[NSMutableArray alloc] init];
}
return _dataSource;
}
- (NSMutableArray *)specificationArray
{
if (!_specificationArray) {
_specificationArray = [[NSMutableArray alloc] init];
}
return _specificationArray;
}
-(NSMutableArray *)productArray
{
if (!_productArray) {
_productArray = [NSMutableArray array];
}
return _productArray;
}
-(NSMutableArray *)specificationCodeArray
{
if (!_specificationCodeArray) {
_specificationCodeArray = [[NSMutableArray alloc] init];
}
return _specificationCodeArray;
}
-(NSMutableArray *)saleListArray
{
if (!_saleListArray) {
_saleListArray = [NSMutableArray array];
}
return _saleListArray;
}
@end