You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

288 lines
11 KiB

//
// SystermMessageViewController.m
// tongxin
//
// Created by WeiChaoZheng on 2018/6/27.
// Copyright © 2018年 xTT. All rights reserved.
//
#import "SystermMessageViewController.h"
#import "DMessage.h"
#import "MessageTableViewCell.h"
@interface SystermMessageViewController ()
@property (nonatomic ,strong) NSMutableArray *listDataSoure;
@property (nonatomic ,strong) NSMutableArray *dateArr;
@end
@implementation SystermMessageViewController
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self loadMoreDataWithDMessage:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.zx_hideBaseNavBar = YES;
self.myTableView.frame = CGRectMake(0, 0, SWIDTH, self.myTableView.bounds.size.height-40);
[self.view addSubview:self.myTableView];
// Do any additional setup after loading the view.
self.listDataSoure = [[NSMutableArray alloc] init];
self.dateArr = [[NSMutableArray alloc] init];
WEAKSELF
self.myTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
[weakSelf loadMoreDataWithDMessage:[weakSelf.listDataSoure lastObject]];
}];
[self.myTableView registerNib:[UINib nibWithNibName:@"MessageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MessageTableViewCellID"];
// [self.myTableView.mj_footer beginRefreshing];
self.myTableView.tableFooterView = [[UIView alloc] init];
// iOS 11 系统 tableView 会自动调整高度 在 Gounp 模式下 头部的高度会有问题 ,需要对 预设的高度设置为0
self.myTableView.estimatedRowHeight = 0;
self.myTableView.estimatedSectionHeaderHeight = 0;
self.myTableView.estimatedSectionFooterHeight = 0;
}
//MARK :刪除方法
-(void)deletAction{
// NSMutableArray * messageIds = [NSMutableArray array];
// for (NSArray* secArr in self.myDataSource){
// for (DMessage *dMsg in secArr) {
// [messageIds addObject: dMsg.id];
// }
// }
WEAKSELF
LGAlertView *alertView = [[LGAlertView alloc] initWithTitle:@"注意" message:@"确认要清空通知消息?" style:LGAlertViewStyleAlert buttonTitles:@[@"确定"] cancelButtonTitle:@"取消" destructiveButtonTitle:nil actionHandler:^(LGAlertView *alertView, NSString *title, NSUInteger index) {
[DMessage delectNotificationWithMessageIds:nil success:^(id responseObject) {
[weakSelf.myDataSource removeAllObjects];
[weakSelf.listDataSoure removeAllObjects];
[weakSelf.dateArr removeAllObjects];
[weakSelf loadMoreDataWithDMessage:nil];
xLog(@"删除记录成功!")
} failure:^{
[SVProgressHUD showSuccessWithStatus:@"删除记录失败,请稍后重试..."];
} ];
} cancelHandler:nil destructiveHandler:nil];
[alertView showAnimated:YES completionHandler:nil];
}
- (void)loadMoreDataWithDMessage:(DMessage *)dMessage{
WEAKSELF
NSString *ID;
if(dMessage){
ID = dMessage.id;
}
[DMessage getNotificationWithID:ID
success:^(NSMutableArray *arr)
{
if(!ID){
[weakSelf.listDataSoure removeAllObjects];
[weakSelf.dateArr removeAllObjects];
[weakSelf.myDataSource removeAllObjects];
}
[weakSelf.listDataSoure addObjectsFromArray:arr];
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[weakSelf.listDataSoure enumerateObjectsUsingBlock:^(DMessage *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[obj.timestamp doubleValue]];
NSString *dateStr = [myHelper getDateFormatWithStr:@"yyyy-MM-dd" date:date];
NSMutableArray *arr = dic[dateStr];
if (!arr) {
arr = [NSMutableArray array];
}
[arr addObject:obj];
[dic setObject:arr forKey:dateStr];
if (![weakSelf.dateArr containsObject:dateStr]) {
[weakSelf.dateArr addObject:dateStr];
}
}];
[weakSelf.myDataSource removeAllObjects];
[weakSelf.dateArr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[weakSelf.myDataSource addObject:dic[obj]];
}];
[weakSelf.myTableView reloadData];
[weakSelf showNoDataSourceView];
// if ([[weakSelf.myDataSource firstObject] count] == 0){
// weakSelf.navigationItem.rightBarButtonItem = nil;
// }
self.zx_navRightBtn.hidden = [[weakSelf.myDataSource firstObject] count] == 0 ? YES : NO;
[weakSelf.myTableView.mj_footer endRefreshing];
} failure:^(NSError *error){
if (!error) {
[weakSelf showNoReachableView];
// [self setRightBarBtnTitle:nil RoImage:[UIImage imageNamed:@"icon_delete"]];
}
[weakSelf.myTableView.mj_footer endRefreshing];
}];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return self.dateArr[section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
DMessage *dMessage = self.myDataSource[indexPath.section][indexPath.row];
NSString *watchImei = dMessage.imei;
NSString *content = dMessage.content;
if([dMessage.type isEqualToString:@"systemUpdate"] && dMessage.content){
CGFloat tempWidth = SWIDTH*(280.0/320);
CGFloat cellHeight = [self getLineHeight:content withWidth:tempWidth withFont:DefineFontSize];
if(cellHeight < 66){
return 66;
}else{
return cellHeight;
}
}else{
NSString *allTmp = @"";
if ([dMessage.type isEqualToString:@"identityTransfered"]) {
//设备(225977895624552)的管理员(13242881234)将管理员转让给你
NSString *tmp = [NSString stringWithFormat:@"(%@)",dMessage.phone];
NSString *tmp2 = [NSString stringWithFormat:@"%@ (%@)",dMessage.name,watchImei];
allTmp = [NSString stringWithFormat:@"%@的管理员%@把管理员转让给您",tmp2,tmp];
}else if ([dMessage.type isEqualToString:@"identityTransfer"]) {
//您将设备(225977895624552)的管理员转让给用户(13212345678)
NSString *tmp = [NSString stringWithFormat:@"用户(%@)",dMessage.phone];
NSString *tmp2 = [NSString stringWithFormat:@" %@ (%@)",dMessage.name,watchImei];
allTmp = [NSString stringWithFormat:@"您将%@的管理员转让给%@",tmp2,tmp];
}
CGFloat tempWidth = SWIDTH*(280.0/320);
return [self getLineHeight:allTmp withWidth:tempWidth withFont:DefineFontSize];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
DMessage *dMessage = self.myDataSource[indexPath.section][indexPath.row];
MessageTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MessageTableViewCellID" forIndexPath:indexPath];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[dMessage.timestamp doubleValue]];
cell.timeLabel.text = [myHelper getDateFormatWithStr:@"HH:mm" date:date]; //时间
//标题
NSString * titleStr ;
NSString *watchImei = dMessage.imei;
//信息
if ([dMessage.type isEqualToString:@"identityTransfered"]) {
titleStr = @"管理员转让记录";
//设备(225977895624552)的管理员(13242881234)将管理员转让给你
NSString *tmp = [NSString stringWithFormat:@"(%@)",dMessage.phone];
NSString *tmp2 = [NSString stringWithFormat:@"%@ (%@)",dMessage.name,watchImei];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@的管理员%@把管理员转让给您",tmp2,tmp]];
[att addAttribute:NSForegroundColorAttributeName
value:mainColor
range:NSMakeRange(0,tmp2.length)];
[att addAttribute:NSForegroundColorAttributeName
value:mainColor
range:NSMakeRange(tmp2.length+4,tmp.length)];
cell.messageLabel.attributedText = att;
}else if ([dMessage.type isEqualToString:@"identityTransfer"]) {
titleStr = @"管理员转让记录";
//您将设备(225977895624552)的管理员转让给用户(13212345678)
NSString *tmp = [NSString stringWithFormat:@"用户(%@)",dMessage.phone];
NSInteger length = tmp.length;
NSString *tmp2 = [NSString stringWithFormat:@" %@ (%@)",dMessage.name,watchImei];
tmp = [NSString stringWithFormat:@"您将%@的管理员转让给%@",tmp2,tmp];
NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:tmp];
[att addAttribute:NSForegroundColorAttributeName
value:mainColor
range:NSMakeRange(att.length-length,length)];
[att addAttribute:NSForegroundColorAttributeName
value:mainColor
range:NSMakeRange(2,tmp2.length)];
cell.messageLabel.attributedText = att;
}else if ([dMessage.type isEqualToString:@"systemUpdate"]) {
titleStr = @"系统通知";
cell.messageLabel.text = dMessage.content;
}
cell.aTitleLabel.text = titleStr;
if (dMessage.read && [dMessage.read intValue] == 0) {
cell.readLabel.hidden = NO;
}else {
cell.readLabel.hidden = YES;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DMessage *dMessage = self.myDataSource[indexPath.section][indexPath.row];
if (dMessage.read && [dMessage.read intValue] == 0) {
WEAKSELF
[cUser updateMsg2Read:dMessage.msgId MsgType:@"notification" Success:^(id responseObject) {
[weakSelf refreshTableView:dMessage];
NSLog(@"%@", responseObject);
} failure:^{
}];
}
}
-(void)refreshTableView:(DMessage *)message {
message.read = @1;
[self.myTableView reloadData];
}
- (CGFloat)getLineHeight:(NSString *)string withWidth:(CGFloat)width withFont:(UIFont *)font{
UILabel *label = [[UILabel alloc]init];
label.numberOfLines = 0;
label.lineBreakMode = NSLineBreakByWordWrapping;
label.textAlignment = NSTextAlignmentLeft;
label.text = string;
label.font = font;
CGSize labelSize = [label sizeThatFits:CGSizeMake(width, MAXFLOAT)];
CGFloat height = ceil(labelSize.height) + 1 + 34 + 10 + 8 ;
// xLog(@"文本框应该高 Height :%f ", height);
if(height < 66){
height = 66;
}
return height;
}
@end