add annotattion and safari modals to web reader

This commit is contained in:
Satindar Dhillon 2022-03-21 23:28:05 -07:00
parent 93a8d4a890
commit cdefac0d50
4 changed files with 72 additions and 14 deletions

View file

@ -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()

View file

@ -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())

View file

@ -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<String>,
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)

View file

@ -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<SafariView>) -> SFSafariViewController {
public init(url: URL) {
self.url = url
}
public func makeUIViewController(context _: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
SFSafariViewController(url: url)
}
// swiftlint:disable:next line_length
func updateUIViewController(_: SFSafariViewController, context _: UIViewControllerRepresentableContext<SafariView>) {}
public func updateUIViewController(_: SFSafariViewController, context _: UIViewControllerRepresentableContext<SafariView>) {}
}
#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
}
}