chore: code cleanup

This commit is contained in:
CMK 2021-03-17 18:17:44 +08:00
parent 5ecce85bfd
commit 556964373e
1 changed files with 20 additions and 38 deletions

View File

@ -281,32 +281,12 @@ extension ComposeViewController: TextEditorViewTextAttributesDelegate {
attributedString.addAttribute(.foregroundColor, value: Asset.Colors.Label.primary.color, range: stringRange) attributedString.addAttribute(.foregroundColor, value: Asset.Colors.Label.primary.color, range: stringRange)
attributedString.addAttribute(.font, value: UIFont.preferredFont(forTextStyle: .body), range: stringRange) attributedString.addAttribute(.font, value: UIFont.preferredFont(forTextStyle: .body), range: stringRange)
// hashtag
for match in highlightMatches { for match in highlightMatches {
// hashtag
if let name = string.substring(with: match, at: 2) {
let attachment: TextAttributes.SuffixedAttachment?
switch name {
// FIXME:
case "person":
attachment = .init(size: CGSize(width: 20.0, height: 20.0),
attachment: .image(UIImage(systemName: "person")!))
default:
attachment = nil
}
if let attachment = attachment {
let index = match.range.upperBound - 1
attributedString.addAttribute(
.suffixedAttachment,
value: attachment,
range: NSRange(location: index, length: 1)
)
}
}
// set highlight // set highlight
var attributes = [NSAttributedString.Key: Any]() var attributes = [NSAttributedString.Key: Any]()
attributes[.foregroundColor] = Asset.Colors.Label.highlight.color attributes[.foregroundColor] = Asset.Colors.Label.highlight.color
// See `traitCollectionDidChange(_:)` // See `traitCollectionDidChange(_:)`
// set accessibility // set accessibility
if #available(iOS 13.0, *) { if #available(iOS 13.0, *) {
@ -320,6 +300,7 @@ extension ComposeViewController: TextEditorViewTextAttributesDelegate {
attributedString.addAttributes(attributes, range: match.range) attributedString.addAttributes(attributes, range: match.range)
} }
// emoji
let emojis = customEmojiViewModel?.emojis.value ?? [] let emojis = customEmojiViewModel?.emojis.value ?? []
if !emojis.isEmpty { if !emojis.isEmpty {
for match in emojiMatches { for match in emojiMatches {
@ -367,25 +348,26 @@ extension ComposeViewController: TextEditorViewTextAttributesDelegate {
} }
} }
// url
for match in urlMatches { for match in urlMatches {
if let name = string.substring(with: match, at: 0) { guard let name = string.substring(with: match, at: 0) else { continue }
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: handle emoji: %s", ((#file as NSString).lastPathComponent), #line, #function, name) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: handle emoji: %s", ((#file as NSString).lastPathComponent), #line, #function, name)
// set highlight // set highlight
var attributes = [NSAttributedString.Key: Any]() var attributes = [NSAttributedString.Key: Any]()
attributes[.foregroundColor] = Asset.Colors.Label.highlight.color attributes[.foregroundColor] = Asset.Colors.Label.highlight.color
// See `traitCollectionDidChange(_:)`
// set accessibility // See `traitCollectionDidChange(_:)`
if #available(iOS 13.0, *) { // set accessibility
switch self.traitCollection.accessibilityContrast { if #available(iOS 13.0, *) {
case .high: switch self.traitCollection.accessibilityContrast {
attributes[.underlineStyle] = NSUnderlineStyle.single.rawValue case .high:
default: attributes[.underlineStyle] = NSUnderlineStyle.single.rawValue
break default:
} break
} }
attributedString.addAttributes(attributes, range: match.range)
} }
attributedString.addAttributes(attributes, range: match.range)
} }
completion(attributedString) completion(attributedString)