Skip to content

Commit

Permalink
element-hq/element-ios/issues/4899 - Added dynamism and compile time …
Browse files Browse the repository at this point in the history
…safety to room name and send reply event localizable strings.
  • Loading branch information
stefanceriu committed Sep 28, 2021
1 parent 3395010 commit a6a5186
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 223 deletions.
136 changes: 68 additions & 68 deletions MatrixSDK.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions MatrixSDK/Contrib/Swift/Data/MXRoom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ public extension MXRoom {
- eventToReply: The event to reply.
- textMessage: The text to send.
- formattedTextMessage: The optional HTML formatted string of the text to send.
- stringLocalizations: String localizations used when building reply message.
- stringLocalizer: String localizations used when building reply message.
- localEcho: a pointer to an MXEvent object.
When the event type is `MXEventType.roomMessage`, this pointer is set to an actual
Expand All @@ -738,8 +738,8 @@ public extension MXRoom {
- returns: a `MXHTTPOperation` instance.
*/
@nonobjc @discardableResult func sendReply(to eventToReply: MXEvent, textMessage: String, formattedTextMessage: String?, stringLocalizations: MXSendReplyEventStringsLocalizable?, localEcho: inout MXEvent?, completion: @escaping (_ response: MXResponse<String?>) -> Void) -> MXHTTPOperation {
return __sendReply(to: eventToReply, withTextMessage: textMessage, formattedTextMessage: formattedTextMessage, stringLocalizations: stringLocalizations, localEcho: &localEcho, success: currySuccess(completion), failure: curryFailure(completion))
@nonobjc @discardableResult func sendReply(to eventToReply: MXEvent, textMessage: String, formattedTextMessage: String?, stringLocalizer: MXSendReplyEventStringLocalizerProtocol?, localEcho: inout MXEvent?, completion: @escaping (_ response: MXResponse<String?>) -> Void) -> MXHTTPOperation {
return __sendReply(to: eventToReply, withTextMessage: textMessage, formattedTextMessage: formattedTextMessage, stringLocalizer: stringLocalizer, localEcho: &localEcho, success: currySuccess(completion), failure: curryFailure(completion))
}


Expand Down
6 changes: 3 additions & 3 deletions MatrixSDK/Data/MXRoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#import "MXEventTimeline.h"
#import "MXEventsEnumerator.h"
#import "MXCryptoConstants.h"
#import "MXSendReplyEventStringsLocalizable.h"
#import "MXSendReplyEventStringLocalizerProtocol.h"

@class MXRoom;
@class MXSession;
Expand Down Expand Up @@ -915,7 +915,7 @@ FOUNDATION_EXPORT NSInteger const kMXRoomAlreadyJoinedErrorCode;
@param eventToReply The event to reply.
@param textMessage the text to send.
@param formattedTextMessage the optional HTML formatted string of the text to send.
@param stringLocalizations string localizations used when building reply message.
@param stringLocalizer string localizations used when building reply message.
@param localEcho a pointer to a MXEvent object (@see sendMessageWithContent: for details).
@param success A block object called when the operation succeeds. It returns
the event id of the event generated on the home server
Expand All @@ -926,7 +926,7 @@ FOUNDATION_EXPORT NSInteger const kMXRoomAlreadyJoinedErrorCode;
- (MXHTTPOperation*)sendReplyToEvent:(MXEvent*)eventToReply
withTextMessage:(NSString*)textMessage
formattedTextMessage:(NSString*)formattedTextMessage
stringLocalizations:(id<MXSendReplyEventStringsLocalizable>)stringLocalizations
stringLocalizer:(id<MXSendReplyEventStringLocalizerProtocol>)stringLocalizer
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;
Expand Down
36 changes: 18 additions & 18 deletions MatrixSDK/Data/MXRoom.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

#import "MXMediaManager.h"
#import "MXRoomOperation.h"
#import "MXSendReplyEventDefaultStringLocalizations.h"
#import "MXSendReplyEventDefaultStringLocalizer.h"

#import "MXError.h"

Expand Down Expand Up @@ -1899,7 +1899,7 @@ - (MXHTTPOperation*)setRelatedGroups:(NSArray<NSString *>*)relatedGroups
- (MXHTTPOperation*)sendReplyToEvent:(MXEvent*)eventToReply
withTextMessage:(NSString*)textMessage
formattedTextMessage:(NSString*)formattedTextMessage
stringLocalizations:(id<MXSendReplyEventStringsLocalizable>)stringLocalizations
stringLocalizer:(id<MXSendReplyEventStringLocalizerProtocol>)stringLocalizer
localEcho:(MXEvent**)localEcho
success:(void (^)(NSString *eventId))success
failure:(void (^)(NSError *error))failure
Expand All @@ -1910,15 +1910,15 @@ - (MXHTTPOperation*)sendReplyToEvent:(MXEvent*)eventToReply
return nil;
}

id<MXSendReplyEventStringsLocalizable> finalStringLocalizations;
id<MXSendReplyEventStringLocalizerProtocol> finalStringLocalizer;

if (stringLocalizations)
if (stringLocalizer)
{
finalStringLocalizations = stringLocalizations;
finalStringLocalizer = stringLocalizer;
}
else
{
finalStringLocalizations = [MXSendReplyEventDefaultStringLocalizations new];
finalStringLocalizer = [MXSendReplyEventDefaultStringLocalizer new];
}

MXHTTPOperation* operation = nil;
Expand All @@ -1931,7 +1931,7 @@ - (MXHTTPOperation*)sendReplyToEvent:(MXEvent*)eventToReply
formattedTextMessage:formattedTextMessage
replyContentBody:&replyToBody
replyContentFormattedBody:&replyToFormattedBody
stringLocalizations:finalStringLocalizations];
stringLocalizer:finalStringLocalizer];

