From 1ab22880f67adad369b6083c678eded2969c3e47 Mon Sep 17 00:00:00 2001 From: redstrate <54911369+redstrate@users.noreply.github.com> Date: Wed, 18 Nov 2020 08:35:34 -0500 Subject: [PATCH] Remove WebView package and support sidebar in macOS --- MobileFort/Common/ProfileView.swift | 15 ++- MobileFort/Common/WebView.swift | 99 +++++++++++++++++++ .../MobileFort.xcodeproj/project.pbxproj | 23 ++--- .../xcshareddata/swiftpm/Package.resolved | 9 -- 4 files changed, 111 insertions(+), 35 deletions(-) create mode 100644 MobileFort/Common/WebView.swift diff --git a/MobileFort/Common/ProfileView.swift b/MobileFort/Common/ProfileView.swift index 47d4c72..0bddb6d 100644 --- a/MobileFort/Common/ProfileView.swift +++ b/MobileFort/Common/ProfileView.swift @@ -1,10 +1,6 @@ import SwiftUI import WebKit -#if !os(macOS) -import WebView -#endif - struct DescriptionView: View { let username: String @@ -49,16 +45,17 @@ struct DescriptionView: View { return view } - @ViewBuilder + #if !os(macOS) var body: some View { - #if !os(macOS) VStack { WebView(webView: webView) }.navigationBarTitle("Sidebar", displayMode: .inline) - #else - Text("Not supported on macOS.") - #endif } + #else + var body: some View { + WebView(webView: webView) + } + #endif } struct ProfileView: View { diff --git a/MobileFort/Common/WebView.swift b/MobileFort/Common/WebView.swift new file mode 100644 index 0000000..4852c2c --- /dev/null +++ b/MobileFort/Common/WebView.swift @@ -0,0 +1,99 @@ +// From https://github.com/kaishin/Markoff/blob/markoff-2/Markoff/Sources/Views/WebView.swift +// Based on https://github.com/kylehickinson/SwiftUI-WebView + +import SwiftUI +import Combine +import WebKit + +class WebViewStore: ObservableObject { + private var observers = Set() + + @Published var webView: WKWebView { + didSet { + setupObservers() + } + } + + init(webView: WKWebView = .init()) { + self.webView = webView + + #if DEBUG + self.webView + .configuration + .preferences + .setValue(true, forKey: "developerExtrasEnabled") + #endif + + setupObservers() + } + + private func setupObservers() { + func subscriber(for keyPath: KeyPath) -> NSKeyValueObservation { + webView.observe(keyPath, options: [.prior]) { _, change in + if change.isPrior { + self.objectWillChange.send() + } + } + } + + observers = [ + subscriber(for: \.title), + subscriber(for: \.url), + subscriber(for: \.isLoading), + subscriber(for: \.estimatedProgress), + subscriber(for: \.hasOnlySecureContent), + subscriber(for: \.serverTrust), + subscriber(for: \.canGoBack), + subscriber(for: \.canGoForward) + ] + } +} + +extension WebViewStore { + func update(_ HTML: String, baseURL: URL? = nil) { + webView.evaluateJavaScript("window.pageYOffset") { [weak self] object, error in + self?.webView.loadHTMLString(HTML, baseURL: baseURL) + + if let offset = object as? Int { + self?.scrollTo(offset) + } + } + } + + private func scrollTo(_ YOffset: Int) { + let script = "window.scrollTo(0, \(YOffset));" + let scrollScript = WKUserScript(source: script, injectionTime: .atDocumentEnd, forMainFrameOnly: true) + self.webView.configuration.userContentController.addUserScript(scrollScript) + } +} + +#if os(iOS) +struct WebView: View, UIViewRepresentable { + let webView: WKWebView + + init(webView: WKWebView) { + self.webView = webView + } + + func makeUIView(context: Context) -> WKWebView { + return webView + } + + func updateUIView(_ view: WKWebView, context: Context) {} +} +#elseif os(macOS) +struct WebView: View, NSViewRepresentable { + let webView: WKWebView + + init(webView: WKWebView) { + self.webView = webView + } + + func makeNSView(context: Context) -> WKWebView { + return webView + } + + func updateNSView(_ view: WKWebView, context: Context) {} +} +#endif + diff --git a/MobileFort/MobileFort.xcodeproj/project.pbxproj b/MobileFort/MobileFort.xcodeproj/project.pbxproj index bdda41b..e5e79b1 100644 --- a/MobileFort/MobileFort.xcodeproj/project.pbxproj +++ b/MobileFort/MobileFort.xcodeproj/project.pbxproj @@ -24,10 +24,11 @@ 03B2BA9F2565555500483FE3 /* MainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03B2BA9E2565555500483FE3 /* MainView.swift */; }; 03B2BAA4256556C800483FE3 /* URLImage in Frameworks */ = {isa = PBXBuildFile; productRef = 03B2BAA3256556C800483FE3 /* URLImage */; }; 03B2BAA8256556D100483FE3 /* URLImage in Frameworks */ = {isa = PBXBuildFile; productRef = 03B2BAA7256556D100483FE3 /* URLImage */; }; + 03B2BAB225655A1100483FE3 /* WebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03B2BAB125655A1100483FE3 /* WebView.swift */; }; + 03B2BAB325655A1100483FE3 /* WebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03B2BAB125655A1100483FE3 /* WebView.swift */; }; 03BCD7432488947200DA1F27 /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03BCD7422488947200DA1F27 /* ProfileView.swift */; }; 03BCD7452488948200DA1F27 /* PostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03BCD7442488948200DA1F27 /* PostView.swift */; }; 03BCD74A2489322500DA1F27 /* AttributedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03BCD7492489322500DA1F27 /* AttributedText.swift */; }; - 03BCD74D2489365600DA1F27 /* WebView in Frameworks */ = {isa = PBXBuildFile; productRef = 03BCD74C2489365600DA1F27 /* WebView */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -48,6 +49,7 @@ 03B2BA6825654E9800483FE3 /* MobileFort.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MobileFort.entitlements; sourceTree = ""; }; 03B2BA84256550FA00483FE3 /* AttributedText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedText.swift; sourceTree = ""; }; 03B2BA9E2565555500483FE3 /* MainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainView.swift; sourceTree = ""; }; + 03B2BAB125655A1100483FE3 /* WebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebView.swift; sourceTree = ""; }; 03BCD7422488947200DA1F27 /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = ""; }; 03BCD7442488948200DA1F27 /* PostView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostView.swift; sourceTree = ""; }; 03BCD7492489322500DA1F27 /* AttributedText.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedText.swift; sourceTree = ""; }; @@ -58,7 +60,6 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 03BCD74D2489365600DA1F27 /* WebView in Frameworks */, 03B2BAA8256556D100483FE3 /* URLImage in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -137,6 +138,7 @@ 03427F6C248887D200A0073D /* Common.swift */, 03BCD7422488947200DA1F27 /* ProfileView.swift */, 03BCD7442488948200DA1F27 /* PostView.swift */, + 03B2BAB125655A1100483FE3 /* WebView.swift */, ); path = Common; sourceTree = ""; @@ -165,7 +167,6 @@ ); name = "MobileFort (iOS)"; packageProductDependencies = ( - 03BCD74C2489365600DA1F27 /* WebView */, 03B2BAA7256556D100483FE3 /* URLImage */, ); productName = MobileFort; @@ -219,7 +220,6 @@ ); mainGroup = 03427F4C2488856C00A0073D; packageReferences = ( - 03BCD74B2489365600DA1F27 /* XCRemoteSwiftPackageReference "SwiftUI-WebView" */, 03B2BAA2256556C800483FE3 /* XCRemoteSwiftPackageReference "url-image" */, ); productRefGroup = 03427F562488856C00A0073D /* Products */; @@ -260,6 +260,7 @@ buildActionMask = 2147483647; files = ( 03427F6D248887D200A0073D /* Common.swift in Sources */, + 03B2BAB225655A1100483FE3 /* WebView.swift in Sources */, 03427F592488856C00A0073D /* AppDelegate.swift in Sources */, 03BCD7432488947200DA1F27 /* ProfileView.swift in Sources */, 03427F5B2488856C00A0073D /* SceneDelegate.swift in Sources */, @@ -279,6 +280,7 @@ 03B2BA7125654F0500483FE3 /* ProfileView.swift in Sources */, 03B2BA7225654F0500483FE3 /* PostView.swift in Sources */, 03B2BA5C25654E9700483FE3 /* AppDelegate.swift in Sources */, + 03B2BAB325655A1100483FE3 /* WebView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -559,14 +561,6 @@ minimumVersion = 2.1.9; }; }; - 03BCD74B2489365600DA1F27 /* XCRemoteSwiftPackageReference "SwiftUI-WebView" */ = { - isa = XCRemoteSwiftPackageReference; - repositoryURL = "https://github.com/kylehickinson/SwiftUI-WebView"; - requirement = { - kind = upToNextMajorVersion; - minimumVersion = 0.2.0; - }; - }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ @@ -580,11 +574,6 @@ package = 03B2BAA2256556C800483FE3 /* XCRemoteSwiftPackageReference "url-image" */; productName = URLImage; }; - 03BCD74C2489365600DA1F27 /* WebView */ = { - isa = XCSwiftPackageProductDependency; - package = 03BCD74B2489365600DA1F27 /* XCRemoteSwiftPackageReference "SwiftUI-WebView" */; - productName = WebView; - }; /* End XCSwiftPackageProductDependency section */ }; rootObject = 03427F4D2488856C00A0073D /* Project object */; diff --git a/MobileFort/MobileFort.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/MobileFort/MobileFort.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 3d35253..54be3b3 100644 --- a/MobileFort/MobileFort.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/MobileFort/MobileFort.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,15 +1,6 @@ { "object": { "pins": [ - { - "package": "WebView", - "repositoryURL": "https://github.com/kylehickinson/SwiftUI-WebView", - "state": { - "branch": null, - "revision": "ec4352095428a06423868b3b2a92fdaa3c6281f7", - "version": "0.2.0" - } - }, { "package": "URLImage", "repositoryURL": "https://github.com/dmytro-anokhin/url-image",