diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift index 646a18001..40ace05c2 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReader.swift @@ -15,9 +15,8 @@ struct WebReader: UIViewRepresentable { @Binding var increaseFontActionID: UUID? @Binding var decreaseFontActionID: UUID? - - @State var annotationSaveTransactionID: UUID? - @State private var annotation = String() + @Binding var annotationSaveTransactionID: UUID? + @Binding var annotation: String func makeCoordinator() -> WebReaderCoordinator { WebReaderCoordinator() diff --git a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift index 6043fee81..f9f021172 100644 --- a/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift +++ b/apple/OmnivoreKit/Sources/App/Views/WebReader/WebReaderContainer.swift @@ -3,6 +3,7 @@ import Models import Services import SwiftUI import Views +import WebKit struct SafariWebLink: Identifiable { let id: UUID @@ -46,6 +47,8 @@ struct WebReaderContainerView: View { @State private var showOverlay = true @State var increaseFontActionID: UUID? @State var decreaseFontActionID: UUID? + @State var annotationSaveTransactionID: UUID? + @State var annotation = String() @EnvironmentObject var dataService: DataService @EnvironmentObject var authenticator: Authenticator @@ -59,6 +62,31 @@ struct WebReaderContainerView: View { ) } + func webViewActionHandler(message: WKScriptMessage) { + if message.name == WebViewAction.highlightAction.rawValue { + handleHighlightAction(message: message) + } + + if message.name == WebViewAction.readingProgressUpdate.rawValue { + guard let messageBody = message.body as? [String: Double] else { return } + guard let progress = messageBody["progress"] else { return } + homeFeedViewModel.updateProgress(itemID: item.id, progress: Double(progress)) + } + } + + private func handleHighlightAction(message: WKScriptMessage) { + guard let messageBody = message.body as? [String: String] else { return } + guard let actionID = messageBody["actionID"] else { return } + + switch actionID { + case "annotate": + annotation = messageBody["annotation"] ?? "" + showHighlightAnnotationModal = true + default: + break + } + } + var navBariOS14: some View { HStack(alignment: .center) { Button( @@ -165,8 +193,14 @@ struct WebReaderContainerView: View { WebReader( htmlContent: htmlContent, item: item, - openLinkAction: { url in print(url) }, - webViewActionHandler: { _ in }, + openLinkAction: { + #if os(macOS) + NSWorkspace.shared.open($0) + #elseif os(iOS) + safariWebLink = SafariWebLink(id: UUID(), url: $0) + #endif + }, + webViewActionHandler: webViewActionHandler, navBarVisibilityRatioUpdater: { if $0 < 1 { showFontSizePopover = false @@ -177,7 +211,8 @@ struct WebReaderContainerView: View { appEnv: dataService.appEnvironment, increaseFontActionID: $increaseFontActionID, decreaseFontActionID: $decreaseFontActionID, - annotationSaveTransactionID: nil + annotationSaveTransactionID: $annotationSaveTransactionID, + annotation: $annotation ) .overlay( Group { @@ -194,6 +229,21 @@ struct WebReaderContainerView: View { } } ) + .sheet(item: $safariWebLink) { + SafariView(url: $0.url) + } + .sheet(isPresented: $showHighlightAnnotationModal) { + HighlightAnnotationSheet( + annotation: $annotation, + onSave: { + annotationSaveTransactionID = UUID() + showHighlightAnnotationModal = false + }, + onCancel: { + showHighlightAnnotationModal = false + } + ) + } } else { Color.clear .contentShape(Rectangle()) diff --git a/apple/OmnivoreKit/Sources/Views/Article/HighlightAnnotationSheet.swift b/apple/OmnivoreKit/Sources/Views/Article/HighlightAnnotationSheet.swift index 59cb5ef7c..30dc1def4 100644 --- a/apple/OmnivoreKit/Sources/Views/Article/HighlightAnnotationSheet.swift +++ b/apple/OmnivoreKit/Sources/Views/Article/HighlightAnnotationSheet.swift @@ -1,13 +1,13 @@ import Introspect import SwiftUI -struct HighlightAnnotationSheet: View { +public struct HighlightAnnotationSheet: View { @Binding var annotation: String let onSave: () -> Void let onCancel: () -> Void - init( + public init( annotation: Binding, onSave: @escaping () -> Void, onCancel: @escaping () -> Void @@ -17,7 +17,7 @@ struct HighlightAnnotationSheet: View { self.onCancel = onCancel } - var body: some View { + public var body: some View { VStack { HStack { Button("Cancel", action: onCancel) diff --git a/apple/OmnivoreKit/Sources/Views/Article/WebAppWrapperView.swift b/apple/OmnivoreKit/Sources/Views/Article/WebAppWrapperView.swift index 7c16c83a8..8d556cd54 100644 --- a/apple/OmnivoreKit/Sources/Views/Article/WebAppWrapperView.swift +++ b/apple/OmnivoreKit/Sources/Views/Article/WebAppWrapperView.swift @@ -114,21 +114,30 @@ public struct WebAppWrapperView: View { } #if os(iOS) - struct SafariView: UIViewControllerRepresentable { + public struct SafariView: UIViewControllerRepresentable { let url: URL - func makeUIViewController(context _: UIViewControllerRepresentableContext) -> SFSafariViewController { + public init(url: URL) { + self.url = url + } + + public func makeUIViewController(context _: UIViewControllerRepresentableContext) -> SFSafariViewController { SFSafariViewController(url: url) } // swiftlint:disable:next line_length - func updateUIViewController(_: SFSafariViewController, context _: UIViewControllerRepresentableContext) {} + public func updateUIViewController(_: SFSafariViewController, context _: UIViewControllerRepresentableContext) {} } #elseif os(macOS) - struct SafariView: View { + public struct SafariView: View { let url: URL - var body: some View { + + public init(url: URL) { + self.url = url + } + + public var body: some View { Color.clear } }