// // NotiferMessageViewController.m // KulexiuForTeacher // // Created by 王智 on 2022/4/18. // #import "NotiferMessageViewController.h" #import "NotiferMessageCell.h" #import "NotiferHeadView.h" #import "NotiferMessageModel.h" #import "KSBaseWKWebViewController.h" #import "CustomNavViewController.h" //#import "ReceiveEvaluateListController.h" #import "HomeworkDetailViewController.h" #import "UIButton+EnlargeEdge.h" @interface NotiferMessageViewController () @property (nonatomic, strong) NotiferHeadView *headView; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSString *groupType; @end @implementation NotiferMessageViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self allocTitle:@"消息盒子"]; [self configRightButton]; [self configUI]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self queryUnReadCount]; [self resetSourceAndRequest]; } - (void)configRightButton { UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; [rightButton setTitle:@"全部已读" forState:UIControlStateNormal]; [rightButton setTitleColor:HexRGB(0x999999) forState:UIControlStateNormal]; [rightButton setImage:[UIImage imageNamed:@"clear_notifer"] forState:UIControlStateNormal]; rightButton.frame =CGRectMake(0, 0, 80, 40); //使图片在右边,文字在左边(正常情况下是文字在右边,图片在左边) [rightButton setSemanticContentAttribute:UISemanticContentAttributeForceRightToLeft]; //设置图片和文字之间的间隙 rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); [rightButton.titleLabel setFont:[UIFont systemFontOfSize:11.0f]]; [rightButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10]; [rightButton addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton]; self.navigationItem.rightBarButtonItem = rightItem; } - (void)rightBtnClick { [KSNetworkingManager batchSetReadRequest:KS_POST success:^(NSDictionary * _Nonnull dic) { if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) { [self MBPShow:@"清除成功"]; [self resetSourceAndRequest]; } else { [self MBPShow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { }]; } - (void)configUI { [self.scrollView removeFromSuperview]; [self.view addSubview:self.headView]; [self.headView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.mas_equalTo(self.view); make.height.mas_equalTo(100); }]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.top.mas_equalTo(self.headView.mas_bottom); make.bottom.mas_equalTo(self.view.mas_bottom).offset(-iPhoneXSafeBottomMargin); }]; MJWeakSelf; self.tableView.mj_header = [KSGifRefreshHeader headerWithRefreshingBlock:^{ [weakSelf resetSourceAndRequest]; }]; self.tableView.mj_footer = [KSGifRefreshFooter footerWithRefreshingBlock:^{ if (weakSelf.isLoadMore) { weakSelf.pages += 1; [weakSelf requestData]; } else { [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData]; } }]; } - (void)resetSourceAndRequest { [self resetParamenter]; [self requestData]; [self queryUnReadCount]; } - (void)queryUnReadCount { [KSNetworkingManager queryCountOfUnreadRequest:KS_GET success:^(NSDictionary * _Nonnull dic) { if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) { NSArray *countArray = [dic arrayValueForKey:@"data"]; NSInteger courseCount = 0; NSInteger sysCount = 0; NSInteger noticeCount = 0; for (NSDictionary *parm in countArray) { if ([[parm stringValueForKey:@"key"] isEqualToString:@"COURSE"]) { courseCount = [parm integerValueForKey:@"value"]; } if ([[parm stringValueForKey:@"key"] isEqualToString:@"SYSTEM"]) { sysCount = [parm integerValueForKey:@"value"]; } if ([[parm stringValueForKey:@"key"] isEqualToString:@"NOTICE"]) { noticeCount = [parm integerValueForKey:@"value"]; } } [self.headView configUnreadCountCourse:courseCount sysMessage:sysCount noticeCount:noticeCount]; } else { [self MBPShow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { }]; } - (void)resetParamenter { self.pages = 1; self.isLoadMore = YES; self.dataArray = [NSMutableArray array]; [self.tableView.mj_footer resetNoMoreData]; [self setPromptString:@"暂无消息~" imageName:@"empty_message" inView:self.tableView]; [self.tableView reloadData]; } - (void)endRefresh { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.tableView.mj_header endRefreshing]; [self.tableView.mj_footer endRefreshing]; }); } - (void)requestData { [self showhud]; [KSNetworkingManager sysMessageListRequest:KS_POST group:self.groupType page:self.pages rows:self.rows success:^(NSDictionary * _Nonnull dic) { [self removehub]; [self endRefresh]; if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) { // rows NSArray *sourceArray = [[dic dictionaryValueForKey:@"data"] arrayValueForKey:@"rows"]; for (NSDictionary *parm in sourceArray) { NotiferMessageModel *model = [[NotiferMessageModel alloc] initWithDictionary:parm]; [self.dataArray addObject:model]; } if (sourceArray.count < self.rows) { self.isLoadMore = NO; } } else { [self MBPShow:MESSAGEKEY]; } [self.tableView reloadData]; [self changePromptLabelState]; } faliure:^(NSError * _Nonnull error) { [self removehub]; [self endRefresh]; if (self.networkAvaiable == NO) { [self setPromptString:@"暂无网络" imageName:@"no_networking" inView:self.tableView]; } [self.dataArray removeAllObjects]; [self.tableView reloadData]; [self changePromptLabelState]; }]; } #pragma mark ----- table data source - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NotiferMessageModel *model = self.dataArray[indexPath.row]; NotiferMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NotiferMessageCell"]; [cell configCellWithSource:model]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // 选择跳转 NotiferMessageModel *model = self.dataArray[indexPath.row]; if (model.readStatus == 0) { NotiferMessageCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [self readMessage:model baseCell:cell]; } else { // 跳转页面 [self toDetailViewWithTypeString:model.memo]; } } - (void)readMessage:(NotiferMessageModel *)model baseCell:(NotiferMessageCell *)cell { [KSNetworkingManager setReadMessage:KS_POST messageId:model.internalBaseClassIdentifier success:^(NSDictionary * _Nonnull dic) { if ([dic integerValueForKey:@"code"] == 200 && [dic boolValueForKey:@"status"]) { model.readStatus = 1; cell.isRead = YES; // 跳转页面 [self toDetailViewWithTypeString:model.memo]; } else { [self MBPShow:MESSAGEKEY]; } } faliure:^(NSError * _Nonnull error) { }]; } - (void)toDetailViewWithTypeString:(NSString *)memo { if (![NSString isEmptyString:memo]) { NSString *headStr = [[memo componentsSeparatedByString:@"?"] firstObject]; if ([headStr isEqualToString:@"buyPractice"] || [headStr isEqualToString:@"courseRemind"]) { // 课表 [self toCourseTable]; } else if ([headStr isEqualToString:@"evaluate"]) { // 评价页面 // ReceiveEvaluateListController *ctrl = [[ReceiveEvaluateListController alloc] init]; // [self.navigationController pushViewController:ctrl animated:YES]; } else if ([headStr isEqualToString:@"H5"]) { // web NSString *valueStr = [[memo componentsSeparatedByString:@"?"] lastObject]; KSBaseWKWebViewController *webCtrl = [[KSBaseWKWebViewController alloc] init]; webCtrl.url = valueStr; CustomNavViewController *navCtrl = self.tabBarController.selectedViewController; [navCtrl pushViewController:webCtrl animated:YES]; } else if ([headStr isEqualToString:@"homework"]) { // 作业详情 NSString *valueStr = [[memo componentsSeparatedByString:@"?"] lastObject]; NSArray *parmArray = [valueStr componentsSeparatedByString:@"&"]; NSString *courseId = nil; for (NSString *subStr in parmArray) { if ([subStr containsString:@"courseId"]) { courseId = [[subStr componentsSeparatedByString:@"="] lastObject]; } } HomeworkDetailViewController *detailVC = [[HomeworkDetailViewController alloc] init]; detailVC.courseId = courseId; [self.navigationController pushViewController:detailVC animated:YES]; } } } - (void)toCourseTable { [self selectBarHomeWithIndex:1]; [self.navigationController popToRootViewControllerAnimated:YES]; } #pragma mark ---- lazying - (NotiferHeadView *)headView { if (!_headView) { _headView = [NotiferHeadView shareIntance]; MJWeakSelf; [_headView chooseTypeCallback:^(NOTIFER_TYPE type) { [weakSelf chooseTypeSort:type]; }]; } return _headView; } - (void)chooseTypeSort:(NOTIFER_TYPE)type { switch (type) { case NOTIFER_TYPE_ALL: { self.groupType = nil; } break; case NOTIFER_TYPE_COURSE: { self.groupType = @"COURSE"; } break; case NOTIFER_TYPE_SYSMSG: { self.groupType = @"SYSTEM"; } break; case NOTIFER_TYPE_NOTICE: { self.groupType = @"NOTICE"; } break; default: break; } [self resetSourceAndRequest]; } - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.backgroundColor = [UIColor clearColor]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [_tableView registerNib:[UINib nibWithNibName:@"NotiferMessageCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NotiferMessageCell"]; _tableView.estimatedRowHeight = 125.0f; _tableView.rowHeight = UITableViewAutomaticDimension; UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15)]; bottomView.backgroundColor = HexRGB(0xf6f8f9); _tableView.tableFooterView = bottomView; } return _tableView; } /* #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