Move file-related parameters to NCMessageFileParameters

Don't return a new object each time "file" is accessed

Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
This commit is contained in:
Marcel Müller 2020-12-05 15:27:19 +01:00
Родитель b626b91f32
Коммит 95946ae6dc
6 изменённых файлов: 106 добавлений и 33 удалений

Просмотреть файл

@ -24,6 +24,7 @@
#import <UIKit/UIKit.h>
#import <Realm/Realm.h>
#import "NCMessageParameter.h"
#import "NCMessageFileParameter.h"
extern NSInteger const kChatMessageGroupTimeDifference;
@ -53,7 +54,7 @@ extern NSInteger const kChatMessageGroupTimeDifference;
- (BOOL)isSystemMessage;
- (BOOL)isEmojiMessage;
- (NCMessageParameter *)file;
- (NCMessageFileParameter *)file;
- (NSDictionary *)messageParameters;
- (NSMutableAttributedString *)parsedMessage;
- (NSMutableAttributedString *)systemMessageFormat;

Просмотреть файл

@ -28,6 +28,13 @@
NSInteger const kChatMessageGroupTimeDifference = 30;
@interface NCChatMessage ()
{
NCMessageFileParameter *_fileParameter;
}
@end
@implementation NCChatMessage
+ (instancetype)messageWithDictionary:(NSDictionary *)messageDict
@ -124,20 +131,25 @@ NSInteger const kChatMessageGroupTimeDifference = 30;
- (NCMessageParameter *)file;
{
NCMessageParameter *fileParam = nil;
for (NSDictionary *parameterDict in [[self messageParameters] allValues]) {
NCMessageParameter *parameter = [NCMessageParameter parameterWithDictionary:parameterDict] ;
if ([parameter.type isEqualToString:@"file"]) {
if (!fileParam) {
fileParam = parameter;
if (!_fileParameter) {
for (NSDictionary *parameterDict in [[self messageParameters] allValues]) {
NCMessageFileParameter *parameter = [[NCMessageFileParameter alloc] initWithDictionary:parameterDict] ;
if (![parameter.type isEqualToString:@"file"]) {
continue;
}
if (!_fileParameter) {
_fileParameter = parameter;
} else {
// If there is more than one file in the message,
// we don't display any preview.
_fileParameter = nil;
return nil;
}
}
}
return fileParam;
return _fileParameter;
}
- (NSDictionary *)messageParameters
@ -181,7 +193,7 @@ NSInteger const kChatMessageGroupTimeDifference = 30;
stringByReplacingOccurrencesOfString:@"}" withString:@""];
NSDictionary *parameterDict = [[self messageParameters] objectForKey:parameterKey];
if (parameterDict) {
NCMessageParameter *messageParameter = [NCMessageParameter parameterWithDictionary:parameterDict] ;
NCMessageParameter *messageParameter = [[NCMessageParameter alloc] initWithDictionary:parameterDict] ;
// Default replacement string is the parameter name
NSString *replaceString = messageParameter.name;
// Format user and call mentions

Просмотреть файл

@ -0,0 +1,40 @@
/**
* @copyright Copyright (c) 2020 Marcel Müller <marcel-mueller@gmx.de>
*
* @author Marcel Müller <marcel-mueller@gmx.de>
*
* @license GNU GPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "NCMessageParameter.h"
NS_ASSUME_NONNULL_BEGIN
@interface NCMessageFileParameter : NCMessageParameter
@property (nonatomic, strong) NSString *path;
@property (nonatomic, strong) NSString *mimetype;
@property (nonatomic, assign) BOOL previewAvailable;
@property (nonatomic, assign) BOOL isDownloading;
@property (nonatomic, assign) CGFloat downloadProgress;
@end
NS_ASSUME_NONNULL_END

Просмотреть файл

@ -0,0 +1,24 @@
//
// NCMessageFileParameter.m
// NextcloudTalk
//
// Created by Marcel Müller on 05.12.20.
//
#import "NCMessageFileParameter.h"
@implementation NCMessageFileParameter
- (instancetype)initWithDictionary:(NSDictionary *)parameterDict
{
self = [super initWithDictionary:parameterDict];
if (self) {
self.path = [parameterDict objectForKey:@"path"];
self.mimetype = [parameterDict objectForKey:@"mimetype"];
self.previewAvailable = [[parameterDict objectForKey:@"preview-available"] boolValue];
}
return self;
}
@end

Просмотреть файл

@ -26,14 +26,11 @@
@property (nonatomic, strong) NSString *parameterId;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *path;
@property (nonatomic, strong) NSString *link;
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSString *mimetype;
@property (nonatomic, assign) BOOL previewAvailable;
@property (nonatomic, assign) NSRange range;
+ (instancetype)parameterWithDictionary:(NSDictionary *)parameterDict;
- (instancetype)initWithDictionary:(NSDictionary *)parameterDict;
- (BOOL)shouldBeHighlighted;
@end

Просмотреть файл

@ -26,29 +26,28 @@
@implementation NCMessageParameter
+ (instancetype)parameterWithDictionary:(NSDictionary *)parameterDict
- (instancetype)initWithDictionary:(NSDictionary *)parameterDict
{
if (!parameterDict || ![parameterDict isKindOfClass:[NSDictionary class]]) {
return nil;
self = [super init];
if (self) {
if (!parameterDict || ![parameterDict isKindOfClass:[NSDictionary class]]) {
return nil;
}
self.parameterId = [parameterDict objectForKey:@"id"];
self.name = [parameterDict objectForKey:@"name"];
self.link = [parameterDict objectForKey:@"link"];
self.type = [parameterDict objectForKey:@"type"];
id parameterId = [parameterDict objectForKey:@"id"];
if ([parameterId isKindOfClass:[NSString class]]) {
self.parameterId = parameterId;
} else {
self.parameterId = [parameterId stringValue];
}
}
NCMessageParameter *messageParameter = [[NCMessageParameter alloc] init];
messageParameter.parameterId = [parameterDict objectForKey:@"id"];
messageParameter.name = [parameterDict objectForKey:@"name"];
messageParameter.type = [parameterDict objectForKey:@"type"];
messageParameter.path = [parameterDict objectForKey:@"path"];
messageParameter.link = [parameterDict objectForKey:@"link"];
messageParameter.mimetype = [parameterDict objectForKey:@"mimetype"];
messageParameter.previewAvailable = [[parameterDict objectForKey:@"preview-available"] boolValue];
id parameterId = [parameterDict objectForKey:@"id"];
if ([parameterId isKindOfClass:[NSString class]]) {
messageParameter.parameterId = parameterId;
} else {
messageParameter.parameterId = [parameterId stringValue];
}
return messageParameter;
return self;
}
- (BOOL)shouldBeHighlighted