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

521 lines
17 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 "IfishMassageViewCell.h"
#import "IfishMassageModel.h"
#import "PushmassageModel.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"
@interface IfishMessageViewController ()<UITableViewDelegate,UITableViewDataSource>
{
BOOL _isRefreshing;
BOOL _isLoadMore;
}
@property(nonatomic,copy) NSString *pushId;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *pushMassageArr;
@property(nonatomic)BOOL isRefreshing;
@property(nonatomic)BOOL isLoadMore;
@property(nonatomic,strong) IfishNomassageView *nomassagView;
@property(nonatomic,copy) NSString* total;
@property(nonatomic) int page;
@property(nonatomic,strong)NSDictionary*para;
@end
extern BOOL launchedByNotification;
@implementation IfishMessageViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.pushMassageArr = [[NSMutableArray alloc] init];
// NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
// self.pushId = [userdefault valueForKey:@"PUSHID"];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = item;
[self initTab];
[self getPushData];
}
-(void)initTab{
if (self.tableView==nil) {
UITableView*table=[[UITableView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
table.dataSource=self;
table.delegate=self;
table.separatorStyle=UITableViewCellSeparatorStyleNone;
self.tableView=table;
self.tableView.showsVerticalScrollIndicator=NO;
self.tableView.backgroundColor = TABLE_BACKGROUD_COLOR;
[self.view addSubview:self.tableView];
[self.tableView registerNib:[UINib nibWithNibName:@"IfishMassageViewCell" bundle:nil] forCellReuseIdentifier:@"IfishMassageViewCell"];
// if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
//
// [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,-100, 0,0)];
// [self.tableView setSeparatorColor:TABLE_SEPRETLINE_COLOR];
// }
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.pushMassageArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([self.pushMassageArr 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;
}
IfishMassageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IfishMassageViewCell"];
if (cell==nil) {
cell=[[IfishMassageViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"IfishMassageViewCell"];
}
PushmassageModel *model = [self.pushMassageArr objectAtIndex:indexPath.row];
cell.pushContent.text = model.massageBody;
cell.pushTitle.text = model.massageTitle;
cell.pushTimeLabel.text = model.createTime;
// cell.IconImage.image = [UIImage imageNamed:model.IconImage];
// 1.wendu_warn 2.remove_device3.qu_reply 4.offline_push
// * 5.remind_water 6 all_push
if ([model.massageType isEqualToString:@"wendu_warn"]) {
cell.userInteractionEnabled = NO;
cell.pushTypeImg.image = [UIImage imageNamed:@"ic_jpush_wendu"];
}else if ([model.massageType isEqualToString:@"remove_device"]){
cell.userInteractionEnabled = NO;
cell.pushTypeImg.image = [UIImage imageNamed:@"ic_jpush_msg"];
}else if ([model.massageType isEqualToString:@"qu_reply"]){
cell.userInteractionEnabled = NO;
cell.pushTypeImg.image = [UIImage imageNamed:@"ic_imchat_msg"];
}else if ([model.massageType isEqualToString:@"remind_water"]){
cell.userInteractionEnabled = NO;
cell.pushTypeImg.image = [UIImage imageNamed:@"ic_jpush_changewater"];
}else if ([model.massageType isEqualToString:@"offline_push"]){
cell.userInteractionEnabled = NO;
cell.pushTypeImg.image = [UIImage imageNamed:@"ic_jpush_offline"];
}else if ([model.massageType isEqualToString:@"all_push"]){
cell.userInteractionEnabled = YES;
cell.pushTypeImg.image = [UIImage imageNamed:@"ic_jpush_all_push"];
}else if ([model.massageType isEqualToString:@"review_push"]){
cell.userInteractionEnabled = NO;
cell.pushTypeImg.image = [UIImage imageNamed:@"ic_imchat_msg"];
}else if ([model.massageType isEqualToString:@"send_report"]){
cell.userInteractionEnabled = YES;
cell.pushTypeImg.image = [UIImage imageNamed:@"ic_jpush_all_push"];
}
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if ([self.pushMassageArr count] ==0) {
return;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
PushmassageModel *model = self.pushMassageArr[indexPath.row];
if ([model.massageType isEqualToString:@"all_push"]) {
PushMasssageWebViewController *webVC = [[PushMasssageWebViewController alloc] init];
webVC.pushlink = model.url;
webVC.pushtitle = model.massageTitle;
self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:webVC animated:YES];
self.hidesBottomBarWhenPushed = NO;
}else if ([model.massageType isEqualToString:@"send_report"]){
if (![model.reportId isKindOfClass:[NSNull class]] && ![model.url isKindOfClass:[NSNull class]]) {
[self kanHuBaoGaoXiangQingwithReportId:model.reportId addUrl:model.url];
}else{
[self.view makeToast:@"reportId 或 url 为空"];
}
}
}
//查询看护报告详情
-(void)kanHuBaoGaoXiangQingwithReportId:(NSString*)reportId addUrl:(NSString *)shareUrl{
[AFNOHeaderHttpTool getLookReportById:reportId success:^(id response) {
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSLog(@"result:%@",resultDic[@"result"]);
if ([resultDic[@"result"] isEqualToString:@"100"]) {
NSDictionary *dataDic = resultDic[@"data"];
ShareReportViewController *shareVC=[[ShareReportViewController alloc] init];
KanHuBaoGaoModel *bagM =[[KanHuBaoGaoModel alloc] init];
bagM.shareImg = dataDic[@"shareImg"];
bagM.timestamp = dataDic[@"timestamp"];
bagM.totalIndex = dataDic[@"totalIndex"];
bagM.baifenbi = dataDic[@"baifenbi"];
bagM.reportId = dataDic[@"reportId"];
shareVC.baGaoMod = bagM;
PingfenModel *PFMode1=[[PingfenModel alloc] init];
PFMode1.pingFenDesc = dataDic[@"index1Text"];
PFMode1.pingFenNumber = dataDic[@"index1"];
PingfenModel *PFMode2=[[PingfenModel alloc] init];
PFMode2.pingFenDesc = dataDic[@"index2Text"];
PFMode2.pingFenNumber = dataDic[@"index2"];
PingfenModel *PFMode3=[[PingfenModel alloc] init];
PFMode3.pingFenDesc = dataDic[@"index3Text"];
PFMode3.pingFenNumber = dataDic[@"index3"];
NSArray *pingfenArr=@[PFMode1,PFMode2,PFMode3];
shareVC.pinFenArr = pingfenArr;
NSString *suggesttrue=dataDic[@"suggestion"];
shareVC.suggesttrue = suggesttrue;
shareVC.shareUrl = shareUrl;
self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:shareVC animated:YES];
self.hidesBottomBarWhenPushed = NO;
}else{
[self.view makeToast:@"看护报告详情查询失败"];
}
} failure:^(NSError *err) {
[self.view makeToast:@"请检查网络"];
}];
}
-(void)viewWillAppear:(BOOL)animated{
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentMassageView) name:@"PresentView" object:nil];
if ([self.pushMassageArr 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;
}
}
-(void)viewWillDisappear:(BOOL)animated{
//[[NSNotificationCenter defaultCenter] removeObserver:self name:@"PresentView" object:nil];
}
//收到通知刷新列表
-(void)presentMassageView{
//[self getPushData];
}
-(void)getPushData{
[self.pushMassageArr removeAllObjects];
[self creatReatRefreshView];
// 确保消息推送过来 更新 消息列表
AFHTTPRequestOperationManager*mannager=[AFHTTPRequestOperationManager manager];
mannager.responseSerializer=[AFHTTPResponseSerializer serializer];
UserModel*userModel = [[DataCenter defaultDtacenter] valueForKey:@"UserLogIn"];
NSDictionary *parameters= [[NSDictionary alloc] init];
parameters = @{ @"firstResult": @"0",
@"pageSize" : @"10",
@"userId":userModel.userId
};
[mannager POST:kGetPushListInf parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"*-*-*--*总共的条数%@",resultDic[@"total"]);
_total = resultDic[@"total"];
NSArray *arr = resultDic[@"data"];
for (NSDictionary *dict in arr) {
PushmassageModel *pushModel = [[PushmassageModel alloc] init];
pushModel.massageType = dict[@"pushType"];
pushModel.massageTitle = dict[@"pushTitle"];
pushModel.massageBody = dict[@"pushContext"];
pushModel.pushId = dict[@"pushId"];
pushModel.createTime = dict[@"createTime"];
pushModel.showName = dict[@"showName"];
pushModel.deviceId = dict[@"deviceId"];
pushModel.userId = dict [@"userId"];
pushModel.url = dict [@"pushLink"];
pushModel.reportId = dict[@"reportId"];
[self.pushMassageArr addObject:pushModel];
}
NSLog(@" _SecondDataCellArray %lu", (unsigned long)self.pushMassageArr.count);
[self.tableView reloadData];
[self endRefreshing];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self endRefreshing];
}];
}
-(void)creatReatRefreshView{
__weak typeof (self)weskSelf=self;
// 下拉刷新
UserModel*userModel = [[DataCenter defaultDtacenter] valueForKey:@"UserLogIn"];
[weskSelf.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshAmazingAniView class] beginRefresh:^{
if (weskSelf.isRefreshing) {
return ;
}
weskSelf.isRefreshing=YES;
[self getPushData];
_page=0;
_para = @{ @"firstResult": @"0",
@"pageSize" : @"10",
@"userId":userModel.userId
};
[weskSelf addTaskWithDic:_para isRefresh:YES];
[self.tableView reloadData];
}];
//上拉加载更多
[weskSelf.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
if (weskSelf.isLoadMore) {
return ;
}
weskSelf.isLoadMore=YES;
NSInteger pagecount =[_total integerValue]/10+1;
if (_page<pagecount-1) {
_page++;
NSInteger first=(NSInteger)_page*10;
NSString *firsStr=[NSString stringWithFormat:@"%ld",(long)first];
_para = @{ @"firstResult": firsStr,
@"pageSize" : @"10",
@"userId":userModel.userId
};
[weskSelf addTaskWithDic:_para isRefresh:NO];
}else{
[self endRefreshing];
[self.view makeToast:@"已无更多数据"];
}
}];
}
-(void)endRefreshing{
if (self.isRefreshing) {
self.isRefreshing=NO;
[self.tableView headerEndRefreshingWithResult:JHRefreshResultNone];
}
if (self.isLoadMore) {
self.isLoadMore=NO;
[self.tableView footerEndRefreshing];
}
}
-(void)addTaskWithDic:(NSDictionary *)para isRefresh:(BOOL)isRefresh{
if (isRefresh) {
[self.pushMassageArr removeAllObjects];
}
AFHTTPRequestOperationManager*mannager=[AFHTTPRequestOperationManager manager];
mannager.responseSerializer=[AFHTTPResponseSerializer serializer];
[mannager POST:kGetPushListInf parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (responseObject) {
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSLog(@"*-*-*--*总共的条数%@",resultDic[@"total"]);
NSArray *arr = resultDic[@"data"];
for (NSDictionary *dict in arr) {
PushmassageModel *pushModel = [[PushmassageModel alloc] init];
pushModel.massageType = dict[@"pushType"];
pushModel.massageTitle = dict[@"pushTitle"];
pushModel.massageBody = dict[@"pushContext"];
pushModel.pushId = dict[@"pushId"];
pushModel.createTime = dict[@"createTime"];
pushModel.showName = dict[@"showName"];
pushModel.deviceId = dict[@"deviceId"];
pushModel.userId = dict [@"userId"];
pushModel.url = dict [@"pushLink"];
pushModel.reportId = dict[@"reportId"];
[self.pushMassageArr addObject:pushModel];
}
NSLog(@" _SecondDataCellArray %lu", (unsigned long)self.pushMassageArr.count);
[self.tableView reloadData];
[self endRefreshing];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self endRefreshing];
}];
}
@end