Skip to content

Commit 76188ce

Browse files
author
Gio
committed
feat(swiftui home): update CardView, check reader support from the underlying item when the navigation happens
1 parent be8e0c3 commit 76188ce

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

PocketKit/Sources/PocketKit/Home/Views/Cards/CardView.swift

+19-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ struct CardView: View {
2626
private var carouselWidth
2727
@Environment(\.homeActions)
2828
private var homeActions
29+
@Environment(\.modelContext)
30+
private var modelContext
2931

3032
@EnvironmentObject private var navigation: HomeNavigation
3133

@@ -128,11 +130,9 @@ private extension CardView {
128130
var externalDestination = false
129131
if let slug = card.slug {
130132
navigation.navigateTo(NativeCollectionDestination(slug: slug, givenURL: card.givenURL))
131-
} else if let savedItem, // We are legally allowed to open the item in reader view
132-
savedItem.item?.isArticle == true, // except one of the following conditions is met:
133-
savedItem.item?.isVideo == false, // a) the item is not an article (i.e. it was not parseable)
134-
savedItem.item?.isImage == false { // b) the item is an image
135-
navigation.navigateTo(ReadableDestination(.saved(card.givenURL), source: .app)) // c) the item is a video
133+
} else if savedItem != nil,
134+
isReaderSupported() {
135+
navigation.navigateTo(ReadableDestination(.saved(card.givenURL), source: .app))
136136
} else if card.isSyndicated {
137137
navigation.navigateTo(ReadableDestination(.syndicated(card.givenURL), source: .app))
138138
} else if URL(string: card.givenURL) != nil {
@@ -151,6 +151,20 @@ private extension CardView {
151151
}
152152
}
153153

154+
/// We are legally allowed to open the item in reader view except one of the following conditions is met:
155+
/// a) the item is not an article (i.e. it was not parseable)
156+
/// b) the item is an image
157+
/// c) the item is a video
158+
/// - Returns: true if we can open this item in the reader
159+
func isReaderSupported() -> Bool {
160+
let givenURL = card.givenURL
161+
let fetchDescriptor = FetchDescriptor<Item>(predicate: #Predicate<Item> { $0.givenURL == givenURL })
162+
guard let item = try? modelContext.fetch(fetchDescriptor).first else {
163+
return false
164+
}
165+
return item.isArticle == true && item.isImage == false && item.isVideo == false
166+
}
167+
154168
/// Builds a Shared With You card, which is a sized card with an attribution view at the bottom
155169
/// - Returns: the card view with the attribution view
156170
func makeSharedWithYouCard(_ urlString: String) -> some View {

0 commit comments

Comments
 (0)