Skip to content

Commit

Permalink
Mimic Strada configuration for Turbo
Browse files Browse the repository at this point in the history
  • Loading branch information
joemasilotti committed Nov 13, 2023
1 parent 249adbf commit 6443d9a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
#if DEBUG
TurboLog.debugLoggingEnabled = true
Turbo.config.debugLoggingEnabled = true
Strada.config.debugLoggingEnabled = true
#endif

Expand Down
5 changes: 2 additions & 3 deletions Demo/SceneController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ final class SceneController: UIResponder {
// MARK: - Setup

private func configureStrada() {
TurboConfig.shared.userAgent +=
" \(Strada.userAgentSubstring(for: BridgeComponent.allTypes))"
Turbo.config.userAgent += " \(Strada.userAgentSubstring(for: BridgeComponent.allTypes))"

TurboConfig.shared.makeCustomWebView = { config in
Turbo.config.makeCustomWebView = { config in
config.defaultWebpagePreferences?.preferredContentMode = .mobile

let webView = WKWebView(frame: .zero, configuration: .appConfiguration)
Expand Down
8 changes: 4 additions & 4 deletions Source/Logging.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import Foundation

public struct TurboLog {
public static var debugLoggingEnabled = false
enum TurboLogger {
static var debugLoggingEnabled = false
}

/// Simple function to help in debugging, a noop in Release builds
func debugLog(_ message: String, _ arguments: [String: Any] = [:]) {
let timestamp = Date()

log("\(timestamp) \(message) \(arguments)")
}

Expand All @@ -16,7 +16,7 @@ func debugPrint(_ message: String) {
}

private func log(_ message: String) {
if TurboLog.debugLoggingEnabled {
if TurboLogger.debugLoggingEnabled {
print(message)
}
}
4 changes: 2 additions & 2 deletions Source/Turbo Navigator/TurboNavigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public class TurboNavigator {
/// - pathConfiguration:
/// - delegate: an optional delegate to handle custom view controllers
public convenience init(pathConfiguration: PathConfiguration, delegate: TurboNavigatorDelegate? = nil) {
let session = Session(webView: TurboConfig.shared.makeWebView())
let session = Session(webView: Turbo.config.makeWebView())
session.pathConfiguration = pathConfiguration

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

self.init(session: session, modalSession: modalSession, delegate: delegate)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import WebKit

public enum Turbo {
public static var config = TurboConfig()
}

public class TurboConfig {
public typealias WebViewBlock = (_ configuration: WKWebViewConfiguration) -> WKWebView

public static let shared = TurboConfig()

/// Override to set a custom user agent.
/// Include "Turbo Native" to use `turbo_native_app?` on your Rails server.
public var userAgent = "Turbo Native iOS"
Expand All @@ -15,6 +17,12 @@ public class TurboConfig {
WKWebView(frame: .zero, configuration: configuration)
}

public var debugLoggingEnabled = false {
didSet {
TurboLogger.debugLoggingEnabled = debugLoggingEnabled
}
}

// MARK: - Internal

public func makeWebView() -> WKWebView {
Expand All @@ -25,8 +33,6 @@ public class TurboConfig {

private let sharedProcessPool = WKProcessPool()

private init() {}

// A method (not a property) because we need a new instance for each web view.
private func makeWebViewConfiguration() -> WKWebViewConfiguration {
let configuration = WKWebViewConfiguration()
Expand Down

0 comments on commit 6443d9a

Please sign in to comment.