-
Notifications
You must be signed in to change notification settings - Fork 93
Description
Hi,
What do you think about adding support for loading xml data from a Data instance in the LayoutLoading extension?. Currently there's only support for loading from a local file or URL.
Suggested function signature (which pretty much mirrors the LayoutNode's init):
public func loadLayout(
xmlData: Data,
url: URL? = nil,
state: Any = (),
constants: [String :Any] = [:]
)
This could be used, amongst other things, to load xml blobs from CoreData or having xml data as strings in the code.
I know that it's already possible to do this if you create the LayoutNode yourself using the init(xmlData: Data) initialiser and set it as the layoutNode instance on the LayoutViewController.
However, I think it would be convenient to have the option available in the LayoutLoading extension and use it inside viewDidLoad. I guess the API found in LayoutViewController is what most developer will use to setup the view, right?
Could probably do a PR for this if it's of any interest?
Some neat this that could be done is showing a spinner while loading content from a remote server.
let data = """
<UIView backgroundColor="#767676" text="Hello"
xml="https://raw.githubusercontent.com/schibsted/layout/master/SampleApp/PageTemplate.xml">
<param name="text" type="String"/> <!-- This is required when loading async -->
<UIActivityIndicatorView top="50% - height/2" left="50% - width/2"
activityIndicatorViewStyle="whiteLarge" isAnimating="true" color="gray" />
</UIView>
"""
loadLayout(xmlData: data.data(using: .utf8)!)
Actually, I think I found an issue while trying this out. If I point to a remote resource, the loading fails with an "unknown property 'text'" error. If I point to the same file locally, it works.
Adding <param name="text" type="String"/> to the body fixes the issue but I don't think it's the correct behaviour, right? Should I file another issue for this?
As a side note... In a post 1.0 world, I think it would be cool to support custom url protocol providers. A protocol provider would be responsible for fetching and providing xml data based on the protocol in the url. For example, I could register a protocol provider, myresources://. When the layout engine finds any xml resources to load with that protocol, it hands over the loading to the provider. A typical provider could be one that fetches a signed resources and before it's handed over to the Layout engine the signature is verified :)