Created
October 6, 2024 19:51
-
-
Save thomsmed/ee2c388ab89c3f56d82f1c7eae4d7e87 to your computer and use it in GitHub Desktop.
Simple example on how to do initial configuration/setup for different modules in an app on app startup.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppModuleEnvironmentConfiguration.swift | |
// | |
import SwiftUI | |
// MARK: AppDelegate | |
final class AppDelegate: NSObject, UIApplicationDelegate { | |
func application( | |
_ application: UIApplication, | |
willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil | |
) -> Bool { | |
let moduleOneConfiguration = ModuleOneEnvironment.Configuration( | |
appName: AppEnvironment.shared.bundleDisplayName, | |
appVersion: AppEnvironment.shared.bundleVersion, | |
appBuildNumber: AppEnvironment.shared.bundleShortVersionString, | |
baseURL: URL(string: AppEnvironment.value(for: "ModuleOneBaseURL"))! | |
) | |
ModuleOneEnvironment.shared.update(basedOn: moduleOneConfiguration) | |
let moduleTwoConfiguration = ModuleTwoEnvironment.Configuration( | |
appName: AppEnvironment.shared.bundleDisplayName, | |
appVersion: AppEnvironment.shared.bundleVersion, | |
appBuildNumber: AppEnvironment.shared.bundleShortVersionString, | |
baseURL: URL(string: AppEnvironment.value(for: "ModuleOneBaseURL"))! | |
) | |
ModuleTwoEnvironment.shared.update(basedOn: moduleTwoConfiguration) | |
return true | |
} | |
func application( | |
_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil | |
) -> Bool { | |
return true | |
} | |
} | |
// MARK: Main Target | |
public final class AppEnvironment: Sendable { | |
public static func value<Value>(for key: String) -> Value { | |
guard | |
let value = Bundle.main.infoDictionary?[key] as? Value | |
else { | |
fatalError("Missing value for \(key) in Info.plist") | |
} | |
return value | |
} | |
public static let shared: AppEnvironment = AppEnvironment() | |
public let bundleDisplayName: String | |
public let bundleVersion: String | |
public let bundleShortVersionString: String | |
public init() { | |
bundleDisplayName = Self.value(for: "CFBundleDisplayName") | |
bundleVersion = Self.value(for: "CFBundleVersion") | |
bundleShortVersionString = Self.value(for: "CFBundleShortVersionString") | |
} | |
} | |
// MARK: Module One | |
public final class ModuleOneEnvironment: Sendable { | |
public struct Configuration { | |
public let appName: String | |
public let appVersion: String | |
public let appBuildNumber: String | |
public let baseURL: URL | |
} | |
public static let shared: ModuleOneEnvironment = ModuleOneEnvironment() | |
nonisolated(unsafe) public private(set) var appName: String = "" | |
nonisolated(unsafe) public private(set) var appVersion: String = "0.0.0" | |
nonisolated(unsafe) public private(set) var appBuildNumber: String = "0000" | |
nonisolated(unsafe) public private(set) var baseURL: URL = URL(string: "https://localhost")! | |
public func update(basedOn configuration: Configuration) { | |
appName = configuration.appName | |
appVersion = configuration.appVersion | |
appBuildNumber = configuration.appBuildNumber | |
baseURL = configuration.baseURL | |
} | |
} | |
extension URL { | |
var moduleOneBaseURL: URL { | |
ModuleOneEnvironment.shared.baseURL | |
} | |
} | |
// MARK: Module Two | |
public final class ModuleTwoEnvironment: Sendable { | |
public struct Configuration { | |
public let appName: String | |
public let appVersion: String | |
public let appBuildNumber: String | |
public let baseURL: URL | |
} | |
public static let shared: ModuleTwoEnvironment = ModuleTwoEnvironment() | |
nonisolated(unsafe) public private(set) var appName: String = "" | |
nonisolated(unsafe) public private(set) var appVersion: String = "0.0.0" | |
nonisolated(unsafe) public private(set) var appBuildNumber: String = "0000" | |
nonisolated(unsafe) public private(set) var baseURL: URL = URL(string: "https://localhost")! | |
public func update(basedOn configuration: Configuration) { | |
appName = configuration.appName | |
appVersion = configuration.appVersion | |
appBuildNumber = configuration.appBuildNumber | |
baseURL = configuration.baseURL | |
} | |
} | |
extension URL { | |
var moduleTwoBaseURL: URL { | |
ModuleTwoEnvironment.shared.baseURL | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment