|
//
// frontPageCell.m
// ThePaperHD
//
// Created by scar1900 on 15/1/5.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "frontPageCell.h"
#import "frontPageCollectionController.h"
#import "customFlowLayout.h"
@interface frontPageCell()<frontPageCollectDelegate> {
CGFloat nodeLabelWidth;
CGFloat assistLabelWidth;
}
@property(nonatomic, strong)UILabel *parentNodeLabel;
@property(nonatomic, strong)UILabel *assistantLabel;
@property(nonatomic, strong)UIActivityIndicatorView *activityView;
@property(nonatomic, strong)frontPageCollectionController *collectVC;
@property(nonatomic, strong)UIButton *moreButton;
@property(nonatomic, strong)NSString *nodeId;
@property(nonatomic, strong)UIButton *labelButton;
@end
@implementation frontPageCell
@synthesize channelInfoDataList = _channelInfoDataList;
@synthesize delegate;
@synthesize indexPath;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = NO;
[self addSubview:self.parentNodeLabel];
[self addSubview:self.assistantLabel];
[self addSubview:self.activityView];
[self addSubview:self.moreButton];
[self addSubview:self.labelButton];
[self addSubview:self.collectVC.view];
}
return self;
}
- (void)setChannelInfoDataList:(NSMutableArray *)list {
_channelInfoDataList = list;
NSDictionary *infoDic = [list lastObject];
self.nodeId = infoDic[@"nodeId"];
self.collectVC.nodeId = self.nodeId;
if (([self.nodeId intValue] == -1 && ![TPUserDefault instance].userBO) ||
([self.nodeId intValue] == -1 && [TPUserDefault instance].userBO && list.count == 1)) {
self.moreButton.hidden = YES;
self.labelButton.hidden = YES;
}else {
self.moreButton.hidden = NO;
self.labelButton.hidden = NO;
}
if ([infoDic[@"name"] isEqualToString:@"澎湃"]) {
self.parentNodeLabel.text = @"精选";
self.assistantLabel.text = infoDic[@"enname"];
}else {
self.parentNodeLabel.text = infoDic[@"name"];
self.assistantLabel.text = infoDic[@"enname"];
}
nodeLabelWidth = widthForString(self.parentNodeLabel.text, self.parentNodeLabel.font, 20, self.parentNodeLabel.lineBreakMode);
assistLabelWidth = widthForString(self.assistantLabel.text, self.assistantLabel.font, 15, self.assistantLabel.lineBreakMode);
if (nodeLabelWidth > assistLabelWidth) {
assistLabelWidth = nodeLabelWidth;
}
if (isBlankString(self.nodeId)) {//0xa89271
self.parentNodeLabel.textColor = [UIColor colorWithHexString:@"0xa89271"];
self.assistantLabel.textColor = self.parentNodeLabel.textColor;
}else {
self.parentNodeLabel.textColor = [UIColor colorWithHexString:infoDic[@"color"]];
self.assistantLabel.textColor = self.parentNodeLabel.textColor;
}
NSMutableArray *tmpArray = [NSMutableArray arrayWithArray:list];
[tmpArray removeLastObject];
self.collectVC.dataList = tmpArray;
// [self layoutSubviews];
}
- (UILabel*)parentNodeLabel {
if (!_parentNodeLabel) {
_parentNodeLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_parentNodeLabel.backgroundColor = [UIColor clearColor];
_parentNodeLabel.textAlignment = NSTextAlignmentLeft;
}
_parentNodeLabel.font = appFont(20, NO);
return _parentNodeLabel;
}
- (UILabel*)assistantLabel {
if (!_assistantLabel) {
_assistantLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_assistantLabel.backgroundColor = [UIColor clearColor];
_assistantLabel.textAlignment = NSTextAlignmentCenter;
}
_assistantLabel.font = appFont(10, NO);
return _assistantLabel;
}
- (UIActivityIndicatorView*)activityView {
if (!_activityView) {
_activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityView.frame = CGRectZero;
[_activityView stopAnimating];
_activityView.hidesWhenStopped = YES;
}
return _activityView;
}
- (frontPageCollectionController*)collectVC {
if (!_collectVC) {
CGSize itemSize = CGSizeMake(300.f, 460.f/2);
customFlowLayout *layout= [[customFlowLayout alloc]init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumLineSpacing = 0;
layout.minimumInteritemSpacing = 20;
layout.itemSize = itemSize;
_collectVC = [[frontPageCollectionController alloc]initWithCollectionViewLayout:layout];
_collectVC.delegate = self;
}
return _collectVC;
}
- (UIButton*)moreButton {
if (!_moreButton) {
_moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_moreButton setImage:Image(@"frontPage/askMoreArrow.png") forState:UIControlStateNormal];
[_moreButton setImage:Image(@"frontPage/askMoreArrow.png") forState:UIControlStateHighlighted];
[_moreButton setTitle:@"查看更多" forState:UIControlStateNormal];
[_moreButton setTitleColor:[UIColor colorWithHexString:TextGray] forState:UIControlStateNormal];
[_moreButton setTitleColor:[UIColor colorWithHexString:BUTTONSELECTBACK] forState:UIControlStateHighlighted];
_moreButton.titleLabel.font = appFont(11, NO);
_moreButton.titleLabel.textAlignment = NSTextAlignmentLeft;
[_moreButton addTarget:self action:@selector(goToMorePage:) forControlEvents:UIControlEventTouchUpInside];
}
return _moreButton;
}
- (UIButton*)labelButton {
if (!_labelButton) {
_labelButton = [UIButton buttonWithType:UIButtonTypeCustom];
_labelButton.backgroundColor = [UIColor clearColor];
_labelButton.showsTouchWhenHighlighted = YES;
[_labelButton addTarget:self action:@selector(goToMorePage:) forControlEvents:UIControlEventTouchUpInside];
}
return _labelButton;
}
- (void)layoutSubviews {
// [super layoutSubviews];
if ([[TPUserDefault instance].isNightMode intValue] > 0) {
self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
}else {
self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
}
if (self.channelInfoDataList) {
self.parentNodeLabel.frame = CGRectMake(15,
10,
nodeLabelWidth,
20);
self.assistantLabel.frame = CGRectMake(15,
CGRectGetMaxY(self.parentNodeLabel.frame)+2.5,
assistLabelWidth,
15);
self.activityView.frame = CGRectMake(CGRectGetMaxX(self.parentNodeLabel.frame)+5,
CGRectGetMinY(self.parentNodeLabel.frame)+5,
15,
15);
self.collectVC.view.frame = CGRectMake(5,
CGRectGetMaxY(self.assistantLabel.frame)+5,
CGRectGetWidth(self.bounds)-10,
430/2);
self.moreButton.frame = CGRectMake(CGRectGetWidth(self.bounds)-90,
CGRectGetMinY(self.parentNodeLabel.frame),
70,
CGRectGetMinY(self.collectVC.view.frame)-CGRectGetMinY(self.parentNodeLabel.frame));
[self.moreButton setImageEdgeInsets:UIEdgeInsetsMake(CGRectGetHeight(self.moreButton.bounds)/2-5,
CGRectGetWidth(self.moreButton.bounds)-13,
CGRectGetHeight(self.moreButton.bounds)/2-5,
0)];
[self.moreButton setTitleEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 13)];
// //首页,精选等频道名称感觉点击热区较小,难以点击(bug:4745)
// self.labelButton.frame = CGRectMake(CGRectGetMinX(self.parentNodeLabel.frame),
// CGRectGetMinY(self.parentNodeLabel.frame),
// CGRectGetWidth(self.parentNodeLabel.frame),
// CGRectGetHeight(self.parentNodeLabel.frame)+CGRectGetHeight(self.assistantLabel.frame)+2.5);
self.labelButton.frame = CGRectMake(CGRectGetMinX(self.parentNodeLabel.frame), CGRectGetMinY(self.parentNodeLabel.frame)-10,CGRectGetWidth(self.assistantLabel.frame),CGRectGetHeight(self.parentNodeLabel.frame)+CGRectGetHeight(self.assistantLabel.frame)+2.5+10);
}
}
- (void)goToMoreInfoPage:(NSDictionary *)infoDic {
if ([self.delegate respondsToSelector:@selector(clickMoreInfo:)]) {
NSDictionary *infoDic = [self.channelInfoDataList lastObject];
[self.delegate clickMoreInfo:infoDic];
}
}
- (void)pushToColumPage:(listContObjectVO *)listBO {
if ([self.delegate respondsToSelector:@selector(pushGotoNodeListPage:infoDic:)]) {
NSDictionary *infoDic = [self.channelInfoDataList lastObject];
[self.delegate pushGotoNodeListPage:listBO infoDic:infoDic];
}
}
- (void)goToMorePage:(UIButton*)btn {
if ([self.delegate respondsToSelector:@selector(clickMoreInfo:)]) {
NSDictionary *infoDic = [self.channelInfoDataList lastObject];
[self.delegate clickMoreInfo:infoDic];
}
}
- (void)startCollectRfresh {
if (isBlankString(self.nodeId)) {
NSDictionary *dic = @{@"sort":@"1",@"category":@""};
[Remote doJsonAction:10
requestUrl:topicListURL
parameter:dic
delegate:self];
}else {
NSDictionary *dic = @{@"n":self.nodeId};
if ([self.nodeId intValue] == -2) {//【需求】热追问加上话题(bug:3928)
[Remote doJsonAction:[self.nodeId intValue]
requestUrl:channelNodeListURL
parameter:dic
delegate:self];
}else {
[Remote doJsonAction:[self.nodeId intValue]
requestUrl:channelNodeListURL
parameter:dic
delegate:self];
}
}
}
- (void)needLogin {
if ([self.delegate respondsToSelector:@selector(popUpLoginWindow)]) {
[self.delegate popUpLoginWindow];
}
}
#pragma mark - remote delegate
- (void)startWaitCursor:(int)actionTag {
[self.activityView startAnimating];
}
- (void)stopWaitCursor:(int)actionTag {
}
- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData {
if (actionTag == [self.nodeId intValue]) {
if (actionTag != -3) {
[self.collectVC endLeftRfresh:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *contList = responsData[@"contList"];
NSMutableArray *tempArray = [NSMutableArray array];
if (contList && contList.count > 0) {
[contList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id contentBO = setJsonDicToDataModel(obj, [listContObjectVO class]);
listContObjectVO *listContentBO = contentBO;
if (!listContentBO.title && [listContentBO.cardMode intValue] != 1) {
[tempArray addObject:contentBO];
}
}];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityView stopAnimating];
if ([self.delegate respondsToSelector:@selector(leftRefreshSuccess:newDataList:)]) {
[self.delegate leftRefreshSuccess:self.indexPath newDataList:tempArray];
}
});
});
}];
}else {
[self.collectVC endLeftRfresh:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *contList = responsData[@"topicList"];
NSMutableArray *tempArray = [NSMutableArray array];
if (contList && contList.count > 0) {
[contList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
TopicInfoBO *infoBO = setJsonDicToDataModel(obj, [TopicInfoBO class]);
if (![infoBO.forwordType isEqualToString:@"4"]) {
[tempArray addObject:infoBO];
}
}];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityView stopAnimating];
if ([self.delegate respondsToSelector:@selector(leftRefreshSuccess:newDataList:)]) {
[self.delegate leftRefreshSuccess:self.indexPath newDataList:tempArray];
}
});
});
}];
}
}else {
[self.collectVC endLeftRfresh:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *contList = responsData[@"topicList"];
NSMutableArray *tempArray = [NSMutableArray array];
if (contList && contList.count > 0) {
[contList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id contentBO = setJsonDicToDataModel(obj, [TopicInfoBO class]);
TopicInfoBO *topicBO = contentBO;
NSString *desc = obj[@"description"];
topicBO.desc = desc;
[tempArray addObject:contentBO];
}];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityView stopAnimating];
if ([self.delegate respondsToSelector:@selector(leftRefreshSuccess:newDataList:)]) {
[self.delegate leftRefreshSuccess:self.indexPath newDataList:tempArray];
}
});
});
}];
}
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code{
if ([message isEqualToString:@"未订阅"]) {
[self.collectVC endLeftRfresh:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableArray *tempArray = [NSMutableArray array];
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityView stopAnimating];
if ([self.delegate respondsToSelector:@selector(leftRefreshSuccess:newDataList:)]) {
[self.delegate leftRefreshSuccess:self.indexPath newDataList:tempArray];
}
});
});
}];
}else {
ShowTextMessage(message);
}
}
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
|