2017-05-27 19:43:27 +02:00
|
|
|
//
|
|
|
|
// TimelineCellData.swift
|
|
|
|
// Evergreen
|
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 2/6/16.
|
|
|
|
// Copyright © 2016 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-02-03 07:51:32 +01:00
|
|
|
import AppKit
|
2017-09-17 21:22:15 +02:00
|
|
|
import Data
|
2017-05-27 19:43:27 +02:00
|
|
|
|
|
|
|
var attributedTitleCache = [String: NSAttributedString]()
|
|
|
|
var attributedDateCache = [String: NSAttributedString]()
|
|
|
|
var attributedFeedNameCache = [String: NSAttributedString]()
|
|
|
|
|
|
|
|
struct TimelineCellData {
|
|
|
|
|
|
|
|
let title: String
|
|
|
|
let text: String
|
|
|
|
let attributedTitle: NSAttributedString //title + text
|
|
|
|
let dateString: String
|
|
|
|
let attributedDateString: NSAttributedString
|
|
|
|
let feedName: String
|
|
|
|
let attributedFeedName: NSAttributedString
|
|
|
|
let showFeedName: Bool
|
2018-01-05 06:20:09 +01:00
|
|
|
let avatar: NSImage? // feed icon, user avatar, or favicon
|
|
|
|
let showAvatar: Bool // Make space even when avatar is nil
|
2017-11-26 06:27:35 +01:00
|
|
|
let featuredImage: NSImage? // image from within the article
|
2017-05-27 19:43:27 +02:00
|
|
|
let read: Bool
|
2018-02-18 07:23:36 +01:00
|
|
|
let starred: Bool
|
2017-05-27 19:43:27 +02:00
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
init(article: Article, appearance: TimelineCellAppearance, showFeedName: Bool, feedName: String?, avatar: NSImage?, showAvatar: Bool, featuredImage: NSImage?) {
|
2018-02-19 01:13:58 +01:00
|
|
|
|
2017-05-27 19:43:27 +02:00
|
|
|
self.title = timelineTruncatedTitle(article)
|
|
|
|
self.text = timelineTruncatedSummary(article)
|
|
|
|
|
|
|
|
let attributedTitleCacheKey = "_title: " + self.title + "_text: " + self.text
|
|
|
|
if let s = attributedTitleCache[attributedTitleCacheKey] {
|
|
|
|
self.attributedTitle = s
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.attributedTitle = attributedTitleString(title, text, appearance)
|
|
|
|
attributedTitleCache[attributedTitleCacheKey] = self.attributedTitle
|
|
|
|
}
|
|
|
|
|
|
|
|
self.dateString = timelineDateString(article.logicalDatePublished)
|
|
|
|
if let s = attributedDateCache[self.dateString] {
|
|
|
|
self.attributedDateString = s
|
|
|
|
}
|
|
|
|
else {
|
2017-09-18 02:03:58 +02:00
|
|
|
self.attributedDateString = NSAttributedString(string: self.dateString, attributes: [NSAttributedStringKey.foregroundColor: appearance.dateColor, NSAttributedStringKey.font: appearance.dateFont])
|
2017-05-27 19:43:27 +02:00
|
|
|
attributedDateCache[self.dateString] = self.attributedDateString
|
|
|
|
}
|
|
|
|
|
2017-12-30 21:45:10 +01:00
|
|
|
if let feedName = feedName {
|
|
|
|
self.feedName = timelineTruncatedFeedName(feedName)
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.feedName = ""
|
|
|
|
}
|
2017-12-30 21:45:10 +01:00
|
|
|
if let s = attributedFeedNameCache[self.feedName] {
|
2017-05-27 19:43:27 +02:00
|
|
|
self.attributedFeedName = s
|
|
|
|
}
|
|
|
|
else {
|
2017-09-18 02:03:58 +02:00
|
|
|
self.attributedFeedName = NSAttributedString(string: self.feedName, attributes: [NSAttributedStringKey.foregroundColor: appearance.feedNameColor, NSAttributedStringKey.font: appearance.feedNameFont])
|
2017-05-27 19:43:27 +02:00
|
|
|
attributedFeedNameCache[self.feedName] = self.attributedFeedName
|
|
|
|
}
|
|
|
|
|
|
|
|
self.showFeedName = showFeedName
|
|
|
|
|
2018-01-05 06:20:09 +01:00
|
|
|
self.showAvatar = showAvatar
|
2017-11-26 06:27:35 +01:00
|
|
|
self.avatar = avatar
|
|
|
|
self.featuredImage = featuredImage
|
|
|
|
|
2017-09-24 21:24:44 +02:00
|
|
|
self.read = article.status.read
|
2018-02-18 07:23:36 +01:00
|
|
|
self.starred = article.status.starred
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
init() { //Empty
|
|
|
|
|
|
|
|
self.title = ""
|
|
|
|
self.attributedTitle = NSAttributedString(string: "")
|
|
|
|
self.text = ""
|
|
|
|
self.dateString = ""
|
|
|
|
self.attributedDateString = NSAttributedString(string: "")
|
|
|
|
self.feedName = ""
|
|
|
|
self.attributedFeedName = NSAttributedString(string: "")
|
|
|
|
self.showFeedName = false
|
2018-01-05 06:20:09 +01:00
|
|
|
self.showAvatar = false
|
2017-11-26 06:27:35 +01:00
|
|
|
self.avatar = nil
|
|
|
|
self.featuredImage = nil
|
2017-05-27 19:43:27 +02:00
|
|
|
self.read = true
|
2018-02-18 07:23:36 +01:00
|
|
|
self.starred = false
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static func emptyCache() {
|
|
|
|
|
|
|
|
attributedTitleCache = [String: NSAttributedString]()
|
|
|
|
attributedDateCache = [String: NSAttributedString]()
|
|
|
|
attributedFeedNameCache = [String: NSAttributedString]()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let emptyCellData = TimelineCellData()
|
|
|
|
|
|
|
|
private func attributedTitleString(_ title: String, _ text: String, _ appearance: TimelineCellAppearance) -> NSAttributedString {
|
|
|
|
|
|
|
|
if !title.isEmpty && !text.isEmpty {
|
|
|
|
|
2017-09-18 01:30:45 +02:00
|
|
|
let titleMutable = NSMutableAttributedString(string: title, attributes: [NSAttributedStringKey.foregroundColor: appearance.titleColor, NSAttributedStringKey.font: appearance.titleFont])
|
|
|
|
let attributedText = NSAttributedString(string: "\n" + text, attributes: [NSAttributedStringKey.foregroundColor: appearance.textColor, NSAttributedStringKey.font: appearance.textFont])
|
2017-05-27 19:43:27 +02:00
|
|
|
titleMutable.append(attributedText)
|
|
|
|
return titleMutable
|
|
|
|
}
|
|
|
|
|
|
|
|
if !title.isEmpty && text.isEmpty {
|
2017-09-18 01:30:45 +02:00
|
|
|
return NSAttributedString(string: title, attributes: [NSAttributedStringKey.foregroundColor: appearance.titleColor, NSAttributedStringKey.font: appearance.titleFont])
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|
2018-02-19 05:28:31 +01:00
|
|
|
return NSAttributedString(string: text, attributes: [NSAttributedStringKey.foregroundColor: appearance.textOnlyColor, NSAttributedStringKey.font: appearance.textOnlyFont])
|
2017-05-27 19:43:27 +02:00
|
|
|
}
|
|
|
|
|