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

Merge Turbo and Strada config into Hotwire config #13

Merged
merged 1 commit into from
Apr 7, 2024
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
4 changes: 2 additions & 2 deletions Source/Bridge/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public extension Message {
{
let updatedData: String?
do {
let jsonData = try Strada.config.jsonEncoder.encode(data)
let jsonData = try Hotwire.config.jsonEncoder.encode(data)
updatedData = String(data: jsonData, encoding: .utf8)
} catch {
logger.error("Error encoding codable object: \(String(describing: data)) -> \(error)")
Expand All @@ -79,7 +79,7 @@ public extension Message {
}

do {
let decoder = Strada.config.jsonDecoder
let decoder = Hotwire.config.jsonDecoder
return try decoder.decode(T.self, from: data)
} catch {
logger.error("Error decoding json: \(jsonData) -> \(error)")
Expand Down
2 changes: 0 additions & 2 deletions Source/Bridge/Strada.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Foundation

public enum Strada {
public static var config: StradaConfig = .init()

public static func userAgentSubstring(for componentTypes: [BridgeComponent.Type]) -> String {
let components = componentTypes.map { $0.name }.joined(separator: " ")
return "bridge-components: [\(components)]"
Expand Down
19 changes: 0 additions & 19 deletions Source/Bridge/StradaConfig.swift

This file was deleted.

69 changes: 44 additions & 25 deletions Source/HotwireConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import UIKit
import WebKit

public struct HotwireConfig {
public typealias WebViewBlock = (_ configuration: WKWebViewConfiguration) -> WKWebView

/// Override to set a custom user agent.
/// - Important: Include "Turbo Native" to use `turbo_native_app?` on your Rails server.
public var userAgent = "Turbo Native iOS"

/// When enabled, adds a `UIBarButtonItem` of type `.done` to the left
/// navigation bar button item on screens presented modally.
public var showDoneButtonOnModals = false
Expand All @@ -13,55 +19,68 @@ public struct HotwireConfig {
/// connecting, disconnecting, receiving/sending messages, and more.
public var debugLoggingEnabled = false {
didSet {
Turbo.config.debugLoggingEnabled = debugLoggingEnabled
Strada.config.debugLoggingEnabled = debugLoggingEnabled
TurboLogger.debugLoggingEnabled = debugLoggingEnabled
StradaLogger.debugLoggingEnabled = debugLoggingEnabled
}
}

// MARK: Turbo

/// Override to set a custom user agent.
/// - Important: Include "Turbo Native" to use `turbo_native_app?` on your Rails server.
public var userAgent = Turbo.config.userAgent {
didSet { Turbo.config.userAgent = userAgent }
}
/// Configure options for matching path rules.
public var pathConfiguration = PathConfiguration()

/// The view controller used in `TurboNavigator` for web requests. Must be
/// a `VisitableViewController` or subclass.
public var defaultViewController = Turbo.config.defaultViewController {
didSet { Turbo.config.defaultViewController = defaultViewController }
public var defaultViewController: (URL) -> VisitableViewController = { url in
VisitableViewController(url: url)
}

/// Optionally customize the web views used by each Turbo Session.
/// Ensure you return a new instance each time.
public var makeCustomWebView = Turbo.config.makeCustomWebView {
didSet { Turbo.config.makeCustomWebView = makeCustomWebView }
/// The navigation controller used in `TurboNavigator` for the main and modal stacks.
/// Must be a `UINavigationController` or subclass.
public var defaultNavigationController: () -> UINavigationController = {
UINavigationController()
}

/// Enable or disable debug logging for Turbo visits.
public var turboDebugLoggingEnabled = Turbo.config.debugLoggingEnabled {
didSet { Turbo.config.debugLoggingEnabled = turboDebugLoggingEnabled }
/// Optionally customize the web views used by each Turbo Session.
/// Ensure you return a new instance each time.
public var makeCustomWebView: WebViewBlock = { (configuration: WKWebViewConfiguration) in
WKWebView(frame: .zero, configuration: configuration)
}

// MARK: Bridge

/// Set a custom JSON encoder when parsing bridge payloads.
/// The custom encoder can be useful when you need to apply specific
/// encoding strategies, like snake case vs. camel case
public var bridgeJsonEncoder = Strada.config.jsonEncoder {
didSet { Strada.config.jsonEncoder = bridgeJsonEncoder }
}
public var jsonEncoder = JSONEncoder()

/// Set a custom JSON decoder when parsing bridge payloads.
/// The custom decoder can be useful when you need to apply specific
/// decoding strategies, like snake case vs. camel case
public var bridgeJsonDecoder = Strada.config.jsonDecoder {
didSet { Strada.config.jsonDecoder = bridgeJsonDecoder }
public var jsonDecoder = JSONDecoder()

// MARK: - Internal

public func makeWebView() -> WKWebView {
makeCustomWebView(makeWebViewConfiguration())
}

/// Enable or disable debug logging for bridge elements connecting,
/// disconnecting, receiving/sending messages, and more.
public var bridgeDebugLoggingEnabled = Strada.config.debugLoggingEnabled {
didSet { Strada.config.debugLoggingEnabled = bridgeDebugLoggingEnabled }
// MARK: - Private

private let sharedProcessPool = WKProcessPool()

// A method (not a property) because we need a new instance for each web view.
private func makeWebViewConfiguration() -> WKWebViewConfiguration {
let configuration = WKWebViewConfiguration()
configuration.applicationNameForUserAgent = userAgent
configuration.processPool = sharedProcessPool
return configuration
}
}

public extension HotwireConfig {
class PathConfiguration {
/// Enable to match the query string when applying rules in addition to the path.
public var matchQueryStrings = false
}
}
2 changes: 1 addition & 1 deletion Source/Turbo/Path Configuration/PathConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public final class PathConfiguration {

/// Returns a merged dictionary containing all the properties that match this URL.
public func properties(for url: URL) -> PathProperties {
if Turbo.config.pathConfiguration.matchQueryStrings, let query = url.query {
if Hotwire.config.pathConfiguration.matchQueryStrings, let query = url.query {
return properties(for: "\(url.path)?\(query)")
}
return properties(for: url.path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class TurboNavigationHierarchyController {

init(
delegate: TurboNavigationHierarchyControllerDelegate,
navigationController: UINavigationController = Turbo.config.defaultNavigationController(),
modalNavigationController: UINavigationController = Turbo.config.defaultNavigationController()
navigationController: UINavigationController = Hotwire.config.defaultNavigationController(),
modalNavigationController: UINavigationController = Hotwire.config.defaultNavigationController()
) {
self.delegate = delegate
self.navigationController = navigationController
Expand Down
8 changes: 4 additions & 4 deletions Source/Turbo/Turbo Navigator/TurboNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public class TurboNavigator {
/// - pathConfiguration: _optional:_ remote configuration reference
/// - delegate: _optional:_ delegate to handle custom view controllers
public convenience init(pathConfiguration: PathConfiguration? = nil, delegate: TurboNavigatorDelegate? = nil) {
let session = Session(webView: Turbo.config.makeWebView())
let session = Session(webView: Hotwire.config.makeWebView())
session.pathConfiguration = pathConfiguration

let modalSession = Session(webView: Turbo.config.makeWebView())
let modalSession = Session(webView: Hotwire.config.makeWebView())
modalSession.pathConfiguration = pathConfiguration

self.init(session: session, modalSession: modalSession, delegate: delegate)
Expand Down Expand Up @@ -131,7 +131,7 @@ public class TurboNavigator {
private func controller(for proposal: VisitProposal) -> UIViewController? {
switch delegate.handle(proposal: proposal) {
case .accept:
Turbo.config.defaultViewController(proposal.url)
Hotwire.config.defaultViewController(proposal.url)
case .acceptCustom(let customViewController):
customViewController
case .reject:
Expand Down Expand Up @@ -289,7 +289,7 @@ extension TurboNavigator {
guard let _ = session.activeVisitable?.visitableViewController,
let url = session.activeVisitable?.visitableURL else { return }

let newSession = Session(webView: Turbo.config.makeWebView())
let newSession = Session(webView: Hotwire.config.makeWebView())
newSession.pathConfiguration = session.pathConfiguration
newSession.delegate = self
newSession.webView.uiDelegate = webkitUIDelegate
Expand Down
65 changes: 0 additions & 65 deletions Source/Turbo/Turbo.swift

This file was deleted.

File renamed without changes.
Loading