fix: refresh logic

This commit is contained in:
Duong Thai 2024-03-14 10:59:31 +07:00
parent 39d7fd5b3b
commit 6433a6b2e5
5 changed files with 60 additions and 43 deletions

View file

@ -0,0 +1,33 @@
//
// FetchedResults<RSSFeed>+toRSSItems.swift
//
//
// Created by Duong Thai on 14/3/24.
//
import SwiftUI
extension FetchedResults<RSSFeed> {
func toRSSItems() -> [RSSItem] { self.flatMap { $0.toRSSItems() } }
}
extension RSSFeed {
func toRSSItems() -> [RSSItem] { ((self.items?.allObjects as? [RSSItem]) ?? []) }
}
extension Optional<Date>: Comparable {
public static func < (lhs: Optional, rhs: Optional) -> Bool {
if let lhs, let rhs { lhs < rhs }
else { false }
}
public static func > (lhs: Optional, rhs: Optional) -> Bool {
if let lhs, let rhs { lhs > rhs }
else { false }
}
public static func == (lhs: Optional, rhs: Optional) -> Bool {
if let lhs, let rhs { lhs == rhs }
else { false }
}
}

View file

@ -153,18 +153,7 @@ public struct RSSAddNewFeed: View {
RSSItemView(item)
}
}
.task {
items = ((feed.items?.allObjects as? [RSSItem]) ?? [])
.sorted { i0, i1 in
if let d0 = i0.date,
let d1 = i1.date
{
d0 > d1
} else {
false
}
}
}
.task { items = feed.toRSSItems().sorted { $0.date > $1.date } }
}
}
}

View file

@ -53,7 +53,6 @@ public struct RSSFeedManager: View {
ToolbarItem(placement: .topBarTrailing) {
Button("rss.rssFeedManager.action.done") {
dismiss()
moContext.refreshAllObjects()
try? moContext.save()
}
}

View file

@ -13,12 +13,9 @@ struct RSSFeedView: View {
@Environment(Theme.self) private var theme
private var items: ArraySlice<RSSItem> {
((feed.items?.allObjects as? [RSSItem]) ?? [])
.sorted { i0, i1 in
if let d0 = i0.date,
let d1 = i1.date
{ d0 > d1 } else { false }
}.prefix(5)
feed.toRSSItems()
.sorted { $0.date > $1.date }
.prefix(5)
}
private var contentPadding: CGFloat {

View file

@ -15,9 +15,10 @@ import AppAccount
import CoreData
public struct RSSTabContentView: View {
@FetchRequest(sortDescriptors: [SortDescriptor(\.date, order: .reverse)],
predicate: NSPredicate(format: "feed.isShowing == TRUE"))
private var items: FetchedResults<RSSItem>
@FetchRequest(sortDescriptors: [],
predicate: NSPredicate(format: "isShowing == true"))
private var feeds: FetchedResults<RSSFeed>
private var items: [RSSItem] { feeds.toRSSItems().sorted { $0.date > $1.date } }
@Environment(\.managedObjectContext) private var moContext
@State private var isLoading = true
@ -27,6 +28,25 @@ public struct RSSTabContentView: View {
@Environment(Theme.self) private var theme
@Environment(RouterPath.self) private var routerPath
public var body: some View {
NavigationStack {
Group {
if items.isEmpty {
makeContentUnavailableView()
} else {
makeItemList()
}
}
.navigationTitle("tab.rss")
.navigationBarTitleDisplayMode(.inline)
.listStyle(PlainListStyle())
.toolbar { makeToolbarItems() }
.onReceive(NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave)) { notification in
let userInfo = notification.userInfo ?? [:]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: userInfo, into: [moContext])
}
}
}
public init() {}
@ -87,25 +107,4 @@ public struct RSSTabContentView: View {
}
}
public var body: some View {
NavigationStack {
Group {
if items.isEmpty {
makeContentUnavailableView()
} else {
makeItemList()
}
}
.navigationTitle("tab.rss")
.navigationBarTitleDisplayMode(.inline)
.listStyle(PlainListStyle())
.onReceive(NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave)) { notification in
let userInfo = notification.userInfo ?? [:]
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: userInfo, into: [moContext])
}
.toolbar {
makeToolbarItems()
}
}
}
}