RoomDataSource: Handle RoomCreationIntroCell conditional display.

This commit is contained in:
SBiOSoftWhare
2021-02-21 23:57:58 +01:00
parent e5796073c8
commit 2b6008cea1
@@ -48,6 +48,10 @@
@property(nonatomic, readwrite) RoomEncryptionTrustLevel encryptionTrustLevel;
@property (nonatomic) RoomBubbleCellData *roomCreationCellData;
@property (nonatomic) BOOL showRoomCreationCell;
@end
@implementation RoomDataSource
@@ -205,6 +209,7 @@
}
[self fetchEncryptionTrustedLevel];
[self enableRoomCreationIntroCellDisplayIfNeeded];
}
- (void)fetchEncryptionTrustedLevel
@@ -213,6 +218,10 @@
[self.roomDataSourceDelegate roomDataSource:self didUpdateEncryptionTrustLevel:self.encryptionTrustLevel];
}
- (void)roomDidChange
{
[self enableRoomCreationIntroCellDisplayIfNeeded];
}
#pragma mark -
@@ -225,6 +234,8 @@
// Enable the containsLastMessage flag for the cell data which contains the last message.
@synchronized(bubbles)
{
[self insertRoomCreationIntroCellDataIfNeeded];
// Reset first all cell data
for (RoomBubbleCellData *cellData in bubbles)
{
@@ -963,4 +974,72 @@
[self.delegate dataSource:self didRecognizeAction:kMXKRoomBubbleCellLongPressOnReactionView inCell:nil userInfo:@{ kMXKRoomBubbleCellEventIdKey: eventId }];
}
#pragma mark - Room creation intro cell
- (BOOL)canShowRoomCreationIntroCell
{
NSString* userId = self.mxSession.myUser.userId;
if (!userId || !self.isLive || self.isPeeking)
{
return NO;
}
// Room creation cell is only shown for the creator
return [self.room.summary.creatorUserId isEqualToString:userId];
}
- (void)enableRoomCreationIntroCellDisplayIfNeeded
{
self.showRoomCreationCell = [self canShowRoomCreationIntroCell];
}
// Insert the room creation intro cell at the begining
- (void)insertRoomCreationIntroCellDataIfNeeded
{
if (self.showRoomCreationCell)
{
NSUInteger existingRoomCreationCellDataIndex = [self roomBubbleDataIndexWithTag:RoomBubbleCellDataTagRoomCreationIntro];
if (existingRoomCreationCellDataIndex != NSNotFound)
{
[bubbles removeObjectAtIndex:existingRoomCreationCellDataIndex];
}
NSUInteger roomCreationConfigCellDataIndex = [self roomBubbleDataIndexWithTag:RoomBubbleCellDataTagRoomCreateConfiguration];
// Only add room creation intro cell if `bubbles` array contains the room creation event
if (roomCreationConfigCellDataIndex != NSNotFound)
{
MXEvent *event = [MXEvent new];
MXRoomState *roomState = [MXRoomState new];
RoomBubbleCellData *roomBubbleCellData = [[RoomBubbleCellData alloc] initWithEvent:event andRoomState:roomState andRoomDataSource:self];
roomBubbleCellData.tag = RoomBubbleCellDataTagRoomCreationIntro;
[bubbles insertObject:roomBubbleCellData atIndex:0];
self.roomCreationCellData = roomBubbleCellData;
}
}
else
{
self.roomCreationCellData = nil;
}
}
- (NSUInteger)roomBubbleDataIndexWithTag:(RoomBubbleCellDataTag)tag
{
return [bubbles indexOfObjectPassingTest:^BOOL(id<MXKRoomBubbleCellDataStoring> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([obj isKindOfClass:RoomBubbleCellData.class])
{
RoomBubbleCellData *roomBubbleCellData = (RoomBubbleCellData*)obj;
if (roomBubbleCellData.tag == tag)
{
return YES;
}
}
return NO;
}];
}
@end