123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- //
- // 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 <SVProgressHUD/SVProgressHUD.h>
- #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<GHRefreshCollectionViewDelegate>)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
|