Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-chicken-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/remote-config': minor
---

feat: add `setDefaults(...)` method
25 changes: 25 additions & 0 deletions packages/remote-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ const removeAllListeners = async () => {
* [`getString(...)`](#getstring)
* [`getInfo()`](#getinfo)
* [`setMinimumFetchInterval(...)`](#setminimumfetchinterval)
* [`setDefaults(...)`](#setdefaults)
* [`setSettings(...)`](#setsettings)
* [`addConfigUpdateListener(...)`](#addconfigupdatelistener)
* [`removeConfigUpdateListener(...)`](#removeconfigupdatelistener)
Expand Down Expand Up @@ -301,6 +302,23 @@ Only available for Web.
--------------------


### setDefaults(...)

```typescript
setDefaults(options: SetDefaultsOptions) => Promise<void>
```

Sets config defaults for parameter keys and values in the default namespace config.

| Param | Type |
| ------------- | ----------------------------------------------------------------- |
| **`options`** | <code><a href="#setdefaultsoptions">SetDefaultsOptions</a></code> |

**Since:** 8.3.0

--------------------


### setSettings(...)

```typescript
Expand Down Expand Up @@ -429,6 +447,13 @@ Remove all listeners for this plugin.
| **`minimumFetchIntervalInSeconds`** | <code>number</code> | Define the maximum age in seconds of an entry in the config cache before it is considered stale. During development, it's recommended to set a relatively low minimum fetch interval. | <code>43200</code> | 1.3.0 |


#### SetDefaultsOptions

| Prop | Type | Description | Since |
| -------------- | -------------------------------------------------------------- | ---------------------------------------------------- | ----- |
| **`defaults`** | <code>Record&lt;string, string \| number \| boolean&gt;</code> | Defines the dictionary of values to set as defaults. | 8.3.0 |


#### SetSettingsOptions

| Prop | Type | Description | Default | Since |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public GetInfoResult getInfo() {
return new GetInfoResult(lastFetchTime, lastFetchStatus);
}

public Task<Void> setDefaults(@NonNull Map<String, Object> defaults) {
return getFirebaseRemoteConfigInstance().setDefaultsAsync(defaults);
}

public Task<Void> setSettings(@Nullable Integer fetchTimeoutInSeconds, @Nullable Integer minimumFetchIntervalInSeconds) {
FirebaseRemoteConfigSettings.Builder builder = new FirebaseRemoteConfigSettings.Builder();
if (fetchTimeoutInSeconds != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class FirebaseRemoteConfigPlugin extends Plugin {
public static final String TAG = "FirebaseRemoteConfig";
public static final String ERROR_KEY_MISSING = "key must be provided.";
public static final String ERROR_CALLBACK_ID_MISSING = "callbackId must be provided.";
public static final String ERROR_DEFAULTS_MISSING = "defaults must be provided.";

private static final int DEFAULT_MINIMUM_FETCH_INTERVAL_IN_SECONDS = 43200;
private static final int DEFAULT_FETCH_TIMEOUT_IN_SECONDS = 60;
Expand Down Expand Up @@ -176,6 +177,39 @@ public void setMinimumFetchInterval(PluginCall call) {
call.reject("Not available on Android.");
}

@PluginMethod
public void setDefaults(PluginCall call) {
try {
JSObject defaults = call.getObject("defaults");
if (defaults == null) {
call.reject(ERROR_DEFAULTS_MISSING);
return;
}

Map<String, Object> parsedDefaults = new HashMap<>();
Iterator<String> keys = defaults.keys();
while (keys.hasNext()) {
String key = keys.next();
parsedDefaults.put(key, defaults.get(key));
}

implementation
.setDefaults(parsedDefaults)
.addOnCompleteListener(t -> {
if (t.isSuccessful()) {
call.resolve();
} else {
Exception exception = t.getException();
String errorMessage = exception != null ? exception.getMessage() : "Failed to set defaults.";
call.reject(errorMessage);
}
});
} catch (Exception exception) {
Logger.error(TAG, exception.getMessage(), exception);
call.reject(exception.getMessage());
}
}

@PluginMethod
public void setSettings(PluginCall call) {
try {
Expand Down
4 changes: 4 additions & 0 deletions packages/remote-config/ios/Plugin/FirebaseRemoteConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ import Capacitor
completion(lastFetchTimeMillis, statusInt, nil)
}

@objc public func setDefaults(_ defaults: [String: NSObject]) {
RemoteConfig.remoteConfig().setDefaults(defaults)
}

@objc public func setSettings(fetchTimeoutInSeconds: Double, minimumFetchIntervalInSeconds: Double) {
let settings = RemoteConfigSettings()
settings.fetchTimeout = fetchTimeoutInSeconds
Expand Down
17 changes: 17 additions & 0 deletions packages/remote-config/ios/Plugin/FirebaseRemoteConfigPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class FirebaseRemoteConfigPlugin: CAPPlugin, CAPBridgedPlugin {
CAPPluginMethod(name: "getNumber", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "getString", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "setMinimumFetchInterval", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "setDefaults", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "setSettings", returnType: CAPPluginReturnPromise),
CAPPluginMethod(name: "addConfigUpdateListener", returnType: CAPPluginReturnCallback),
CAPPluginMethod(name: "removeConfigUpdateListener", returnType: CAPPluginReturnPromise)
Expand All @@ -26,6 +27,7 @@ public class FirebaseRemoteConfigPlugin: CAPPlugin, CAPBridgedPlugin {
public let errorKeyMissing = "key must be provided."
public let errorFetchAndActivatefailed = "fetchAndActivate failed."
public let errorCallbackIdMissing = "callbackId must be provided."
public let errorDefaultsMissing = "defaults must be provided."

private let defaultMinimumFetchIntervalInSeconds: Double = 43200
private let defaultFetchTimeoutInSeconds: Double = 60
Expand Down Expand Up @@ -107,6 +109,21 @@ public class FirebaseRemoteConfigPlugin: CAPPlugin, CAPBridgedPlugin {
call.reject("Not available on iOS.")
}

@objc func setDefaults(_ call: CAPPluginCall) {
guard let defaults = call.getObject("defaults") else {
call.reject(errorDefaultsMissing)
return
}

// Convert JSObject ([String: Any]) to [String: NSObject]
let bridgedDefaults = defaults.compactMapValues { $0 as? NSObject }

// Pass the correctly typed dictionary to your implementation
implementation?.setDefaults(bridgedDefaults)

call.resolve()
}

@objc func setSettings(_ call: CAPPluginCall) {
let fetchTimeoutInSeconds = call.getDouble("fetchTimeoutInSeconds") ?? defaultFetchTimeoutInSeconds
let minimumFetchIntervalInSeconds = call.getDouble("minimumFetchIntervalInSeconds") ?? defaultMinimumFetchIntervalInSeconds
Expand Down
18 changes: 18 additions & 0 deletions packages/remote-config/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ export interface FirebaseRemoteConfigPlugin {
setMinimumFetchInterval(
options: SetMinimumFetchIntervalOptions,
): Promise<void>;
/**
* Sets config defaults for parameter keys and values in the default namespace config.
*
* @since 8.3.0
*/
setDefaults(options: SetDefaultsOptions): Promise<void>;
/**
* Set the remote config settings.
*
Expand Down Expand Up @@ -207,6 +213,18 @@ export interface SetMinimumFetchIntervalOptions {
minimumFetchIntervalInSeconds: number;
}

/**
* @since 8.3.0
*/
export interface SetDefaultsOptions {
/**
* Defines the dictionary of values to set as defaults.
*
* @since 8.3.0
*/
defaults: Record<string, string | number | boolean>;
}

/**
* @since 6.2.0
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/remote-config/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
GetStringResult,
RemoveConfigUpdateListenerOptions,
SetMinimumFetchIntervalOptions,
SetDefaultsOptions,
SetSettingsOptions,
} from './definitions';
import { LastFetchStatus } from './definitions';
Expand Down Expand Up @@ -84,6 +85,11 @@
options.minimumFetchIntervalInSeconds * 1000;
}

public async setDefaults(options: SetDefaultsOptions): Promise<void> {
const remoteConfig = getRemoteConfig();
remoteConfig.defaultConfig = options.defaults;
}

public async setSettings(options: SetSettingsOptions): Promise<void> {
const remoteConfig = getRemoteConfig();
if (options.fetchTimeoutInSeconds !== undefined) {
Expand All @@ -97,13 +103,13 @@
}

public async addConfigUpdateListener(
_callback: AddConfigUpdateListenerOptionsCallback,

Check warning on line 106 in packages/remote-config/src/web.ts

View workflow job for this annotation

GitHub Actions / Lint

'_callback' is defined but never used
): Promise<string> {
this.throwUnimplementedError();
}

public async removeConfigUpdateListener(
_options: RemoveConfigUpdateListenerOptions,

Check warning on line 112 in packages/remote-config/src/web.ts

View workflow job for this annotation

GitHub Actions / Lint

'_options' is defined but never used
): Promise<void> {
this.throwUnimplementedError();
}
Expand Down
Loading