Add StatusContentLabel entity detection

This commit is contained in:
Shadowfacts 2018-08-27 12:40:22 -04:00
parent a025926c0f
commit e512afbd5c
3 changed files with 78 additions and 3 deletions

@ -1 +1 @@
Subproject commit 30aa01aa0ffb7cdd27030869d816cc0bc718ab3d Subproject commit b3653523af6b15fae5e3f8d425304c93d9bef9c7

View File

@ -7,10 +7,23 @@
// //
import UIKit import UIKit
import MastodonKit
import SwiftSoup import SwiftSoup
protocol StatusContentLabelDelegate {
func selected(mention: Mention)
func selected(tag: MastodonKit.Tag)
func selected(url: URL)
}
class StatusContentLabel: UILabel { class StatusContentLabel: UILabel {
var delegate: StatusContentLabelDelegate?
override var text: String? { override var text: String? {
didSet { didSet {
parseHTML() parseHTML()
@ -35,6 +48,12 @@ class StatusContentLabel: UILabel {
} }
} }
var status: Status! {
didSet {
text = status.content
}
}
private var _customizing = true private var _customizing = true
private lazy var textStorage = NSTextStorage() private lazy var textStorage = NSTextStorage()
@ -165,6 +184,35 @@ class StatusContentLabel: UILabel {
// MARK: - Interaction // MARK: - Interaction
private func getMention(for url: URL, text: String) -> Mention? {
return status.mentions.first(where: { mention -> Bool in
(text.dropFirst() == mention.username || text == mention.username) && url.host == URL(string: mention.url)!.host
})
}
private func getTag(for url: URL, text: String) -> MastodonKit.Tag? {
if let tag = status.tags.first(where: { tag -> Bool in
tag.url == url.absoluteString
}) {
return tag
} else if text.starts(with: "#") {
let tag = String(text.dropFirst())
return MastodonKit.Tag(name: tag, url: url.absoluteString)
} else {
return nil
}
}
private func getEntity(for url: URL, text: String) -> Any {
if let mention = getMention(for: url, text: text) {
return mention
} else if let tag = getTag(for: url, text: text) {
return tag
} else {
return url
}
}
private func onTouch(_ touch: UITouch) -> Bool { private func onTouch(_ touch: UITouch) -> Bool {
let location = touch.location(in: self) let location = touch.location(in: self)
var avoidSuperCall = false var avoidSuperCall = false
@ -185,7 +233,17 @@ class StatusContentLabel: UILabel {
case .ended: case .ended:
guard let selectedLink = selectedLink else { return avoidSuperCall } guard let selectedLink = selectedLink else { return avoidSuperCall }
print("tapped \(selectedLink)") let text = String(self.text![Range(selectedLink.range, in: self.text!)!])
// print(getEntity(for: selectedLink.url, text: text))
if let delegate = delegate {
if let mention = getMention(for: selectedLink.url, text: text) {
delegate.selected(mention: mention)
} else if let tag = getTag(for: selectedLink.url, text: text) {
delegate.selected(tag: tag)
} else {
delegate.selected(url: selectedLink.url)
}
}
let when = DispatchTime.now() + Double(Int64(0.25 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC) let when = DispatchTime.now() + Double(Int64(0.25 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)
DispatchQueue.main.asyncAfter(deadline: when) { DispatchQueue.main.asyncAfter(deadline: when) {

View File

@ -49,7 +49,8 @@ class StatusTableViewCell: UITableViewCell {
} }
} }
contentLabel.text = status.content contentLabel.status = status
contentLabel.delegate = self
} }
override func prepareForReuse() { override func prepareForReuse() {
@ -59,3 +60,19 @@ class StatusTableViewCell: UITableViewCell {
} }
} }
extension StatusTableViewCell: StatusContentLabelDelegate {
func selected(mention: Mention) {
print("selected mention: \(mention.acct)")
}
func selected(tag: MastodonKit.Tag) {
print("selected tag: \(tag.name)")
}
func selected(url: URL) {
print("selected url: \(url)")
}
}