if (replyToBody && replyToFormattedBody)
{
Expand Down Expand Up @@ -1972,15 +1972,15 @@ - (MXHTTPOperation*)sendReplyToEvent:(MXEvent*)eventToReply
@param formattedTextMessage the optional HTML formatted string of the text to send.
@param replyContentBody reply string of the text to send.
@param replyContentFormattedBody reply HTML formatted string of the text to send.
@param stringLocalizations string localizations used when building reply content bodies.
@param stringLocalizer string localizations used when building reply content bodies.
*/
- (void)getReplyContentBodiesWithEventToReply:(MXEvent*)eventToReply
textMessage:(NSString*)textMessage
formattedTextMessage:(NSString*)formattedTextMessage
replyContentBody:(NSString**)replyContentBody
replyContentFormattedBody:(NSString**)replyContentFormattedBody
stringLocalizations:(id<MXSendReplyEventStringsLocalizable>)stringLocalizations
stringLocalizer:(id<MXSendReplyEventStringLocalizerProtocol>)stringLocalizer
{
NSString *msgtype;
MXJSONModelSetString(msgtype, eventToReply.content[@"msgtype"]);
Expand Down Expand Up @@ -2014,27 +2014,27 @@ - (void)getReplyContentBodiesWithEventToReply:(MXEvent*)eventToReply
}
else if ([msgtype isEqualToString:kMXMessageTypeImage])
{
senderMessageBody = stringLocalizations.senderSentAnImage;
senderMessageBody = stringLocalizer.senderSentAnImage;
senderMessageFormattedBody = senderMessageBody;
}
else if ([msgtype isEqualToString:kMXMessageTypeVideo])
{
senderMessageBody = stringLocalizations.senderSentAVideo;
senderMessageBody = stringLocalizer.senderSentAVideo;
senderMessageFormattedBody = senderMessageBody;
}
else if (eventToReply.isVoiceMessage)
{
senderMessageBody = stringLocalizations.senderSentAVoiceMessage;
senderMessageBody = stringLocalizer.senderSentAVoiceMessage;
senderMessageFormattedBody = senderMessageBody;
}
else if ([msgtype isEqualToString:kMXMessageTypeAudio])
{
senderMessageBody = stringLocalizations.senderSentAnAudioFile;
senderMessageBody = stringLocalizer.senderSentAnAudioFile;
senderMessageFormattedBody = senderMessageBody;
}
else if ([msgtype isEqualToString:kMXMessageTypeFile])
{
senderMessageBody = stringLocalizations.senderSentAFile;
senderMessageBody = stringLocalizer.senderSentAFile;
senderMessageFormattedBody = senderMessageBody;
}
else
Expand All @@ -2058,7 +2058,7 @@ - (void)getReplyContentBodiesWithEventToReply:(MXEvent*)eventToReply
senderMessageFormattedBody:senderMessageFormattedBody
isSenderMessageAnEmote:isSenderMessageAnEmote
replyFormattedMessage:finalFormattedTextMessage
stringLocalizations:stringLocalizations];
stringLocalizer:stringLocalizer];
}
}

