// // SessionInfoViewController.m // YUMI // // Created by YUMI on 2022/4/25. // #import "SessionInfoViewController.h" ///Third #import #import ///Tool #import "DJDKMIMOMColor.h" #import "NetImageView.h" #import "TTPopup.h" #import "YUMIHtmlUrl.h" #import "YUMIMacroUitls.h" #import "NSArray+Safe.h" #import "NSMutableDictionary+Saft.h" #import "StatisticsServiceHelper.h" #import "ClientConfig.h" #import "XNDJTDDLoadingTool.h" ///Model #import "SessionSettingModel.h" #import "UserInfoModel.h" ///View #import "SessionSettingTableViewCell.h" #import "SessionSettingUserView.h" #import "XPWebViewController.h" #import "XPMineUserInfoViewController.h" @interface SessionInfoViewController () ///头部试图 @property (nonatomic,strong) SessionSettingUserView *headerView; ///列表 @property (nonatomic,strong) UITableView *tableView; ///数据 @property (nonatomic,strong) NSArray *datasource; @end @implementation SessionInfoViewController - (void)viewDidLoad { [super viewDidLoad]; [self initSubViews]; [self initSubViewConstraints]; } #pragma mark - Private Method - (void)initSubViews { self.title = YMLocalizedString(@"SessionInfoViewController0"); [self.view addSubview:self.tableView]; self.tableView.tableHeaderView = self.headerView; self.headerView.userInfo = self.userInfo; } - (void)initSubViewConstraints { [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.leading.trailing.mas_equalTo(self.view).inset(15); make.top.mas_equalTo(self.view).offset(12); make.height.mas_equalTo(50 * 3 + 70); }]; } #pragma mark - UITableViewDelegate And UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.datasource.count; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 50; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { SessionSettingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SessionSettingTableViewCell class])]; if (cell == nil) { cell = [[SessionSettingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([SessionSettingTableViewCell class])]; } SessionSettingModel * model= [self.datasource safeObjectAtIndex1:indexPath.row]; cell.chatItem = model; cell.delegate = self; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; if (self.datasource.count > 0) { SessionSettingModel * model = [self.datasource safeObjectAtIndex1:indexPath.row]; if (model.settingType == SessionSettingType_report) { [StatisticsServiceHelper trackEventWithKey:StatisticsServiceEvent_chat_set_report_click]; XPWebViewController *webVC = [[XPWebViewController alloc] init]; webVC.url = [NSString stringWithFormat:@"%@?reportUid=%ld&source=CHAT", URLWithType(kReportRoomURL), self.userInfo.uid]; [self.navigationController pushViewController:webVC animated:YES]; } } } #pragma mark - SessionSettingTableViewCellDelegate - (void)sessionSettingTableViewCell:(SessionSettingModel *)item switchState:(UISwitch *)switchOn { NSString * userId = [NSString stringWithFormat:@"%ld",self.userInfo.uid]; if (item.settingType == SessionSettingType_Top) { [StatisticsServiceHelper trackEventWithKey:StatisticsServiceEvent_chat_set_top_click]; if (switchOn.on) { NIMAddStickTopSessionParams * params = [[NIMAddStickTopSessionParams alloc] initWithSession:self.session]; [[NIMSDK sharedSDK].chatExtendManager addStickTopSession:params completion:^(NSError * _Nullable error, NIMStickTopSessionInfo * _Nullable newInfo) { if (error == nil) { [self.stickTopMessages safeSetObject:newInfo forKey:self.session]; } }]; } else { NIMStickTopSessionInfo * topInfo; NIMSession * topSession; for (int i = 0; i < self.stickTopMessages.allKeys.count; i++) { NIMSession * session = [self.stickTopMessages.allKeys safeObjectAtIndex1:i]; if (session.sessionId == self.session.sessionId) { topSession = session; break; } } if (topSession) { topInfo = [self.stickTopMessages objectForKey:topSession]; } if (topInfo) { [[NIMSDK sharedSDK].chatExtendManager removeStickTopSession:topInfo completion:^(NSError * _Nullable error, NIMStickTopSessionInfo * _Nullable removedInfo) { if (error == nil) { [self.stickTopMessages removeObjectForKey:topSession]; } }]; } } } else if (item.settingType == SessionSettingType_black) { [StatisticsServiceHelper trackEventWithKey:StatisticsServiceEvent_chat_set_blacklist_click]; NSString *title = nil; NSString *message = nil; if (switchOn.on) { title = YMLocalizedString(@"SessionInfoViewController2"); message = YMLocalizedString(@"SessionInfoViewController3"); } else { title = YMLocalizedString(@"SessionInfoViewController4"); message = YMLocalizedString(@"SessionInfoViewController5"); } TTAlertConfig *config = [[TTAlertConfig alloc] init]; config.title = title; config.message = message; [TTPopup alertWithConfig:config confirmHandler:^{ if (!switchOn.on) { [[NIMSDK sharedSDK].userManager removeFromBlackBlackList:userId completion:^(NSError * _Nullable error) { if (error == nil) { [self showErrorToast:YMLocalizedString(@"SessionInfoViewController6")]; item.state = NO; [self.tableView reloadData]; } else { [self showErrorToast:error.description]; } }]; } else { [[NIMSDK sharedSDK].userManager addToBlackList:userId completion:^(NSError * _Nullable error) { if (error == nil) { [self showErrorToast:YMLocalizedString(@"SessionInfoViewController7")]; item.state = YES; [self.tableView reloadData]; } else { [self showErrorToast:error.description]; } }]; } } cancelHandler:^{ }]; } } #pragma mark - Event Response - (void)didTapUserRecognizer { [StatisticsServiceHelper trackEventWithKey:StatisticsServiceEvent_chat_set_data_click]; XPMineUserInfoViewController * mineVC = [[XPMineUserInfoViewController alloc] init]; mineVC.uid = self.userInfo.uid; [self.navigationController pushViewController:mineVC animated:YES]; } #pragma mark - Getters And Setters - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.tableFooterView = [UIView new]; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = [UIColor whiteColor]; _tableView.layer.masksToBounds = YES; _tableView.layer.cornerRadius = 8; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } [_tableView registerClass:[SessionSettingTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SessionSettingTableViewCell class])]; } return _tableView; } - (SessionSettingUserView *)headerView { if (!_headerView) { _headerView = [[SessionSettingUserView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 70)]; UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapUserRecognizer)]; [_headerView addGestureRecognizer:tap]; } return _headerView; } - (NSArray *)datasource { if (!_datasource) { SessionSettingModel * topModel = [[SessionSettingModel alloc] init]; topModel.title = YMLocalizedString(@"SessionInfoViewController8"); topModel.settingType = SessionSettingType_Top; NIMSession * topSession; for (int i = 0; i < self.stickTopMessages.allKeys.count; i++) { NIMSession * session = [self.stickTopMessages.allKeys safeObjectAtIndex1:i]; if (session.sessionId == self.session.sessionId) { topSession = session; break; } } if (topSession && topSession.sessionId.length > 0) { topModel.state = YES; } else { topModel.state = NO; } NSString * uid = [NSString stringWithFormat:@"%ld", self.userInfo.uid]; BOOL isBlack = [[NIMSDK sharedSDK].userManager isUserInBlackList:uid]; SessionSettingModel * blackModel = [[SessionSettingModel alloc] init]; blackModel.title = YMLocalizedString(@"SessionInfoViewController9"); blackModel.state = isBlack; blackModel.settingType = SessionSettingType_black; SessionSettingModel * reportModel = [[SessionSettingModel alloc] init]; reportModel.title = YMLocalizedString(@"SessionInfoViewController10"); reportModel.settingType = SessionSettingType_report; _datasource = @[topModel, blackModel, reportModel]; } return _datasource; } @end