Skip to content

Commit

Permalink
feat(ios): move CAPConfig to be per-instance rather than a singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
mlynch committed Jun 5, 2019
1 parent b41a101 commit e964068
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
10 changes: 6 additions & 4 deletions ios/Capacitor/Capacitor/CAPBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum BridgeError: Error {

public var lastPlugin: CAPPlugin?

public var config = [String:Any]()
@objc public var config: CAPConfig
// Map of all loaded and instantiated plugins by pluginId -> instance
public var plugins = [String:CAPPlugin]()
// List of known plugins by pluginId -> Plugin Type
Expand All @@ -44,14 +44,16 @@ enum BridgeError: Error {

public var notificationsDelegate : CAPUNUserNotificationCenterDelegate

public init(_ bridgeDelegate: CAPBridgeDelegate, _ userContentController: WKUserContentController) {
public init(_ bridgeDelegate: CAPBridgeDelegate, _ userContentController: WKUserContentController, _ config: CAPConfig) {
self.bridgeDelegate = bridgeDelegate
self.userContentController = userContentController
self.notificationsDelegate = CAPUNUserNotificationCenterDelegate()
CAPConfig.loadConfig()
self.config = config

super.init()

self.notificationsDelegate.bridge = self;
localUrl = "\(CAPBridge.CAP_SCHEME)://\(CAPConfig.getString("server.hostname") ?? "localhost")"
localUrl = "\(CAPBridge.CAP_SCHEME)://\(config.getString("server.hostname") ?? "localhost")"
exportCoreJS(localUrl: localUrl!)
registerPlugins()
setupCordovaCompatibility()
Expand Down
9 changes: 5 additions & 4 deletions ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
@objc public var supportedOrientations: Array<Int> = []

@objc public var startDir = ""
@objc public var config: String?

// Construct the Capacitor runtime
public var bridge: CAPBridge?
Expand Down Expand Up @@ -65,8 +66,8 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr

setKeyboardRequiresUserInteraction(false)

bridge = CAPBridge(self, o)
if let scrollEnabled = CAPConfig.getValue("ios.scrollEnabled") as? Bool {
bridge = CAPBridge(self, o, CAPConfig(self.config))
if let scrollEnabled = bridge!.config.getValue("ios.scrollEnabled") as? Bool {
webView?.scrollView.isScrollEnabled = scrollEnabled
}
}
Expand Down Expand Up @@ -116,8 +117,8 @@ public class CAPBridgeViewController: UIViewController, CAPBridgeDelegate, WKScr
fatalLoadError()
}

hostname = CAPConfig.getString("server.url") ?? "\(bridge!.getLocalUrl())"
allowNavigationConfig = CAPConfig.getValue("server.allowNavigation") as? Array<String>
hostname = bridge!.config.getString("server.url") ?? "\(bridge!.getLocalUrl())"
allowNavigationConfig = bridge!.config.getValue("server.allowNavigation") as? Array<String>


print("⚡️ Loading app at \(hostname!)...")
Expand Down
53 changes: 31 additions & 22 deletions ios/Capacitor/Capacitor/CAPConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,43 @@

private var config: [String:Any?]? = [String:Any?]()

public static func getInstance() -> CAPConfig {
if instance == nil {
instance = CAPConfig()
public init(_ configText: String? = nil) {
super.init()
if let contents = configText {
guard let configData = contents.data(using: .utf8) else {
print("Unable to process config JSON string as UTF8")
return
}

parseAndSetConfig(configData)
} else {
loadGlobalConfig()
}
return instance!
}

public static func loadConfig() {
CAPConfig.getInstance()._loadConfig()
}
private func _loadConfig() {

private func loadGlobalConfig() {
guard let configUrl = Bundle.main.url(forResource: "capacitor.config", withExtension: "json") else {
print("Unable to find capacitor.config.json, make sure it exists and run npx cap copy")
return
}
do {
let contents = try Data(contentsOf: configUrl)
guard let json = try JSONSerialization.jsonObject(with: contents) as? [String: Any] else {
return
}
self.config = json
parseAndSetConfig(contents)
} catch {
print("Unable to parse capacitor.config.json. Make sure it's valid JSON")
}
}

private func parseAndSetConfig(_ data: Data) {
do {
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
self.config = json
} catch {
print("Unable to parse config JSON")
print(error.localizedDescription)
}
}

private func getConfigObjectDeepest(key: String) -> [String:Any?]? {
let parts = key.split(separator: ".")

Expand All @@ -41,7 +51,7 @@
return o
}

private static func getConfigKey(_ key: String) -> String {
private func getConfigKey(_ key: String) -> String {
let parts = key.split(separator: ".")
if parts.last != nil {
return String(parts.last!)
Expand All @@ -52,8 +62,8 @@
/**
* Get the value of a configuration option for a specific plugin.
*/
@objc public static func getPluginConfigValue(_ pluginId: String, _ configKey: String) -> Any? {
guard let plugins = getInstance().config!["plugins"] as? [String:Any] else {
@objc public func getPluginConfigValue(_ pluginId: String, _ configKey: String) -> Any? {
guard let plugins = config!["plugins"] as? [String:Any] else {
return nil
}

Expand All @@ -64,18 +74,17 @@
return pluginOptions[configKey]
}

@objc public static func getValue(_ key: String) -> Any? {
let k = CAPConfig.getConfigKey(key)
let o = getInstance().getConfigObjectDeepest(key: key)
@objc public func getValue(_ key: String) -> Any? {
let k = getConfigKey(key)
let o = getConfigObjectDeepest(key: key)
return o?[k] ?? nil
}

@objc public static func getString(_ key: String) -> String? {
@objc public func getString(_ key: String) -> String? {
let value = getValue(key)
if value == nil {
return nil
}
return value as? String
}

}
2 changes: 1 addition & 1 deletion ios/Capacitor/Capacitor/CAPPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ -(BOOL) getBool:(CAPPluginCall *)call field:(NSString *)field defaultValue:(BOOL
}

-(id)getConfigValue:(NSString *)key {
return [CAPConfig getPluginConfigValue:self.pluginName :key];
return [self.bridge.config getPluginConfigValue:self.pluginName :key];
}

-(void)load {}
Expand Down

0 comments on commit e964068

Please sign in to comment.