using System;
namespace JustTrack
{
#if !UNITY_WEBGL
///
/// Implementation of IRemoteConfig interface.
///
internal class RemoteConfig : IRemoteConfig
{
private readonly ISDKAgent agent;
private readonly Action waitForInitialization;
///
/// Initializes a new instance of the class.
///
/// The SDK agent instance.
/// Action to wait for SDK initialization before executing callbacks.
internal RemoteConfig(ISDKAgent agent, Action waitForInitialization)
{
this.agent = agent;
this.waitForInitialization = waitForInitialization;
}
///
/// Fetches remote config values from the server if the minimum fetch interval has elapsed.
///
/// Callback invoked on successful fetch.
/// Callback invoked on fetch failure.
public void Fetch(Action pOnSuccess, Action pOnFailure)
{
waitForInitialization(() => this.agent.FetchRemoteConfig(pOnSuccess, pOnFailure));
}
///
/// Activates experiment assignments by confirming enrollment with the server.
///
/// The list of experiments to activate.
/// Callback invoked on successful activation.
/// Callback invoked on activation failure.
public void Activate(string[] experiments, Action pOnSuccess, Action pOnFailure)
{
waitForInitialization(() => this.agent.ActivateRemoteConfig(experiments, pOnSuccess, pOnFailure));
}
///
/// A convenience method that fetches and then activates all pending experiments in one call.
///
/// Callback invoked on successful fetch and activation.
/// Callback invoked on fetch or activation failure.
public void FetchAndActivate(Action pOnSuccess, Action pOnFailure)
{
waitForInitialization(() => this.agent.FetchAndActivateRemoteConfig(pOnSuccess, pOnFailure));
}
///
/// Sets the Remote Config settings.
///
/// The settings to apply.
public void SetConfig(JusttrackRemoteConfigSettings settings)
{
waitForInitialization(() => this.agent.SetRemoteConfigSettings(settings));
}
///
/// Gets all current experiment assignments.
///
/// An array of all current experiment assignments.
public Assignment[] GetAll()
{
if (!agent.IsInitialized())
{
return new Assignment[0];
}
return this.agent.GetAllAssignments();
}
///
/// Gets a boolean value from Remote Config.
///
/// The key of the config value.
/// Config value as Boolean. If the config value is not a valid Boolean or is not found, null is returned.
public bool? GetBoolean(string configKey)
{
if (!agent.IsInitialized())
{
return null;
}
return this.agent.GetRemoteConfigBoolean(configKey);
}
///
/// Gets a double value from Remote Config.
///
/// The key of the config value.
/// Config value as Double. If the config value is not a valid Double or is not found, null is returned.
public double? GetDouble(string configKey)
{
if (!agent.IsInitialized())
{
return null;
}
return this.agent.GetRemoteConfigDouble(configKey);
}
///
/// Gets an integer value from Remote Config.
///
/// The key of the config value.
/// Config value as Integer. If the config value is not a valid Integer or is not found, null is returned.
public int? GetInt(string configKey)
{
if (!agent.IsInitialized())
{
return null;
}
return this.agent.GetRemoteConfigInt(configKey);
}
///
/// Gets a long value from Remote Config.
///
/// The key of the config value.
/// Config value as Long. If the config value is not a valid Long or is not found, null is returned.
public long? GetLong(string configKey)
{
if (!agent.IsInitialized())
{
return null;
}
return this.agent.GetRemoteConfigLong(configKey);
}
///
/// Gets a string value from Remote Config.
///
/// The key of the config value.
/// Config value as String. If the config value is not a valid String or is not found, null is returned.
public string? GetString(string configKey)
{
if (!agent.IsInitialized())
{
return null;
}
return this.agent.GetRemoteConfigString(configKey);
}
}
#endif
}