mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-05-17 20:12:41 +00:00
fix: refresh logic
This commit is contained in:
parent
39d7fd5b3b
commit
6433a6b2e5
|
@ -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 }
|
||||
}
|
||||
}
|
|
@ -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 } }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,6 @@ public struct RSSFeedManager: View {
|
|||
ToolbarItem(placement: .topBarTrailing) {
|
||||
Button("rss.rssFeedManager.action.done") {
|
||||
dismiss()
|
||||
moContext.refreshAllObjects()
|
||||
try? moContext.save()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue