公屏增加@别人

This commit is contained in:
chenguilong
2022-04-24 18:58:30 +08:00
parent 6e88275eba
commit 514081b699
8 changed files with 302 additions and 13 deletions

View File

@@ -42,6 +42,8 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
@property (nonatomic,strong) XPRoomMessageHeaderView *headerView;
///
@property (nonatomic,strong) UIButton *messageTipsBtn;
///at
@property (nonatomic, strong) UIButton *atTipBtn;
///
@property (nonatomic,assign) BOOL isPending;
///
@@ -52,6 +54,10 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
@property (nonatomic,strong) NSMutableArray<NIMMessage *> *incomingMessages;
///
@property (nonatomic, strong) NSMutableArray *messageBubbles;
///at
@property (nonatomic, assign) NSInteger atCount;
///@
@property (nonatomic, strong) NSMutableArray *locationArray;
///messageView TODO:
@property (nonatomic,strong) XPRoomMessageParser *messageParser;
///
@@ -82,11 +88,143 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
[self appendAndScrollToBottom];
}
- (void)atTipsBtnAction:(UIButton *)sender {
self.isPending = YES;
[self appendAndScrollToAtUser];
}
///
- (void)appendAndScrollToAtUser {
if (self.incomingMessages.count < 1) {
NSInteger rows = self.datasource.count;
if (!self.locationArray.count) {
[self.messageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(rows-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
self.messageTipsBtn.hidden = YES;
self.isPending = NO;
self.atCount = 0;
self.atTipBtn.hidden = YES;
return;
}
int index = [self.locationArray[0] intValue];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
if (rows > indexPath.row) {
[self.messageTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
if (rows == indexPath.row + 1) {
self.messageTipsBtn.hidden = YES;
self.isPending = NO;
}
} else {
[self.messageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(rows-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
//@
self.messageTipsBtn.hidden = YES;
}
if (self.locationArray.count) {
[self.locationArray removeObjectAtIndex:0];
self.atCount -= 1;
[self.atTipBtn setTitle:[NSString stringWithFormat:@"有%lu人@你", (unsigned long)self.locationArray.count] forState:UIControlStateNormal];
} else {
self.atTipBtn.hidden = YES;
}
} else {
if (self.datasource.count > kRoomMessageMaxLength) {
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, kRoomMessageMaxLength/2)];
NSArray *needRemoveMsgArray = [self.datasource objectsAtIndexes:set];
[self.datasource removeObjectsInArray:needRemoveMsgArray];
[self.messageBubbles removeObjectsAtIndexes:set];
NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
for (int i = 0; i<self.locationArray.count; i++) {///at
NSNumber *number = self.locationArray[i];
if (number.integerValue < kRoomMessageMaxLength / 2) {
self.atCount--;
[indexSet addIndex:i];
}
}
[self.locationArray removeObjectsAtIndexes:indexSet];
for (int i = 0; i<self.locationArray.count; i++) {//at,at
NSNumber *number = self.locationArray[i];
number = @(number.integerValue - kRoomMessageMaxLength / 2);
[self.locationArray replaceObjectAtIndex:i withObject:number];
}
}
//
NSMutableArray *indexPaths = [NSMutableArray array];
for (NIMMessage *item in self.incomingMessages) {
[self.datasource addObject:[self.messageParser parseMessageAttribute:item]];
[self.messageBubbles addObject:[self.messageParser parseMessageBubble:item]];
[indexPaths addObject:[NSIndexPath indexPathForRow:self.datasource.count - 1 inSection:0]];
id nickNameNifo = item.remoteExt[@"atUids"];
if ([nickNameNifo isKindOfClass:[NSArray class]]) {
for (NSString *nick in nickNameNifo) {
if ([nick isEqualToString:[AccountInfoStorage instance].getUid]) {
[self.locationArray addObject:@(self.datasource.count-1)];
}
}
}
}
[self.incomingMessages removeAllObjects];
[self.messageTableView reloadData];
NSInteger rows = self.datasource.count;
if (!self.locationArray.count) {
[self.messageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(rows-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
self.messageTipsBtn.hidden = YES;
self.isPending = NO;
self.atCount = 0;
self.atTipBtn.hidden = YES;
return;
}
int index = [self.locationArray[0] intValue];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
if (rows > indexPath.row) {
[self.messageTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
if (rows == indexPath.row + 1) {
self.messageTipsBtn.hidden = YES;
self.isPending = NO;
}
} else {
[self.messageTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:(rows-1) inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
//@
self.messageTipsBtn.hidden = YES;
}
if (self.locationArray.count) {
[self.locationArray removeObjectAtIndex:0];
self.atCount -= 1;
[self.atTipBtn setTitle:[NSString stringWithFormat:@"有%lu人@你", (unsigned long)self.locationArray.count] forState:UIControlStateNormal];
} else {
self.atTipBtn.hidden = YES;
}
}
}
#pragma mark - @
///@
- (void)findAtMeNumber {
if (self.incomingMessages.count) {
NIMMessage *message = self.incomingMessages.lastObject;
id nickNameNifo = message.remoteExt[@"atUids"];
if ([nickNameNifo isKindOfClass:[NSArray class]]) {
for (NSString *nick in nickNameNifo) {
if ([nick isEqualToString:[AccountInfoStorage instance].getUid]) {
self.atCount += 1;
}
}
}
if (self.atCount > 0) {
self.atTipBtn.hidden = NO;
[self.atTipBtn setTitle:[NSString stringWithFormat:@"有%zd人@你", self.atCount] forState:UIControlStateNormal];
} else {
self.atTipBtn.hidden = YES;
}
}
}
#pragma mark - Private Method
- (void)initSubViews {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addCustomMessage:) name:@"message" object:nil];
[self addSubview:self.messageTableView];
[self addSubview:self.messageTipsBtn];
[self addSubview:self.atTipBtn];
self.messageTableView.tableHeaderView = self.headerView;
}
@@ -103,6 +241,12 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
make.bottom.mas_equalTo(self.mas_bottom).offset(-5);
make.left.mas_equalTo(self);
}];
[self.atTipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(100);
make.height.mas_equalTo(30);
make.bottom.mas_equalTo(self.mas_bottom).offset(-5);
make.left.mas_equalTo(self);
}];
}
///
@@ -132,6 +276,7 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
}
if (self.isPending) {
self.messageTipsBtn.hidden = NO;
[self findAtMeNumber];
} else {
[self appendAndScrollToBottom];
}
@@ -140,6 +285,8 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
///
- (void)appendAndScrollToBottom {
if (self.incomingMessages.count < 1) {
///atincomeatatat
[self scrollToBottom:YES];
return;
}
@@ -172,6 +319,9 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
if (r<1) return;
NSIndexPath *ip = [NSIndexPath indexPathForRow:r-1 inSection:s-1]; //
[self.messageTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:animated]; //
self.atCount = 0;
self.atTipBtn.hidden = YES;
[self.locationArray removeAllObjects];
}
///
@@ -341,6 +491,9 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
[self.datasource removeAllObjects];
[self.incomingMessages removeAllObjects];
[self.messageBubbles removeAllObjects];
[self.locationArray removeAllObjects];
self.atCount = 0;
self.atTipBtn.hidden = YES;
[[XPRoomMiniManager shareManager] resetLocalMessage];
[self addRoomMessage:message];
return;
@@ -474,6 +627,9 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
self.isPending = contentSizeH - contentOffsetY - sizeH > 20.0;
if (!self.isPending) {
self.messageTipsBtn.hidden = YES;
self.atTipBtn.hidden = YES;
self.atCount = 0;
[self.locationArray removeAllObjects];
if (self.incomingMessages.count > 0) {
[self appendAndScrollToBottom];
}
@@ -561,6 +717,21 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
return _messageTipsBtn;
}
- (UIButton *)atTipBtn {
if (!_atTipBtn) {
_atTipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_atTipBtn setTitle:@"有人@你" forState:UIControlStateNormal];
[_atTipBtn setTitleColor:[ThemeColor appMainColor] forState:UIControlStateNormal];
_atTipBtn.layer.cornerRadius = 15.0;
_atTipBtn.layer.masksToBounds = YES;
_atTipBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
_atTipBtn.backgroundColor = [UIColor whiteColor];
[_atTipBtn addTarget:self action:@selector(atTipsBtnAction:) forControlEvents:UIControlEventTouchUpInside];
_atTipBtn.hidden = YES;
}
return _atTipBtn;
}
- (NSMutableArray<NSAttributedString *> *)datasource {
if (!_datasource) {
_datasource = [NSMutableArray array];
@@ -582,6 +753,13 @@ NSString * const kRoomShowTopicKey = @"kRoomShowTopicKey";
return _messageBubbles;
}
- (NSMutableArray *)locationArray {
if (!_locationArray) {
_locationArray = [NSMutableArray array];
}
return _locationArray;
}
- (XPRoomMessageParser *)messageParser {
if (!_messageParser) {
_messageParser = [[XPRoomMessageParser alloc] init];