ifish/Ifish/controllers/IfishTabControllers/消息/IfishMessageViewController.m

540 lines
20 KiB
Objective-C

//
// IfishMessageViewController.m
// ifishTabTest
//
// Created by imac on 16/7/25.
// Copyright © 2016年 xiang. All rights reserved.
//
#import "IfishMessageViewController.h"
#import "AFNetworking.h"
#import "IfishMessageViewCell.h"
//#import "IfishDatabaseManager.h"
//#import "JHRefresh.h"
#import "IfishNomassageView.h"
//#import "PushMasssageWebViewController.h"
//#import "ShareReportViewController.h"
//#import "KanHuBaoGaoModel.h"
//#import "PingfenModel.h"
#import "AFNOHeaderHttpTool.h"
#import "MessageAlertView.h"
#import "SVProgressHUD.h"
#import "IfishUserAsset.h"
#import "YYCache.h"
@interface IfishMessageViewController ()<UITableViewDelegate,UITableViewDataSource>
{
BOOL _isRefreshing;
BOOL _isLoadMore;
}
@property(nonatomic,copy) NSString *pushId;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *messageArray;//新消息
@property(nonatomic,strong) NSMutableArray *oldMessageArr;//旧消息
@property(nonatomic,strong) NSMutableArray *dataSource;//数据源
@property(nonatomic)BOOL isRefreshing;
@property(nonatomic)BOOL isLoadMore;
@property(nonatomic,strong) IfishNomassageView *nomassagView;
@property(nonatomic,copy) NSString* total;
@property(nonatomic) int page;
Copy NSString *lastPushId;//最近一次推送的Id
@property(nonatomic,strong)NSDictionary*para;
Strong NSMutableArray *deleteArray;//将要删除字符串的id集合
Assign BOOL isDelteAll;//是否全选删除
Assign BOOL isShowDelete;//是否显示删除按钮
Strong MessageAlertView *comboxView;
Strong UIView *deleteView;
Strong UIButton *deleteBtn;
Strong UIAlertController *alertController;
Strong UIAlertAction *okAction;
Strong UIAlertAction *cancelAction;
@end
extern BOOL launchedByNotification;
@implementation IfishMessageViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.isShowDelete = NO;
self.isDelteAll = NO;
self.titleString = @"消息";
[self.rightButton setImage:IMAGEBYENAME(@"message_more") forState:UIControlStateNormal];
[self.rightButton addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.tableView];
[self.view addSubview:self.comboxView];
[self.comboxView loadInfoWithTitle:@[@"删除"] Icons:@[@"ifishdropview_delect_device"]];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.view);
}];
[self.view addSubview:self.deleteView];
[self getRequest];
}
-(NSMutableArray *)dataSource
{
if (!_dataSource) {
_dataSource = [[NSMutableArray alloc]init];
}
return _dataSource;
}
-(NSMutableArray *)messageArray
{
if (!_messageArray) {
_messageArray = [[NSMutableArray alloc]init];
}
return _messageArray;
}
-(NSMutableArray *)oldMessageArr
{
if (!_oldMessageArr) {
_oldMessageArr = [[NSMutableArray alloc]init];
}
return _oldMessageArr;
}
-(NSMutableArray *)deleteArray
{
if (!_deleteArray) {
_deleteArray = [[NSMutableArray alloc]init];
}
return _deleteArray;
}
-(UITableView*)tableView{
if (!_tableView) {
_tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_tableView.dataSource=self;
_tableView.delegate=self;
_tableView.showsVerticalScrollIndicator=NO;
_tableView.backgroundColor = TABLE_BACKGROUD_COLOR;
}
return _tableView;
}
-(MessageAlertView *)comboxView{
if (!_comboxView) {
_comboxView = [[MessageAlertView alloc]initWithFrame:RECT(screen_width - kSizeFrom750(220), 0, kSizeFrom750(220), kSizeFrom750(90))];
WEAK_SELF;
//点击删除
_comboxView.comboxBlock = ^(NSInteger tag) {
[weakSelf showComboxView:NO];
weakSelf.isShowDelete = YES;
[weakSelf.deleteView setHidden:NO];
[weakSelf.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-kSizeFrom750(100));
}];
//刷新页面,显示删除按钮
[weakSelf.tableView reloadData];
};
}
return _comboxView;
}
-(UIView *)deleteView{
if (!_deleteView) {
_deleteView = [[UIView alloc]initWithFrame:RECT(0, screen_height - NavigationBarHeight - kSizeFrom750(100), screen_width, kSizeFrom750(100))];
_deleteView.backgroundColor = [UIColor whiteColor];
_deleteView.hidden = YES;
[self loadDelteSubViews];
}
return _deleteView;
}
-(void)loadDelteSubViews{
self.deleteBtn = InitObject(UIButton);
[self.deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
[self.deleteBtn.titleLabel setFont:SYSTEMSIZE(34)];
[self.deleteBtn setTitleColor:RGB(178, 178, 178) forState:UIControlStateNormal];
[self.deleteBtn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[self.deleteBtn addTarget:self action:@selector(deleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.deleteView addSubview:self.deleteBtn];
UIButton *allDeleteBtn = InitObject(UIButton);
[allDeleteBtn setTitle:@"全选" forState:UIControlStateNormal];
[allDeleteBtn setTitle:@"取消" forState:UIControlStateSelected];
[allDeleteBtn.titleLabel setFont:SYSTEMSIZE(34)];
[allDeleteBtn setTitleColor:RGB(4, 99, 201) forState:UIControlStateNormal];
[allDeleteBtn addTarget:self action:@selector(allDeleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.deleteView addSubview:allDeleteBtn];
[self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.deleteView);
make.left.mas_equalTo(0);
make.width.mas_equalTo(screen_width/2);
make.top.height.mas_equalTo(self.deleteView);
}];
[allDeleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.width.height.mas_equalTo(self.deleteBtn);
make.left.mas_equalTo(self.deleteBtn.mas_right);
}];
UIView *line = [[UIView alloc]init];
line.backgroundColor = RGB(178, 178, 178);
[_deleteView addSubview:line];
[line mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.deleteView);
make.left.mas_equalTo(kSizeFrom750(30));
make.right.mas_equalTo(-kSizeFrom750(30));
make.height.mas_equalTo(ONE_PIXEL_SIZE);
}];
}
#pragma mark --buttonClick
-(void)deleBtnClick:(UIButton *)sender{
if (self.deleteArray.count==0) {
[SVProgressHUD showInfoWithStatus:@"请先选择要删除的消息"];
}else{
//调用删除接口,删除选中的消息内容
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确认删除所有选中消息么" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"cancel Action");
}];
UIAlertAction *delteActicon = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"delete Action");
[self deleteMessage];
}];
[alertController addAction:cancelAction];
[alertController addAction:delteActicon];
[self presentViewController:alertController animated:YES completion:nil];
}
}
-(void)allDeleteBtnClick:(UIButton *)sender{
if (!sender.selected) {//全选
//全部为选中状态
self.isDelteAll = YES;
self.deleteBtn.selected = YES;
[self.deleteArray removeAllObjects];
for (NSArray *arr in self.dataSource) {
for (PushmessageModel *model in arr) {
[self.deleteArray addObject:model.pushId];
}
}
}else{//取消全选
[self.deleteArray removeAllObjects];
self.isDelteAll = NO;
self.deleteBtn.selected = NO;
}
sender.selected = !sender.selected;
[self.tableView reloadData];
}
#pragma mark --comboxView显示与隐藏
-(void)rightBtnClick:(UIButton *)sender{
BOOL isHidden = self.comboxView.hidden;
if (isHidden) {
self.comboxView.hidden = NO;
}
[UIView animateWithDuration:0.3 animations:^{
self.comboxView.transform = CGAffineTransformScale(self.comboxView.transform,isHidden?100:0.01,isHidden?100:0.01);
} completion:^(BOOL finished) {
self.comboxView.hidden = !isHidden;
}];
}
-(void)showComboxView:(BOOL)isShow{
BOOL isHidden = self.comboxView.hidden;
if (isHidden) {
self.comboxView.hidden = NO;
}
[UIView animateWithDuration:0.3 animations:^{
self.comboxView.transform = CGAffineTransformScale(self.comboxView.transform,isHidden?100:0.01,isHidden?100:0.01);
} completion:^(BOOL finished) {
self.comboxView.hidden = !isHidden;
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
-(void)getRequest
{
[SVProgressHUD show];
self.pushId = @"";
UserModel*userModel=[dataContorl getUserInfo];
NSDictionary *params = @{@"pushId":self.pushId,
@"userId":userModel.userId,
@"firstResult":@(0),
@"pageSize":@(999)
};
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:Push_Message_Url params:params success:^(id response) {
[SVProgressHUD dismiss];
if (response) {
id resault=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSString *totalCount = [resault objectForKey:@"total"];
NSArray *dataArray = [resault objectForKey:@"data"];
if ([totalCount integerValue]>0) {
NSMutableArray *newMessageArray = InitObject(NSMutableArray);
NSMutableArray *oldMessageArray = InitObject(NSMutableArray);
for (int i=0; i<dataArray.count; i++) {
PushmessageModel *messageModel = [PushmessageModel yy_modelWithJSON:[dataArray objectAtIndex:i]];
[messageModel getCellHeight];
if ([messageModel.isRead integerValue]==1) {//未读消息
[newMessageArray addObject:messageModel];
}else{
[oldMessageArray addObject:messageModel];
}
}
[self.dataSource addObject:newMessageArray];
[self.dataSource addObject:oldMessageArray];
}
[self.tableView reloadData];
[self postReadMessageRequest];
}
} failure:^(NSError *err) {
[SVProgressHUD dismiss];
}];
}
//进入消息列表,则设置所有消息为已读
-(void)postReadMessageRequest{
UserModel*userModel=[dataContorl getUserInfo];
NSDictionary *params = @{@"userId":userModel.userId,
};
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:Push_Message_Readed params:params success:^(id response) {
if (response) {
id resault=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSString *result = [resault objectForKey:RESPONSE_CODE];
if ([result integerValue]==kReqSuccess) {
//已读
}else{
}
}
} failure:^(NSError *err) {
}];
}
//单独删除某一条消息
-(void)deleteSingleMessageWithIndex:(NSIndexPath *)indexPath{
UserModel*userModel=[dataContorl getUserInfo];
PushmessageModel *model = [[self.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
NSMutableDictionary *deleteDic = InitObject(NSMutableDictionary);
[deleteDic setValue:model.pushId forKey:@"pushId"];
NSDictionary *params = @{@"userId":userModel.userId,
@"pushIds":[CommonUtils arrayToJSONString:@[deleteDic]]
};
[SVProgressHUD show];
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:Push_Message_Delete_Url params:params success:^(id response) {
if (response) {
id resault=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSString *result = [resault objectForKey:RESPONSE_CODE];
if ([result integerValue]==kReqSuccess) {
[[self.dataSource objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
// //刷新页面
// [self.tableView reloadData];
// [self.tableView scrollsToTop];
}else{
[SVProgressHUD showInfoWithStatus:@"删除失败"];
}
}
} failure:^(NSError *err) {
[SVProgressHUD showInfoWithStatus:@"删除失败"];
}];
}
//删除接口(多选删除)
-(void)deleteMessage{
UserModel*userModel=[dataContorl getUserInfo];
NSMutableArray *deleteArray = InitObject(NSMutableArray);
for (int i=0; i<self.deleteArray.count; i++) {
NSString *pushId = [self.deleteArray objectAtIndex:i];
NSMutableDictionary *deleteDic = InitObject(NSMutableDictionary);
[deleteDic setValue:pushId forKey:@"pushId"];
[deleteArray addObject:deleteDic];
}
NSDictionary *params = @{@"userId":userModel.userId,
@"pushIds":[CommonUtils arrayToJSONString:deleteArray]
};
[SVProgressHUD show];
[AFNOHeaderHttpTool requestWihtMethod:RequestTypePost url:Push_Message_Delete_Url params:params success:^(id response) {
if (response) {
id successDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSString *result = [successDic objectForKey:RESPONSE_CODE];
if ([result integerValue]==kReqSuccess) {
self.deleteBtn.selected = NO;
for (int i=0;i< self.dataSource.count;i++) {
NSArray *arr = [self.dataSource objectAtIndex:i];
NSMutableArray *newArray = InitObject(NSMutableArray);
for (int j=0; j<arr.count; j++) {
PushmessageModel *model = [arr objectAtIndex:j];
if ([self.deleteArray containsObject:model.pushId]) {
}else{
[newArray addObject:model];
}
}
[self.dataSource setObject:newArray atIndexedSubscript:i];
}
//走删除接口之后,清空删除内容数组
[self.deleteArray removeAllObjects];
self.isDelteAll = NO;
[self.deleteView setHidden:YES];
[self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.view);
}];
self.isShowDelete = NO;
//刷新页面
[self.tableView reloadData];
[self.tableView scrollsToTop];
}else{
[SVProgressHUD showInfoWithStatus:@"删除失败"];
}
}
} failure:^(NSError *err) {
[SVProgressHUD showInfoWithStatus:@"删除失败"];
}];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return ((NSArray *)[self.dataSource objectAtIndex:section]).count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (((NSArray *)[self.dataSource firstObject]).count ==0&&((NSArray *)[self.dataSource lastObject]).count==0) {
if (!self.nomassagView) {
self.nomassagView = [[IfishNomassageView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height/2 -40, kScreenSize.width, 80)];
[self.view addSubview:self.nomassagView];
}
self.nomassagView.hidden = NO;
self.tableView.scrollEnabled = NO;
}else{
if (self.nomassagView) {
[self.nomassagView removeFromSuperview];
self.nomassagView = nil;
}
self.nomassagView.hidden = YES;
self.tableView.scrollEnabled = YES;
}
static NSString *cellId = @"IfishMessageViewCell";
IfishMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if (cell==nil) {
cell=[[IfishMessageViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
}
PushmessageModel *model = [[self.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
[cell loadInfoWithModel:model isShowDelete:self.isShowDelete isDeleteAll:self.isDelteAll];
WEAK_SELF;
cell.deleteBlock = ^(NSString *messageId, BOOL isDelete) {
if (isDelete) {
[weakSelf.deleteArray addObject:messageId];//添加到删除列表
}else{
[weakSelf.deleteArray removeObject:messageId];//从删除列表中清除
}
if (self.deleteArray.count>0) {
self.deleteBtn.selected = YES;
}else
self.deleteBtn.selected = NO;
};
return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section==1) {
UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screen_width, kSizeFrom750(90))];
header.backgroundColor = [UIColor whiteColor];
UILabel *headerLabel = [[UILabel alloc]init];
headerLabel.frame = CGRectMake(0, kSizeFrom750(30), screen_width, kSizeFrom750(30));
headerLabel.textColor = RGB(178, 178, 178);
headerLabel.textAlignment = NSTextAlignmentCenter;
headerLabel.text = @"···············以上为最新消息···············";
headerLabel.font = SYSTEMSIZE(30);
[header addSubview:headerLabel];
return header;
}else
return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section==1) {
return kSizeFrom750(90);
}else
return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.dataSource.count!=0) {
PushmessageModel *model = [[self.dataSource objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
return model.cellHeight;
}
return 0;
}
//
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
dispatch_async(dispatch_get_main_queue(), ^{
//默认消息已读
// [self postReadMessageRequestWithIndexPath:indexPath];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确认删除该消息么" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"cancel Action");
}];
WEAK_SELF;
UIAlertAction *delteActicon = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf deleteSingleMessageWithIndex:indexPath];
}];
[alertController addAction:cancelAction];
[alertController addAction:delteActicon];
[weakSelf presentViewController:alertController animated:YES completion:nil];
});
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"blackbar.png"] forBarMetrics:UIBarMetricsDefault];
}
@end