Room details: Handle local echo

This commit is contained in:
giomfo
2014-10-17 11:00:46 +02:00
parent 74ed964ab7
commit b5e133063c
2 changed files with 75 additions and 23 deletions

View File

@@ -114,25 +114,32 @@
<constraint firstAttribute="width" constant="40" id="tbf-QO-jKR"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Unsent" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="alD-cg-uMl">
<rect key="frame" x="286" y="14" width="58" height="21"/>
<constraints>
<constraint firstAttribute="width" constant="58" id="GX0-HR-Wge"/>
<constraint firstAttribute="height" constant="21" id="q5G-LH-tGH"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<color key="textColor" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="mks-jh-AiZ" secondAttribute="trailing" constant="8" id="1Fh-Wx-A41"/>
<constraint firstAttribute="bottom" secondItem="7qn-gi-w7s" secondAttribute="bottom" constant="5" id="KPt-Vo-ntg"/>
<constraint firstAttribute="bottom" secondItem="mks-jh-AiZ" secondAttribute="bottom" constant="5" id="QiX-ZA-N2e"/>
<constraint firstItem="alD-cg-uMl" firstAttribute="top" secondItem="5tf-BC-9Ed" secondAttribute="top" constant="14" id="M1S-HJ-o3b"/>
<constraint firstItem="mks-jh-AiZ" firstAttribute="top" secondItem="5tf-BC-9Ed" secondAttribute="top" constant="5" id="SSl-4u-03L"/>
<constraint firstItem="7qn-gi-w7s" firstAttribute="leading" secondItem="alD-cg-uMl" secondAttribute="trailing" constant="5" id="Vde-Q3-e8O"/>
<constraint firstItem="mks-jh-AiZ" firstAttribute="leading" secondItem="7qn-gi-w7s" secondAttribute="trailing" constant="3" id="cCA-xk-XBe"/>
<constraint firstItem="7qn-gi-w7s" firstAttribute="top" secondItem="5tf-BC-9Ed" secondAttribute="top" constant="5" id="owD-KZ-snG"/>
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="QiX-ZA-N2e"/>
</mask>
</variation>
</tableViewCellContentView>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<connections>
<outlet property="messageTextView" destination="7qn-gi-w7s" id="0b2-5P-dqR"/>
<outlet property="unsentLabel" destination="alD-cg-uMl" id="UXm-mh-sux"/>
<outlet property="userPicture" destination="mks-jh-AiZ" id="bxN-GB-VOx"/>
</connections>
</tableViewCell>

View File

@@ -23,6 +23,9 @@
#define ROOM_MESSAGE_CELL_BOTTOM_MARGIN 5
#define INCOMING_MESSAGE_CELL_USER_LABEL_HEIGHT 20
NSString *const kLocalEchoEventIdPrefix = @"localEcho-";
NSString *const kFailedEventId = @"failedEventId";
// Table view cell
@interface RoomMessageCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UIImageView *userPicture;
@@ -38,6 +41,7 @@
@end
@interface OutgoingMessageCell : RoomMessageCell
@property (weak, nonatomic) IBOutlet UILabel *unsentLabel;
@end
@implementation OutgoingMessageCell
@end
@@ -358,8 +362,9 @@
}
}
// Hide userName in incoming message if the previous message is from the same user
// Update incoming/outgoing message layout
if (isIncomingMsg) {
// Hide userName in incoming message if the previous message is from the same user
IncomingMessageCell* incomingMsgCell = (IncomingMessageCell*)cell;
CGRect frame = incomingMsgCell.userNameLabel.frame;
if (cell.userPicture.hidden) {
@@ -373,6 +378,20 @@
incomingMsgCell.userNameLabel.text = [NSString stringWithFormat:@"- %@", userName];
}
incomingMsgCell.userNameLabel.frame = frame;
} else {
// Hide unsent label by default
UILabel *unsentLabel = ((OutgoingMessageCell*)cell).unsentLabel;
unsentLabel.hidden = YES;
// Set the right text color for outgoing messages
if ([mxEvent.event_id hasPrefix:kLocalEchoEventIdPrefix]) {
cell.messageTextView.textColor = [UIColor lightGrayColor];
} else if ([mxEvent.event_id hasPrefix:kFailedEventId]) {
cell.messageTextView.textColor = [UIColor redColor];
unsentLabel.hidden = NO;
} else {
cell.messageTextView.textColor = [UIColor blackColor];
}
}
cell.messageTextView.text = [mxHandler displayTextFor:mxEvent inSubtitleMode:NO];
@@ -417,31 +436,57 @@
return YES;
}
#pragma mark -
#pragma mark - Actions
- (IBAction)onButtonPressed:(id)sender {
if (sender == _sendBtn) {
NSString *msgTxt = self.messageTextField.text;
// Create a temporary event to displayed outgoing message (local echo)
NSString *localEventId = [NSString stringWithFormat:@"%@%@", kLocalEchoEventIdPrefix, [[NSProcessInfo processInfo] globallyUniqueString]];
MXEvent *mxEvent = [[MXEvent alloc] init];
mxEvent.room_id = self.roomId;
mxEvent.event_id = localEventId;
mxEvent.eventType = MXEventTypeRoomMessage;
mxEvent.type = kMXEventTypeStringRoomMessage;
mxEvent.content = @{@"msgtype":@"m.text", @"body":msgTxt};
mxEvent.user_id = [MatrixHandler sharedHandler].userId;
// Update table sources
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:messages.count inSection:0];
[messages addObject:mxEvent];
// Refresh table display
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
[self scrollToBottomAnimated:YES];
// Send message to the room
[[[MatrixHandler sharedHandler] mxSession] postTextMessage:self.roomId text:msgTxt success:^(NSString *event_id) {
// Create a temporary event to displayed outgoing message
MXEvent *mxEvent = [[MXEvent alloc] init];
mxEvent.room_id = self.roomId;
mxEvent.event_id = event_id;
mxEvent.eventType = MXEventTypeRoomMessage;
mxEvent.type = kMXEventTypeStringRoomMessage;
mxEvent.content = @{@"msgtype":@"m.text", @"body":msgTxt};
mxEvent.user_id = [MatrixHandler sharedHandler].userId;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:messages.count inSection:0];
[messages addObject:mxEvent];
[self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
[self scrollToBottomAnimated:YES];
// Update the temporary event with the actual event id
NSUInteger index = messages.count;
while (index--) {
MXEvent *mxEvent = [messages objectAtIndex:index];
if ([mxEvent.event_id isEqualToString:localEventId]) {
mxEvent.event_id = event_id;
// Refresh table display
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
}
}
} failure:^(NSError *error) {
NSLog(@"Failed to send message (%@): %@", self.messageTextField.text, error);
//Alert user
[[AppDelegate theDelegate] showErrorAsAlert:error];
// Update the temporary event with the failed event id
NSUInteger index = messages.count;
while (index--) {
MXEvent *mxEvent = [messages objectAtIndex:index];
if ([mxEvent.event_id isEqualToString:localEventId]) {
mxEvent.event_id = kFailedEventId;
// Refresh table display
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
[self scrollToBottomAnimated:YES];
break;
}
}
}];
self.messageTextField.text = nil;