From e28cf07666d2a3fe51bcdd94b0bb596189cf1a34 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Tue, 22 Mar 2022 08:02:34 -0700 Subject: [PATCH] pass highlights into webreader --- .../App/Views/WebReader/WebReader.swift | 4 ++-- .../Views/WebReader/WebReaderContainer.swift | 10 ++++----- .../Views/WebReader/WebReaderContent.swift | 10 ++++----- .../Sources/Models/Highlight.swift | 10 +++++++-- .../Queries/ArticleContentQuery.swift | 21 ++++++++++++++++--- .../Views/Article/WebAppWrapperView.swift | 4 +++- 6 files changed, 41 insertions(+), 18 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift index 40ace05c2..5e2e8c6dc 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift @@ -5,7 +5,7 @@ import Views import WebKit struct WebReader: UIViewRepresentable { - let htmlContent: String + let articleContent: ArticleContent let item: FeedItem let openLinkAction: (URL) -> Void let webViewActionHandler: (WKScriptMessage) -> Void @@ -33,7 +33,7 @@ struct WebReader: UIViewRepresentable { webView.loadHTMLString( WebReaderContent( - htmlContent: htmlContent, + articleContent: articleContent, item: item, authToken: authToken, isDark: UITraitCollection.current.userInterfaceStyle == .dark, diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift index f9f021172..eb8a00f11 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift @@ -13,7 +13,7 @@ struct SafariWebLink: Identifiable { // TODO: load highlights final class WebReaderViewModel: ObservableObject { @Published var isLoading = false - @Published var htmlContent: String? + @Published var articleContent: ArticleContent? var subscriptions = Set() @@ -27,8 +27,8 @@ final class WebReaderViewModel: ObservableObject { guard case .failure = completion else { return } self?.isLoading = false }, - receiveValue: { [weak self] htmlContent in - self?.htmlContent = htmlContent + receiveValue: { [weak self] articleContent in + self?.articleContent = articleContent } ) .store(in: &subscriptions) @@ -189,9 +189,9 @@ struct WebReaderContainerView: View { var body: some View { ZStack { - if let htmlContent = viewModel.htmlContent { + if let articleContent = viewModel.articleContent { WebReader( - htmlContent: htmlContent, + articleContent: articleContent, item: item, openLinkAction: { #if os(macOS) diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContent.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContent.swift index cd649d987..39b330335 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContent.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContent.swift @@ -4,14 +4,14 @@ import Utils struct WebReaderContent { let textFontSize: Int - let content: String + let articleContent: ArticleContent let item: FeedItem let themeKey: String let authToken: String let appEnv: AppEnvironment init( - htmlContent: String, + articleContent: ArticleContent, item: FeedItem, authToken: String, isDark: Bool, @@ -19,7 +19,7 @@ struct WebReaderContent { appEnv: AppEnvironment ) { self.textFontSize = fontSize - self.content = htmlContent + self.articleContent = articleContent self.item = item self.themeKey = isDark ? "Gray" : "LightGray" self.authToken = authToken @@ -53,12 +53,12 @@ struct WebReaderContent { savedAt: new Date().toISOString(), url: "https://example.com", title: `\(item.title)`, - content: `\(content)`, + content: `\(articleContent.htmlContent)`, originalArticleUrl: "https://example.com", contentReader: "WEB", readingProgressPercent: \(item.readingProgress), readingProgressAnchorIndex: \(item.readingProgressAnchor), - highlights: [], + highlights: \(articleContent.highlightsJSONString), } window.fontSize = \(textFontSize) diff --git a/apple/OmnivoreKit/Sources/Models/Highlight.swift b/apple/OmnivoreKit/Sources/Models/Highlight.swift index cbd0c8aa6..db1dd425c 100644 --- a/apple/OmnivoreKit/Sources/Models/Highlight.swift +++ b/apple/OmnivoreKit/Sources/Models/Highlight.swift @@ -1,6 +1,6 @@ import Foundation -public struct Highlight: Identifiable, Hashable { +public struct Highlight: Identifiable, Hashable, Codable { public let id: String public let shortId: String public let quote: String @@ -8,6 +8,8 @@ public struct Highlight: Identifiable, Hashable { public let suffix: String? public let patch: String public let annotation: String? + public let createdAt: Date? + public let updatedAt: Date? public init( id: String, @@ -16,7 +18,9 @@ public struct Highlight: Identifiable, Hashable { prefix: String?, suffix: String?, patch: String, - annotation: String? + annotation: String?, + createdAt: Date? = nil, + updatedAt: Date? = nil ) { self.id = id self.shortId = shortId @@ -25,5 +29,7 @@ public struct Highlight: Identifiable, Hashable { self.suffix = suffix self.patch = patch self.annotation = annotation + self.createdAt = createdAt + self.updatedAt = updatedAt } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift index da95ddfdc..1fdcd7db6 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift @@ -4,14 +4,29 @@ import Models import SwiftGraphQL public extension DataService { - func articleContentPublisher(username: String, slug: String) -> AnyPublisher { + func articleContentPublisher(username: String, slug: String) -> AnyPublisher { enum QueryResult { - case success(result: String) + case success(result: ArticleContent) case error(error: String) } + let highlightSelection = Selection.Highlight { + Highlight( + id: try $0.id(), + shortId: try $0.shortId(), + quote: try $0.quote(), + prefix: try $0.prefix(), + suffix: try $0.suffix(), + patch: try $0.patch(), + annotation: try $0.annotation() + ) + } + let articleSelection = Selection.Article { - try $0.content() + ArticleContent( + htmlContent: try $0.content(), + highlights: try $0.highlights(selection: highlightSelection.list) + ) } let selection = Selection { diff --git a/apple/OmnivoreKit/Sources/Views/Article/WebAppWrapperView.swift b/apple/OmnivoreKit/Sources/Views/Article/WebAppWrapperView.swift index 8d556cd54..e6cba3544 100644 --- a/apple/OmnivoreKit/Sources/Views/Article/WebAppWrapperView.swift +++ b/apple/OmnivoreKit/Sources/Views/Article/WebAppWrapperView.swift @@ -121,7 +121,9 @@ public struct WebAppWrapperView: View { self.url = url } - public func makeUIViewController(context _: UIViewControllerRepresentableContext) -> SFSafariViewController { + public func makeUIViewController( + context _: UIViewControllerRepresentableContext + ) -> SFSafariViewController { SFSafariViewController(url: url) }