|
//
// frontPageCollectionController.m
// ThePaperHD
//
// Created by scar1900 on 15/1/5.
// Copyright (c) 2015年 scar1900. All rights reserved.
//
#import "frontPageCollectionController.h"
#import "paperCollectCell.h"
#import "hotAnswerCell.h"
#import "loginCollectCell.h"
#import "orderController.h"
#import "topicListCell.h"
typedef enum {
RefreshStatePulling = 1, // 松开就可以进行刷新的状态
RefreshStateNormal = 2, // 普通状态
RefreshStateRefreshing = 3, // 正在刷新中的状态
RefreshStateCancel = 4,
RefreshStateSucess = 5,
} RefreshState;
@interface leftRefreshView : UIView
@property(nonatomic, strong)UILabel *textLabel;
@property(nonatomic, assign)RefreshState state;
@end
@implementation leftRefreshView
@synthesize state = _state;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self addSubview:self.textLabel];
self.state = RefreshStateNormal;
self.backgroundColor = [UIColor clearColor];
}
return self;
}
- (UILabel*)textLabel {
if (!_textLabel) {
_textLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_textLabel.font = appFont(15, NO);
_textLabel.textAlignment = NSTextAlignmentRight;
_textLabel.backgroundColor = [UIColor clearColor];
}
_textLabel.textColor = [UIColor colorWithHexString:TextBlack];
return _textLabel;
}
- (void)setState:(RefreshState)sta {
if (_state == sta) return;
_state = sta;
if (sta == RefreshStateNormal) {
self.textLabel.text = @"左拉刷新";
}else if (sta == RefreshStatePulling) {
self.textLabel.text = @"松开刷新";
}else if (sta == RefreshStateRefreshing) {
self.textLabel.text = @"正在刷新";
}else if (sta == RefreshStateCancel) {
self.state = RefreshStateNormal;
}else if (sta == RefreshStateSucess) {
self.state = RefreshStateNormal;
}
}
- (void)layoutSubviews {
[super layoutSubviews];
self.textLabel.frame = CGRectMake(0, 230/2-40, CGRectGetWidth(self.bounds), 40);
}
@end
@interface frontpageCollectViewMoreCell : UICollectionViewCell
@property(nonatomic, strong)UILabel *textLabel;
@property(nonatomic, strong)UIView *roundView;
@end
@implementation frontpageCollectViewMoreCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self addSubview:self.roundView];
[self.roundView addSubview:self.textLabel];
}
return self;
}
- (UIView*)roundView {
if (!_roundView) {
_roundView = [[UIView alloc]initWithFrame:CGRectZero];
_roundView.backgroundColor = [UIColor colorWithHexString:BUTTONDISABLEBACK];
_roundView.layer.cornerRadius = 25;
}
return _roundView;
}
- (UILabel*)textLabel {
if (!_textLabel) {
_textLabel = [[UILabel alloc]initWithFrame:CGRectZero];
_textLabel.font = appFont(11, NO);
_textLabel.textColor = [UIColor whiteColor];
_textLabel.textAlignment = NSTextAlignmentCenter;
_textLabel.lineBreakMode = NSLineBreakByCharWrapping;
_textLabel.numberOfLines = 0;
_textLabel.backgroundColor = [UIColor clearColor];
}
return _textLabel;
}
- (void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
if (highlighted) {
self.textLabel.textColor = [UIColor colorWithHexString:BUTTONSELECTBACK];
}else self.textLabel.textColor = [UIColor whiteColor];
}
- (void)layoutSubviews {
[super layoutSubviews];
self.roundView.frame = CGRectMake(0, CGRectGetHeight(self.bounds)/2-25, 50, 50);
self.textLabel.frame = CGRectMake(CGRectGetWidth(self.roundView.bounds)/2-15,
CGRectGetHeight(self.roundView.bounds)/2-15,
30,
30);
}
@end
@interface frontPageCollectionController ()<paperCollectDelegate,RemoteDelegate,hotAnswerCellDelegate> {
}
@property(nonatomic, strong)leftRefreshView *refreshView;
@end
@implementation frontPageCollectionController
@synthesize dataList = _dataList;
@synthesize delegate;
@synthesize nodeId = _nodeId;
static NSString * const paperCollectReuseIdentifier = @"frontPageCollectItem";
static NSString * const askMoreReuseIdentifier = @"askMoreItem";
static NSString * const hotAnswerCellReuseIdentifier = @"hotAnswerItem";
static NSString * const topicListCellReuseIdentifier = @"topicListItem";
static NSString * const loginCellReuseIdentifier = @"loginItem";
- (void)viewDidLoad {
[super viewDidLoad];
self.view.clipsToBounds = NO;
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = NO;
// Register cell classes3
[self.collectionView registerClass:[paperCollectCell class] forCellWithReuseIdentifier:paperCollectReuseIdentifier];
[self.collectionView registerClass:[frontpageCollectViewMoreCell class] forCellWithReuseIdentifier:askMoreReuseIdentifier];
[self.collectionView registerClass:[hotAnswerCell class] forCellWithReuseIdentifier:hotAnswerCellReuseIdentifier];
[self.collectionView registerClass:[topicListCell class] forCellWithReuseIdentifier:topicListCellReuseIdentifier];
[self.collectionView registerClass:[loginCollectCell class] forCellWithReuseIdentifier:loginCellReuseIdentifier];
self.collectionView.allowsSelection = YES;
self.collectionView.scrollEnabled = YES;
self.collectionView.showsHorizontalScrollIndicator= NO;
self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.alwaysBounceHorizontal = YES;
[self.collectionView addSubview:self.refreshView];
[self addObserver:self forKeyPath:@"self.collectionView.contentOffset" options:NSKeyValueObservingOptionNew context:nil];
// Do any additional setup after loading the view.
}
- (leftRefreshView*)refreshView {
if (!_refreshView) {
_refreshView = [[leftRefreshView alloc]initWithFrame:CGRectMake(-180, 0, 150, CGRectGetHeight(self.view.bounds))];
}
return _refreshView;
}
- (void)setDataList:(NSMutableArray *)list {
if ([self.nodeId intValue] == -1 && ![TPUserDefault instance].userBO) {
_dataList = [NSMutableArray array];
[_dataList addObject:@"login"];
[self.collectionView reloadData];
[self.collectionView setContentOffset:CGPointZero animated:NO];
self.collectionView.scrollEnabled = NO;
return;
}else if ([self.nodeId intValue] == -1 && [TPUserDefault instance].userBO && list.count == 0) {
[list addObject:@"subscribe"];
_dataList = list;
[self.collectionView reloadData];
[self.collectionView setContentOffset:CGPointZero animated:NO];
self.collectionView.scrollEnabled = YES;
return;
}
[list addObject:@"more"];
_dataList = list;
self.collectionView.scrollEnabled = YES;
[self.collectionView reloadData];
[self.collectionView setContentOffset:CGPointZero animated:NO];
}
- (void)setNodeId:(NSString *)nodeIdStr {
_nodeId = nodeIdStr;
}
- (void)endLeftRfresh:(void (^)(void))completion {
self.refreshView.state = RefreshStateSucess;
[UIView animateWithDuration:0.25 animations:^{
self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}completion:^(BOOL finished) {
if (completion) {
completion();
}
}];
}
- (void)startLeftRfresh {
if (self.refreshView.state == RefreshStateRefreshing) {
return;
}
if ([self.delegate respondsToSelector:@selector(startCollectRfresh)]) {
[self.delegate startCollectRfresh];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
id data = self.dataList[indexPath.row];
if ([data isKindOfClass:[NSString class]]) {
if ([data isEqualToString:@"more"]) {
return [self getAskMoreCell:collectionView cellForItemAtIndexPath:indexPath];
}else if ([data isEqualToString:@"subscribe"]) {
return [self getLoginCell:collectionView cellForItemAtIndexPath:indexPath];
}else {
return [self getLoginCell:collectionView cellForItemAtIndexPath:indexPath];
}
}else if ([data isKindOfClass:[listContObjectVO class]]){
return [self getCollectCell:collectionView cellForItemAtIndexPath:indexPath];
}else if ([data isKindOfClass:[commentObjectVO class]]) {
return [self getHotAnswerCell:collectionView cellForItemAtIndexPath:indexPath];
}else {
return [self getTopicListCell:collectionView cellForItemAtIndexPath:indexPath];
}
}
- (paperCollectCell *)getCollectCell:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
paperCollectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:paperCollectReuseIdentifier forIndexPath:indexPath];
id data = self.dataList[indexPath.row];
cell.listContentBO = data;
cell.delegate = self;
return cell;
}
- (hotAnswerCell*)getHotAnswerCell:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
hotAnswerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:hotAnswerCellReuseIdentifier forIndexPath:indexPath];
id data = self.dataList[indexPath.row];
cell.commentBO = data;
cell.delegate = self;
cell.indexPath = indexPath;
return cell;
}
- (topicListCell*)getTopicListCell:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
topicListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:topicListCellReuseIdentifier forIndexPath:indexPath];
id data = self.dataList[indexPath.row];
cell.topicBO = data;
return cell;
}
- (frontpageCollectViewMoreCell *)getAskMoreCell:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
frontpageCollectViewMoreCell *cell = (frontpageCollectViewMoreCell*)[collectionView dequeueReusableCellWithReuseIdentifier:askMoreReuseIdentifier forIndexPath:indexPath];
cell.textLabel.text = @"查看更多";
return cell;
}
- (loginCollectCell *)getLoginCell:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
loginCollectCell *cell = (loginCollectCell*)[collectionView dequeueReusableCellWithReuseIdentifier:loginCellReuseIdentifier forIndexPath:indexPath];
id data = self.dataList[indexPath.row];
if ([data isEqualToString:@"subscribe"]) {
cell.isLogin = NO;
}else cell.isLogin = YES;
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
id data = self.dataList[indexPath.row];
if ([data isKindOfClass:[NSString class]] && [data isEqualToString:@"more"]) {
return CGSizeMake(50, CGRectGetHeight(self.view.bounds));
}else {
return CGSizeMake(300, CGRectGetHeight(self.view.bounds));
}
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UIEdgeInsets top = {0,10,0,10};
return top;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
id data = self.dataList[indexPath.row];
if ([data isKindOfClass:[NSString class]] ) {
if ( [data isEqualToString:@"more"]) {
if ([self.delegate respondsToSelector:@selector(goToMoreInfoPage:)]) {
[self.delegate goToMoreInfoPage:data];
}
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}else {
if ([data isEqualToString:@"login"]) {
loginCollectCell *cell = (loginCollectCell*)[collectionView cellForItemAtIndexPath:indexPath];
if (ISNotRETINA) {
if ([self.delegate respondsToSelector:@selector(needLogin)]) {
[self.delegate needLogin];
}
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}else {
[UIView animateWithDuration:0.25 animations:^{
cell.transform = CGAffineTransformMakeScale(0.95, 0.95);
} completion:^(BOOL finished) {
cell.transform = CGAffineTransformMakeScale(1.00, 1.00);
if ([self.delegate respondsToSelector:@selector(needLogin)]) {
[self.delegate needLogin];
}
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
}
}else if ([data isEqualToString:@"subscribe"]) {
loginCollectCell *cell = (loginCollectCell*)[collectionView cellForItemAtIndexPath:indexPath];
if (ISNotRETINA) {
[[NSNotificationCenter defaultCenter] postNotificationName:POPUPORDERCENTER object:nil];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}else {
[UIView animateWithDuration:0.25 animations:^{
cell.transform = CGAffineTransformMakeScale(0.95, 0.95);
} completion:^(BOOL finished) {
cell.transform = CGAffineTransformMakeScale(1.00, 1.00);
[[NSNotificationCenter defaultCenter] postNotificationName:POPUPORDERCENTER object:nil];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
}
}
}
}else if ([data isKindOfClass:[listContObjectVO class]]){
paperCollectCell *cell = (paperCollectCell*)[collectionView cellForItemAtIndexPath:indexPath];
if (ISNotRETINA) {
NSDictionary *dic = @{@"listContentData":data};
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHTODETAILCONTENT object:nil userInfo:dic];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}else {
[UIView animateWithDuration:0.25 animations:^{
cell.transform = CGAffineTransformMakeScale(0.95, 0.95);
} completion:^(BOOL finished) {
cell.transform = CGAffineTransformMakeScale(1.00, 1.00);
NSDictionary *dic = @{@"listContentData":data};
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHTODETAILCONTENT object:nil userInfo:dic];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
}
}else if ([data isKindOfClass:[commentObjectVO class]]){
hotAnswerCell *cell = (hotAnswerCell*)[collectionView cellForItemAtIndexPath:indexPath];
if (ISNotRETINA) {
NSDictionary *dic = @{@"data":data};
[[NSNotificationCenter defaultCenter] postNotificationName:PRESENTHOTASK object:nil userInfo:dic];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}else {
[UIView animateWithDuration:0.25 animations:^{
cell.transform = CGAffineTransformMakeScale(0.95, 0.95);
} completion:^(BOOL finished) {
cell.transform = CGAffineTransformMakeScale(1.00, 1.00);
NSDictionary *dic = @{@"data":data};
[[NSNotificationCenter defaultCenter] postNotificationName:PRESENTHOTASK object:nil userInfo:dic];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
}
}else {
topicListCell *cell = (topicListCell*)[collectionView cellForItemAtIndexPath:indexPath];
if (ISNotRETINA) {
NSDictionary *dic = @{@"listContentData":data};
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHTODETAILCONTENT object:nil userInfo:dic];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}else {
[UIView animateWithDuration:0.25 animations:^{
cell.transform = CGAffineTransformMakeScale(0.95, 0.95);
} completion:^(BOOL finished) {
cell.transform = CGAffineTransformMakeScale(1.00, 1.00);
NSDictionary *dic = @{@"listContentData":data};
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHTODETAILCONTENT object:nil userInfo:dic];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}];
}
}
}
#pragma mark - button Handler
- (void)goToColumnPage:(listContObjectVO *)listBO {
if ([self.delegate respondsToSelector:@selector(pushToColumPage:)]) {
[self.delegate pushToColumPage:listBO];
}
}
- (void)titleClick:(commentObjectVO *)contBO {
NSDictionary *dic = @{@"listContentData":contBO};
if ([contBO.objectType integerValue] != 3) {
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHTODETAILCONTENT object:nil userInfo:dic];
}else{
[[NSNotificationCenter defaultCenter] postNotificationName:PUSHTOTOPICDETAILCONTENT object:nil userInfo:dic];
}
}
- (void)popDetailAnswerPage:(commentObjectVO *)comment backColor:(UIColor *)color{
NSDictionary *dic = @{@"commentData":comment,@"color":color};
[[NSNotificationCenter defaultCenter] postNotificationName:POPANSWERDETAIL object:nil userInfo:dic];
}
- (void)hasPraisedComment:(NSIndexPath *)indexPath withCommentBO:(commentObjectVO *)commentBO {
// NSDictionary *dic = setDataModelToDic(commentBO, [commentBO class]);
// commentObjectVO *comment = self.dataList[indexPath.row];
// NSArray *array = [NSArray arrayWithObject:dic];
// comment.answerList = array;
// [self.dataList replaceObjectAtIndex:indexPath.row withObject:comment];
//
// if ([self.delegate respondsToSelector:@selector(hasPraisedComment:)]) {
// [self.delegate hasPraisedComment:<#(commentObjectVO *)#>]
// }
}
#pragma mark <UICollectionViewDelegate>
// Uncomment this method to specify if the specified item should be highlighted during tracking
//- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
// return YES;
//}
/*
Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
*/
/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
return NO;
}
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
}
*/
#pragma mark - KVO handler
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (self.collectionView.contentOffset.x >= 0) {
return;
}; //向左滚出视区
if (self.refreshView.state == RefreshStateRefreshing) {
return;
}
if (self.collectionView.isDragging) {
CGFloat normal2pullingOffsetX = 0-120.f;
if (self.refreshView.state == RefreshStateNormal && self.collectionView.contentOffset.x < normal2pullingOffsetX) {
// 转为即将刷新状态
self.refreshView.state = RefreshStatePulling;
} else if (self.refreshView.state == RefreshStatePulling && self.collectionView.contentOffset.x >= normal2pullingOffsetX) {
// 转为普通状态
self.refreshView.state = RefreshStateNormal;
}
}else {
if (self.refreshView.state == RefreshStatePulling) { //松手转为刷新状态
// 开始刷新
[UIView animateWithDuration:0.2 animations:^{
self.collectionView.contentInset = UIEdgeInsetsMake(0, 120, 0, 0);
}];
[self startLeftRfresh];
self.refreshView.state = RefreshStateRefreshing;
}
}
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"self.collectionView.contentOffset"];
}
@end
|