// // pointsMallTableController.m // ThePaperBase // // Created by Huixin on 15/10/27. // Copyright © 2015年 scar1900. All rights reserved. // #import "pointsMallTableController.h" #import "seaShellCell.h" #import "taskCell.h" #import "recommendCell.h" #import "commodityCell.h" #import "commodityController.h" #import "mallRuleController.h" #import "shareUtil.h" @interface pointsMallTableController () { NSInteger backFlag; seaShellCell *seashellCell; recommendCell *recommendcell; } @property(nonatomic, strong)NSMutableArray *commodityList; @property(nonatomic, strong)NSMutableArray *labelHeightList; @property(nonatomic, strong)NSMutableArray *labelWidthList; @property(nonatomic, strong)NSMutableArray *taskList; @property(nonatomic, strong)NSMutableArray *taskHeightList; @property(nonatomic, strong)NSString *todaySeashells; @property(nonatomic, strong)NSString *totalSeashells; @property(nonatomic, strong)NSString *shareTitle; @property(nonatomic, strong)NSString *shareSummary; @property(nonatomic, strong)NSString *sharePic; @property(nonatomic, strong)NSString *shareUrl; @property(nonatomic, strong)UILabel *heightLabel; @property(nonatomic, strong)UILabel *widthLabel; @property(nonatomic, strong)UIButton *ruleBtn; @property(nonatomic, strong)UILabel *bottomLabel; @end @implementation pointsMallTableController static NSString *seaShellCellID = @"seaShellCell"; static NSString *taskCellID = @"taskCell"; static NSString *recommendCellID = @"recommendCell"; static NSString *commodityCellID = @"commodityCell"; static NSString *bottomCellID = @"bottomCell"; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor clearColor]; self.tableView.backgroundColor = [UIColor clearColor]; self.tableView.clipsToBounds = YES; [self configCanRefresh:NO canLoad:YES]; [self needNoFirstAutoRefresh]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.showsVerticalScrollIndicator = NO; self.loadFooter.frame = CGRectMake(0, CGRectGetHeight(self.tableView.bounds), CGRectGetWidth(self.view.bounds), 120); [self.loadFooter addSubview:self.ruleBtn]; [self.loadFooter addSubview:self.bottomLabel]; self.tableView.tableFooterView = self.loadFooter; self.loadFooter.isMall = YES; [self.tableView registerClass:[seaShellCell class] forCellReuseIdentifier:seaShellCellID]; [self.tableView registerClass:[taskCell class] forCellReuseIdentifier:taskCellID]; [self.tableView registerClass:[recommendCell class] forCellReuseIdentifier:recommendCellID]; [self.tableView registerClass:[commodityCell class] forCellReuseIdentifier:commodityCellID]; [self remoteActionWithUrl:nil]; self.tableView.hidden = YES; backFlag = 0; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (backFlag == 1) { [self refreshPoints]; } } - (UIButton*)ruleBtn { if (!_ruleBtn) { _ruleBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _ruleBtn.frame = CGRectMake(CGRectGetWidth(self.loadFooter.bounds)/2-75/2, 60, 75, 20); _ruleBtn.layer.cornerRadius = 2.0f; _ruleBtn.layer.masksToBounds = YES; _ruleBtn.layer.borderWidth = 1.0f; _ruleBtn.layer.borderColor = [UIColor colorWithHexString:BLUECOLOR].CGColor; [_ruleBtn setTitle:@"商城使用规则" forState:UIControlStateNormal]; _ruleBtn.titleLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO); [_ruleBtn setTitleColor:[UIColor colorWithHexString:BLUECOLOR] forState:UIControlStateNormal]; [_ruleBtn setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateHighlighted]; [_ruleBtn addTarget:self action:@selector(viewRule) forControlEvents:UIControlEventTouchUpInside]; } return _ruleBtn; } - (UILabel*)bottomLabel { if (!_bottomLabel) { _bottomLabel = [UILabel new]; _bottomLabel.frame = CGRectMake(0, CGRectGetMaxY(self.ruleBtn.frame)+10, CGRectGetWidth(self.loadFooter.bounds), 13); _bottomLabel.textColor = [UIColor colorWithHexString:TextLightGray]; _bottomLabel.font = appFont(TEXT_SIX_LEVELSIZE, NO); _bottomLabel.textAlignment = NSTextAlignmentCenter; _bottomLabel.text = @"*所有奖品和活动均与苹果公司无关"; } return _bottomLabel; } - (UILabel*)heightLabel { if (!_heightLabel) { _heightLabel = [UILabel new]; _heightLabel.backgroundColor = [UIColor clearColor]; _heightLabel.textColor = [UIColor colorWithHexString:TextBlack]; _heightLabel.font = appFont(TEXT_FIVE_LEVELSIZE, NO); _heightLabel.numberOfLines = 0; } return _heightLabel; } - (UILabel*)widthLabel { if (!_widthLabel) { _widthLabel = [UILabel new]; _widthLabel.backgroundColor = [UIColor clearColor]; _widthLabel.textColor = [UIColor colorWithHexString:BLUECOLOR]; _widthLabel.font = appFont(TEXT_FOUR_LEVELSIZE, NO); } return _widthLabel; } #pragma mark - btn handler - (void)viewRule { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ mallRuleController *vc = [mallRuleController new]; [self.navigationController pushViewController:vc animated:YES]; }); } #pragma mark - remote action - (void)remoteActionWithUrl:(NSString*)url { if (url == nil) { __weak typeof(self) weakSelf = self; [Remote doJsonActionWithBlock:1 requestUrl:mySeashellURL parameter:nil withWaitCursor:YES completion:^(BOOL success, NSString *message, id responseData) { if (success) { NSArray *list = responseData[@"commodityList"]; NSMutableArray *objectList = [NSMutableArray array]; [list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { commodityBO *commodity = setJsonDicToDataModel(obj, [commodityBO class]); [objectList addObject:commodity]; }]; weakSelf.commodityList = objectList; [weakSelf setHeightAndWidthList:weakSelf.commodityList]; weakSelf.taskList = responseData[@"taskInfos"]; NSMutableArray *heightList = [NSMutableArray array]; [self.taskList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSDictionary *task = obj; if ([task[@"type"] isEqualToString:@"4"]) { [heightList addObject:[NSString stringWithFormat:@"%f", (66+rect_screen.size.width*0.125)]]; } else [heightList addObject:[NSString stringWithFormat:@"%f", 45.0]]; }]; [heightList addObject:@"10.0"]; weakSelf.taskHeightList = heightList; weakSelf.todaySeashells = responseData[@"seashellInfo"][@"todaySeashells"]; weakSelf.totalSeashells = responseData[@"seashellInfo"][@"totalSeashells"]; weakSelf.shareTitle = responseData[@"shareInfo"][@"title"]; weakSelf.shareSummary = responseData[@"shareInfo"][@"summary"]; weakSelf.sharePic = responseData[@"shareInfo"][@"sharePic"]; weakSelf.shareUrl = responseData[@"shareInfo"][@"shareUrl"]; weakSelf.nextUrl = responseData[@"nextUrl"]; weakSelf.tableView.hidden = NO; [weakSelf.tableView reloadData]; } else { ShowTextMessage(message); } }]; //[Remote doJsonAction:1 requestUrl:mySeashellURL parameter:nil delegate:self]; } else if (!isBlankString(url)) { [Remote doJsonAction:2 requestUrl:url parameter:nil delegate:self]; } } #pragma mark - TPtableview delegate - (void)pullLoadMoreHander { if (isBlankString(self.nextUrl)) { [self loadFailed]; return; } NSString *url = [self.nextUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; [self remoteActionWithUrl:url]; } #pragma mark - remote delegate - (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData { if (actionTag == 1) { NSArray *list = responsData[@"commodityList"]; NSMutableArray *objectList = [NSMutableArray array]; [list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { commodityBO *commodity = setJsonDicToDataModel(obj, [commodityBO class]); [objectList addObject:commodity]; }]; self.commodityList = objectList; [self setHeightAndWidthList:self.commodityList]; self.taskList = responsData[@"taskInfos"]; NSMutableArray *heightList = [NSMutableArray array]; [self.taskList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSDictionary *task = obj; if ([task[@"type"] isEqualToString:@"4"]) { [heightList addObject:[NSString stringWithFormat:@"%f", (66+rect_screen.size.width*0.125)]]; } else [heightList addObject:[NSString stringWithFormat:@"%f", 45.0]]; }]; [heightList addObject:@"10.0"]; self.taskHeightList = heightList; self.todaySeashells = responsData[@"seashellInfo"][@"todaySeashells"]; self.totalSeashells = responsData[@"seashellInfo"][@"totalSeashells"]; self.shareTitle = responsData[@"shareInfo"][@"title"]; self.shareSummary = responsData[@"shareInfo"][@"summary"]; self.sharePic = responsData[@"shareInfo"][@"sharePic"]; self.shareUrl = responsData[@"shareInfo"][@"shareUrl"]; self.nextUrl = responsData[@"nextUrl"]; [self.tableView reloadData]; } else if (actionTag == 2) { NSMutableArray *oldList= [NSMutableArray arrayWithArray:self.commodityList]; NSMutableArray *newList = [NSMutableArray array]; NSArray *list = responsData[@"commodityList"]; [list enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { commodityBO *commodity = setJsonDicToDataModel(obj, [commodityBO class]); [newList addObject:commodity]; }]; [oldList addObjectsFromArray:newList]; self.commodityList = oldList; [self setHeightAndWidthList:self.commodityList]; [self loadSuccess]; self.nextUrl = responsData[@"nextUrl"]; [self.tableView reloadData]; } } - (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code { if (actionTag == 1) { ShowTextMessage(message); } else if (actionTag == 2) { ShowTextMessage(message); [self loadFailed]; } } - (void)setHeightAndWidthList:(NSArray*)commodityList { NSMutableArray *hlist = [NSMutableArray array]; NSMutableArray *wlist = [NSMutableArray array]; __weak typeof(self) weakSelf = self; [commodityList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { commodityBO *commodity = obj; weakSelf.heightLabel.text = commodity.name; weakSelf.widthLabel.text = commodity.seashell; CGFloat height = [weakSelf.heightLabel sizeThatFits:CGSizeMake((rect_screen.size.width-40.5)/2.0, 0)].height; CGFloat numWidth = [weakSelf.widthLabel sizeThatFits:CGSizeMake(0, 16)].width; [hlist addObject:[NSString stringWithFormat:@"%f", height]]; [wlist addObject:[NSString stringWithFormat:@"%f", numWidth]]; }]; self.labelHeightList = hlist; self.labelWidthList = wlist; } #pragma mark - uitableview delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 3; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) return 1; else if (section == 1) return self.taskHeightList.count; else return ceilf(self.commodityList.count/2.0); } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { seaShellCell *cell = [tableView dequeueReusableCellWithIdentifier:seaShellCellID]; if (cell == nil) { cell = [[seaShellCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:seaShellCellID]; cell.userInteractionEnabled = false; } seashellCell = cell; if (!isBlankString(self.todaySeashells)) { cell.todaySeashells = self.todaySeashells; } if (!isBlankString(self.totalSeashells)) { cell.totalSeashells = self.totalSeashells; } return cell; } else if (indexPath.section == 1) { if (indexPath.row == self.taskHeightList.count-1) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:bottomCellID]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:bottomCellID]; cell.backgroundColor = [UIColor colorWithHexString:CELLBACKCOLOR]; } return cell; } else { if ([self.taskList[indexPath.row][@"type"] isEqualToString:@"4"]) { recommendCell *cell = [tableView dequeueReusableCellWithIdentifier:recommendCellID]; if (cell == nil) { cell = [[recommendCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:recommendCellID]; } cell.delegate = self; recommendcell = cell; if (self.taskList.count) { cell.taskInfo = self.taskList[indexPath.row]; } if (indexPath.row < self.taskList.count-1) { [cell addBottomLine]; } return cell; } else { taskCell *cell = [tableView dequeueReusableCellWithIdentifier:taskCellID]; if (cell == nil) { cell = [[taskCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:taskCellID]; cell.userInteractionEnabled = false; } if (self.taskList.count) { cell.taskInfo = self.taskList[indexPath.row]; } if (indexPath.row < self.taskList.count-1) { [cell addBottomLine]; } return cell; } } } else { commodityCell *cell = [tableView dequeueReusableCellWithIdentifier:commodityCellID]; if (cell == nil) { cell = [[commodityCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:commodityCellID]; } if (self.commodityList.count && indexPath.row*2 < self.commodityList.count) { cell.delegate = self; if (indexPath.row*2+1 >= self.commodityList.count) // [cell setLeftCommodity:self.commodityList[indexPath.row*2] andRightCommodity:nil]; [cell setLeftCommodity:self.commodityList[indexPath.row*2] leftHeight:[self.labelHeightList[indexPath.row*2] floatValue] leftWidth:[self.labelWidthList[indexPath.row*2] floatValue] RightCommodity:nil rightHeight:0 rightWidth:0]; else [cell setLeftCommodity:self.commodityList[indexPath.row*2] leftHeight:[self.labelHeightList[indexPath.row*2] floatValue] leftWidth:[self.labelWidthList[indexPath.row*2] floatValue] RightCommodity:self.commodityList[indexPath.row*2+1] rightHeight:[self.labelHeightList[indexPath.row*2+1] floatValue] rightWidth:[self.labelWidthList[indexPath.row*2+1] floatValue]]; // [cell setLeftCommodity:self.commodityList[indexPath.row*2] andRightCommodity:self.commodityList[indexPath.row*2+1]]; } return cell; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) return 160; else if (indexPath.section == 1) { // if (indexPath.row == self.taskList.count-1) // return 76+rect_screen.size.width*0.125; // else // return 45; return [self.taskHeightList[indexPath.row] floatValue]; } else return (rect_screen.size.width-40.5)/2.0+85; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) return 0; else return 28.5; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 0) return nil; else if (section == 1) { return [self returnSectionHeaderView:@"每日任务"]; } else { return [self returnSectionHeaderView:@"海贝商城"]; } } - (UIView *)returnSectionHeaderView:(NSString*)string { UIView *view = [UIView new]; view.frame = CGRectMake(0, 0, rect_screen.size.width, 28.5); view.backgroundColor = [UIColor colorWithHexString:BackGroundColor]; UILabel *label = [UILabel new]; label.font = appFont(TEXT_FIVE_LEVELSIZE, NO); label.textColor = [UIColor colorWithHexString:BLUECOLOR]; label.text = string; [view addSubview:label]; [label makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(view.left).offset(10); make.right.equalTo(view.right); make.height.mas_equalTo(@15); make.centerY.equalTo(view.centerY); }]; UIView *line = [UIView new]; line.backgroundColor = [UIColor colorWithHexString:LINECOLOR]; [view addSubview:line]; [line makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(view.left); make.right.equalTo(view.right); make.bottom.equalTo(view.bottom); make.height.mas_equalTo(@0.5); }]; return view; } #pragma mark - commoditycell delegate - (void)gotoCommodity:(commodityBO*)commodity { backFlag = 1; commodityController *vc = [commodityController new]; vc.commodity = commodity; [self.navigationController pushViewController:vc animated:YES]; } #pragma mark - recommendcell delegate - (void)wechatRecommend { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.shareUrl imageUrl:self.sharePic title:self.shareTitle summary:self.shareSummary shareType:weChatSessionShareType shareStyle:shareNewsStyle completion:^(BOOL isSuccess) { NSLog(@"share fnish"); [weakSelf completion:isSuccess andWeiboType:@"WEIXIN"]; }]; } - (void)friendRecommend { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.shareUrl imageUrl:self.sharePic title:self.shareTitle summary:self.shareSummary shareType:weChatTimeLineShareType shareStyle:shareNewsStyle completion:^(BOOL isSuccess) { NSLog(@"share fnish"); [weakSelf completion:isSuccess andWeiboType:@"WEIXIN"]; }]; } - (void)weiboRecommend { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.shareUrl imageUrl:self.sharePic title:self.shareTitle summary:self.shareSummary shareType:sinaWeiBoShareType shareStyle:shareNewsStyle completion:^(BOOL isSuccess) { NSLog(@"share fnish"); [weakSelf completion:isSuccess andWeiboType:@"SINA"]; }]; } - (void)qqZoneRecommend { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.shareUrl imageUrl:self.sharePic title:self.shareTitle summary:self.shareSummary shareType:QQZoneShareType shareStyle:shareNewsStyle completion:^(BOOL isSuccess) { NSLog(@"share fnish"); [weakSelf completion:isSuccess andWeiboType:@"TENCENT"]; }]; } - (void)qqRecommend { __weak typeof(self) weakSelf = self; [shareUtil shareNews:self.shareUrl imageUrl:self.sharePic title:self.shareTitle summary:self.shareSummary shareType:QQFriendShareType shareStyle:shareNewsStyle completion:^(BOOL isSuccess) { NSLog(@"share fnish"); [weakSelf completion:isSuccess andWeiboType:@"TENCENT"]; }]; } - (void)completion:(BOOL)isSuccess andWeiboType:(NSString*)weiboType { if (isSuccess) { NSLog(@"upload sharelog"); __weak typeof(self) weakSelf = self; NSDictionary *dic = @{@"weiboType": weiboType, @"shareType": @"4"}; [Remote doJsonActionWithBlock:1 requestUrl:shareLogURL parameter:dic withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) { if (success) [weakSelf refreshPoints]; else NSLog(@"upload failed"); }]; } else NSLog(@"share failed"); } - (void)refreshPoints { NSLog(@"get points"); __weak typeof(self) weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [Remote doJsonActionWithBlock:1 requestUrl:mySeashellURL parameter:nil withWaitCursor:NO completion:^(BOOL success, NSString *message, id responseData) { if (success) { NSLog(@"refresh points"); weakSelf.todaySeashells = responseData[@"seashellInfo"][@"todaySeashells"]; weakSelf.totalSeashells = responseData[@"seashellInfo"][@"totalSeashells"]; weakSelf.taskList = responseData[@"taskInfos"]; seashellCell.todaySeashells = weakSelf.todaySeashells; seashellCell.totalSeashells = weakSelf.totalSeashells; for (NSDictionary *obj in weakSelf.taskList) { if ([obj[@"type"] isEqualToString:@"4"]) { recommendcell.taskInfo = obj; break; } } } else ShowTextMessage(message); }]; }); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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. } */ @end