/* Copyright 2016 OpenMarket Ltd Copyright (c) 2021 BWI GmbH 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 "Tools.h" #import "GeneratedInterface-Swift.h" #import "MXKSwiftHeader.h" #define MAX_BWI_IMAGE_SIZE 1600 @implementation Tools + (NSString *)presenceText:(MXUser *)user { NSString* presenceText = [VectorL10n roomParticipantsUnknown]; if (user) { switch (user.presence) { case MXPresenceOnline: presenceText = [VectorL10n roomParticipantsOnline]; break; case MXPresenceUnavailable: presenceText = [VectorL10n roomParticipantsIdle]; break; case MXPresenceUnknown: // Fix https://github.com/vector-im/element-ios/issues/6597 // Return nil because we don't want to display anything if the status is unknown return nil; case MXPresenceOffline: presenceText = [VectorL10n roomParticipantsOffline]; break; default: break; } if (user.currentlyActive) { presenceText = [presenceText stringByAppendingString:[NSString stringWithFormat:@" %@",[VectorL10n roomParticipantsNow]]]; } else if (-1 != user.lastActiveAgo && 0 < user.lastActiveAgo) { presenceText = [presenceText stringByAppendingString:[NSString stringWithFormat:@" %@ %@", [MXKTools formatSecondsIntervalFloored:(user.lastActiveAgo / 1000)], [VectorL10n roomParticipantsAgo]]]; } } return presenceText; } #pragma mark - Universal link + (BOOL)isUniversalLink:(NSURL*)url { BOOL isUniversalLink = NO; for (NSString *matrixPermalinkHost in BWIBuildSettings.shared.permalinkSupportedHosts) { if ([url.host isEqualToString:matrixPermalinkHost]) { NSArray *hostPaths = BWIBuildSettings.shared.permalinkSupportedHosts[matrixPermalinkHost]; if (hostPaths.count) { // iOS Patch: fix urls before using it NSURL *fixedURL = [Tools fixURLWithSeveralHashKeys:url]; if (NSNotFound != [hostPaths indexOfObject:fixedURL.path]) { isUniversalLink = YES; break; } } else { isUniversalLink = YES; break; } } } if ([url.host isEqualToString:[ServerURLHelper.shared permalink]]) { isUniversalLink = YES; } return isUniversalLink; } + (NSURL *)fixURLWithSeveralHashKeys:(NSURL *)url { NSURL *fixedURL = url; // The NSURL may have no fragment because it contains more that '%23' occurence if (!url.fragment) { // Replacing the first '%23' occurence into a '#' makes NSURL works correctly NSString *urlString = url.absoluteString; NSRange range = [urlString rangeOfString:@"%23"]; if (NSNotFound != range.location) { urlString = [urlString stringByReplacingCharactersInRange:range withString:@"#"]; fixedURL = [NSURL URLWithString:urlString]; } } return fixedURL; } #pragma mark - String utilities + (NSAttributedString *)setTextColorAlpha:(CGFloat)alpha inAttributedString:(NSAttributedString*)attributedString { NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString]; // Check all attributes one by one [string enumerateAttributesInRange:NSMakeRange(0, attributedString.length) options:0 usingBlock:^(NSDictionary *attrs, NSRange range, BOOL *stop) { // Replace only colored texts if (attrs[NSForegroundColorAttributeName]) { UIColor *color = attrs[NSForegroundColorAttributeName]; color = [color colorWithAlphaComponent:alpha]; NSMutableDictionary *newAttrs = [NSMutableDictionary dictionaryWithDictionary:attrs]; newAttrs[NSForegroundColorAttributeName] = color; [string setAttributes:newAttrs range:range]; } }]; return string; } #pragma mark - NSData utilities + (NSData*)mediaConvertMaxImageData:(NSData*) imageData withUTI:(MXKUTI *)uti { NSData *convertedData; /** * Reduce the image size to MAX_BWI_IMAGE_SIZE to be conform * with the BWI back-end server system. */ CGSize newImageSize = CGSizeMake(MAX_BWI_IMAGE_SIZE, MAX_BWI_IMAGE_SIZE); UIImage* convertedImage = [MXKTools resizeImageWithData:imageData toFitInSize:newImageSize]; if (convertedImage) { if ([uti.mimeType isEqualToString:@"image/png"]) { convertedData = UIImagePNGRepresentation(convertedImage); } else if ([uti.mimeType isEqualToString:@"image/jpeg"]) { convertedData = UIImageJPEGRepresentation(convertedImage, 0.9); } else { // Return the original image data pointer return imageData; } } return convertedData; } @end