2020-06-30 18:03:33 +02:00
|
|
|
//
|
|
|
|
// TimelineItem.swift
|
|
|
|
// NetNewsWire
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 6/30/20.
|
|
|
|
// Copyright © 2020 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import Articles
|
|
|
|
|
2020-07-01 23:33:07 +02:00
|
|
|
enum TimelineItemStatus {
|
|
|
|
case showStar
|
|
|
|
case showUnread
|
|
|
|
case showNone
|
|
|
|
}
|
|
|
|
|
2020-06-30 18:03:33 +02:00
|
|
|
struct TimelineItem: Identifiable {
|
|
|
|
|
2020-07-18 03:55:18 +02:00
|
|
|
var id: String
|
2020-07-25 20:53:46 +02:00
|
|
|
var position: Int
|
2020-07-01 18:13:11 +02:00
|
|
|
var article: Article
|
2020-07-18 03:55:18 +02:00
|
|
|
|
|
|
|
var status: TimelineItemStatus = .showNone
|
2020-07-17 02:36:20 +02:00
|
|
|
var truncatedTitle: String
|
|
|
|
var truncatedSummary: String
|
2020-07-18 03:55:18 +02:00
|
|
|
var byline: String
|
|
|
|
var dateTimeString: String
|
2020-06-30 18:03:33 +02:00
|
|
|
|
2020-07-25 20:53:46 +02:00
|
|
|
init(position: Int, article: Article) {
|
2020-07-18 03:55:18 +02:00
|
|
|
self.id = article.articleID
|
2020-07-25 20:53:46 +02:00
|
|
|
self.position = position
|
2020-07-10 03:10:52 +02:00
|
|
|
self.article = article
|
2020-07-18 03:55:18 +02:00
|
|
|
self.byline = article.webFeed?.nameForDisplay ?? ""
|
|
|
|
self.dateTimeString = ArticleStringFormatter.dateString(article.logicalDatePublished)
|
2020-07-17 02:36:20 +02:00
|
|
|
self.truncatedTitle = ArticleStringFormatter.truncatedTitle(article)
|
|
|
|
self.truncatedSummary = ArticleStringFormatter.truncatedSummary(article)
|
2020-07-10 03:10:52 +02:00
|
|
|
updateStatus()
|
|
|
|
}
|
|
|
|
|
2020-07-25 20:53:46 +02:00
|
|
|
var isReadOnly: Bool {
|
|
|
|
return article.status.read == true && article.status.starred == false
|
|
|
|
}
|
|
|
|
|
2020-07-10 03:10:52 +02:00
|
|
|
mutating func updateStatus() {
|
|
|
|
if article.status.starred == true {
|
|
|
|
status = .showStar
|
|
|
|
} else {
|
|
|
|
if article.status.read == false {
|
|
|
|
status = .showUnread
|
|
|
|
} else {
|
|
|
|
status = .showNone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 02:36:20 +02:00
|
|
|
func numberOfTitleLines(width: CGFloat) -> Int {
|
|
|
|
guard !truncatedTitle.isEmpty else { return 0 }
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
let descriptor = NSFont.preferredFont(forTextStyle: .body).fontDescriptor.withSymbolicTraits(.bold)
|
|
|
|
guard let font = NSFont(descriptor: descriptor, size: 0) else { return 0 }
|
|
|
|
#else
|
|
|
|
guard let descriptor = UIFont.preferredFont(forTextStyle: .body).fontDescriptor.withSymbolicTraits(.traitBold) else { return 0 }
|
|
|
|
let font = UIFont(descriptor: descriptor, size: 0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
let lines = Int(AppDefaults.shared.timelineNumberOfLines)
|
|
|
|
let sizeInfo = TimelineTextSizer.size(for: truncatedTitle, font: font, numberOfLines: lines, width: adjustedWidth(width))
|
|
|
|
return sizeInfo.numberOfLinesUsed
|
|
|
|
}
|
|
|
|
|
|
|
|
func numberOfSummaryLines(width: CGFloat, titleLines: Int) -> Int {
|
|
|
|
guard !truncatedSummary.isEmpty else { return 0 }
|
|
|
|
|
|
|
|
let remainingLines = Int(AppDefaults.shared.timelineNumberOfLines) - titleLines
|
|
|
|
guard remainingLines > 0 else { return 0 }
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
let font = NSFont.preferredFont(forTextStyle: .body)
|
|
|
|
#else
|
|
|
|
let font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
let sizeInfo = TimelineTextSizer.size(for: truncatedSummary, font: font, numberOfLines: remainingLines, width: adjustedWidth(width))
|
|
|
|
return sizeInfo.numberOfLinesUsed
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension TimelineItem {
|
|
|
|
|
|
|
|
// This clearly isn't correct yet, but it gets us close enough for now. -Maurice
|
|
|
|
func adjustedWidth(_ width: CGFloat) -> Int {
|
|
|
|
return Int(width - CGFloat(AppDefaults.shared.timelineIconDimensions + 64))
|
|
|
|
}
|
|
|
|
|
2020-06-30 18:03:33 +02:00
|
|
|
}
|