check previous content

This commit is contained in:
Xinlei(Ryan) Pan 2020-04-01 19:15:49 -04:00
parent 9851050aca
commit 9cb75817e5

View File

@ -27,6 +27,10 @@ import WebKit
super.setupView() super.setupView()
createWebView(messageHandler: mvmWebViewMessageHandler) createWebView(messageHandler: mvmWebViewMessageHandler)
pinSpinnerView() pinSpinnerView()
//init height for loading spinner
webViewHeight = webView?.heightAnchor.constraint(equalToConstant: 44)
webViewHeight?.isActive = true
} }
func createWebView(messageHandler: String?) { func createWebView(messageHandler: String?) {
@ -44,24 +48,37 @@ import WebKit
addSubview(webView) addSubview(webView)
NSLayoutConstraint.constraintPinSubview(toSuperview: webView) NSLayoutConstraint.constraintPinSubview(toSuperview: webView)
} }
// MARK: - MVMCoreUIMoleculeViewProtocol // MARK: - MVMCoreUIMoleculeViewProtocol
override open func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) { override open func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable : Any]?) {
//store the previous webview properties
let previousHtmlString = webviewModel?.htmlString
let previousURL = webView?.url
super.set(with: model, delegateObject, additionalData) super.set(with: model, delegateObject, additionalData)
self.delegateObject = delegateObject self.delegateObject = delegateObject
//init height for loading spinner
webViewHeight = webView?.heightAnchor.constraint(equalToConstant: 44)
webViewHeight?.isActive = true
if let height = webviewModel?.height { if let height = webviewModel?.height {
webViewHeight?.constant = height webViewHeight?.constant = height
dynamicHeight = false dynamicHeight = false
} }
if let url = webviewModel?.url { if let url = webviewModel?.url {
webView?.load(URLRequest(url: url)) if let previousUrl = previousURL, url == previousUrl {
//dont load the new
} else {
webView?.load(URLRequest(url: url))
webViewHeight?.constant = 44
loadingSpinner.resumeSpinner()
}
} else if let htmlString = webviewModel?.htmlString { } else if let htmlString = webviewModel?.htmlString {
webView?.loadHTMLString(htmlString, baseURL: nil) if let previousHTML = previousHtmlString, previousHTML == htmlString {
//dont load the new html since they are the same html string as preivous
} else {
webViewHeight?.constant = 44
webView?.loadHTMLString(htmlString, baseURL: nil)
loadingSpinner.resumeSpinner()
}
} }
if let borderColor = webviewModel?.borderColor?.cgColor { if let borderColor = webviewModel?.borderColor?.cgColor {
@ -105,8 +122,16 @@ extension WebView : WKUIDelegate {
*/ */
if !webView.isLoading { if !webView.isLoading {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
guard let self = self, let height = result as? CGFloat else { return } guard let self = self else {
self.webViewHeight?.constant = height return
}
if let height = result as? CGFloat {
self.webViewHeight?.constant = height
} else {
//if failed to get height from javascript, using scrollview.contensize's height
let scrollHeight = self.webView?.scrollView.contentSize.height
self.webViewHeight?.constant = scrollHeight ?? 44
}
self.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self) self.delegateObject?.moleculeDelegate?.moleculeLayoutUpdated(self)
}) })
} }