Skip to content

Commit 7bbd034

Browse files
committed
move emoji formatter to a better location
1 parent 3209f8b commit 7bbd034

File tree

4 files changed

+27
-11
lines changed

4 files changed

+27
-11
lines changed

TinodeSDKTests/DraftyTest.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ class DraftyTest: XCTestCase {
111111
XCTAssertEqual(d1, d2, "String 13 - two lines with emoji in the first and style in the second")
112112

113113
// String 14: another compound Unicode test
114-
d1 = Drafty(content: "🔴Hello🔴\n🟠Hello🟠")
115-
d2 = Drafty(text: "🔴Hello🔴 🟠Hello🟠", fmt: [Style(tp: "BR", at: 7, len: 1)], ent: nil)
116-
XCTAssertEqual(d1, d2, "String 14 - two lines with compound emojis")
114+
d1 = Drafty(content: "🔴Hello🔴\n🟠Hello🟠\n🟡Hello🟡")
115+
d2 = Drafty(text: "🔴Hello🔴 🟠Hello🟠 🟡Hello🟡", fmt: [Style(tp: "BR", at: 7, len: 1), Style(tp: "BR", at: 15, len: 1)], ent: nil)
116+
XCTAssertEqual(d1, d2, "String 14 - three lines with compound emojis")
117117
}
118118

119119
func testShorten() {
@@ -588,6 +588,12 @@ class DraftyTest: XCTestCase {
588588
)
589589
XCTAssertEqual(expected, actual, "Reply 5 has failed");
590590

591+
// ------- Reply 6 (emoji + reply text)
592+
src = Drafty(
593+
content: "☝️"
594+
)
595+
// This is actually broken, it should be {"at":0,"len":14,"tp":"QQ"}
596+
// 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"}}}
591597
}
592598

593599
func testFormat() {

Tinodios/format/AbstractFormatter.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import TinodeSDK
1212
/// DraftyFormatter implementation to break out individual format handlers.
1313
/// Implemented as a class instead of a protocol because of this bug: https://bugs.swift.org/browse/SR-103
1414
class AbstractFormatter: DraftyFormatter {
15-
private static let emojiScaling = [1.26, 1.55, 1.93, 2.40, 3.00]
16-
1715
var defaultAttrs: [NSAttributedString.Key: Any]
1816

1917
init(defaultAttributes attrs: [NSAttributedString.Key: Any], defaultFont: UIFont) {
@@ -182,11 +180,6 @@ class AbstractFormatter: DraftyFormatter {
182180
/// - fitIn: maximum size of attached images.
183181
public func toAttributed(_ content: Drafty, fitIn maxSize: CGSize) -> NSAttributedString {
184182
if content.isPlain {
185-
// If message consists of 1-5 emoji only, make font larger.
186-
if content.string.isEmojiOnly && content.string.count <= AbstractFormatter.emojiScaling.count && !content.string.isEmpty {
187-
let defaultFont = self.defaultAttrs[.font]! as! UIFont
188-
self.defaultAttrs[.font] = defaultFont.withSize(defaultFont.pointSize * AbstractFormatter.emojiScaling[AbstractFormatter.emojiScaling.count - content.string.count])
189-
}
190183
return NSMutableAttributedString(string: content.string, attributes: self.defaultAttrs)
191184
}
192185

Tinodios/format/FullFormatter.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import UIKit
1515
class FullFormatter: AbstractFormatter {
1616
internal enum Constants {
1717
static let kDefaultFont: UIFont = UIFont.preferredFont(forTextStyle: .body)
18+
static let emojiScaling = [1.26, 1.55, 1.93, 2.40, 3.00]
1819
}
1920

2021
var quoteFormatter: QuoteFormatter?
@@ -219,6 +220,22 @@ class FullFormatter: AbstractFormatter {
219220
return node
220221
}
221222

223+
/// Convert drafty object into NSAttributedString
224+
/// - Parameters:
225+
/// - content: Drafty object to convert
226+
/// - fitIn: maximum size of attached images.
227+
override public func toAttributed(_ content: Drafty, fitIn maxSize: CGSize) -> NSAttributedString {
228+
if content.isPlain {
229+
var attrs = self.defaultAttrs
230+
// If a message consists of 1-5 emoji only, make font larger.
231+
if content.string.isEmojiOnly && content.string.count <= Constants.emojiScaling.count && !content.string.isEmpty, let defaultFont = self.defaultAttrs[.font] as? UIFont {
232+
attrs[.font] = defaultFont.withSize(defaultFont.pointSize * Constants.emojiScaling[Constants.emojiScaling.count - content.string.count])
233+
}
234+
return NSMutableAttributedString(string: content.string, attributes: attrs)
235+
}
236+
return super.toAttributed(content, fitIn: maxSize)
237+
}
238+
222239
// Convert button payload to an URL.
223240
// NSAttributedString.Key.link wants payload to be NSURL.
224241
internal static func buttonDataAsUri(face: FormatNode, attr: [String: JSONValue]?) -> String? {

prod.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ APP_NAME = Tinode
1515

1616
// Versioning. The values are automatically updated by a build script.
1717
GIT_TAG = 1.22.12
18-
GIT_COMMIT_COUNT = 1739
18+
GIT_COMMIT_COUNT = 1740

0 commit comments

Comments
 (0)