Skip to content

Commit

Permalink
Support updated SwiftUI APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehickinson committed Jan 2, 2020
1 parent 216288e commit 250098c
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions Sources/WebView/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@ import SwiftUI
import Combine
import WebKit

public class WebViewStore: BindableObject {
public let webView: WKWebView
public let didChange = PassthroughSubject<Void, Never>()
public class WebViewStore: ObservableObject {
@Published public var webView: WKWebView {
didSet {
setupObservers()
}
}

public init(webView: WKWebView = WKWebView()) {
self.webView = webView

func subscriber<Value>(for keyPath: KeyPath<WKWebView, Value>) -> AnyCancellable {
return AnyCancellable(webView.publisher(for: keyPath).sink { _ in self.didChange.send() })
setupObservers()
}

private func setupObservers() {
func subscriber<Value>(for keyPath: KeyPath<WKWebView, Value>) -> NSKeyValueObservation {
return webView.observe(keyPath, options: [.prior]) { _, change in
if change.isPrior {
self.objectWillChange.send()
}
}
}
// Setup observers for all KVO compliant properties
observers = [
Expand All @@ -25,13 +35,13 @@ public class WebViewStore: BindableObject {
]
}

private var observers: [AnyCancellable] = []
private var observers: [NSKeyValueObservation] = []

deinit {
observers.forEach {
// Not even sure if this is required?
// Probably wont be needed in future betas?
$0.cancel()
$0.invalidate()
}
}
}
Expand Down

0 comments on commit 250098c

Please sign in to comment.