From 56672d604e89f9308085254a9d34cba23dafbd6f Mon Sep 17 00:00:00 2001 From: SBiOSoftWhare Date: Fri, 27 Jul 2018 18:45:23 +0200 Subject: [PATCH] [EventFormatter] Handle room create predecessor string building --- Riot/Utils/EventFormatter.m | 46 ++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/Riot/Utils/EventFormatter.m b/Riot/Utils/EventFormatter.m index 2015a9bbc..bfd7fdfd2 100644 --- a/Riot/Utils/EventFormatter.m +++ b/Riot/Utils/EventFormatter.m @@ -114,7 +114,12 @@ NSString *const kEventFormatterOnReRequestKeysLinkActionSeparator = @"/"; return [self renderString:displayText forEvent:event]; } } - + + if (event.eventType == MXEventTypeRoomCreate && roomState.hasRoomPredecessor) + { + return [self roomCreatePredecessorAttributedStringFromRoomCreateContent:roomState.roomCreateContent]; + } + NSAttributedString *attributedString = [super attributedStringFromEvent:event withRoomState:roomState error:error]; if (event.sentState == MXEventSentStateSent @@ -552,4 +557,43 @@ NSString *const kEventFormatterOnReRequestKeysLinkActionSeparator = @"/"; } } +#pragma mark - Room create predecessor + +- (NSAttributedString*)roomCreatePredecessorAttributedStringFromRoomCreateContent:(MXRoomCreateContent*)roomCreateContent +{ + if (!roomCreateContent) + { + return nil; + } + + NSString *predecessorRoomId = roomCreateContent.roomPredecessorInfo.roomId; + NSString *predecessorRoomPermalink = [MXTools permalinkToRoom:predecessorRoomId]; + + NSDictionary *roomPredecessorReasonAttributes = @{ + NSFontAttributeName : self.defaultTextFont + }; + + NSDictionary *roomLinkAttributes = @{ + NSFontAttributeName : self.defaultTextFont, + NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle), + NSLinkAttributeName : predecessorRoomPermalink, + }; + + NSMutableAttributedString *roomPredecessorAttributedString = [NSMutableAttributedString new]; + + NSString *roomPredecessorReasonString = [NSString stringWithFormat:@"%@\n", NSLocalizedStringFromTable(@"room_predecessor_information", @"Vector", nil)]; + NSAttributedString *roomPredecessorReasonAttributedString = [[NSAttributedString alloc] initWithString:roomPredecessorReasonString attributes:roomPredecessorReasonAttributes]; + + NSString *predecessorRoomLinkString = NSLocalizedStringFromTable(@"room_predecessor_link", @"Vector", nil); + NSAttributedString *predecessorRoomLinkAttributedString = [[NSAttributedString alloc] initWithString:predecessorRoomLinkString attributes:roomLinkAttributes]; + + [roomPredecessorAttributedString appendAttributedString:roomPredecessorReasonAttributedString]; + [roomPredecessorAttributedString appendAttributedString:predecessorRoomLinkAttributedString]; + + NSRange wholeStringRange = NSMakeRange(0, roomPredecessorAttributedString.length); + [roomPredecessorAttributedString addAttribute:NSForegroundColorAttributeName value:self.defaultTextColor range:wholeStringRange]; + + return roomPredecessorAttributedString; +} + @end