|
//
// nodeInfoController.m
// ThePaperDemo
//
// Created by scar1900 on 14-9-30.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "nodeInfoController.h"
#import "CoreAnimationEffect.h"
#import "nodeInfoCell.h"
//#import "orderManagerController.h"
#import "columnListController.h"
#import "loginPopUpController.h"
#import "customFlowLayout.h"
@interface nodeInfoController()<UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic,strong)UIView *orderBackView;
@property(nonatomic,strong)UIButton *orderButton;
@property(nonatomic,strong)UILabel *orderBackLabel;
@property(nonatomic, strong)UITapGestureRecognizer *tapGesture;
@end
@implementation nodeInfoController
@synthesize nodeList = _nodeList;
@synthesize orderNodeBO = _orderNodeBO;
@synthesize orderButton;
@synthesize orderBackLabel;
@synthesize isOpenDeleteMode;
@synthesize tapGesture;
@synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
isOpenDeleteMode = NO;
self.tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
self.collectionView.frame = self.view.bounds;
self.orderBackView.frame = self.view.bounds;
self.orderButton.frame = CGRectMake(CGRectGetWidth(_orderBackView.bounds)/2-50/2, CGRectGetHeight(_orderBackView.bounds)/2-75, 50, 50);
self.orderBackLabel.frame = CGRectMake(CGRectGetWidth(_orderBackView.bounds)/2-270/4, CGRectGetMaxY(self.orderButton.frame)+15, 270/2, 40);
[self hideDeleteMode];
}
- (void)setNodeList:(NSMutableArray *)list {
_nodeList = list;
if (!_collectionView) {
[self.view addSubview:self.collectionView];
[self.collectionView addGestureRecognizer:self.tapGesture];
self.tapGesture.enabled = NO;
}
if (list.count == 0) {
self.collectionView.hidden = YES;
}else {
self.collectionView.hidden = NO;
}
[self.collectionView reloadData];
[self.collectionView setContentOffset:CGPointMake(0, 0) animated:NO];
[CoreAnimationEffect animationEaseOut:self.collectionView];
}
- (UICollectionView*)collectionView {
if (!_collectionView) {
CGSize itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds)/3, CGRectGetWidth(self.view.bounds)/3);
customFlowLayout *layout= [[customFlowLayout alloc]init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 0;
layout.itemSize = itemSize;
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))
collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.clipsToBounds = YES;
[_collectionView registerClass:[nodeInfoCell class] forCellWithReuseIdentifier:@"nodeCollectCellID"];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"addCollectCell"];
_collectionView.allowsSelection = YES;
_collectionView.scrollEnabled = YES;
_collectionView.alwaysBounceVertical = YES;
_collectionView.showsVerticalScrollIndicator= NO;
_collectionView.showsHorizontalScrollIndicator= NO;
_collectionView.backgroundColor = [UIColor clearColor];
// _collectionView.bounces = NO;
[self.view addSubview:_collectionView];
}
return _collectionView;
}
- (UIView*)orderBackView {
if (!_orderBackView) {
_orderBackView = [[UIView alloc]initWithFrame:self.view.bounds];
_orderBackView.hidden = NO;
_orderBackView.autoresizingMask = AutoresizingFull;
self.orderButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.orderButton setImage:Image(@"Button/orderButton.png") forState:UIControlStateNormal];
self.orderButton.frame = CGRectMake(CGRectGetWidth(_orderBackView.bounds)/2-50/2, CGRectGetHeight(_orderBackView.bounds)/2-75, 50, 50);
[_orderBackView addSubview:self.orderButton];
self.orderBackLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(_orderBackView.bounds)/2-270/4, CGRectGetMaxY(self.orderButton.frame)+15, 270/2, 40)];
self.orderBackLabel.textAlignment = NSTextAlignmentLeft;
self.orderBackLabel.textColor = [UIColor colorWithHexString:LINECOLOR];
self.orderBackLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.orderBackLabel.numberOfLines = 0;
self.orderBackLabel.backgroundColor = [UIColor clearColor];
[_orderBackView addSubview:self.orderBackLabel];
}
_orderBackView.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
return _orderBackView;
}
- (void)setOrderNodeBO:(nodeObjectBO *)nodeBO {
_orderNodeBO = nodeBO;
[self.orderBackView removeFromSuperview];
[self.view addSubview:self.orderBackView];
if ([nodeBO.nodeId isEqualToString:@"-1"]) {
self.orderBackView.hidden = NO;
self.orderBackLabel.font = appFont(15, NO);
if ([nodeBO.myOrderStatus intValue] == 2 && ![TPUserDefault instance].userBO) {
[self.orderButton removeTarget:self action:@selector(jumpToOrder:) forControlEvents:UIControlEventTouchUpInside];
[self.orderButton addTarget:self action:@selector(loginHandler:) forControlEvents:UIControlEventTouchUpInside];
self.orderBackLabel.text = @"登录后,马上添加感兴趣的栏目到这里";
}else if ([nodeBO.myOrderStatus intValue] == 3) {
[self.orderButton removeTarget:self action:@selector(loginHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.orderButton addTarget:self action:@selector(jumpToOrder:) forControlEvents:UIControlEventTouchUpInside];
self.orderBackLabel.text = @"马上添加感兴趣\n的栏目到这里";
}else if ([nodeBO.myOrderStatus intValue] == 4) {
self.orderBackView.hidden = YES;
}else {
self.orderBackView.hidden = YES;
[self.orderButton removeTarget:self action:@selector(loginHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.orderButton addTarget:self action:@selector(jumpToOrder:) forControlEvents:UIControlEventTouchUpInside];
self.orderBackLabel.text = @"马上添加感兴趣\n的栏目到这里";
}
self.tapGesture.enabled = YES;
}else {
self.orderBackView.hidden = YES;
self.tapGesture.enabled = NO;
}
}
- (void)loginHandler:(UIButton*)btn {
loginPopUpController *loginVC = loginPopUpController.new;
[self presentController:loginVC animated:YES presentSize:loginPopUpSize completion:^{
} dismiss:^{
if (refreshRemoteOrderData && [TPUserDefault instance].userBO) {
refreshRemoteOrderData();
}
}];
}
- (void)jumpToOrder:(UIButton*)btn {
[self jumpToOrderCenter];
}
- (void)jumpToOrderCenter {
if ([self.delegate respondsToSelector:@selector(popUpOrderCenter)]) {
[self.delegate popUpOrderCenter];
}
}
- (void)jumpToColumnManager:(nodeObjectBO*)nodeObj {
customFlowLayout *layout = nil;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
CGSize itemSize = CGSizeZero;
layout= [[customFlowLayout alloc]init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.minimumLineSpacing = 35;
layout.minimumInteritemSpacing = 35;
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
CGFloat width = (self.view.bounds.size.width-35*3)/2;
itemSize = CGSizeMake(width, 470/2);
}else {
CGFloat width = (self.view.bounds.size.width-35*4)/3;
itemSize = CGSizeMake(width, 446/2);
}
layout.itemSize = itemSize;
columnListController *vc = [[columnListController alloc]initWithCollectionViewLayout:layout];
vc.nodeObj = nodeObj;
[self.navigationController pushViewController:vc animated:YES];
layout = nil;
}
- (void)openDeleteMode {
isOpenDeleteMode = YES;
[self.collectionView reloadData];
[CoreAnimationEffect animationEaseIn:self.collectionView];
}
- (void)hideDeleteMode {
isOpenDeleteMode = NO;
[self.collectionView reloadData];
[CoreAnimationEffect animationEaseIn:self.collectionView];
}
- (void)tap:(UITapGestureRecognizer*)tap {
// UIGestureRecognizerState state = tap.state;
CGPoint location = [tap locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:location];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
if (cell) {
if (isOpenDeleteMode) {
[self hideDeleteMode];
}else {
if (indexPath.row == 0) {
// [self jumpToOrderCenter];
}else {
nodeObjectBO* node = self.nodeList[indexPath.row];
[self jumpToColumnManager:node];
}
}
}else {
if (isOpenDeleteMode) {
[self hideDeleteMode];
}
}
}
#pragma mark - collction View delegate and datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (self.nodeList && self.nodeList.count > 0) {
return self.nodeList.count;
}else return 0;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellId = @"nodeCollectCellID";
static NSString* addCellId = @"addCollectCell";
if ([self.orderNodeBO.nodeId isEqualToString:@"-1"] && indexPath.row == 0) {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:addCellId forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
cell.layer.borderColor = [UIColor colorWithHexString:LINECOLOR].CGColor;
cell.layer.borderWidth = 0.5;
UIView *selectView = [[UIView alloc]initWithFrame:cell.bounds];
selectView.backgroundColor = [UIColor colorWithHexString:LINECOLOR];
cell.selectedBackgroundView = selectView;
UIImageView *imgView = (UIImageView*)[cell viewWithTag:1500];
if (!imgView) {
imgView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(cell.bounds)/2-25, CGRectGetHeight(cell.bounds)/2-25, 50, 50)];
imgView.userInteractionEnabled = YES;
imgView.image = Image(@"Button/orderButton.png");
imgView.tag = 1500;
[cell addSubview:imgView];
}
return cell;
}else {
nodeInfoCell *cell = (nodeInfoCell*)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
// [cell sizeToFit];
cell.nodeInfoBO = self.nodeList[indexPath.row];
if (isOpenDeleteMode) {
cell.isDelete = YES;
}else {
cell.isDelete = NO;
}
cell.cellIndex = indexPath.row;
__weak typeof(self) Self = self;
__block NSInteger deleteRow = 0;
[cell cancelOrderBlock:^(nodeObjectBO *nodeBO) {
[Self.nodeList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[nodeObjectBO class]]) {
nodeObjectBO *nodeOBJ = obj;
if ([nodeOBJ.nodeId doubleValue] == [nodeBO.nodeId doubleValue]) {
deleteRow = idx;
}
}
}];
if (deleteRow > 0) {
NSIndexPath *IndexPath = [NSIndexPath indexPathForItem:deleteRow inSection:0];
[Self.nodeList removeObjectAtIndex:IndexPath.row];
NSArray *array = [NSArray arrayWithObject:IndexPath];
[Self.collectionView deleteItemsAtIndexPaths:array];
if (Self.nodeList.count == 1) {
[Self.collectionView reloadData];
Self.orderBackView.hidden = NO;
Self.orderBackLabel.font = appFont(15, NO);
[Self.orderButton addTarget:Self action:@selector(jumpToOrderCenter) forControlEvents:UIControlEventTouchUpInside];
Self.orderBackLabel.text = @"马上添加感兴趣\n的栏目到这里";
[Self hideDeleteMode];
}
ShowMessage(@"退订成功", YES);
if ([Self.delegate respondsToSelector:@selector(haveDeleteNode)]) {
[Self.delegate haveDeleteNode];
}
}
}];
return cell;
}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
if ([self.orderNodeBO.nodeId isEqualToString:@"-1"]) {
if (isOpenDeleteMode) {
[self hideDeleteMode];
}else {
if (indexPath.row == 0) {
[self jumpToOrderCenter];
}else {
nodeObjectBO* node = self.nodeList[indexPath.row];
[self jumpToColumnManager:node];
}
}
}else {
nodeObjectBO* node = self.nodeList[indexPath.row];
[self jumpToColumnManager:node];
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(CGRectGetWidth(self.view.bounds)/3, CGRectGetWidth(self.view.bounds)/3);
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
UIEdgeInsets top = {0,0,0,0};
return top;
}
- (void)refreshRemoteOrderData:(void (^)())block {
refreshRemoteOrderData = [block copy];
}
#pragma mark - statusBarOrientationChange
- (void)statusBarOrientationChange:(NSNotification *)notification
{
self.collectionView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
self.orderBackView.frame = self.view.bounds;
self.orderButton.frame = CGRectMake(CGRectGetWidth(_orderBackView.bounds)/2-50/2, CGRectGetHeight(_orderBackView.bounds)/2-75, 50, 50);
self.orderBackLabel.frame = CGRectMake(CGRectGetWidth(_orderBackView.bounds)/2-270/4, CGRectGetMaxY(self.orderButton.frame)+15, 270/2, 40);
}
@end
|