// // GHRefreshCollectionView.m // GameHelper // // Created by 青秀斌 on 16/12/29. // Copyright © 2016年 kylincc. All rights reserved. // #import "GHRefreshCollectionView.h" #import "GHRefreshHeader.h" #import "GHRefreshFooter.h" #import "UIView+GHBlank.h" //#import #import "AFNetworkReachabilityManager.h" #define NET_CONNECT_ERROR @"当前网络不给力,请稍后重试" @interface GHRefreshCollectionView () @property (strong, nonatomic) NSMutableArray *dataArray; @property (assign, nonatomic) NSUInteger pageIndex; @property (assign, nonatomic) NSUInteger offset; @end @implementation GHRefreshCollectionView - (instancetype)init { self = [super init]; if (self) { [self awakeFromNib]; } return self; } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self awakeFromNib]; } return self; } - (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout { self = [super initWithFrame:frame collectionViewLayout:layout]; if (self) { [self awakeFromNib]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; self.pageIndex = 1; self.offset = 0; self.allowShowMore = YES; self.allowShowBlank = YES; self.allowShowNoNetworkBlank = YES; self.blankImage = nil; self.blankTitle = nil; self.blankMessage = nil; } - (void)setRefreshDelegate:(id)refreshDelegate { _refreshDelegate = refreshDelegate; if ([self.refreshDelegate respondsToSelector:@selector(collectionView:pageIndex:offset:success:failure:)]) { [self addRefreshHeader]; } } - (void)setAllowShowMore:(BOOL)allowShowMore { _allowShowMore = allowShowMore; if(_allowShowMore){ self.mj_footer.hidden = NO; }else{ self.mj_footer.hidden = YES; } // if (self.mj_footer) { // [self.mj_footer removeFromSuperview]; // } } -(void)setAllowShowBlank:(BOOL)allowShowBlank{ _allowShowBlank = allowShowBlank; if(!_allowShowBlank){ [self dismissBlank]; } } //针对个人中心主页 - 先出现一个暂无数据的空白页,之后才出现正确的空白页 -(void)closeBlankView{ [self dismissBlank]; } /**********************************************************************/ #pragma mark - Private /**********************************************************************/ - (void)addRefreshHeader { if (self.mj_header) { return; } GHRefreshHeader *header = [GHRefreshHeader headerWithRefreshingBlock:^{ NSUInteger pageIndex = 1; NSInteger offset = 0; WS(weakSelf); [self.refreshDelegate collectionView:self pageIndex:pageIndex offset:offset success:^(NSArray * _Nullable list, BOOL hasNext) { [weakSelf.mj_header endRefreshing]; //添加RefreshFooter [weakSelf addRefreshFooter]; if (hasNext) { [weakSelf.mj_footer endRefreshing]; } else { [weakSelf.mj_footer endRefreshingWithNoMoreData]; } //加载数据 weakSelf.pageIndex = pageIndex; weakSelf.offset = offset; if (weakSelf.reloadView) { weakSelf.reloadView(list); } else { weakSelf.dataArray = [list mutableCopy]; [weakSelf reloadData]; } if(self.noDataShowBlankByAddFakeDataFlag){ weakSelf.mj_footer.hidden = YES; [weakSelf dismissBlank]; [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; }else{ //显示占位图 if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } else { weakSelf.mj_footer.hidden = NO; [weakSelf dismissBlank]; } } } failure:^(NSError *error) { [weakSelf.mj_header endRefreshing]; //显示占位图 NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle; if(self.noDataShowBlankByAddFakeDataFlag){ weakSelf.mj_footer.hidden = YES; [weakSelf dismissBlank]; BOOL isHaveNet = NO; if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { isHaveNet = YES; } if (weakSelf.allowShowNoNetworkBlank && isHaveNet) { [weakSelf showBlankWithImage:@"no_network" title:NET_CONNECT_ERROR message:nil action:nil offsetY:weakSelf.offsetY]; } else { [weakSelf showBlankWithImage:@"failed_to_load" title:blankTitle?:@"加载失败" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } }else{ if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; BOOL isHaveNet = NO; if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { isHaveNet = YES; } if (weakSelf.allowShowNoNetworkBlank && isHaveNet) { [weakSelf showBlankWithImage:@"no_network" title:NET_CONNECT_ERROR message:nil action:nil offsetY:weakSelf.offsetY]; } else { [weakSelf showBlankWithImage:@"failed_to_load" title:blankTitle?:@"加载失败" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } } else { weakSelf.mj_footer.hidden = NO; [weakSelf dismissBlank]; // [SVProgressHUD showErrorWithStatus:blankTitle]; } } }]; }]; self.mj_header = header; } - (void)addRefreshFooter { if (!self.allowShowMore) { return; } if (self.mj_footer) { return; } GHRefreshFooter *footer = [GHRefreshFooter footerWithRefreshingBlock:^{ NSUInteger pageIndex = self.pageIndex + 1; NSInteger offset = self.dataArray.count + 1; WS(weakSelf); [self.refreshDelegate collectionView:self pageIndex:pageIndex offset:offset success:^(NSArray * _Nullable list, BOOL hasNext) { if (hasNext) { [weakSelf.mj_footer endRefreshing]; } else { [weakSelf.mj_footer endRefreshingWithNoMoreData]; } //加载数据 weakSelf.pageIndex = pageIndex; weakSelf.offset = offset; if (weakSelf.reloadView) { weakSelf.reloadView(list); } else { [weakSelf.dataArray addObjectsFromArray:list]; [weakSelf reloadData]; } if(self.noDataShowBlankByAddFakeDataFlag){ weakSelf.mj_footer.hidden = YES; [weakSelf dismissBlank]; [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; }else{ //显示占位图 if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data" title:weakSelf.blankTitle?:@"暂无数据" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } else { weakSelf.mj_footer.hidden = NO; [weakSelf dismissBlank]; } } } failure:^(NSError *error) { [weakSelf.mj_footer endRefreshing]; //显示占位图 NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle; if(self.noDataShowBlankByAddFakeDataFlag){ weakSelf.mj_footer.hidden = YES; [weakSelf dismissBlank]; BOOL isHaveNet = NO; if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { isHaveNet = YES; } if (weakSelf.allowShowNoNetworkBlank && isHaveNet) { [weakSelf showBlankWithImage:@"no_network" title:NET_CONNECT_ERROR message:nil action:nil offsetY:weakSelf.offsetY]; } else { [weakSelf showBlankWithImage:@"failed_to_load" title:blankTitle?:@"加载失败" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } }else{ if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) { weakSelf.mj_footer.hidden = YES; BOOL isHaveNet = NO; if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) { isHaveNet = YES; } if (weakSelf.allowShowNoNetworkBlank && isHaveNet) { [weakSelf showBlankWithImage:@"no_network" title:NET_CONNECT_ERROR message:nil action:nil offsetY:weakSelf.offsetY]; } else { [weakSelf showBlankWithImage:@"failed_to_load" title:blankTitle?:@"加载失败" message:weakSelf.blankMessage action:nil offsetY:weakSelf.offsetY]; } } else { weakSelf.mj_footer.hidden = NO; [weakSelf dismissBlank]; // [SVProgressHUD showErrorWithStatus:blankTitle]; } } }]; }]; self.mj_footer = footer; } /**********************************************************************/ #pragma mark - Public /**********************************************************************/ - (void)refreshData { [self.mj_header beginRefreshing]; } @end