From 7bbd03406c2558c27fdc2574ad4ca180eddf207d Mon Sep 17 00:00:00 2001 From: or-else Date: Thu, 30 May 2024 14:54:49 -0700 Subject: [PATCH] move emoji formatter to a better location --- TinodeSDKTests/DraftyTest.swift | 12 +++++++++--- Tinodios/format/AbstractFormatter.swift | 7 ------- Tinodios/format/FullFormatter.swift | 17 +++++++++++++++++ prod.xcconfig | 2 +- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/TinodeSDKTests/DraftyTest.swift b/TinodeSDKTests/DraftyTest.swift index 4957cb69..e9d4b7ed 100644 --- a/TinodeSDKTests/DraftyTest.swift +++ b/TinodeSDKTests/DraftyTest.swift @@ -111,9 +111,9 @@ class DraftyTest: XCTestCase { XCTAssertEqual(d1, d2, "String 13 - two lines with emoji in the first and style in the second") // String 14: another compound Unicode test - d1 = Drafty(content: "🔴Hello🔴\n🟠Hello🟠") - d2 = Drafty(text: "🔴Hello🔴 🟠Hello🟠", fmt: [Style(tp: "BR", at: 7, len: 1)], ent: nil) - XCTAssertEqual(d1, d2, "String 14 - two lines with compound emojis") + d1 = Drafty(content: "🔴Hello🔴\n🟠Hello🟠\n🟡Hello🟡") + d2 = Drafty(text: "🔴Hello🔴 🟠Hello🟠 🟡Hello🟡", fmt: [Style(tp: "BR", at: 7, len: 1), Style(tp: "BR", at: 15, len: 1)], ent: nil) + XCTAssertEqual(d1, d2, "String 14 - three lines with compound emojis") } func testShorten() { @@ -588,6 +588,12 @@ class DraftyTest: XCTestCase { ) XCTAssertEqual(expected, actual, "Reply 5 has failed"); + // ------- Reply 6 (emoji + reply text) + src = Drafty( + content: "☝️" + ) + // This is actually broken, it should be {"at":0,"len":14,"tp":"QQ"} + // expected = {"ent":[{"data":{"val":"usr1YxpmUGIjRk"},"tp":"MN"}],"fmt":[{"at":0,"key":0,"len":12},{"at":12,"len":1,"tp":"BR"},{"at":0,"len":15,"tp":"QQ"}],"txt":"Alice Hatter ☝️yes"}}} } func testFormat() { diff --git a/Tinodios/format/AbstractFormatter.swift b/Tinodios/format/AbstractFormatter.swift index 5841c8af..75ed94d6 100644 --- a/Tinodios/format/AbstractFormatter.swift +++ b/Tinodios/format/AbstractFormatter.swift @@ -12,8 +12,6 @@ import TinodeSDK /// DraftyFormatter implementation to break out individual format handlers. /// Implemented as a class instead of a protocol because of this bug: https://bugs.swift.org/browse/SR-103 class AbstractFormatter: DraftyFormatter { - private static let emojiScaling = [1.26, 1.55, 1.93, 2.40, 3.00] - var defaultAttrs: [NSAttributedString.Key: Any] init(defaultAttributes attrs: [NSAttributedString.Key: Any], defaultFont: UIFont) { @@ -182,11 +180,6 @@ class AbstractFormatter: DraftyFormatter { /// - fitIn: maximum size of attached images. public func toAttributed(_ content: Drafty, fitIn maxSize: CGSize) -> NSAttributedString { if content.isPlain { - // If message consists of 1-5 emoji only, make font larger. - if content.string.isEmojiOnly && content.string.count <= AbstractFormatter.emojiScaling.count && !content.string.isEmpty { - let defaultFont = self.defaultAttrs[.font]! as! UIFont - self.defaultAttrs[.font] = defaultFont.withSize(defaultFont.pointSize * AbstractFormatter.emojiScaling[AbstractFormatter.emojiScaling.count - content.string.count]) - } return NSMutableAttributedString(string: content.string, attributes: self.defaultAttrs) } diff --git a/Tinodios/format/FullFormatter.swift b/Tinodios/format/FullFormatter.swift index 0fb302ac..599bbabf 100644 --- a/Tinodios/format/FullFormatter.swift +++ b/Tinodios/format/FullFormatter.swift @@ -15,6 +15,7 @@ import UIKit class FullFormatter: AbstractFormatter { internal enum Constants { static let kDefaultFont: UIFont = UIFont.preferredFont(forTextStyle: .body) + static let emojiScaling = [1.26, 1.55, 1.93, 2.40, 3.00] } var quoteFormatter: QuoteFormatter? @@ -219,6 +220,22 @@ class FullFormatter: AbstractFormatter { return node } + /// Convert drafty object into NSAttributedString + /// - Parameters: + /// - content: Drafty object to convert + /// - fitIn: maximum size of attached images. + override public func toAttributed(_ content: Drafty, fitIn maxSize: CGSize) -> NSAttributedString { + if content.isPlain { + var attrs = self.defaultAttrs + // If a message consists of 1-5 emoji only, make font larger. + if content.string.isEmojiOnly && content.string.count <= Constants.emojiScaling.count && !content.string.isEmpty, let defaultFont = self.defaultAttrs[.font] as? UIFont { + attrs[.font] = defaultFont.withSize(defaultFont.pointSize * Constants.emojiScaling[Constants.emojiScaling.count - content.string.count]) + } + return NSMutableAttributedString(string: content.string, attributes: attrs) + } + return super.toAttributed(content, fitIn: maxSize) + } + // Convert button payload to an URL. // NSAttributedString.Key.link wants payload to be NSURL. internal static func buttonDataAsUri(face: FormatNode, attr: [String: JSONValue]?) -> String? { diff --git a/prod.xcconfig b/prod.xcconfig index 8a9b89bb..2eae2795 100644 --- a/prod.xcconfig +++ b/prod.xcconfig @@ -15,4 +15,4 @@ APP_NAME = Tinode // Versioning. The values are automatically updated by a build script. GIT_TAG = 1.22.12 -GIT_COMMIT_COUNT = 1739 +GIT_COMMIT_COUNT = 1740