Polish bundleConfig with strong type checking

This commit is contained in:
Davide De Rosa 2022-07-03 20:46:30 +02:00
parent 3e8a49c970
commit a442603696
4 changed files with 19 additions and 8 deletions

View File

@ -27,10 +27,6 @@ import Foundation
import UniformTypeIdentifiers import UniformTypeIdentifiers
import SwiftyBeaver import SwiftyBeaver
extension Constants {
private static let bundleConfig = Bundle.main.infoDictionary?["com.algoritmico.Passepartout.config"] as? [String: Any]
}
extension Constants { extension Constants {
enum App { enum App {
static var appId: String { static var appId: String {
@ -40,9 +36,9 @@ extension Constants {
return identifier 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 = { static let isBeta: Bool = {
Bundle.main.isTestFlight Bundle.main.isTestFlight
@ -57,7 +53,7 @@ extension Constants {
return testAppType return testAppType
} }
if let infoValue = bundleConfig?["app_type"] as? Int, if let infoValue: Int = bundleConfig("app_type"),
let testAppType = ProductManager.AppType(rawValue: infoValue) { let testAppType = ProductManager.AppType(rawValue: infoValue) {
return testAppType return testAppType

View File

@ -26,6 +26,16 @@
import Foundation import Foundation
enum Constants { enum Constants {
static func bundleConfig<T>(_ 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 { enum Global {
static let appName = "Passepartout" static let appName = "Passepartout"

View File

@ -27,7 +27,7 @@ import Foundation
extension Constants { extension Constants {
enum Launcher { enum Launcher {
static let appId = "com.algoritmico.ios.Passepartout" static let appId: String = bundleConfig("app_id")
private static let appPath: String = { private static let appPath: String = {
let path = Bundle.main.bundlePath as NSString let path = Bundle.main.bundlePath as NSString

View File

@ -20,5 +20,10 @@
<string>$(EXECUTABLE_NAME)</string> <string>$(EXECUTABLE_NAME)</string>
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string> <string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>com.algoritmico.Passepartout.config</key>
<dict>
<key>app_id</key>
<string>$(CFG_APP_ID)</string>
</dict>
</dict> </dict>
</plist> </plist>