//
// LNCustomCollectionViewController.m
// LNMobileProject
//
// Created by 六牛科技 on 2018/7/24.
//
// 山东六牛网络科技有限公司 https://liuniukeji.com
//
#import "LNCustomCollectionViewController.h"
@interface LNCustomCollectionViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) EmptyDataSource *emptyDataSource;
@property (assign, nonatomic) BOOL isLoading;
@end
@implementation LNCustomCollectionViewController
- (NSMutableArray *)dataArray {
if (!_dataArray) {
_dataArray = [NSMutableArray new];
}
return _dataArray;
}
- (NSInteger)page {
if (!_page) {
_page = 1;
}
return _page;
}
- (EmptyDataSource *)emptyDataSource{
if (!_emptyDataSource) {
_emptyDataSource = [[EmptyDataSource alloc] init];
}
return _emptyDataSource;
}
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
if (!self.collectionView) {
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layout];
collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.view = self.collectionView = collectionView;
} else {
self.collectionView.collectionViewLayout = layout;
}
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
self.collectionView.emptyDataSetSource = self.emptyDataSource;
self.collectionView.emptyDataType = EmptyDataTypeList;
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[self reloadData];
}];
header.lastUpdatedTimeLabel.hidden = YES;
self.collectionView.mj_header = header;
self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
if (self.dataArray.count) {
self.page++;
} else {
self.page = 1;
}
[self loadData];
}];
}
- (void)reloadData {
self.page = 1;
[self loadData];
}
- (void)loadData {
[self.dataArray addObject:[NSDate date].description];
[self.collectionView reloadData];
[self endRefresh];
}
- (void)endRefresh {
[self.collectionView.mj_header endRefreshing];
[self.collectionView.mj_footer endRefreshing];
}
- (void)loadDataWithUrl:(NSString *)url params:(NSDictionary *)params success:(void(^)(id json))success {
if (self.isLoading) {
return;
}
self.isLoading = YES;
if (![self.collectionView.mj_header isRefreshing] && ![self.collectionView.mj_footer isRefreshing]) {
[SVProgressHUD show];
}
[[[LNNetWorkAPI alloc] initWithUrl:url parameters:params] startWithBlockSuccess:^(__kindof LCBaseRequest *request) {
self.isLoading = NO;
[SVProgressHUD dismiss];
[self endRefresh];
if (self.page == 1) {
[self.dataArray removeAllObjects];
}
id json = request.responseJSONObject;
if ([json[@"status"] integerValue] != 1) {
[SVProgressHUD showInfoWithStatus:json[@"msg"]];
return;
}
if (self.page > 1 || ([json[@"data"] respondsToSelector:@selector(count)] && [json[@"data"] count])) {
[self.collectionView removeEmptyDataSet];
} else {
[self.collectionView reloadEmptyDataSet];
}
if (success) {
success(json);
}
} failure:^(__kindof LCBaseRequest *request, NSError *error) {
self.isLoading = NO;
[SVProgressHUD dismiss];
[self endRefresh];
[SVProgressHUD showInfoWithStatus:error.domain];
}];
}
#pragma mark - Collection view
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataArray.count;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"UICollectionReusableView" forIndexPath:indexPath];
return view;
}
return nil;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
const NSInteger kItemNumOfRow = 3;
UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)collectionViewLayout;
NSInteger row = indexPath.row;
CGFloat totalWidth = self.collectionView.frame.size.width - layout.sectionInset.left - layout.sectionInset.right - layout.minimumInteritemSpacing * (kItemNumOfRow - 1);
CGFloat width;
if (row % kItemNumOfRow) {
width = roundf(totalWidth / kItemNumOfRow);
} else {
width = totalWidth - roundf(totalWidth / kItemNumOfRow) * (kItemNumOfRow - 1);
}
return CGSizeMake(width, width);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
}
@end