#import "XMGTopicViewController.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import "XMGTopic.h"
#import <SVProgressHUD.h>
#import "XMGTopicCell.h"
#import <SDImageCache.h>
#import <MJRefresh.h>
@interface XMGTopicViewController ()
/** 当前最后一条帖子数据的描述信息,专门用来加载下一页数据 */
@property (nonatomic, copy) NSString *maxtime;
/** 所有的帖子数据 */
@property (nonatomic, strong) NSMutableArray<XMGTopic *> *topics;
/** 请求管理者 */
@property (nonatomic, strong) AFHTTPSessionManager *manager;
@end
@implementation XMGTopicViewController
/** 在这里实现type方法,仅仅是为了消除警告 */
- (XMGTopicType)type {return 0;}
/* cell的重用标识 */
static NSString * const XMGTopicCellId = @"XMGTopicCellId";
- (AFHTTPSessionManager *)manager
{
if (!_manager) {
_manager = [AFHTTPSessionManager manager];
}
return _manager;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = XMGGrayColor(206);
self.tableView.contentInset = UIEdgeInsetsMake(XMGNavMaxY + XMGTitlesViewH, 0, XMGTabBarH, 0);
self.tableView.scrollIndicatorInsets = self.tableView.contentInset;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 注册cell
UINib *nib = [UINib nibWithNibName:NSStringFromClass([XMGTopicCell class]) bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:XMGTopicCellId];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabBarButtonDidRepeatClick) name:XMGTabBarButtonDidRepeatClickNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(titleButtonDidRepeatClick) name:XMGTitleButtonDidRepeatClickNotification object:nil];
[self setupRefresh];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)setupRefresh
{
// header
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopics)]; //设置下拉刷新时调用的方法
// 自动切换透明度
self.tableView.mj_header.automaticallyChangeAlpha = YES;
[self.tableView.mj_header beginRefreshing]; //开始刷新
// footer
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)]; //上拉刷新
}
#pragma mark - 数据处理
/**
* 发送请求给服务器,下拉刷新数据
*/
- (void)loadNewTopics
{
// 1.取消之前的请求
[self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
// 2.拼接参数
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"a"] = @"list";
parameters[@"c"] = @"data";
parameters[@"type"] = @(self.type);
// 3.发送请求
[self.manager GET:XMGCommonURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 存储maxtime
self.maxtime = responseObject[@"info"][@"maxtime"];
// 字典数组 -> 模型数据
self.topics = [XMGTopic mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
// 刷新表格
[self.tableView reloadData];
// 结束刷新
[self.tableView.mj_header endRefreshing]; //结束刷新
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error.code != NSURLErrorCancelled) { // 并非是取消任务导致的error,其他网络问题导致的error
[SVProgressHUD showErrorWithStatus:@"网络繁忙,请稍后再试!"]; //刷新失败时的提示
}
// 结束刷新
[self.tableView.mj_header endRefreshing];
}];
}
/**
* 发送请求给服务器,上拉加载更多数据
*/
- (void)loadMoreTopics
{
// 1.取消之前的请求
[self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
// 2.拼接参数
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
parameters[@"a"] = @"list";
parameters[@"c"] = @"data";
parameters[@"type"] = @(self.type);
parameters[@"maxtime"] = self.maxtime;
// 3.发送请求
[self.manager GET:XMGCommonURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
// 存储maxtime
self.maxtime = responseObject[@"info"][@"maxtime"];
// 字典数组 -> 模型数据
NSArray *moreTopics = [XMGTopic mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
// 累加到旧数组的后面
[self.topics addObjectsFromArray:moreTopics];
// 刷新表格
[self.tableView reloadData];
// 结束刷新
[self.tableView.mj_footer endRefreshing];
// if (self.topics.count >= 60) {
// [self.tableView.mj_footer endRefreshingWithNoMoreData];
// } else {
// [self.tableView.mj_footer endRefreshing];
// }
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error.code != NSURLErrorCancelled) { // 并非是取消任务导致的error,其他网络问题导致的error
[SVProgressHUD showErrorWithStatus:@"网络繁忙,请稍后再试!"];
}
// 结束刷新
[self.tableView.mj_footer endRefreshing];
}];
}