Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CHNL-16220] FileIO changes #258

Merged
merged 9 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class JSTestWebViewModel: KlaviyoWebViewModeling {
private static func initializeLoadScripts() -> [String: WKUserScript] {
var scripts: [String: WKUserScript] = [:]

if let toggleHandlerScript = try? FileIO.getFileContents(path: "toggleHandler", type: "js") {
if let toggleHandlerScript = try? ResourceLoader.getResourceContents(path: "toggleHandler", type: "js") {
let script = WKUserScript(source: toggleHandlerScript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
scripts["toggleMessageHandler"] = script
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ func createKlaviyoWebPreview(viewModel: KlaviyoWebViewModeling) -> UIViewControl

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

@available(iOS 17.0, *)
#Preview("JS Test Page") {
let indexHtmlFileUrl = Bundle.module.url(forResource: "jstest", withExtension: "html")!
let indexHtmlFileUrl = try! ResourceLoader.getResourceUrl(path: "jstest", type: "html")
let viewModel = JSTestWebViewModel(url: indexHtmlFileUrl)
return KlaviyoWebViewController(viewModel: viewModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class KlaviyoWebViewModel: KlaviyoWebViewModeling {
private static func initializeLoadScripts() -> [String: WKUserScript] {
var scripts: [String: WKUserScript] = [:]

if let closeHandlerScript = try? FileIO.getFileContents(path: "closeHandler", type: "js") {
if let closeHandlerScript = try? ResourceLoader.getResourceContents(path: "closeHandler", type: "js") {
let script = WKUserScript(source: closeHandlerScript, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
scripts["closeHandler"] = script
}
Expand Down
35 changes: 0 additions & 35 deletions Sources/KlaviyoUI/Utilities/FileIO.swift

This file was deleted.

64 changes: 64 additions & 0 deletions Sources/KlaviyoUI/Utilities/ResourceLoader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// ResourceLoader.swift
// KlaviyoSwiftUIWebView
//
// Created by Andrew Balmer on 9/27/24.
//

#if DEBUG
import Foundation

enum ResourceLoaderError: Error {
case resourceNotFound
}

enum ResourceLoader {
static func getResourceUrl(path: String, type: String) throws -> URL {
let bundle = resourceBundle()

guard let resourceUrl = bundle?.url(forResource: path, withExtension: type) else {
throw ResourceLoaderError.resourceNotFound
}

return resourceUrl
}

static func getResourceContents(path: String, type: String) throws -> String {
let bundle = resourceBundle()

guard let resourcePath = bundle?.path(forResource: path, ofType: type) else {
throw ResourceLoaderError.resourceNotFound
}

do {
let contents = try String(contentsOfFile: resourcePath, encoding: .utf8)
return contents
} catch {
throw error
}
}

// Determines the appropriate bundle based on the build system
private static func resourceBundle() -> Bundle? {
#if SWIFT_PACKAGE
return Bundle.module
#else
return Bundle(for: BundleLocator.self).resourceBundle(named: "KlaviyoUIResources")
#endif
}
}

// Helper class for locating the resource bundle (CocoaPods)
private class BundleLocator {}

extension Bundle {
fileprivate func resourceBundle(named name: String) -> Bundle? {
guard let bundleUrl = url(forResource: name, withExtension: "bundle"),
let bundle = Bundle(url: bundleUrl) else {
return nil
}

return bundle
}
}
#endif
Loading