Skip to content

Commit

Permalink
feat(ios): add getConfig to CAPPlugin (#5495)
Browse files Browse the repository at this point in the history
* feat(ios): add getConfig to CAPPlugin

* rework to use class with helpers

* Update project.pbxproj

* Update ios/Capacitor/Capacitor/CAPPlugin.m

Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>

* Update ios/Capacitor/Capacitor/CAPPlugin.h

Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>

* adjust getArray and fmt

Co-authored-by: jcesarmobile <jcesarmobile@gmail.com>
  • Loading branch information
IT-MikeS and jcesarmobile committed Mar 14, 2022
1 parent 1eaaa5f commit 224a9d0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 2 deletions.
4 changes: 4 additions & 0 deletions ios/Capacitor/Capacitor.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0F8F33B327DA980A003F49D6 /* PluginConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0F8F33B127DA980A003F49D6 /* PluginConfig.swift */; };
2F81F5C926FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 2F81F5C726FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.h */; };
2F81F5CA26FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2F81F5C826FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.m */; };
373A69C1255C9360000A6F44 /* NotificationHandlerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373A69C0255C9360000A6F44 /* NotificationHandlerProtocol.swift */; };
Expand Down Expand Up @@ -136,6 +137,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
0F8F33B127DA980A003F49D6 /* PluginConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PluginConfig.swift; sourceTree = "<group>"; };
2F81F5C726FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "CAPBridgeViewController+CDVScreenOrientationDelegate.h"; sourceTree = "<group>"; };
2F81F5C826FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "CAPBridgeViewController+CDVScreenOrientationDelegate.m"; sourceTree = "<group>"; };
373A69C0255C9360000A6F44 /* NotificationHandlerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationHandlerProtocol.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -299,6 +301,7 @@
62959AE12524DA7700A3D7F1 /* Capacitor */ = {
isa = PBXGroup;
children = (
0F8F33B127DA980A003F49D6 /* PluginConfig.swift */,
62959B0F2524DA7700A3D7F1 /* Capacitor.h */,
62959B132524DA7700A3D7F1 /* CAPPlugin.h */,
62959B012524DA7700A3D7F1 /* CAPPlugin.m */,
Expand Down Expand Up @@ -610,6 +613,7 @@
62959B172524DA7800A3D7F1 /* JSExport.swift in Sources */,
373A69C1255C9360000A6F44 /* NotificationHandlerProtocol.swift in Sources */,
625AF1ED258963C700869675 /* WebViewAssetHandler.swift in Sources */,
0F8F33B327DA980A003F49D6 /* PluginConfig.swift in Sources */,
62959B3C2524DA7800A3D7F1 /* CAPBridgeDelegate.swift in Sources */,
62959B2F2524DA7800A3D7F1 /* DefaultPlugins.m in Sources */,
623D691E254C7462002D01D1 /* CAPInstanceConfiguration.m in Sources */,
Expand Down
8 changes: 8 additions & 0 deletions ios/Capacitor/Capacitor/CAPInstanceConfiguration.swift
Expand Up @@ -15,10 +15,18 @@ extension InstanceConfiguration {
return serverURL
}

@available(*, deprecated, message: "Use getPluginConfig")
@objc public func getPluginConfigValue(_ pluginId: String, _ configKey: String) -> Any? {
return (pluginConfigurations as? JSObject)?[keyPath: KeyPath("\(pluginId).\(configKey)")]
}

@objc public func getPluginConfig(_ pluginId: String) -> PluginConfig {
if let cfg = (pluginConfigurations as? JSObject)?[keyPath: KeyPath("\(pluginId)")] as? JSObject {
return PluginConfig(config: cfg)
}
return PluginConfig(config: JSObject())
}

@objc public func shouldAllowNavigation(to host: String) -> Bool {
for hostname in allowedNavigationHostnames {
if doesHost(host, match: hostname) {
Expand Down
5 changes: 4 additions & 1 deletion ios/Capacitor/Capacitor/CAPPlugin.h
Expand Up @@ -4,6 +4,8 @@
@protocol CAPBridgeProtocol;
@class CAPPluginCall;

@class PluginConfig;

@interface CAPPlugin : NSObject

@property (nonatomic, weak, nullable) WKWebView *webView;
Expand Down Expand Up @@ -44,7 +46,8 @@
-(NSString* _Nonnull)getId;
-(BOOL)getBool:(CAPPluginCall* _Nonnull) call field:(NSString* _Nonnull)field defaultValue:(BOOL)defaultValue DEPRECATED_MSG_ATTRIBUTE("Use accessors on CAPPluginCall instead. See CAPBridgedJSTypes.h for Obj-C implementations.");
-(NSString* _Nullable)getString:(CAPPluginCall* _Nonnull)call field:(NSString* _Nonnull)field defaultValue:(NSString* _Nonnull)defaultValue DEPRECATED_MSG_ATTRIBUTE("Use accessors on CAPPluginCall instead. See CAPBridgedJSTypes.h for Obj-C implementations.");
-(id _Nullable)getConfigValue:(NSString* _Nonnull)key;
-(id _Nullable)getConfigValue:(NSString* _Nonnull)key __deprecated_msg("use getConfig() and access config values using the methods available depending on the type.");
-(PluginConfig* _Nonnull)getConfig;
-(void)setCenteredPopover:(UIViewController* _Nonnull)vc;
-(BOOL)supportsPopover;

Expand Down
6 changes: 5 additions & 1 deletion ios/Capacitor/Capacitor/CAPPlugin.m
Expand Up @@ -29,10 +29,14 @@ - (NSString *) getString:(CAPPluginCall *)call field:(NSString *)field defaultVa
return [call getString:field defaultValue:defaultValue];
}

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

-(PluginConfig*)getConfig {
return [self.bridge.config getPluginConfig:self.pluginName];
}

-(void)load {}

- (void)addEventListener:(NSString *)eventName listener:(CAPPluginCall *)listener {
Expand Down
56 changes: 56 additions & 0 deletions ios/Capacitor/Capacitor/PluginConfig.swift
@@ -0,0 +1,56 @@
import Foundation

@objc public class PluginConfig: NSObject {

// The object containing the plugin config values
private var config: JSObject

init(config: JSObject) {
self.config = config
}

public func getString(configKey: String, defaultValue: String? = nil) -> String? {
if let val = (self.config)[keyPath: KeyPath(configKey)] as? String {
return val
}
return defaultValue
}

public func getBoolean(configKey: String, defaultValue: Bool) -> Bool {
if let val = (self.config)[keyPath: KeyPath(configKey)] as? Bool {
return val
}
return defaultValue
}

public func getInt(configKey: String, defaultValue: Int) -> Int {
if let val = (self.config)[keyPath: KeyPath(configKey)] as? Int {
return val
}
return defaultValue
}

public func getArray(configKey: String, defaultValue: JSArray? = nil) -> JSArray? {
if let val = (self.config)[keyPath: KeyPath(configKey)] as? JSArray {
return val
}
return defaultValue
}

public func getObject(configKey: String) -> JSObject? {
return (self.config)[keyPath: KeyPath(configKey)] as? JSObject
}

public func isEmpty() -> Bool {
return self.config.isEmpty
}

/**
* Gets the JSObject containing the config of the the provided plugin ID.
*
* @return The config for that plugin
*/
public func getConfigJSON() -> JSObject {
return self.config
}
}

0 comments on commit 224a9d0

Please sign in to comment.