Cache the displayable text for messages.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent ada4880dc3
commit bd7b7f3d11

@ -93,6 +93,18 @@ NS_ASSUME_NONNULL_BEGIN
return self; return self;
} }
+ (NSCache *)displayableTextCache
{
static NSCache *cache = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cache = [NSCache new];
// Cache the results for up to 1,000 messages.
cache.countLimit = 1000;
});
return cache;
}
+ (id<OWSMessageData>)messageViewDataWithInteraction:(TSInteraction *)interaction inThread:(TSThread *)thread contactsManager:(id<ContactsManagerProtocol>)contactsManager + (id<OWSMessageData>)messageViewDataWithInteraction:(TSInteraction *)interaction inThread:(TSThread *)thread contactsManager:(id<ContactsManagerProtocol>)contactsManager
{ {
TSMessageAdapter *adapter = [[TSMessageAdapter alloc] initWithInteraction:interaction]; TSMessageAdapter *adapter = [[TSMessageAdapter alloc] initWithInteraction:interaction];
@ -138,23 +150,30 @@ NS_ASSUME_NONNULL_BEGIN
if ([attachment isKindOfClass:[TSAttachmentStream class]]) { if ([attachment isKindOfClass:[TSAttachmentStream class]]) {
TSAttachmentStream *stream = (TSAttachmentStream *)attachment; TSAttachmentStream *stream = (TSAttachmentStream *)attachment;
if ([attachment.contentType isEqualToString:OWSMimeTypeOversizeTextMessage]) { if ([attachment.contentType isEqualToString:OWSMimeTypeOversizeTextMessage]) {
NSData *textData = [NSData dataWithContentsOfURL:stream.mediaURL]; NSString *displayableText = [[self displayableTextCache] objectForKey:interaction.uniqueId];
NSString *fullText = [[NSString alloc] initWithData:textData encoding:NSUTF8StringEncoding]; if (!displayableText) {
// Only show up to 2kb of text. NSData *textData = [NSData dataWithContentsOfURL:stream.mediaURL];
const NSUInteger kMaxTextDisplayLength = 2 * 1024; NSString *fullText = [[NSString alloc] initWithData:textData encoding:NSUTF8StringEncoding];
NSString *displayText = [[DisplayableTextFilter new] displayableText:fullText]; // Only show up to 2kb of text.
if (displayText.length > kMaxTextDisplayLength) { const NSUInteger kMaxTextDisplayLength = 2 * 1024;
// Trim whitespace before _AND_ after slicing the snipper from the string. displayableText = [[DisplayableTextFilter new] displayableText:fullText];
NSString *snippet = if (displayableText.length > kMaxTextDisplayLength) {
[[[displayText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] // Trim whitespace before _AND_ after slicing the snipper from the string.
NSString *snippet = [[[displayableText
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
substringWithRange:NSMakeRange(0, kMaxTextDisplayLength)] substringWithRange:NSMakeRange(0, kMaxTextDisplayLength)]
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
displayText = displayableText =
[NSString stringWithFormat:NSLocalizedString(@"OVERSIZE_TEXT_DISPLAY_FORMAT", [NSString stringWithFormat:NSLocalizedString(@"OVERSIZE_TEXT_DISPLAY_FORMAT",
@"A display format for oversize text messages."), @"A display format for oversize text messages."),
snippet]; snippet];
}
if (!displayableText) {
displayableText = @"";
}
[[self displayableTextCache] setObject:displayableText forKey:interaction.uniqueId];
} }
adapter.messageBody = displayText; adapter.messageBody = displayableText;
} else if ([stream isAnimated]) { } else if ([stream isAnimated]) {
adapter.mediaItem = adapter.mediaItem =
[[TSAnimatedAdapter alloc] initWithAttachment:stream incoming:isIncomingAttachment]; [[TSAnimatedAdapter alloc] initWithAttachment:stream incoming:isIncomingAttachment];
@ -188,6 +207,16 @@ NS_ASSUME_NONNULL_BEGIN
NSStringFromClass([attachment class])); NSStringFromClass([attachment class]));
} }
} }
} else {
NSString *displayableText = [[self displayableTextCache] objectForKey:interaction.uniqueId];
if (!displayableText) {
displayableText = [[DisplayableTextFilter new] displayableText:message.body];
if (!displayableText) {
displayableText = @"";
}
[[self displayableTextCache] setObject:displayableText forKey:interaction.uniqueId];
}
adapter.messageBody = displayableText;
} }
} else if ([interaction isKindOfClass:[TSCall class]]) { } else if ([interaction isKindOfClass:[TSCall class]]) {
TSCall *callRecord = (TSCall *)interaction; TSCall *callRecord = (TSCall *)interaction;

@ -47,6 +47,7 @@ NSString *const kTSStorageManager_AccountLastNames = @"kTSStorageManager_Account
return self; return self;
} }
// TODO: We need to configure the limits of this cache.
_avatarCache = [NSCache new]; _avatarCache = [NSCache new];
_allContacts = @[]; _allContacts = @[];
_signalAccountMap = @{}; _signalAccountMap = @{};

Loading…
Cancel
Save