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()
createWebView(messageHandler: mvmWebViewMessageHandler)
pinSpinnerView()
//init height for loading spinner
webViewHeight = webView?.heightAnchor.constraint(equalToConstant: 44)
webViewHeight?.isActive = true
}
func createWebView(messageHandler: String?) {
@ -44,24 +48,37 @@ import WebKit
addSubview(webView)
NSLayoutConstraint.constraintPinSubview(toSuperview: webView)
}
// MARK: - MVMCoreUIMoleculeViewProtocol
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)
self.delegateObject = delegateObject
//init height for loading spinner
webViewHeight = webView?.heightAnchor.constraint(equalToConstant: 44)
webViewHeight?.isActive = true
if let height = webviewModel?.height {
webViewHeight?.constant = height
dynamicHeight = false
}
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 {
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 {
@ -105,8 +122,16 @@ extension WebView : WKUIDelegate {
*/
if !webView.isLoading {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
guard let self = self, let height = result as? CGFloat else { return }
self.webViewHeight?.constant = height
guard let self = self else {
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)
})
}