// // PBMessageController.m // IphoneBIMe // // Created by zjf on 2018/9/14. // Copyright © 2018年 ProBIM. All rights reserved. // #import "PBMessageController.h" #import "PBUnReadTableViewCell.h" #import "PBNetworkModel.h" #import "PBUnReadMesModel.h" #import "PBAddIssueController.h" #import "PBNavigationController.h" #import "PBProjectModel.h" #import "PBIssueListModel.h" #import "PBResultModel.h" #import "PBAddExamineController.h" #import "PBExamineListModel.h" #import #import "PBIssueNavModel.h" #import "PBProjectModel.h" #import "PBIssueListModel.h" #import "PBRefreshGifHeader.h" #define pageSize 15 static NSString *const cellID = @"cellID"; @interface PBMessageController () @property (nonatomic, strong) NSMutableArray *dataList; @property (nonatomic, assign) NSInteger pageIndex; @property (nonatomic, assign) NSInteger unReadCount; @property (nonatomic, strong) PBRefreshGifHeader *headerView; @end @implementation PBMessageController - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:NO animated:YES]; } - (void)viewDidLoad { [super viewDidLoad]; [self setupRefresh]; [self setupUI]; [self loadDate]; } - (void)setupRefresh { MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.pageIndex = 0; [self.dataList removeAllObjects]; [self loadUnReadMessage]; }]; header.lastUpdatedTimeLabel.hidden = YES; header.stateLabel.textColor = TitleColor; [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle]; [header setTitle:@"释放更新" forState:MJRefreshStatePulling]; [header setTitle:@"加载中..." forState:MJRefreshStateRefreshing]; self.tableView.mj_header = header; // self.tableView.mj_header = self.headerView; // MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ // [self loadUnReadMessage]; // }]; // // self.tableView.mj_footer = footer; // CGFloat insetBottom; // if (IS_IPHONE_X) { // insetBottom = 34.f; // }else { // insetBottom = 0.f; // } // self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = insetBottom; } - (void)loadDate { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 需要延迟执行的代码 [self.tableView.mj_header beginRefreshing]; }); } - (void)loadUnReadMessage { [[PBNetworkTools sharedTools] RequestGetNotReadMsgWithPageIndex:++self.pageIndex andPageSize:pageSize andIsRead:@"" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { [self.tableView.mj_header endRefreshing]; // [self.tableView.mj_footer endRefreshing]; if (error) { NSLog(@"%@",error); // --self.pageIndex; [YJProgressHUD showMessage:@"加载失败" inView:nil]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { NSArray *list = [networkModel.Data valueForKey:@"List"]; for (NSDictionary *obj in list) { PBUnReadMesModel *mesModel = [PBUnReadMesModel yy_modelWithDictionary:obj]; [self.dataList addObject:mesModel]; } [JPUSHService setBadge:0]; [UIApplication sharedApplication].applicationIconBadgeNumber = 0; // NSInteger mesCount = [[networkModel.Data valueForKey:@"msgCount"] integerValue]; // self.unReadCount = mesCount; // [JPUSHService setBadge:mesCount]; // [UIApplication sharedApplication].applicationIconBadgeNumber = mesCount; [self.tableView reloadData]; // if ([[networkModel.Data valueForKey:@"msgArr"] count] < pageSize) { // self.tableView.mj_footer.hidden = YES; // }else { // self.tableView.mj_footer.hidden = NO; // } }else { // --self.pageIndex; [YJProgressHUD showMessage:@"加载失败" inView:nil]; } }]; } - (void)setupUI { self.title = @"消息"; PBBackNavItem *backNav = [PBBackNavItem backNacItem]; backNav.title = @" "; [backNav addTarget:self action:@selector(backItemAction) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *backNavItem = [[UIBarButtonItem alloc] initWithCustomView:backNav]; self.navigationItem.leftBarButtonItem = backNavItem; UIBarButtonItem *allReadItem = [[UIBarButtonItem alloc] initWithTitle:@"设为已读" style:UIBarButtonItemStylePlain target:self action:@selector(setAllRead)]; self.navigationItem.rightBarButtonItem = allReadItem; [self.tableView registerClass:[PBUnReadTableViewCell class] forCellReuseIdentifier:cellID]; self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 80.f; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; } - (void)backItemAction { [self dismissViewControllerAnimated:YES completion:nil]; // [self.navigationController popViewControllerAnimated:YES]; } - (void)setAllRead { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"全部设为已读状态" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *setAllRedAction = [UIAlertAction actionWithTitle:@"设为已读" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) { [YJProgressHUD showProgress:@"" inView:nil]; [[PBNetworkTools sharedTools] RequestSetMsgTypeReadWithMsgType:@"" andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { [YJProgressHUD showMessage:@"全部设为已读设置失败" inView:nil]; NSLog(@"%@",error); return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel =[PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { [YJProgressHUD hide]; [self.tableView.mj_header beginRefreshing]; } else { [YJProgressHUD showMessage:@"全部设为已读设置失败" inView:nil]; } }]; }]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]; [alertVC addAction:setAllRedAction]; [alertVC addAction:cancelAction]; [self presentViewController:alertVC animated:YES completion:nil]; } #pragma mark - Table view data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataList.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { PBUnReadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; cell.unReadMesModel = self.dataList[indexPath.row]; return cell; } #pragma mark - UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { PBUnReadMesModel *unReadMesModel = self.dataList[indexPath.row]; if ([unReadMesModel.isRead isEqualToString:@"0"]) { [YJProgressHUD showProgress:@"" inView:self.view]; PBUnReadMesModel *unReadMesModel = self.dataList[indexPath.row]; [[PBNetworkTools sharedTools] RequestSetReadWithMu_guid:unReadMesModel.mu_guid andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { [YJProgressHUD showMessage:@"设置失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { unReadMesModel.isRead = @"1"; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; //加载详情 判断是否是删除 // if (unReadMesModel.LogAndMsgType == 4 || unReadMesModel.LogAndMsgType == 5 || unReadMesModel.LogAndMsgType == 7 || unReadMesModel.LogAndMsgType == 8 || unReadMesModel.LogAndMsgType == 9 || unReadMesModel.LogAndMsgType == 10 || unReadMesModel.LogAndMsgType == 1007){ // if (unReadMesModel.mm_objid == nil || [unReadMesModel.mm_objid isEqualToString:@""]) { // [self presentAlertVC]; // }else { // [self getProjectParentIdWithModel:unReadMesModel]; // } // }else { [YJProgressHUD hide]; [self presentAlertVC:unReadMesModel]; // } }else { [YJProgressHUD showMessage:networkModel.Msg inView:self.view]; } }]; }else { //加载详情 判断是否是删除 // if (unReadMesModel.LogAndMsgType == 4 || unReadMesModel.LogAndMsgType == 5 || unReadMesModel.LogAndMsgType == 7 || unReadMesModel.LogAndMsgType == 8 || unReadMesModel.LogAndMsgType == 9 || unReadMesModel.LogAndMsgType == 10 || unReadMesModel.LogAndMsgType == 1007){ // if (unReadMesModel.mm_objid == nil || [unReadMesModel.mm_objid isEqualToString:@""]) { // [self presentAlertVC]; // }else { // [self getProjectParentIdWithModel:unReadMesModel]; // } // }else { // [self presentAlertVC]; // } } } - (void)getProjectParentIdWithModel:(PBUnReadMesModel *)unReadMesModel { // [YJProgressHUD showCustomAnimation:@"" inview:self.view]; [YJProgressHUD showProgress:@"" inView:self.view]; [[PBNetworkTools sharedTools] getProjectParentIDWithProjectID:unReadMesModel.mme_ProjectID andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"加载状态失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSLog(@"%@",str); PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1){ [self loadTypeAndStatusWith:networkModel.Data andModel:unReadMesModel]; }else { [YJProgressHUD showMessage:@"加载状态、类型失败" inView:self.view]; } }]; } - (void)loadTypeAndStatusWith:(NSString *)parentId andModel:(PBUnReadMesModel *)unreadMesModel { __block NSArray *statusArr; __block NSArray *typeArr; dispatch_semaphore_t sem = dispatch_semaphore_create(0); NSInteger commandCount = 2; __block NSInteger httpFinishCount = 0; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[PBNetworkTools sharedTools] getIssueStatusWithCompanyId:parentId andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"加载状态失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if ([networkModel.Msg isEqualToString:@"OK"]) { statusArr = networkModel.Data; if (++httpFinishCount == commandCount) { dispatch_semaphore_signal(sem); } }else { [YJProgressHUD showMessage:@"加载状态失败" inView:self.view]; return; } }]; [[PBNetworkTools sharedTools] getIssueTypesWithCompanyId:parentId andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { NSLog(@"%@",error); [YJProgressHUD showMessage:@"加载类型失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if ([networkModel.Msg isEqualToString:@"OK"]) { typeArr = networkModel.Data; if (++httpFinishCount == commandCount) { dispatch_semaphore_signal(sem); } }else { [YJProgressHUD showMessage:@"加载类型失败" inView:self.view]; return; } }]; //如果全部请求没有返回则该线程会一直阻塞 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); NSLog(@"全部加载完成==================: %@", [NSThread currentThread]); dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"更新视图UI update in main thread!:%@",[NSThread currentThread]); [YJProgressHUD hide]; NSMutableArray *statusArrM = [[NSMutableArray alloc] init]; for (NSDictionary *dict in statusArr) { PBIssueNavModel *issueNavModel = [PBIssueNavModel yy_modelWithDictionary:dict]; [statusArrM addObject:issueNavModel]; } NSMutableArray *typeArrM = [[NSMutableArray alloc] init]; for (NSDictionary *dict in typeArr) { PBIssueNavModel *issueNavModel = [PBIssueNavModel yy_modelWithDictionary:dict]; [typeArrM addObject:issueNavModel]; } NSDictionary *statusAndTypeData = @{@"IssueStatus":statusArrM.mutableCopy, @"IssueTypes":typeArrM.mutableCopy}; PBProjectModel *projectModel = [[PBProjectModel alloc] init]; projectModel.organizeid = unreadMesModel.mme_ProjectID; projectModel.bimcomposerid = unreadMesModel.BIMComposerId; PBAddIssueController *detailsVC = [[PBAddIssueController alloc] init]; detailsVC.isAddIssue = NO; detailsVC.projectModel = projectModel; PBIssueListModel *issueListModel = [[PBIssueListModel alloc] init]; issueListModel.IssueId = unreadMesModel.mm_objid; detailsVC.issueListModel = issueListModel; detailsVC.statusAndTypeData = statusAndTypeData; PBNavigationController *nav = [[PBNavigationController alloc] initWithRootViewController:detailsVC]; nav.modalPresentationStyle = UIModalPresentationFullScreen; [self presentViewController:nav animated:YES completion:nil]; }); }); } - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { UITableViewRowAction *deleteAction =[UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"设为已读" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { [self deleteMesWithIndexPath:indexPath]; }]; deleteAction.backgroundColor = WarningColor; return @[deleteAction]; } - (void)deleteMesWithIndexPath:(NSIndexPath *)indexPath { [YJProgressHUD showProgress:@"" inView:self.view]; PBUnReadMesModel *unReadMesModel = self.dataList[indexPath.row]; [[PBNetworkTools sharedTools] RequestSetReadWithMu_guid:unReadMesModel.mu_guid andCallBack:^(NSURLSessionDataTask *task, id response, NSError *error) { if (error) { [YJProgressHUD showMessage:@"设置失败" inView:self.view]; return; } NSString *str = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; PBNetworkModel *networkModel = [PBNetworkModel yy_modelWithJSON:str]; if (networkModel.Ret == 1) { [YJProgressHUD hide]; unReadMesModel.isRead = @"1"; [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; }else { [YJProgressHUD showMessage:networkModel.Msg inView:self.view]; } }]; } - (NSMutableArray *)dataList { if (_dataList == nil) { _dataList = [[NSMutableArray alloc] init]; } return _dataList; } - (void)presentAlertVC:(PBUnReadMesModel *)unReadMesModel { NSString *message = @"我知道了!"; if (unReadMesModel.LogAndMsgType == 47 || unReadMesModel.LogAndMsgType == 48 || unReadMesModel.LogAndMsgType == 49) { message = @"您收到一条任务流转提醒,如需处理请到网页端"; } UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }]; [alertVC addAction:sureAction]; [self presentViewController:alertVC animated:YES completion:nil]; } - (void)loadExamineDetailWithReadMesModel:(PBUnReadMesModel *)unReadMesModel { // [YJProgressHUD showProgress:@"" inView:nil]; // [[PBNetworkTools sharedTools] RequestGetExamineDetailWithExamineID:unReadMesModel.mm_objid andCallBack:^(NSURLResponse *response, id responseObject, NSError *error) { // if (error) { // NSLog(@"%@",error); // [YJProgressHUD showMessage:@"加载检查详情失败" inView:nil]; // return; // } // NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; // PBResultModel *resultModel = [PBResultModel yy_modelWithJSON:str]; // if ([resultModel.Message isEqualToString:@"Success"]) { // [YJProgressHUD hide]; // if(resultModel.ResultData == nil) { // [YJProgressHUD showMessage:@"此检查已删除" inView:nil]; // return; // } // PBAddExamineController *addExamineVC = [[PBAddExamineController alloc] init]; // addExamineVC.isAddExamine = NO; // PBProjectModel *projectModel = [[PBProjectModel alloc] init]; // projectModel.bimcomposerid = unReadMesModel.mm_organizeid; // addExamineVC.projectModel = projectModel; // addExamineVC.examineListDict = resultModel.ResultData; // addExamineVC.examineListModel = [PBExamineListModel yy_modelWithDictionary:resultModel.ResultData]; // PBNavigationController *nav = [[PBNavigationController alloc] initWithRootViewController:addExamineVC]; // nav.modalPresentationStyle = UIModalPresentationFullScreen; // [self presentViewController:nav animated:YES completion:nil]; // }else { // [YJProgressHUD showMessage:@"加载检查详情失败" inView:nil]; // return; // } // }]; } - (PBRefreshGifHeader *)headerView { if(_headerView == nil) { _headerView = [[PBRefreshGifHeader alloc] init]; __weak typeof(self) weakSelf = self; [self.headerView setRefreshingBlock:^{ weakSelf.pageIndex = 0; [weakSelf.dataList removeAllObjects]; [weakSelf loadUnReadMessage]; }]; } return _headerView; } /* // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } */ /* // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view } } */ /* // Override to support rearranging the table view. - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { } */ /* // Override to support conditional rearranging of the table view. - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the item to be re-orderable. return YES; } */ /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end