mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-11-28 00:04:25 +00:00
Add Dinosaurs window.
This commit is contained in:
1 parent
2cdd6d89ba
commit
eb5bfd6ca5
15 files changed
+459
-2
No files matched your search
@@ -94,6 +94,7 @@ let appName = "NetNewsWire"
|
||||
private var activityWindowController: ActivityWindowController?
|
||||
private var activityLogWindowController: ActivityLogWindowController?
|
||||
private var errorLogWindowController: ErrorLogWindowController?
|
||||
private var dinosaurWindowController: DinosaurWindowController?
|
||||
private var crashReportWindowController: CrashReportWindowController? // For testing only
|
||||
private var cloudKitStatsWindowController: CloudKitStatsWindowController?
|
||||
private var accountStatsWindowController: AccountStatsWindowController?
|
||||
@@ -582,6 +583,13 @@ let appName = "NetNewsWire"
|
||||
errorLogWindowController!.showWindow(self)
|
||||
}
|
||||
|
||||
@IBAction func showDinosaursWindow(_ sender: Any?) {
|
||||
if dinosaurWindowController == nil {
|
||||
dinosaurWindowController = DinosaurWindowController()
|
||||
}
|
||||
dinosaurWindowController!.showWindow(self)
|
||||
}
|
||||
|
||||
@IBAction func showActivityWindow(_ sender: Any?) {
|
||||
if activityWindowController == nil {
|
||||
activityWindowController = ActivityWindowController()
|
||||
|
||||
@@ -600,6 +600,12 @@
|
||||
<action selector="showErrorLog:" target="Ady-hI-5gd" id="erl-1h-Lx8"/>
|
||||
</connections>
|
||||
</menuItem>
|
||||
<menuItem title="Dinosaurs" id="din-0s-aUR">
|
||||
<modifierMask key="keyEquivalentModifierMask"/>
|
||||
<connections>
|
||||
<action selector="showDinosaursWindow:" target="Ady-hI-5gd" id="din-1w-rCn"/>
|
||||
</connections>
|
||||
</menuItem>
|
||||
<menuItem title="iCloud Storage Stats" id="dLy-xR-u6N">
|
||||
<modifierMask key="keyEquivalentModifierMask"/>
|
||||
<connections>
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// DinosaurRow.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Brent Simmons on 5/31/26.
|
||||
//
|
||||
|
||||
import Account
|
||||
|
||||
struct DinosaurRow: Identifiable {
|
||||
|
||||
let id: String
|
||||
let feed: Feed
|
||||
let account: Account
|
||||
let accountName: String
|
||||
let feedName: String
|
||||
let feedURL: String
|
||||
let lastArticleDate: Date?
|
||||
let lastResponseCode: Int?
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// DinosaurWindowController.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Brent Simmons on 5/31/26.
|
||||
//
|
||||
|
||||
@preconcurrency import AppKit
|
||||
import SwiftUI
|
||||
|
||||
final class DinosaurWindowController: NSWindowController {
|
||||
|
||||
init() {
|
||||
let window = NSWindow(
|
||||
contentRect: NSRect(x: 0, y: 0, width: 800, height: 500),
|
||||
styleMask: [.titled, .closable, .miniaturizable, .resizable],
|
||||
backing: .buffered,
|
||||
defer: true
|
||||
)
|
||||
window.title = NSLocalizedString("Dinosaurs", comment: "Dinosaurs window title")
|
||||
window.contentMinSize = NSSize(width: 500, height: 300)
|
||||
window.contentViewController = NSHostingController(rootView: DinosaursView())
|
||||
window.center()
|
||||
|
||||
super.init(window: window)
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) is not supported")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// DinosaursFeedProvider.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Brent Simmons on 5/31/26.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import Account
|
||||
|
||||
@MainActor struct DinosaursFeedProvider {
|
||||
|
||||
let rows: [DinosaurRow]
|
||||
let showAccountColumn: Bool
|
||||
|
||||
static func fetch(monthThreshold: Int) async -> DinosaursFeedProvider {
|
||||
let directReadingAccounts = AccountManager.shared.activeAccounts.filter {
|
||||
$0.type == .onMyMac || $0.type == .cloudKit
|
||||
}
|
||||
|
||||
let showAccountColumn = directReadingAccounts.count > 1
|
||||
let cutoffDate = Calendar.current.date(byAdding: .month, value: -monthThreshold, to: Date())
|
||||
|
||||
var rows = [DinosaurRow]()
|
||||
|
||||
for account in directReadingAccounts {
|
||||
let latestDates: [String: Date]
|
||||
do {
|
||||
latestDates = try await account.fetchLastUpdateDates()
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
|
||||
for feed in account.flattenedFeeds() {
|
||||
let latestDate = latestDates[feed.feedID]
|
||||
|
||||
let isDinosaur: Bool
|
||||
if let latestDate, let cutoffDate {
|
||||
isDinosaur = latestDate < cutoffDate
|
||||
} else if latestDate == nil {
|
||||
isDinosaur = true
|
||||
} else {
|
||||
isDinosaur = false
|
||||
}
|
||||
|
||||
if isDinosaur {
|
||||
let row = DinosaurRow(
|
||||
id: feed.url + account.accountID,
|
||||
feed: feed,
|
||||
account: account,
|
||||
accountName: account.nameForDisplay,
|
||||
feedName: feed.nameForDisplay,
|
||||
feedURL: feed.url,
|
||||
lastArticleDate: latestDate,
|
||||
lastResponseCode: feed.lastResponseCode
|
||||
)
|
||||
rows.append(row)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return DinosaursFeedProvider(rows: rows, showAccountColumn: showAccountColumn)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
//
|
||||
// DinosaursView.swift
|
||||
// NetNewsWire
|
||||
//
|
||||
// Created by Brent Simmons on 5/31/26.
|
||||
//
|
||||
|
||||
@preconcurrency import AppKit
|
||||
import SwiftUI
|
||||
import Account
|
||||
|
||||
struct DinosaursView: View {
|
||||
|
||||
@State private var monthThreshold = 3
|
||||
@State private var monthThresholdText = "3"
|
||||
@State private var rows = [DinosaurRow]()
|
||||
@State private var showAccountColumn = false
|
||||
@State private var selection = Set<String>()
|
||||
@State private var sortOrder = [KeyPathComparator(\DinosaurRow.feedName)]
|
||||
@State private var showDeleteConfirmation = false
|
||||
|
||||
private static let dateFormatter: DateFormatter = {
|
||||
let formatter = DateFormatter()
|
||||
formatter.dateStyle = .medium
|
||||
formatter.timeStyle = .none
|
||||
return formatter
|
||||
}()
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 0) {
|
||||
filterBar
|
||||
Divider()
|
||||
table
|
||||
Divider()
|
||||
buttonBar
|
||||
}
|
||||
.task {
|
||||
await fetchRows()
|
||||
}
|
||||
}
|
||||
|
||||
private var filterBar: some View {
|
||||
HStack {
|
||||
Text("Show feeds that haven’t updated in", comment: "Dinosaurs filter prefix")
|
||||
TextField("", text: $monthThresholdText)
|
||||
.frame(width: 36)
|
||||
.multilineTextAlignment(.center)
|
||||
.onSubmit {
|
||||
if let value = Int(monthThresholdText), value > 0 {
|
||||
monthThreshold = value
|
||||
Task {
|
||||
await fetchRows()
|
||||
}
|
||||
} else {
|
||||
monthThresholdText = "\(monthThreshold)"
|
||||
}
|
||||
}
|
||||
Text("months", comment: "Dinosaurs filter unit")
|
||||
Spacer()
|
||||
}
|
||||
.padding(12)
|
||||
}
|
||||
|
||||
private var table: some View {
|
||||
Table(rows, selection: $selection, sortOrder: $sortOrder) {
|
||||
TableColumn(Text("Name", comment: "Dinosaurs table column"), value: \.feedName)
|
||||
.width(min: 100, ideal: 200)
|
||||
TableColumn(Text("URL", comment: "Dinosaurs table column"), value: \.feedURL)
|
||||
.width(min: 100, ideal: 200)
|
||||
if showAccountColumn {
|
||||
TableColumn(Text("Account", comment: "Dinosaurs table column"), value: \.accountName)
|
||||
.width(min: 80, ideal: 120)
|
||||
}
|
||||
TableColumn(Text("Last Article", comment: "Dinosaurs table column"), value: \.lastArticleDate, comparator: OptionalDateComparator()) { row in
|
||||
Text(row.lastArticleDate.map { Self.dateFormatter.string(from: $0) } ?? "—")
|
||||
}
|
||||
.width(min: 80, ideal: 120)
|
||||
TableColumn(Text("Last Response", comment: "Dinosaurs table column"), value: \.lastResponseCode, comparator: OptionalIntComparator()) { row in
|
||||
Text(row.lastResponseCode.map { "\($0)" } ?? "—")
|
||||
}
|
||||
.width(min: 60, ideal: 80)
|
||||
}
|
||||
.onChange(of: sortOrder) {
|
||||
rows.sort(using: sortOrder)
|
||||
}
|
||||
}
|
||||
|
||||
private var buttonBar: some View {
|
||||
HStack {
|
||||
Button(NSLocalizedString("Select in Sidebar", comment: "Dinosaurs button")) {
|
||||
selectInSidebar()
|
||||
}
|
||||
.disabled(selection.count != 1)
|
||||
|
||||
Button(NSLocalizedString("Delete…", comment: "Dinosaurs button")) {
|
||||
showDeleteConfirmation = true
|
||||
}
|
||||
.disabled(selection.isEmpty)
|
||||
|
||||
Button(NSLocalizedString("Go to Home Page", comment: "Dinosaurs button")) {
|
||||
goToHomePage()
|
||||
}
|
||||
.disabled(selection.isEmpty)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.padding(12)
|
||||
.alert(NSLocalizedString("Delete Feeds", comment: "Dinosaurs delete confirmation title"), isPresented: $showDeleteConfirmation) {
|
||||
Button(NSLocalizedString("Cancel", comment: "Dinosaurs delete confirmation button"), role: .cancel) {}
|
||||
Button(NSLocalizedString("Delete", comment: "Dinosaurs delete confirmation button"), role: .destructive) {
|
||||
deleteSelectedFeeds()
|
||||
}
|
||||
} message: {
|
||||
if selection.count == 1 {
|
||||
if let row = selectedRows.first {
|
||||
Text("Are you sure you want to delete the feed \(row.feedName)?", comment: "Dinosaurs delete confirmation message — single feed")
|
||||
}
|
||||
} else {
|
||||
Text("Are you sure you want to delete \(selection.count) feeds?", comment: "Dinosaurs delete confirmation message — multiple feeds")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var selectedRows: [DinosaurRow] {
|
||||
rows.filter { selection.contains($0.id) }
|
||||
}
|
||||
|
||||
private func fetchRows() async {
|
||||
let provider = await DinosaursFeedProvider.fetch(monthThreshold: monthThreshold)
|
||||
rows = provider.rows.sorted(using: sortOrder)
|
||||
showAccountColumn = provider.showAccountColumn
|
||||
selection.removeAll()
|
||||
}
|
||||
|
||||
private func selectInSidebar() {
|
||||
guard let row = selectedRows.first else {
|
||||
return
|
||||
}
|
||||
guard let mainWindowController = NSApp.mainWindow?.windowController as? MainWindowController else {
|
||||
return
|
||||
}
|
||||
mainWindowController.selectFeedInSidebar(row.feed)
|
||||
}
|
||||
|
||||
private func deleteSelectedFeeds() {
|
||||
for row in selectedRows {
|
||||
let containers = row.account.existingContainers(withFeed: row.feed)
|
||||
for container in containers {
|
||||
row.account.removeFeed(row.feed, from: container) { _ in }
|
||||
}
|
||||
}
|
||||
Task {
|
||||
await fetchRows()
|
||||
}
|
||||
}
|
||||
|
||||
private func goToHomePage() {
|
||||
for row in selectedRows {
|
||||
if let homePageURL = row.feed.homePageURL, let url = URL(string: homePageURL) {
|
||||
NSWorkspace.shared.open(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct OptionalDateComparator: SortComparator {
|
||||
var order: SortOrder = .forward
|
||||
|
||||
func compare(_ lhs: Date?, _ rhs: Date?) -> ComparisonResult {
|
||||
switch (lhs, rhs) {
|
||||
case (nil, nil):
|
||||
return .orderedSame
|
||||
case (nil, _):
|
||||
return order == .forward ? .orderedAscending : .orderedDescending
|
||||
case (_, nil):
|
||||
return order == .forward ? .orderedDescending : .orderedAscending
|
||||
case let (l?, r?):
|
||||
let result = l.compare(r)
|
||||
return order == .forward ? result : result.inverted
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct OptionalIntComparator: SortComparator {
|
||||
var order: SortOrder = .forward
|
||||
|
||||
func compare(_ lhs: Int?, _ rhs: Int?) -> ComparisonResult {
|
||||
switch (lhs, rhs) {
|
||||
case (nil, nil):
|
||||
return .orderedSame
|
||||
case (nil, _):
|
||||
return order == .forward ? .orderedAscending : .orderedDescending
|
||||
case (_, nil):
|
||||
return order == .forward ? .orderedDescending : .orderedAscending
|
||||
case let (l?, r?):
|
||||
let result: ComparisonResult
|
||||
if l < r {
|
||||
result = .orderedAscending
|
||||
} else if l > r {
|
||||
result = .orderedDescending
|
||||
} else {
|
||||
result = .orderedSame
|
||||
}
|
||||
return order == .forward ? result : result.inverted
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension ComparisonResult {
|
||||
|
||||
var inverted: ComparisonResult {
|
||||
switch self {
|
||||
case .orderedAscending:
|
||||
return .orderedDescending
|
||||
case .orderedDescending:
|
||||
return .orderedAscending
|
||||
case .orderedSame:
|
||||
return .orderedSame
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,6 +126,10 @@ final class MainWindowController: NSWindowController, NSUserInterfaceValidations
|
||||
return sidebarViewController?.selectedObjects
|
||||
}
|
||||
|
||||
func selectFeedInSidebar(_ feed: Feed) {
|
||||
sidebarViewController?.selectFeed(feed)
|
||||
}
|
||||
|
||||
func handle(_ response: UNNotificationResponse) {
|
||||
let userInfo = response.notification.request.content.userInfo
|
||||
guard let articlePathUserInfo = userInfo[UserInfoKey.articlePath] as? [AnyHashable: Any] else { return }
|
||||
|
||||
@@ -884,6 +884,11 @@ public enum FetchType {
|
||||
try database.fetchAllArticlesCount(feedIDs: flattenedFeedsIDs)
|
||||
}
|
||||
|
||||
/// Returns a dictionary of feedID → latest article date for all feeds with articles.
|
||||
public func fetchLastUpdateDates() async throws -> [String: Date] {
|
||||
try await database.fetchLastUpdateDates()
|
||||
}
|
||||
|
||||
public func fetchUnreadCountForTodayAsync() async throws -> Int {
|
||||
try await database.fetchUnreadCountForTodayAsync(feedIDs: flattenedFeedsIDs)
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ public extension Feed {
|
||||
case externalID
|
||||
case folderRelationship
|
||||
case lastCheckDate
|
||||
case lastResponseCode
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -189,6 +189,17 @@ import Articles
|
||||
settings.lastCheckDate = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// HTTP status code from the most recent feed download attempt.
|
||||
public var lastResponseCode: Int? {
|
||||
get {
|
||||
settings.lastResponseCode
|
||||
}
|
||||
set {
|
||||
settings.lastResponseCode = newValue
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - DisplayNameProvider
|
||||
|
||||
public var nameForDisplay: String {
|
||||
|
||||
@@ -149,6 +149,16 @@ import Articles
|
||||
}
|
||||
}
|
||||
|
||||
/// HTTP status code from the most recent feed download attempt.
|
||||
var lastResponseCode: Int? {
|
||||
didSet {
|
||||
if lastResponseCode != oldValue {
|
||||
database.setInt(lastResponseCode, for: feedURL, column: .lastResponseCode)
|
||||
postSettingDidChange(.lastResponseCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Create from database row (bulk load at startup).
|
||||
init(feedURL: String, row: FeedSettingsDatabase.Row, database: FeedSettingsDatabase) {
|
||||
self.feedURL = feedURL
|
||||
@@ -168,6 +178,7 @@ import Articles
|
||||
self.externalID = row.externalID
|
||||
self.folderRelationship = row.folderRelationship
|
||||
self.lastCheckDate = row.lastCheckDate
|
||||
self.lastResponseCode = row.lastResponseCode
|
||||
}
|
||||
|
||||
/// Create for a new feed not yet in the database.
|
||||
|
||||
@@ -31,6 +31,7 @@ final class FeedSettingsDatabase: Sendable {
|
||||
case externalID
|
||||
case folderRelationship
|
||||
case lastCheckDate
|
||||
case lastResponseCode
|
||||
}
|
||||
|
||||
struct Row {
|
||||
@@ -49,6 +50,7 @@ final class FeedSettingsDatabase: Sendable {
|
||||
let externalID: String?
|
||||
let folderRelationship: [String: String]?
|
||||
let lastCheckDate: Date?
|
||||
let lastResponseCode: Int?
|
||||
}
|
||||
|
||||
let databasePath: String
|
||||
@@ -63,6 +65,9 @@ final class FeedSettingsDatabase: Sendable {
|
||||
self.database = FMDatabase.openAndSetUpDatabase(path: databasePath)
|
||||
serialDispatchQueue.sync { [database] in
|
||||
database.runCreateStatements(Self.tableCreationStatements)
|
||||
if !database.columnExists("lastResponseCode", inTableWithName: "feedSettings") {
|
||||
database.executeStatements("ALTER TABLE feedSettings ADD COLUMN lastResponseCode INTEGER;")
|
||||
}
|
||||
}
|
||||
vacuumIfNeeded()
|
||||
}
|
||||
@@ -159,6 +164,19 @@ final class FeedSettingsDatabase: Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Int
|
||||
|
||||
func setInt(_ value: Int?, for feedURL: String, column: Column) {
|
||||
let name = column.rawValue
|
||||
serialDispatchQueue.async {
|
||||
if let value {
|
||||
self.database.executeUpdate("UPDATE feedSettings SET \(name) = ? WHERE feedURL = ?;", withArgumentsIn: [value, feedURL])
|
||||
} else {
|
||||
self.database.executeUpdate("UPDATE feedSettings SET \(name) = NULL WHERE feedURL = ?;", withArgumentsIn: [feedURL])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Date
|
||||
|
||||
func setDate(_ value: Date?, for feedURL: String, column: Column) {
|
||||
@@ -253,7 +271,7 @@ final class FeedSettingsDatabase: Sendable {
|
||||
private extension FeedSettingsDatabase {
|
||||
|
||||
static let tableCreationStatements = """
|
||||
CREATE TABLE IF NOT EXISTS feedSettings (feedURL TEXT PRIMARY KEY, feedID TEXT NOT NULL DEFAULT '', homePageURL TEXT, iconURL TEXT, faviconURL TEXT, editedName TEXT, contentHash TEXT, newArticleNotificationsEnabled INTEGER NOT NULL DEFAULT 0, readerViewAlwaysEnabled INTEGER NOT NULL DEFAULT 0, authors TEXT, conditionalGetInfoLastModified TEXT, conditionalGetInfoEtag TEXT, conditionalGetInfoDate REAL, cacheControlInfoDateCreated REAL, cacheControlInfoMaxAge REAL, externalID TEXT, folderRelationship TEXT, lastCheckDate REAL);
|
||||
CREATE TABLE IF NOT EXISTS feedSettings (feedURL TEXT PRIMARY KEY, feedID TEXT NOT NULL DEFAULT '', homePageURL TEXT, iconURL TEXT, faviconURL TEXT, editedName TEXT, contentHash TEXT, newArticleNotificationsEnabled INTEGER NOT NULL DEFAULT 0, readerViewAlwaysEnabled INTEGER NOT NULL DEFAULT 0, authors TEXT, conditionalGetInfoLastModified TEXT, conditionalGetInfoEtag TEXT, conditionalGetInfoDate REAL, cacheControlInfoDateCreated REAL, cacheControlInfoMaxAge REAL, externalID TEXT, folderRelationship TEXT, lastCheckDate REAL, lastResponseCode INTEGER);
|
||||
"""
|
||||
|
||||
func row(from resultSet: FMResultSet) -> Row {
|
||||
@@ -289,6 +307,11 @@ private extension FeedSettingsDatabase {
|
||||
lastCheckDate = Date(timeIntervalSinceReferenceDate: resultSet.double(forColumn: Column.lastCheckDate.rawValue))
|
||||
}
|
||||
|
||||
var lastResponseCode: Int?
|
||||
if !resultSet.columnIsNull(Column.lastResponseCode.rawValue) {
|
||||
lastResponseCode = Int(resultSet.int(forColumn: Column.lastResponseCode.rawValue))
|
||||
}
|
||||
|
||||
return Row(
|
||||
feedID: resultSet.swiftString(forColumn: Column.feedID.rawValue) ?? "",
|
||||
homePageURL: resultSet.swiftString(forColumn: Column.homePageURL.rawValue),
|
||||
@@ -304,7 +327,8 @@ private extension FeedSettingsDatabase {
|
||||
cacheControlInfo: cacheControlInfo,
|
||||
externalID: resultSet.swiftString(forColumn: Column.externalID.rawValue),
|
||||
folderRelationship: folderRelationship,
|
||||
lastCheckDate: lastCheckDate
|
||||
lastCheckDate: lastCheckDate,
|
||||
lastResponseCode: lastResponseCode
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -240,6 +240,8 @@ import os
|
||||
return
|
||||
}
|
||||
|
||||
feed.lastResponseCode = httpResponse.statusCode
|
||||
|
||||
let statusIsOK = httpResponse.statusIsOK
|
||||
let statusIsOKOrNotModified = statusIsOK || httpResponse.statusCode == HTTPResponseCode.notModified
|
||||
guard statusIsOKOrNotModified else {
|
||||
|
||||
@@ -151,6 +151,16 @@ public struct ArticleChanges: Sendable {
|
||||
return try articlesTable.fetchArticlesMatchingWithArticleIDs(searchString, articleIDs)
|
||||
}
|
||||
|
||||
/// Returns a dictionary of feedID → latest article date for all feeds with articles.
|
||||
public func fetchLastUpdateDates() async throws -> [String: Date] {
|
||||
Self.logger.debug("ArticlesDatabase: \(#function, privacy: .public) \(self.accountID, privacy: .public)")
|
||||
return try await withCheckedThrowingContinuation { continuation in
|
||||
articlesTable.fetchLastUpdateDatesAsync { result in
|
||||
continuation.resume(with: result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Fetching Articles Async
|
||||
|
||||
public func fetchArticlesAsync(feedID: String) async throws -> Set<Article> {
|
||||
|
||||
@@ -112,6 +112,24 @@ final class ArticlesTable: DatabaseTable, Sendable {
|
||||
return try fetchArticlesCount { self.fetchAllArticlesCount(feedIDs, $0) }
|
||||
}
|
||||
|
||||
// MARK: - Fetching Last Update Dates
|
||||
|
||||
func fetchLastUpdateDatesAsync(_ completion: @escaping @Sendable (Result<[String: Date], DatabaseError>) -> Void) {
|
||||
queue.runInDatabase { databaseResult in
|
||||
switch databaseResult {
|
||||
case .success(let database):
|
||||
let result = self.fetchLastUpdateDates(database)
|
||||
DispatchQueue.main.async {
|
||||
completion(.success(result))
|
||||
}
|
||||
case .failure(let databaseError):
|
||||
DispatchQueue.main.async {
|
||||
completion(.failure(databaseError))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Fetching Search Articles
|
||||
|
||||
func fetchArticlesMatching(_ searchString: String) throws -> Set<Article> {
|
||||
@@ -986,6 +1004,26 @@ nonisolated private extension ArticlesTable {
|
||||
return articles.filter { articleIDs.contains($0.articleID) }
|
||||
}
|
||||
|
||||
func fetchLastUpdateDates(_ database: FMDatabase) -> [String: Date] {
|
||||
guard let resultSet = database.executeQuery("SELECT feedID, MAX(coalesce(datePublished, dateModified)) as latestDate FROM articles GROUP BY feedID;", withArgumentsIn: []) else {
|
||||
return [:]
|
||||
}
|
||||
defer {
|
||||
resultSet.close()
|
||||
}
|
||||
|
||||
var result = [String: Date]()
|
||||
while resultSet.next() {
|
||||
guard let feedID = resultSet.string(forColumn: "feedID") else {
|
||||
continue
|
||||
}
|
||||
if !resultSet.columnIsNull("latestDate") {
|
||||
result[feedID] = resultSet.date(forColumn: "latestDate")
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// MARK: - Saving Parsed Items
|
||||
|
||||
func callUpdateArticlesCompletionBlock(_ newArticles: Set<Article>?, _ updatedArticles: Set<Article>?, _ deletedArticles: Set<Article>?, _ completion: @escaping UpdateArticlesCompletionBlock) {
|
||||
|
||||
Reference in new issue
Block a user