Skip to content

Commit

Permalink
move emoji formatter to a better location
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed May 30, 2024
1 parent 3209f8b commit 7bbd034
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
12 changes: 9 additions & 3 deletions TinodeSDKTests/DraftyTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
7 changes: 0 additions & 7 deletions Tinodios/format/AbstractFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down
17 changes: 17 additions & 0 deletions Tinodios/format/FullFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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? {
Expand Down
2 changes: 1 addition & 1 deletion prod.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7bbd034

Please sign in to comment.