mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-06-15 11:40:37 +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)
|
RSSItemView(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.task {
|
.task { items = feed.toRSSItems().sorted { $0.date > $1.date } }
|
||||||
items = ((feed.items?.allObjects as? [RSSItem]) ?? [])
|
|
||||||
.sorted { i0, i1 in
|
|
||||||
if let d0 = i0.date,
|
|
||||||
let d1 = i1.date
|
|
||||||
{
|
|
||||||
d0 > d1
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,7 +53,6 @@ public struct RSSFeedManager: View {
|
||||||
ToolbarItem(placement: .topBarTrailing) {
|
ToolbarItem(placement: .topBarTrailing) {
|
||||||
Button("rss.rssFeedManager.action.done") {
|
Button("rss.rssFeedManager.action.done") {
|
||||||
dismiss()
|
dismiss()
|
||||||
moContext.refreshAllObjects()
|
|
||||||
try? moContext.save()
|
try? moContext.save()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,12 +13,9 @@ struct RSSFeedView: View {
|
||||||
@Environment(Theme.self) private var theme
|
@Environment(Theme.self) private var theme
|
||||||
|
|
||||||
private var items: ArraySlice<RSSItem> {
|
private var items: ArraySlice<RSSItem> {
|
||||||
((feed.items?.allObjects as? [RSSItem]) ?? [])
|
feed.toRSSItems()
|
||||||
.sorted { i0, i1 in
|
.sorted { $0.date > $1.date }
|
||||||
if let d0 = i0.date,
|
.prefix(5)
|
||||||
let d1 = i1.date
|
|
||||||
{ d0 > d1 } else { false }
|
|
||||||
}.prefix(5)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private var contentPadding: CGFloat {
|
private var contentPadding: CGFloat {
|
||||||
|
|
|
@ -15,9 +15,10 @@ import AppAccount
|
||||||
import CoreData
|
import CoreData
|
||||||
|
|
||||||
public struct RSSTabContentView: View {
|
public struct RSSTabContentView: View {
|
||||||
@FetchRequest(sortDescriptors: [SortDescriptor(\.date, order: .reverse)],
|
@FetchRequest(sortDescriptors: [],
|
||||||
predicate: NSPredicate(format: "feed.isShowing == TRUE"))
|
predicate: NSPredicate(format: "isShowing == true"))
|
||||||
private var items: FetchedResults<RSSItem>
|
private var feeds: FetchedResults<RSSFeed>
|
||||||
|
private var items: [RSSItem] { feeds.toRSSItems().sorted { $0.date > $1.date } }
|
||||||
|
|
||||||
@Environment(\.managedObjectContext) private var moContext
|
@Environment(\.managedObjectContext) private var moContext
|
||||||
@State private var isLoading = true
|
@State private var isLoading = true
|
||||||
|
@ -27,6 +28,25 @@ public struct RSSTabContentView: View {
|
||||||
@Environment(Theme.self) private var theme
|
@Environment(Theme.self) private var theme
|
||||||
@Environment(RouterPath.self) private var routerPath
|
@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() {}
|
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