Skip to content

Commit

Permalink
Merge pull request #55 from hotwired/global-config-consolidation
Browse files Browse the repository at this point in the history
Global configuration consolidation
  • Loading branch information
svara authored Dec 16, 2024
2 parents 08bdabe + 05373db commit 900e458
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 47 deletions.
29 changes: 22 additions & 7 deletions Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import UIKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Hotwire.config.backButtonDisplayMode = .minimal
Hotwire.config.showDoneButtonOnModals = true

#if DEBUG
Hotwire.config.debugLoggingEnabled = true
#endif

configureHotwire()
return true
}

Expand All @@ -19,4 +13,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

private func configureHotwire() {
// Load the path configuration
Hotwire.loadPathConfiguration(from: [
.file(Bundle.main.url(forResource: "path-configuration", withExtension: "json")!)
])

// Register bridge components
Hotwire.registerBridgeComponents([
FormComponent.self,
MenuComponent.self,
OverflowMenuComponent.self,
])

// Set configuration options
Hotwire.config.backButtonDisplayMode = .minimal
Hotwire.config.showDoneButtonOnModals = true
#if DEBUG
Hotwire.config.debugLoggingEnabled = true
#endif
}
}
30 changes: 2 additions & 28 deletions Demo/SceneController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,24 @@ final class SceneController: UIResponder {
var window: UIWindow?

private let rootURL = Demo.current
private lazy var navigator = Navigator(pathConfiguration: pathConfiguration, delegate: self)

// MARK: - Setup

private func configureBridge() {
Hotwire.registerBridgeComponents([
FormComponent.self,
MenuComponent.self,
OverflowMenuComponent.self,
])
}

private func configureRootViewController() {
guard let window = window else {
fatalError()
}

window.rootViewController = navigator.rootViewController
}
private lazy var navigator = Navigator(delegate: self)

// MARK: - Authentication

private func promptForAuthentication() {
let authURL = rootURL.appendingPathComponent("/signin")
navigator.route(authURL)
}

// MARK: - Path Configuration

private lazy var pathConfiguration = PathConfiguration(sources: [
.file(Bundle.main.url(forResource: "path-configuration", withExtension: "json")!),
])
}

extension SceneController: UIWindowSceneDelegate {
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }

window = UIWindow(windowScene: windowScene)
window?.rootViewController = navigator.rootViewController
window?.makeKeyAndVisible()

configureBridge()
configureRootViewController()

navigator.route(rootURL)
}
}
Expand Down
9 changes: 9 additions & 0 deletions Source/Hotwire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,14 @@ public enum Hotwire {
}
}


/// Loads the `PathConfiguration` JSON file(s) from the provided sources
/// to configure navigation rules
/// - Parameter sources: An array of `PathConfiguration.Source` objects representing
/// the sources to load.
public static func loadPathConfiguration(from sources: [PathConfiguration.Source]) {
config.pathConfiguration.sources = sources
}

static var bridgeComponentTypes = [BridgeComponent.Type]()
}
8 changes: 0 additions & 8 deletions Source/HotwireConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,3 @@ public struct HotwireConfig {
return configuration
}
}

public extension HotwireConfig {
class PathConfiguration {
/// Enable to include the query string (in addition to the path) when applying rules.
/// Disable to only consider the path when applying rules.
public var matchQueryStrings = true
}
}
7 changes: 3 additions & 4 deletions Source/Turbo/Navigator/Navigator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ public class Navigator {

/// Convenience initializer that doesn't require manually creating `Session` instances.
/// - Parameters:
/// - pathConfiguration: _optional:_ remote configuration reference
/// - delegate: _optional:_ delegate to handle custom view controllers
public convenience init(pathConfiguration: PathConfiguration? = nil, delegate: NavigatorDelegate? = nil) {
public convenience init(delegate: NavigatorDelegate? = nil) {
let session = Session(webView: Hotwire.config.makeWebView())
session.pathConfiguration = pathConfiguration
session.pathConfiguration = Hotwire.config.pathConfiguration

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

self.init(session: session, modalSession: modalSession, delegate: delegate)
}
Expand Down
4 changes: 4 additions & 0 deletions Source/Turbo/Path Configuration/PathConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ public struct PathConfigurationLoaderOptions {
public final class PathConfiguration {
public weak var delegate: PathConfigurationDelegate?

/// Enable to include the query string (in addition to the path) when applying rules.
/// Disable to only consider the path when applying rules.
public var matchQueryStrings = true

/// Returns top-level settings: `{ settings: {} }`
public private(set) var settings: [String: AnyHashable] = [:]

Expand Down

0 comments on commit 900e458

Please sign in to comment.