GHRefreshCollectionView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // GHRefreshCollectionView.m
  3. // GameHelper
  4. //
  5. // Created by 青秀斌 on 16/12/29.
  6. // Copyright © 2016年 kylincc. All rights reserved.
  7. //
  8. #import "GHRefreshCollectionView.h"
  9. #import "GHRefreshHeader.h"
  10. #import "GHRefreshFooter.h"
  11. #import "UIView+GHBlank.h"
  12. //#import <SVProgressHUD/SVProgressHUD.h>
  13. #import "AFNetworkReachabilityManager.h"
  14. #define NET_CONNECT_ERROR @"当前网络不给力,请稍后重试"
  15. @interface GHRefreshCollectionView ()
  16. @property (strong, nonatomic) NSMutableArray *dataArray;
  17. @property (assign, nonatomic) NSUInteger pageIndex;
  18. @property (assign, nonatomic) NSUInteger offset;
  19. @end
  20. @implementation GHRefreshCollectionView
  21. - (instancetype)init {
  22. self = [super init];
  23. if (self) {
  24. [self awakeFromNib];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithFrame:(CGRect)frame {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. [self awakeFromNib];
  32. }
  33. return self;
  34. }
  35. - (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {
  36. self = [super initWithFrame:frame collectionViewLayout:layout];
  37. if (self) {
  38. [self awakeFromNib];
  39. }
  40. return self;
  41. }
  42. - (void)awakeFromNib {
  43. [super awakeFromNib];
  44. self.pageIndex = 1;
  45. self.offset = 0;
  46. self.allowShowMore = YES;
  47. self.allowShowBlank = YES;
  48. self.allowShowNoNetworkBlank = YES;
  49. self.blankImage = nil;
  50. self.blankTitle = nil;
  51. self.blankMessage = nil;
  52. }
  53. - (void)setRefreshDelegate:(id<GHRefreshCollectionViewDelegate>)refreshDelegate {
  54. _refreshDelegate = refreshDelegate;
  55. if ([self.refreshDelegate respondsToSelector:@selector(collectionView:pageIndex:offset:success:failure:)]) {
  56. [self addRefreshHeader];
  57. }
  58. }
  59. - (void)setAllowShowMore:(BOOL)allowShowMore {
  60. _allowShowMore = allowShowMore;
  61. if(_allowShowMore){
  62. self.mj_footer.hidden = NO;
  63. }else{
  64. self.mj_footer.hidden = YES;
  65. }
  66. // if (self.mj_footer) {
  67. // [self.mj_footer removeFromSuperview];
  68. // }
  69. }
  70. -(void)setAllowShowBlank:(BOOL)allowShowBlank{
  71. _allowShowBlank = allowShowBlank;
  72. if(!_allowShowBlank){
  73. [self dismissBlank];
  74. }
  75. }
  76. //针对个人中心主页 - 先出现一个暂无数据的空白页,之后才出现正确的空白页
  77. -(void)closeBlankView{
  78. [self dismissBlank];
  79. }
  80. /**********************************************************************/
  81. #pragma mark - Private
  82. /**********************************************************************/
  83. - (void)addRefreshHeader {
  84. if (self.mj_header) {
  85. return;
  86. }
  87. GHRefreshHeader *header = [GHRefreshHeader headerWithRefreshingBlock:^{
  88. NSUInteger pageIndex = 1;
  89. NSInteger offset = 0;
  90. WS(weakSelf);
  91. [self.refreshDelegate collectionView:self pageIndex:pageIndex offset:offset success:^(NSArray * _Nullable list, BOOL hasNext) {
  92. [weakSelf.mj_header endRefreshing];
  93. //添加RefreshFooter
  94. [weakSelf addRefreshFooter];
  95. if (hasNext) {
  96. [weakSelf.mj_footer endRefreshing];
  97. } else {
  98. [weakSelf.mj_footer endRefreshingWithNoMoreData];
  99. }
  100. //加载数据
  101. weakSelf.pageIndex = pageIndex;
  102. weakSelf.offset = offset;
  103. if (weakSelf.reloadView) {
  104. weakSelf.reloadView(list);
  105. } else {
  106. weakSelf.dataArray = [list mutableCopy];
  107. [weakSelf reloadData];
  108. }
  109. if(self.noDataShowBlankByAddFakeDataFlag){
  110. weakSelf.mj_footer.hidden = YES;
  111. [weakSelf dismissBlank];
  112. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data"
  113. title:weakSelf.blankTitle?:@"暂无数据"
  114. message:weakSelf.blankMessage
  115. action:nil offsetY:weakSelf.offsetY];
  116. }else{
  117. //显示占位图
  118. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  119. weakSelf.mj_footer.hidden = YES;
  120. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data"
  121. title:weakSelf.blankTitle?:@"暂无数据"
  122. message:weakSelf.blankMessage
  123. action:nil offsetY:weakSelf.offsetY];
  124. } else {
  125. weakSelf.mj_footer.hidden = NO;
  126. [weakSelf dismissBlank];
  127. }
  128. }
  129. } failure:^(NSError *error) {
  130. [weakSelf.mj_header endRefreshing];
  131. //显示占位图
  132. NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle;
  133. if(self.noDataShowBlankByAddFakeDataFlag){
  134. weakSelf.mj_footer.hidden = YES;
  135. [weakSelf dismissBlank];
  136. BOOL isHaveNet = NO;
  137. if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
  138. isHaveNet = YES;
  139. }
  140. if (weakSelf.allowShowNoNetworkBlank && isHaveNet) {
  141. [weakSelf showBlankWithImage:@"no_network"
  142. title:NET_CONNECT_ERROR
  143. message:nil
  144. action:nil offsetY:weakSelf.offsetY];
  145. } else {
  146. [weakSelf showBlankWithImage:@"failed_to_load"
  147. title:blankTitle?:@"加载失败"
  148. message:weakSelf.blankMessage
  149. action:nil offsetY:weakSelf.offsetY];
  150. }
  151. }else{
  152. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  153. weakSelf.mj_footer.hidden = YES;
  154. BOOL isHaveNet = NO;
  155. if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
  156. isHaveNet = YES;
  157. }
  158. if (weakSelf.allowShowNoNetworkBlank && isHaveNet) {
  159. [weakSelf showBlankWithImage:@"no_network"
  160. title:NET_CONNECT_ERROR
  161. message:nil
  162. action:nil offsetY:weakSelf.offsetY];
  163. } else {
  164. [weakSelf showBlankWithImage:@"failed_to_load"
  165. title:blankTitle?:@"加载失败"
  166. message:weakSelf.blankMessage
  167. action:nil offsetY:weakSelf.offsetY];
  168. }
  169. } else {
  170. weakSelf.mj_footer.hidden = NO;
  171. [weakSelf dismissBlank];
  172. // [SVProgressHUD showErrorWithStatus:blankTitle];
  173. }
  174. }
  175. }];
  176. }];
  177. self.mj_header = header;
  178. }
  179. - (void)addRefreshFooter {
  180. if (!self.allowShowMore) {
  181. return;
  182. }
  183. if (self.mj_footer) {
  184. return;
  185. }
  186. GHRefreshFooter *footer = [GHRefreshFooter footerWithRefreshingBlock:^{
  187. NSUInteger pageIndex = self.pageIndex + 1;
  188. NSInteger offset = self.dataArray.count + 1;
  189. WS(weakSelf);
  190. [self.refreshDelegate collectionView:self pageIndex:pageIndex offset:offset success:^(NSArray * _Nullable list, BOOL hasNext) {
  191. if (hasNext) {
  192. [weakSelf.mj_footer endRefreshing];
  193. } else {
  194. [weakSelf.mj_footer endRefreshingWithNoMoreData];
  195. }
  196. //加载数据
  197. weakSelf.pageIndex = pageIndex;
  198. weakSelf.offset = offset;
  199. if (weakSelf.reloadView) {
  200. weakSelf.reloadView(list);
  201. } else {
  202. [weakSelf.dataArray addObjectsFromArray:list];
  203. [weakSelf reloadData];
  204. }
  205. if(self.noDataShowBlankByAddFakeDataFlag){
  206. weakSelf.mj_footer.hidden = YES;
  207. [weakSelf dismissBlank];
  208. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data"
  209. title:weakSelf.blankTitle?:@"暂无数据"
  210. message:weakSelf.blankMessage
  211. action:nil offsetY:weakSelf.offsetY];
  212. }else{
  213. //显示占位图
  214. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  215. weakSelf.mj_footer.hidden = YES;
  216. [weakSelf showBlankWithImage:weakSelf.blankImage?:@"no_data"
  217. title:weakSelf.blankTitle?:@"暂无数据"
  218. message:weakSelf.blankMessage
  219. action:nil offsetY:weakSelf.offsetY];
  220. } else {
  221. weakSelf.mj_footer.hidden = NO;
  222. [weakSelf dismissBlank];
  223. }
  224. }
  225. } failure:^(NSError *error) {
  226. [weakSelf.mj_footer endRefreshing];
  227. //显示占位图
  228. NSString *blankTitle = error.localizedDescription?:weakSelf.blankTitle;
  229. if(self.noDataShowBlankByAddFakeDataFlag){
  230. weakSelf.mj_footer.hidden = YES;
  231. [weakSelf dismissBlank];
  232. BOOL isHaveNet = NO;
  233. if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
  234. isHaveNet = YES;
  235. }
  236. if (weakSelf.allowShowNoNetworkBlank && isHaveNet) {
  237. [weakSelf showBlankWithImage:@"no_network"
  238. title:NET_CONNECT_ERROR
  239. message:nil
  240. action:nil offsetY:weakSelf.offsetY];
  241. } else {
  242. [weakSelf showBlankWithImage:@"failed_to_load"
  243. title:blankTitle?:@"加载失败"
  244. message:weakSelf.blankMessage
  245. action:nil offsetY:weakSelf.offsetY];
  246. }
  247. }else{
  248. if (weakSelf.allowShowBlank && weakSelf.dataArray.count==0) {
  249. weakSelf.mj_footer.hidden = YES;
  250. BOOL isHaveNet = NO;
  251. if (AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusUnknown || AFNetworkReachabilityManager.sharedManager.networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
  252. isHaveNet = YES;
  253. }
  254. if (weakSelf.allowShowNoNetworkBlank && isHaveNet) {
  255. [weakSelf showBlankWithImage:@"no_network"
  256. title:NET_CONNECT_ERROR
  257. message:nil
  258. action:nil offsetY:weakSelf.offsetY];
  259. } else {
  260. [weakSelf showBlankWithImage:@"failed_to_load"
  261. title:blankTitle?:@"加载失败"
  262. message:weakSelf.blankMessage
  263. action:nil offsetY:weakSelf.offsetY];
  264. }
  265. } else {
  266. weakSelf.mj_footer.hidden = NO;
  267. [weakSelf dismissBlank];
  268. // [SVProgressHUD showErrorWithStatus:blankTitle];
  269. }
  270. }
  271. }];
  272. }];
  273. self.mj_footer = footer;
  274. }
  275. /**********************************************************************/
  276. #pragma mark - Public
  277. /**********************************************************************/
  278. - (void)refreshData {
  279. [self.mj_header beginRefreshing];
  280. }
  281. @end