From a44260369664e38f0d9a0e5f47fd4233687a3000 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Sun, 3 Jul 2022 20:46:30 +0200 Subject: [PATCH] Polish bundleConfig with strong type checking --- Passepartout/App/Constants/Constants+App.swift | 10 +++------- Passepartout/Constants.swift | 10 ++++++++++ .../Launcher/Constants/Constants+Launcher.swift | 2 +- Passepartout/Launcher/Info.plist | 5 +++++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Passepartout/App/Constants/Constants+App.swift b/Passepartout/App/Constants/Constants+App.swift index 779ac479..f0249af2 100644 --- a/Passepartout/App/Constants/Constants+App.swift +++ b/Passepartout/App/Constants/Constants+App.swift @@ -27,10 +27,6 @@ import Foundation import UniformTypeIdentifiers import SwiftyBeaver -extension Constants { - private static let bundleConfig = Bundle.main.infoDictionary?["com.algoritmico.Passepartout.config"] as? [String: Any] -} - extension Constants { enum App { static var appId: String { @@ -40,9 +36,9 @@ extension Constants { return identifier } - static let appStoreId = bundleConfig?["appstore_id"] as? String ?? "DUMMY_appstore_id" + static let appStoreId: String = bundleConfig("appstore_id") - static let appGroupId = bundleConfig?["group_id"] as? String ?? "DUMMY_group_id" + static let appGroupId: String = bundleConfig("group_id") static let isBeta: Bool = { Bundle.main.isTestFlight @@ -57,7 +53,7 @@ extension Constants { return testAppType } - if let infoValue = bundleConfig?["app_type"] as? Int, + if let infoValue: Int = bundleConfig("app_type"), let testAppType = ProductManager.AppType(rawValue: infoValue) { return testAppType diff --git a/Passepartout/Constants.swift b/Passepartout/Constants.swift index 9ad20bd1..bef96aa8 100644 --- a/Passepartout/Constants.swift +++ b/Passepartout/Constants.swift @@ -26,6 +26,16 @@ import Foundation enum Constants { + static func bundleConfig(_ key: String, in bundle: Bundle? = nil) -> T { + guard let config = (bundle ?? .main).infoDictionary?["com.algoritmico.Passepartout.config"] as? [String: Any] else { + fatalError("Unable to find config bundle") + } + guard let value = config[key] as? T else { + fatalError("Missing \(key) from config bundle") + } + return value + } + enum Global { static let appName = "Passepartout" diff --git a/Passepartout/Launcher/Constants/Constants+Launcher.swift b/Passepartout/Launcher/Constants/Constants+Launcher.swift index df34047c..74b00d89 100644 --- a/Passepartout/Launcher/Constants/Constants+Launcher.swift +++ b/Passepartout/Launcher/Constants/Constants+Launcher.swift @@ -27,7 +27,7 @@ import Foundation extension Constants { enum Launcher { - static let appId = "com.algoritmico.ios.Passepartout" + static let appId: String = bundleConfig("app_id") private static let appPath: String = { let path = Bundle.main.bundlePath as NSString diff --git a/Passepartout/Launcher/Info.plist b/Passepartout/Launcher/Info.plist index c4ed38c8..f7fae3b1 100644 --- a/Passepartout/Launcher/Info.plist +++ b/Passepartout/Launcher/Info.plist @@ -20,5 +20,10 @@ $(EXECUTABLE_NAME) CFBundlePackageType $(PRODUCT_BUNDLE_PACKAGE_TYPE) + com.algoritmico.Passepartout.config + + app_id + $(CFG_APP_ID) +