Files
NetNewsWire/Shared/Timeline/ArticleArray.swift
T

139 lines
3.6 KiB
Swift
Raw Normal View History

2017-11-01 20:45:38 -07:00
//
// ArticleArray.swift
2018-08-28 22:18:24 -07:00
// NetNewsWire
2017-11-01 20:45:38 -07:00
//
// Created by Brent Simmons on 11/1/17.
// Copyright © 2017 Ranchero Software. All rights reserved.
//
import Foundation
import Articles
2017-11-01 20:45:38 -07:00
2017-11-01 22:40:28 -07:00
typealias ArticleArray = [Article]
2025-11-13 17:11:34 -08:00
@MainActor extension Array where Element == Article {
2017-11-01 20:45:38 -07:00
func articleAtRow(_ row: Int) -> Article? {
if row < 0 || row == NSNotFound || row > count - 1 {
return nil
}
return self[row]
}
func article(matching articleSpecifier: ArticleSpecifier) -> Article? {
first { articleSpecifier.matchesArticle($0) }
}
func orderedRowIndexes(fromIndex startIndex: Int, wrappingToTop wrapping: Bool) -> [Int] {
if startIndex >= self.count {
// Wrap around to the top if specified
2026-01-02 15:58:36 -08:00
return wrapping ? [Int](0..<self.count) : []
} else {
// Start at the selection and wrap around to the beginning
2026-01-02 15:58:36 -08:00
return [Int](startIndex..<self.count) + (wrapping ? [Int](0..<startIndex) : [])
}
}
func rowOfNextUnreadArticle(_ selectedRow: Int, wrappingToTop wrapping: Bool) -> Int? {
2017-11-01 22:40:28 -07:00
if isEmpty {
return nil
}
for rowIndex in orderedRowIndexes(fromIndex: selectedRow + 1, wrappingToTop: wrapping) {
2017-11-01 22:40:28 -07:00
let article = articleAtRow(rowIndex)!
if !article.status.read {
return rowIndex
}
}
return nil
}
2025-05-09 20:30:04 -07:00
func articlesForIndexes(_ indexes: IndexSet) -> [Article] {
2025-12-31 11:38:56 -08:00
return indexes.compactMap { (oneIndex) -> Article? in
2017-11-01 22:40:28 -07:00
return articleAtRow(oneIndex)
2025-05-09 20:30:04 -07:00
}
2017-11-01 22:40:28 -07:00
}
2025-11-05 11:22:10 -08:00
func sortedByDate(_ sortDirection: ComparisonResult, groupByFeed: Bool = false) -> ArticleArray {
return ArticleSorter.sortedByDate(articles: self, sortDirection: sortDirection, groupByFeed: groupByFeed)
2017-11-01 22:40:28 -07:00
}
2025-11-05 11:22:10 -08:00
2026-09-18 21:47:51 -07:00
func sorted(by parameters: ArticleSortParameters) -> ArticleArray {
2026-09-19 10:03:22 -07:00
ArticleSorter.sorted(articles: self, parameters: parameters)
2026-09-18 21:47:51 -07:00
}
2017-11-01 22:40:28 -07:00
func canMarkAllAsRead() -> Bool {
2018-02-09 23:16:12 -08:00
return anyArticleIsUnread()
}
func anyArticlePassesTest(_ test: ((Article) -> Bool)) -> Bool {
2017-11-01 22:40:28 -07:00
for article in self {
2018-02-09 23:16:12 -08:00
if test(article) {
2017-11-01 22:40:28 -07:00
return true
}
}
return false
}
2018-02-09 23:16:12 -08:00
func anyArticleIsReadAndCanMarkUnread() -> Bool {
return anyArticlePassesTest { $0.status.read && $0.isAvailableToMarkUnread }
2018-02-09 23:16:12 -08:00
}
func anyArticleIsUnread() -> Bool {
return anyArticlePassesTest { !$0.status.read }
}
func anyArticleIsStarred() -> Bool {
return anyArticlePassesTest { $0.status.starred }
}
func anyArticleIsUnstarred() -> Bool {
return anyArticlePassesTest { !$0.status.starred }
}
func unreadArticles() -> [Article]? {
2026-01-01 16:53:37 -08:00
let articles = self.filter { !$0.status.read }
return articles.isEmpty ? nil : articles
}
func representSameArticlesInSameOrder(as otherArticles: [Article]) -> Bool {
if self.count != otherArticles.count {
return false
}
var i = 0
for article in self {
let otherArticle = otherArticles[i]
if article.account != otherArticle.account || article.articleID != otherArticle.articleID {
return false
}
i += 1
}
return true
}
func articlesAbove(article: Article) -> [Article] {
guard let position = firstIndex(of: article) else { return [] }
return articlesAbove(position: position)
}
2025-11-05 11:22:10 -08:00
func articlesAbove(position: Int) -> [Article] {
guard position < count else { return [] }
let articlesAbove = self[..<position]
return Array(articlesAbove)
}
func articlesBelow(article: Article) -> [Article] {
guard let position = firstIndex(of: article) else { return [] }
return articlesBelow(position: position)
}
2025-11-05 11:22:10 -08:00
func articlesBelow(position: Int) -> [Article] {
guard position < count else { return [] }
var articlesBelow = Array(self[position...])
guard !articlesBelow.isEmpty else {
return []
}
articlesBelow.removeFirst()
return articlesBelow
}
2017-11-01 22:40:28 -07:00
}