Room Details: display user name for incoming messages

This commit is contained in:
giomfo 2014-10-16 23:48:42 +02:00
parent 9c3160e689
commit 74ed964ab7
5 changed files with 92 additions and 17 deletions

View file

@ -37,7 +37,7 @@
<rect key="frame" x="0.0" y="0.0" width="600" height="556"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<prototypes>
<tableViewCell contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="IncomingMessageCell" id="Lq0-jR-3i5" customClass="IncomingMessageCell">
<tableViewCell contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" reuseIdentifier="IncomingMessageCell" rowHeight="70" id="Lq0-jR-3i5" customClass="IncomingMessageCell">
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="Lq0-jR-3i5" id="iJp-sA-hG6">
<autoresizingMask key="autoresizingMask"/>
@ -52,7 +52,7 @@
</constraints>
</imageView>
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" scrollEnabled="NO" editable="NO" translatesAutoresizingMaskIntoConstraints="NO" id="J5R-Mh-3hV">
<rect key="frame" x="51" y="5" width="200" height="40"/>
<rect key="frame" x="51" y="25" width="200" height="40"/>
<color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="width" constant="200" id="rsq-bW-mGt"/>
@ -61,25 +61,33 @@
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="User name:" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="8" translatesAutoresizingMaskIntoConstraints="NO" id="egJ-aY-QVW">
<rect key="frame" x="51" y="5" width="200" height="20"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" constant="20" placeholder="YES" id="8Iq-fG-vCR"/>
<constraint firstAttribute="width" constant="200" id="unP-98-1hS"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="bottom" secondItem="uhu-R0-9NH" secondAttribute="bottom" constant="5" id="2Yn-q9-M4a"/>
<constraint firstAttribute="bottom" secondItem="J5R-Mh-3hV" secondAttribute="bottom" constant="5" id="662-Ze-6ia"/>
<constraint firstItem="J5R-Mh-3hV" firstAttribute="top" secondItem="iJp-sA-hG6" secondAttribute="top" constant="5" id="Esp-Qs-hAs"/>
<constraint firstItem="egJ-aY-QVW" firstAttribute="top" secondItem="iJp-sA-hG6" secondAttribute="top" constant="5" id="N8f-0n-ObR"/>
<constraint firstItem="uhu-R0-9NH" firstAttribute="leading" secondItem="iJp-sA-hG6" secondAttribute="leading" constant="8" id="fNV-Tp-p31"/>
<constraint firstItem="uhu-R0-9NH" firstAttribute="top" secondItem="iJp-sA-hG6" secondAttribute="top" constant="5" id="mer-CT-cEg"/>
<constraint firstItem="J5R-Mh-3hV" firstAttribute="leading" secondItem="uhu-R0-9NH" secondAttribute="trailing" constant="3" id="qkW-ys-ckn"/>
<constraint firstItem="J5R-Mh-3hV" firstAttribute="top" secondItem="egJ-aY-QVW" secondAttribute="bottom" id="s5L-L8-rcb"/>
<constraint firstItem="egJ-aY-QVW" firstAttribute="leading" secondItem="uhu-R0-9NH" secondAttribute="trailing" constant="3" id="wym-In-Raa"/>
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="2Yn-q9-M4a"/>
</mask>
</variation>
</tableViewCellContentView>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<connections>
<outlet property="messageTextView" destination="J5R-Mh-3hV" id="d45-NI-bod"/>
<outlet property="userNameLabel" destination="egJ-aY-QVW" id="IWg-7t-5Vp"/>
<outlet property="userPicture" destination="uhu-R0-9NH" id="b7D-KD-aga"/>
</connections>
</tableViewCell>

View file

@ -37,7 +37,8 @@
- (void)logout;
- (BOOL)isAttachment:(MXEvent*)message;
- (NSString*)displayTextFor:(MXEvent*)message inDetailMode:(BOOL)isDetailMode;
- (BOOL)isNotification:(MXEvent*)message;
- (NSString*)displayTextFor:(MXEvent*)message inSubtitleMode:(BOOL)isSubtitle;
- (NSString*)displayNameFor:(MXRoomMember*)member;
@end

View file