Expand Down Expand Up @@ -2148,15 +2148,15 @@ - (NSString*)replyMessageBodyFromSender:(NSString*)sender
@param senderMessageFormattedBody The message body of the sender.
@param isSenderMessageAnEmote Indicate if the sender message is an emote (/me).
@param replyFormattedMessage The response for the sender message. HTML formatted string if any otherwise non formatted string as reply formatted body is mandatory.
@param stringLocalizations string localizations used when building formatted body.
@param stringLocalizer string localizations used when building formatted body.
@return reply message body.
*/
- (NSString*)replyMessageFormattedBodyFromEventToReply:(MXEvent*)eventToReply
senderMessageFormattedBody:(NSString*)senderMessageFormattedBody
isSenderMessageAnEmote:(BOOL)isSenderMessageAnEmote
replyFormattedMessage:(NSString*)replyFormattedMessage
stringLocalizations:(id<MXSendReplyEventStringsLocalizable>)stringLocalizations
stringLocalizer:(id<MXSendReplyEventStringLocalizerProtocol>)stringLocalizer
{
NSString *eventId = eventToReply.eventId;
NSString *roomId = eventToReply.roomId;
Expand Down Expand Up @@ -2202,7 +2202,7 @@ - (NSString*)replyMessageFormattedBodyFromEventToReply:(MXEvent*)eventToReply
[replyMessageFormattedBody appendString:@"<mx-reply><blockquote>"];

// Add event link
[replyMessageFormattedBody appendFormat:@"<a href=\"%@\">%@</a> ", eventPermalink, stringLocalizations.messageToReplyToPrefix];
[replyMessageFormattedBody appendFormat:@"<a href=\"%@\">%@</a> ", eventPermalink, stringLocalizer.messageToReplyToPrefix];

if (isSenderMessageAnEmote)
{
Expand Down
43 changes: 0 additions & 43 deletions MatrixSDK/Data/MXRoomNameDefaultStringLocalizations.m

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

@import Foundation;

#import "MXRoomNameStringsLocalizable.h"
#import "MXRoomNameStringLocalizerProtocol.h"

/**
The `MXRoomNameDefaultStringLocalizations` implements default English localization
strings for `MXRoomNameStringsLocalizable`.
The `MXRoomNameDefaultStringLocalizer` implements default English localization
strings for `MXRoomNameStringLocalizerProtocol`.
*/
@interface MXRoomNameDefaultStringLocalizations : NSObject<MXRoomNameStringsLocalizable>
@interface MXRoomNameDefaultStringLocalizer : NSObject<MXRoomNameStringLocalizerProtocol>

@end
41 changes: 41 additions & 0 deletions MatrixSDK/Data/MXRoomNameDefaultStringLocalizer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2018 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "MXRoomNameDefaultStringLocalizer.h"

@implementation MXRoomNameDefaultStringLocalizer

- (NSString *)emptyRoom
{
return @"Empty room";
}

- (NSString *)twoMembers:(NSString *)firstMember second:(NSString *)secondMember
{
return [NSString stringWithFormat:@"%@ and %@", firstMember, secondMember];
}

- (NSString *)moreThanTwoMembers:(NSString *)firstMember count:(NSNumber *)memberCount
{
return [NSString stringWithFormat:@"%@ & %@ others", firstMember, memberCount];
}

- (NSString *)allOtherMembersLeft:(NSString *)member
{
return [NSString stringWithFormat:@"%@ (Left)", member];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,24 @@

@import Foundation;

NS_ASSUME_NONNULL_BEGIN

/**
The `MXRoomNameStringsLocalizable` protocol defines an interface that must be implemented
The `MXRoomNameStringLocalizerProtocol` protocol defines an interface that must be implemented
to provide string localizations for computing room name according to Matrix
room summaries (https://github.com/matrix-org/matrix-doc/issues/688).
This interface is used by `MXRoomSummaryUpdater`.
*/
@protocol MXRoomNameStringsLocalizable <NSObject>
@protocol MXRoomNameStringLocalizerProtocol <NSObject>

- (NSString *)emptyRoom;

- (NSString *)twoMembers:(NSString *)firstMember second:(NSString *)secondMember;

- (NSString *)moreThanTwoMembers:(NSString *)firstMember count:(NSNumber *)memberCount;

@property (copy, readonly, nonnull) NSString *emptyRoom;
@property (copy, readonly, nonnull) NSString *twoMembers;
@property (copy, readonly, nonnull) NSString *moreThanTwoMembers;
@property (copy, readonly, nonnull) NSString *allOtherParticipantsLeft;
- (NSString *)allOtherMembersLeft:(NSString *)member;

@end

NS_ASSUME_NONNULL_END
6 changes: 3 additions & 3 deletions MatrixSDK/Data/MXRoomSummaryUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#import <Foundation/Foundation.h>

#import "MXRoomSummary.h"
#import "MXRoomNameStringsLocalizable.h"
#import "MXRoomNameStringLocalizerProtocol.h"

/**
`MXRoomSummaryUpdater` is the default implementation for the `MXRoomSummaryUpdating` protocol.
Expand Down Expand Up @@ -62,9 +62,9 @@
/**
String localizations used when computing names for room with no name.
Default is an instance of `MXRoomNameDefaultStringLocalizations`.
Default is an instance of `MXRoomNameDefaultStringLocalizer`.
*/
@property id<MXRoomNameStringsLocalizable> roomNameStringLocalizations;
@property id<MXRoomNameStringLocalizerProtocol> roomNameStringLocalizer;

/**
Indicate YES to handle room types with nil or empty value.
Expand Down
Loading

0 comments on commit a6a5186

Please sign in to comment.