Skip to content

Commit

Permalink
refactored previews
Browse files Browse the repository at this point in the history
  • Loading branch information
ab1470 committed Nov 29, 2024
1 parent cecaf34 commit f980580
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions Sources/KlaviyoUI/KlaviyoWebView/KlaviyoWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,24 @@ extension KlaviyoWebViewController: WKScriptMessageHandler {
// MARK: - Previews

#if DEBUG
func createKlaviyoWebPreview(url: URL) -> UIViewController {
let viewModel = KlaviyoWebViewModel(url: url)
func createKlaviyoWebPreview(viewModel: KlaviyoWebViewModeling) -> UIViewController {
let viewController = KlaviyoWebViewController(viewModel: viewModel)

// Add a dummy view as a parent to the KlaviyoWebViewController to preview what the
// KlaviyoWebViewController might look like when it's displayed on top of a view in an app.
let parentViewController = PreviewTabViewController()

parentViewController.addChild(viewController)
parentViewController.view.addSubview(viewController.view)
viewController.didMove(toParent: parentViewController)

viewController.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
viewController.view.topAnchor.constraint(equalTo: parentViewController.view.topAnchor),
viewController.view.bottomAnchor.constraint(equalTo: parentViewController.view.bottomAnchor),
viewController.view.leadingAnchor.constraint(equalTo: parentViewController.view.leadingAnchor),
viewController.view.trailingAnchor.constraint(equalTo: parentViewController.view.trailingAnchor)
])

return parentViewController
}
Expand All @@ -149,13 +159,15 @@ func createKlaviyoWebPreview(url: URL) -> UIViewController {
@available(iOS 17.0, *)
#Preview("Klaviyo.com") {
let url = URL(string: "https://picsum.photos/200/300")!
return createKlaviyoWebPreview(url: url)
let viewModel = KlaviyoWebViewModel(url: url)
return createKlaviyoWebPreview(viewModel: viewModel)
}

@available(iOS 17.0, *)
#Preview("Klaviyo Form") {
let indexHtmlFileUrl = Bundle.module.url(forResource: "klaviyo", withExtension: "html")!
return createKlaviyoWebPreview(url: indexHtmlFileUrl)
let viewModel = KlaviyoWebViewModel(url: indexHtmlFileUrl)
return createKlaviyoWebPreview(viewModel: viewModel)
}

@available(iOS 17.0, *)
Expand Down

0 comments on commit f980580

Please sign in to comment.