@ -174,7 +174,19 @@ static MatrixHandler *sharedHandler = nil;
return NO;
}
- (NSString*)displayTextFor:(MXEvent*)message inDetailMode:(BOOL)isDetailMode {
- (BOOL)isNotification:(MXEvent*)message {
// We consider as notification mxEvent which is not a text message or an attachment
if (message.eventType == MXEventTypeRoomMessage) {
NSString *msgtype = message.content[@"msgtype"];
if ([msgtype isEqualToString:@"m.emote"]) {
return YES;
}
return NO;
}
return YES;
}
- (NSString*)displayTextFor:(MXEvent*)message inSubtitleMode:(BOOL)isSubtitle {
NSString *displayText = nil;
// Retrieve roomData related to the message
MXRoomData *roomData = [self.mxData getRoomData:message.room_id];
@ -255,7 +267,7 @@ static MatrixHandler *sharedHandler = nil;
}
// Check whether the sender name has to be added
if (isDetailMode && [msgtype isEqualToString:@"m.emote"] == NO) {
if (isSubtitle && [msgtype isEqualToString:@"m.emote"] == NO) {
displayText = [NSString stringWithFormat:@"%@: %@", userDisplayName, displayText];
}

View file

@ -251,7 +251,7 @@
MXRoomData *mxRoomData = [mxHandler.mxData getRoomData:mxEvent.room_id];
cell.roomTitle.text = [mxRoomData displayname];
cell.lastEventDescription.text = [mxHandler displayTextFor:mxEvent inDetailMode:YES];
cell.lastEventDescription.text = [mxHandler displayTextFor:mxEvent inSubtitleMode:YES];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:mxEvent.ts/1000];
NSString *dateFormat = @"MMM dd HH:mm";

View file

@ -19,7 +19,9 @@
#import "MatrixHandler.h"
#import "AppDelegate.h"
#define TEXT_VIEW_VERTICAL_MARGIN 5
#define ROOM_MESSAGE_CELL_TOP_MARGIN 5
#define ROOM_MESSAGE_CELL_BOTTOM_MARGIN 5
#define INCOMING_MESSAGE_CELL_USER_LABEL_HEIGHT 20
// Table view cell
@interface RoomMessageCell : UITableViewCell
@ -30,6 +32,7 @@
@end
@interface IncomingMessageCell : RoomMessageCell
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@end
@implementation IncomingMessageCell
@end
@ -295,13 +298,29 @@
// Get event related to this row
MatrixHandler *mxHandler = [MatrixHandler sharedHandler];
MXEvent *mxEvent = [messages objectAtIndex:indexPath.row];
BOOL isIncomingMsg = ([mxEvent.user_id isEqualToString:mxHandler.userId] == NO);
// Use a TextView template to compute cell height
UITextView *dummyTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 200, MAXFLOAT)];
dummyTextView.font = [UIFont systemFontOfSize:14];
dummyTextView.text = [mxHandler displayTextFor:mxEvent inDetailMode:NO];
dummyTextView.text = [mxHandler displayTextFor:mxEvent inSubtitleMode:NO];
CGSize contentSize = [dummyTextView sizeThatFits:dummyTextView.frame.size];
rowHeight = contentSize.height + (TEXT_VIEW_VERTICAL_MARGIN * 2);
// Handle incoming / outgoing layout
if (isIncomingMsg) {
// By default the user name is displayed above the message
rowHeight = contentSize.height + ROOM_MESSAGE_CELL_TOP_MARGIN + INCOMING_MESSAGE_CELL_USER_LABEL_HEIGHT + ROOM_MESSAGE_CELL_BOTTOM_MARGIN;
if (indexPath.row) {
// This user name is hide if the previous message is from the same user
MXEvent *previousMxEvent = [messages objectAtIndex:indexPath.row - 1];
if ([previousMxEvent.user_id isEqualToString:mxEvent.user_id]) {
rowHeight -= INCOMING_MESSAGE_CELL_USER_LABEL_HEIGHT;
}
}
} else {
rowHeight = contentSize.height + ROOM_MESSAGE_CELL_TOP_MARGIN + ROOM_MESSAGE_CELL_BOTTOM_MARGIN;
}
// Force minimum height: 50
if (rowHeight < 50) {
@ -314,14 +333,49 @@
RoomMessageCell *cell;
MatrixHandler *mxHandler = [MatrixHandler sharedHandler];
MXEvent *mxEvent = [messages objectAtIndex:indexPath.row];
BOOL isIncomingMsg = NO;
if ([mxEvent.user_id isEqualToString:mxHandler.userId]) {
cell = [tableView dequeueReusableCellWithIdentifier:@"OutgoingMessageCell" forIndexPath:indexPath];
cell.messageTextView.backgroundColor = [UIColor groupTableViewBackgroundColor];
} else {
cell = [tableView dequeueReusableCellWithIdentifier:@"IncomingMessageCell" forIndexPath:indexPath];
cell.messageTextView.backgroundColor = [UIColor lightGrayColor];
isIncomingMsg = YES;
}
cell.messageTextView.text = [mxHandler displayTextFor:mxEvent inDetailMode:NO];
// Clear background for notifications (We consider as notification mxEvent which is not a text message or an attachment)
if ([mxHandler isNotification:mxEvent]) {
cell.messageTextView.backgroundColor = [UIColor clearColor];
}
// Hide user picture if the previous message is from the same user
cell.userPicture.hidden = NO;
if (indexPath.row) {
MXEvent *previousMxEvent = [messages objectAtIndex:indexPath.row - 1];
if ([previousMxEvent.user_id isEqualToString:mxEvent.user_id]) {
cell.userPicture.hidden = YES;
}
}
// Hide userName in incoming message if the previous message is from the same user
if (isIncomingMsg) {
IncomingMessageCell* incomingMsgCell = (IncomingMessageCell*)cell;
CGRect frame = incomingMsgCell.userNameLabel.frame;
if (cell.userPicture.hidden) {
incomingMsgCell.userNameLabel.text = nil;
frame.size.height = 0;
incomingMsgCell.userNameLabel.hidden = YES;
} else {
frame.size.height = INCOMING_MESSAGE_CELL_USER_LABEL_HEIGHT;
incomingMsgCell.userNameLabel.hidden = NO;
NSString *userName = [mxHandler displayNameFor:[mxRoomData getMember:mxEvent.user_id]];
incomingMsgCell.userNameLabel.text = [NSString stringWithFormat:@"- %@", userName];
}
incomingMsgCell.userNameLabel.frame = frame;
}
cell.messageTextView.text = [mxHandler displayTextFor:mxEvent inSubtitleMode:NO];
return cell;
}