Merge branch 'feature/webviewmodel' into 'develop'
Feature/webviewmodel See merge request BPHV_MIPS/mvm_core_ui!335
This commit is contained in:
commit
fcbec4d60f
@ -139,6 +139,8 @@
|
||||
9432A79F23DB47BA00719041 /* EntryFieldContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9432A79E23DB47BA00719041 /* EntryFieldContainer.swift */; };
|
||||
943784F5236B77BB006A1E82 /* GraphView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 943784F3236B77BB006A1E82 /* GraphView.swift */; };
|
||||
943784F6236B77BB006A1E82 /* GraphViewAnimationHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 943784F4236B77BB006A1E82 /* GraphViewAnimationHandler.swift */; };
|
||||
943820842432382400B43AF3 /* WebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 943820832432382400B43AF3 /* WebView.swift */; };
|
||||
94382086243238D100B43AF3 /* WebViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94382085243238D100B43AF3 /* WebViewModel.swift */; };
|
||||
9445890C2385BCE300DE9FD4 /* ProgressBarModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9445890B2385BCE300DE9FD4 /* ProgressBarModel.swift */; };
|
||||
9445890E2385C3F800DE9FD4 /* MultiProgressModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9445890D2385C3F800DE9FD4 /* MultiProgressModel.swift */; };
|
||||
9445891F2385D2E900DE9FD4 /* CaretViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9445891E2385D2E900DE9FD4 /* CaretViewModel.swift */; };
|
||||
@ -530,6 +532,8 @@
|
||||
9432A79E23DB47BA00719041 /* EntryFieldContainer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EntryFieldContainer.swift; sourceTree = "<group>"; };
|
||||
943784F3236B77BB006A1E82 /* GraphView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GraphView.swift; sourceTree = "<group>"; };
|
||||
943784F4236B77BB006A1E82 /* GraphViewAnimationHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GraphViewAnimationHandler.swift; sourceTree = "<group>"; };
|
||||
943820832432382400B43AF3 /* WebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebView.swift; sourceTree = "<group>"; };
|
||||
94382085243238D100B43AF3 /* WebViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewModel.swift; sourceTree = "<group>"; };
|
||||
9445890B2385BCE300DE9FD4 /* ProgressBarModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgressBarModel.swift; sourceTree = "<group>"; };
|
||||
9445890D2385C3F800DE9FD4 /* MultiProgressModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiProgressModel.swift; sourceTree = "<group>"; };
|
||||
9445891E2385D2E900DE9FD4 /* CaretViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CaretViewModel.swift; sourceTree = "<group>"; };
|
||||
@ -1552,6 +1556,8 @@
|
||||
0AA33B392398524F0067DD0F /* Toggle.swift */,
|
||||
0AE98BB623FF18E9004C5109 /* ArrowModel.swift */,
|
||||
0AE98BB423FF18D2004C5109 /* Arrow.swift */,
|
||||
94382085243238D100B43AF3 /* WebViewModel.swift */,
|
||||
943820832432382400B43AF3 /* WebView.swift */,
|
||||
);
|
||||
path = Views;
|
||||
sourceTree = "<group>";
|
||||
@ -1928,6 +1934,7 @@
|
||||
0116A4E5228B19640094F3ED /* RadioButtonSelectionHelper.swift in Sources */,
|
||||
017BEB48236230DB0024EF95 /* MoleculeViewProtocol.swift in Sources */,
|
||||
D2E1FADB2260D3D200AEFD8C /* MVMCoreUIDelegateObject.swift in Sources */,
|
||||
94382086243238D100B43AF3 /* WebViewModel.swift in Sources */,
|
||||
D27CD40E2322EEAF00C1DC07 /* TabsTableViewCell.swift in Sources */,
|
||||
D224799B231965AD003FCCF9 /* AccordionMoleculeTableViewCell.swift in Sources */,
|
||||
0A6682A42434DB8D00AD3CA1 /* ListLeftVariableRadioButtonBodyTextModel.swift in Sources */,
|
||||
@ -1998,6 +2005,7 @@
|
||||
D29DF2A221E7AF4E003B2FB9 /* MVMCoreUIUtility.m in Sources */,
|
||||
D29DF12B21E6851E003B2FB9 /* MVMCoreUITopAlertExpandableView.m in Sources */,
|
||||
94C2D9A723872DA90006CF46 /* LabelAttributeColorModel.swift in Sources */,
|
||||
943820842432382400B43AF3 /* WebView.swift in Sources */,
|
||||
0103B84E23D7E33A009C315C /* HeadlineBodyToggleModel.swift in Sources */,
|
||||
D2755D7B23689C7500485468 /* TableViewCell.swift in Sources */,
|
||||
0A21DB85235E06EF00C160A2 /* MFTextField.m in Sources */,
|
||||
|
||||
196
MVMCoreUI/Atomic/Atoms/Views/WebView.swift
Normal file
196
MVMCoreUI/Atomic/Atoms/Views/WebView.swift
Normal file
@ -0,0 +1,196 @@
|
||||
//
|
||||
// MVMCoreUIWebView.swift
|
||||
// MVMCoreUI
|
||||
//
|
||||
// Created by Ryan on 8/29/19.
|
||||
// Copyright © 2019 Verizon Wireless. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import WebKit
|
||||
|
||||
@objcMembers open class WebView: View, MVMCoreUIViewConstrainingProtocol {
|
||||
|
||||
let mvmWebViewMessageHandler = "mvmWebViewMessageHandler"
|
||||
|
||||
var webviewModel: WebViewModel? {
|
||||
return model as? WebViewModel
|
||||
}
|
||||
var webView: WKWebView?
|
||||
var overLayer = MVMCoreUICommonViewsUtility.commonView()
|
||||
public let loadingSpinner = MFLoadingSpinner(frame: .zero)
|
||||
var delegateObject: MVMCoreUIDelegateObject?
|
||||
var webViewHeight: NSLayoutConstraint?
|
||||
var dynamicHeight: Bool = true
|
||||
|
||||
|
||||
override open func setupView() {
|
||||
super.setupView()
|
||||
createWebView(messageHandler: mvmWebViewMessageHandler)
|
||||
setupOverLayer()
|
||||
pinSpinnerView()
|
||||
|
||||
//init height for loading spinner
|
||||
webViewHeight = webView?.heightAnchor.constraint(equalToConstant: 44)
|
||||
webViewHeight?.isActive = true
|
||||
}
|
||||
|
||||
func createWebView(messageHandler: String?) {
|
||||
let wkUserController = WKUserContentController()
|
||||
if let messageHandlerName = messageHandler {
|
||||
wkUserController.add(self, name: messageHandlerName)
|
||||
}
|
||||
let wkConfig = WKWebViewConfiguration()
|
||||
wkConfig.userContentController = wkUserController
|
||||
let webView = WKWebView(frame: .zero, configuration: wkConfig)
|
||||
webView.translatesAutoresizingMaskIntoConstraints = false
|
||||
webView.uiDelegate = self
|
||||
webView.navigationDelegate = self
|
||||
self.webView = webView
|
||||
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
|
||||
var webViewHeightConstant: CGFloat = 44
|
||||
if let height = webviewModel?.height {
|
||||
webViewHeightConstant = height
|
||||
webViewHeight?.constant = height
|
||||
dynamicHeight = false
|
||||
}
|
||||
if let url = webviewModel?.url {
|
||||
if let previousUrl = previousURL, url == previousUrl {
|
||||
//dont load the new
|
||||
} else {
|
||||
webView?.stopLoading()
|
||||
webView?.load(URLRequest(url: url))
|
||||
webViewHeight?.constant = webViewHeightConstant
|
||||
overLayer.isHidden = false
|
||||
loadingSpinner.resumeSpinner()
|
||||
}
|
||||
} else if let htmlString = webviewModel?.htmlString {
|
||||
if let previousHTML = previousHtmlString, previousHTML == htmlString {
|
||||
//dont load the new html since they are the same html string as preivous
|
||||
} else {
|
||||
webView?.stopLoading()
|
||||
webViewHeight?.constant = webViewHeightConstant
|
||||
webView?.loadHTMLString(htmlString, baseURL: nil)
|
||||
overLayer.isHidden = false
|
||||
loadingSpinner.resumeSpinner()
|
||||
}
|
||||
}
|
||||
|
||||
if let borderColor = webviewModel?.borderColor?.cgColor {
|
||||
webView?.layer.borderWidth = 1.0
|
||||
webView?.layer.borderColor = borderColor
|
||||
} else {
|
||||
webView?.layer.borderWidth = 0.0
|
||||
webView?.layer.borderColor = UIColor.clear.cgColor
|
||||
}
|
||||
}
|
||||
|
||||
func pinSpinnerView() {
|
||||
addSubview(loadingSpinner)
|
||||
// Setup spinner.
|
||||
loadingSpinner.clipsToBounds = true
|
||||
loadingSpinner.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
loadingSpinner.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
|
||||
loadingSpinner.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
|
||||
loadingSpinner.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
|
||||
loadingSpinner.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
|
||||
}
|
||||
|
||||
func setupOverLayer() {
|
||||
addSubview(overLayer)
|
||||
overLayer.backgroundColor = .white
|
||||
NSLayoutConstraint.constraintPinSubview(toSuperview: overLayer)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - WKUIDelegate
|
||||
extension WebView : WKUIDelegate {
|
||||
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
||||
// hide loading
|
||||
overLayer.isHidden = true
|
||||
loadingSpinner.pause()
|
||||
|
||||
//update webview's heigth when webview is ready
|
||||
if !dynamicHeight {
|
||||
return
|
||||
}
|
||||
/* was using "document.readyState" to check the state, while evaluateJavaScript "document.readyState",only works when webview contains userscrpt.otherwise, it would return WKErrorDomain Code=4 "A JavaScript exception occurred".
|
||||
so webView.isLoading to check load finished state
|
||||
*/
|
||||
if !webView.isLoading {
|
||||
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
|
||||
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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
|
||||
//actually no error handle page show in webview. We can handle the error display view by our self.
|
||||
//or stop loading by default
|
||||
overLayer.isHidden = true
|
||||
loadingSpinner.pause()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// MARK: - WKNavigationDelegate
|
||||
extension WebView : WKNavigationDelegate {
|
||||
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
||||
//validate request url
|
||||
//all validated link should be open in safari
|
||||
if (navigationAction.navigationType == .linkActivated), let urlString = navigationAction.request.url?.absoluteString.removingPercentEncoding, !urlString.contains("#") {
|
||||
MVMCoreActionHandler.shared()?.openURL(inWebView: navigationAction.request.url, actionInformation: nil, additionalData: nil, delegateObject: nil)
|
||||
decisionHandler(.cancel)
|
||||
} else {
|
||||
decisionHandler(.allow)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - WKScriptMessageHandler
|
||||
extension WebView: WKScriptMessageHandler {
|
||||
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
||||
if message.name == mvmWebViewMessageHandler, let text = message.body as? String {
|
||||
/*
|
||||
receiving JavaScript func webkit.messageHandlers.{callHandler}.postMessage(body);
|
||||
if body is string, will decode to actionmodel.
|
||||
use legacy MVMCoreActionHanlder handleAction method for now
|
||||
*/
|
||||
if let data = text.data(using: .utf8) {
|
||||
do {
|
||||
let actionMap = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
|
||||
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: nil, delegateObject: self.delegateObject)
|
||||
} catch {
|
||||
//actionModel should report error when actionhandler is finshed with actionmodel
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
50
MVMCoreUI/Atomic/Atoms/Views/WebViewModel.swift
Normal file
50
MVMCoreUI/Atomic/Atoms/Views/WebViewModel.swift
Normal file
@ -0,0 +1,50 @@
|
||||
//
|
||||
// WebviewModel.swift
|
||||
// MVMCoreUI
|
||||
//
|
||||
// Created by Kruthika KP on 09/03/20.
|
||||
// Copyright © 2020 Verizon Wireless. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@objcMembers public class WebViewModel: MoleculeModelProtocol {
|
||||
public static var identifier: String = "webview"
|
||||
public var moleculeName: String = WebViewModel.identifier
|
||||
public var backgroundColor: Color?
|
||||
public var url: URL?
|
||||
public var htmlString: String?
|
||||
public var height: CGFloat?
|
||||
public var borderColor: Color?
|
||||
|
||||
private enum CodingKeys: String, CodingKey{
|
||||
case moleculeName
|
||||
case backgroundColor
|
||||
case url
|
||||
case htmlString
|
||||
case height
|
||||
case borderColor
|
||||
}
|
||||
|
||||
required public init(from decoder: Decoder) throws {
|
||||
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
|
||||
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
|
||||
url = try typeContainer.decodeIfPresent(URL.self, forKey: .url)
|
||||
htmlString = try typeContainer.decodeIfPresent(String.self, forKey: .htmlString)
|
||||
if url == nil, htmlString == nil {
|
||||
throw ModelRegistry.Error.decoderErrorModelNotMapped
|
||||
}
|
||||
height = try typeContainer.decodeIfPresent(CGFloat.self, forKey: .height)
|
||||
borderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .borderColor)
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
try container.encode(moleculeName, forKey: .moleculeName)
|
||||
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
|
||||
try container.encodeIfPresent(url, forKey: .url)
|
||||
try container.encodeIfPresent(htmlString, forKey: .htmlString)
|
||||
try container.encodeIfPresent(height, forKey: .height)
|
||||
try container.encodeIfPresent(borderColor, forKey: .borderColor)
|
||||
}
|
||||
}
|
||||
@ -80,6 +80,7 @@ import Foundation
|
||||
MoleculeObjectMapping.shared()?.register(viewClass: Arrow.self, viewModelClass: ArrowModel.self)
|
||||
MoleculeObjectMapping.shared()?.register(viewClass: RadioButton.self, viewModelClass: RadioButtonModel.self)
|
||||
MoleculeObjectMapping.shared()?.register(viewClass: RadioButtonLabel.self, viewModelClass: RadioButtonLabelModel.self)
|
||||
MoleculeObjectMapping.shared()?.register(viewClass: WebView.self, viewModelClass: WebViewModel.self)
|
||||
|
||||
// Horizontal Combination Molecules
|
||||
MoleculeObjectMapping.shared()?.register(viewClass: StringAndMoleculeView.self, viewModelClass: StringAndMoleculeModel.self)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user