NetNewsWire/Frameworks/Account/FeedProvider/Twitter/TwitterEntities.swift

71 lines
1.4 KiB
Swift
Raw Normal View History

2020-04-18 19:03:37 +02:00
//
// TwitterEntities.swift
// Account
//
// Created by Maurice Parker on 4/18/20.
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import Foundation
2020-04-20 02:19:39 +02:00
protocol TwitterEntity {
var indices: [Int]? { get }
func renderAsHTML() -> String
}
extension TwitterEntity {
2020-04-20 02:27:37 +02:00
var startIndex: Int {
if let indices = indices, indices.count > 0 {
return indices[0]
2020-04-20 02:19:39 +02:00
}
2020-04-20 02:27:37 +02:00
return 0
2020-04-20 02:19:39 +02:00
}
2020-04-20 02:27:37 +02:00
var endIndex: Int {
if let indices = indices, indices.count > 1 {
return indices[1]
2020-04-20 02:19:39 +02:00
}
2020-04-20 02:27:37 +02:00
return 0
2020-04-20 02:19:39 +02:00
}
}
2020-04-18 19:03:37 +02:00
struct TwitterEntities: Codable {
let hashtags: [TwitterHashtag]?
let urls: [TwitterURL]?
let userMentions: [TwitterMention]?
let symbols: [TwitterSymbol]?
let media: [TwitterMedia]?
2020-04-18 19:03:37 +02:00
enum CodingKeys: String, CodingKey {
case hashtags = "hashtags"
case urls = "urls"
case userMentions = "user_mentions"
case symbols = "symbols"
case media = "media"
2020-04-18 19:03:37 +02:00
}
2020-04-20 02:19:39 +02:00
func combineAndSort() -> [TwitterEntity] {
var entities = [TwitterEntity]()
if let hashtags = hashtags {
entities.append(contentsOf: hashtags)
}
if let urls = urls {
entities.append(contentsOf: urls)
}
if let userMentions = userMentions {
entities.append(contentsOf: userMentions)
}
if let symbols = symbols {
entities.append(contentsOf: symbols)
}
if let media = media {
entities.append(contentsOf: media)
}
2020-04-20 02:27:37 +02:00
return entities.sorted(by: { $0.startIndex < $1.startIndex })
2020-04-20 02:19:39 +02:00
}
2020-04-18 19:03:37 +02:00
}