using System; using System.Collections.Generic; using System.Reflection; using System.Threading; using System.Threading.Tasks; using LeanCloud.Storage.Internal; using LeanCloud.Utilities; namespace LeanCloud { /// /// The AVConfig is a representation of the remote configuration object, /// that enables you to add things like feature gating, a/b testing or simple "Message of the day". /// public class AVConfig : IJsonConvertible { private IDictionary properties = new Dictionary(); /// /// Gets the latest fetched AVConfig. /// /// AVConfig object public static AVConfig CurrentConfig { get { Task task = ConfigController.CurrentConfigController.GetCurrentConfigAsync(); task.Wait(); return task.Result; } } internal static void ClearCurrentConfig() { ConfigController.CurrentConfigController.ClearCurrentConfigAsync().Wait(); } internal static void ClearCurrentConfigInMemory() { ConfigController.CurrentConfigController.ClearCurrentConfigInMemoryAsync().Wait(); } private static IAVConfigController ConfigController { get { return AVPlugins.Instance.ConfigController; } } internal AVConfig() : base() { } internal AVConfig(IDictionary fetchedConfig) { var props = AVDecoder.Instance.Decode(fetchedConfig["params"]) as IDictionary; properties = props; } /// /// Retrieves the AVConfig asynchronously from the server. /// /// AVConfig object that was fetched public static Task GetAsync() { return GetAsync(CancellationToken.None); } /// /// Retrieves the AVConfig asynchronously from the server. /// /// The cancellation token. /// AVConfig object that was fetched public static Task GetAsync(CancellationToken cancellationToken) { return ConfigController.FetchConfigAsync(AVUser.CurrentSessionToken, cancellationToken); } /// /// Gets a value for the key of a particular type. /// /// The type to convert the value to. Supported types are /// AVObject and its descendents, LeanCloud types such as AVRelation and AVGeopoint, /// primitive types,IList<T>, IDictionary<string, T> and strings. /// The key of the element to get. /// The property is retrieved /// and is not found. /// The property under this /// key was found, but of a different type. public T Get(string key) { return Conversion.To(this.properties[key]); } /// /// Populates result with the value for the key, if possible. /// /// The desired type for the value. /// The key to retrieve a value for. /// The value for the given key, converted to the /// requested type, or null if unsuccessful. /// true if the lookup and conversion succeeded, otherwise false. public bool TryGetValue(string key, out T result) { if (this.properties.ContainsKey(key)) { try { var temp = Conversion.To(this.properties[key]); result = temp; return true; } catch (Exception) { // Could not convert, do nothing } } result = default(T); return false; } /// /// Gets a value on the config. /// /// The key for the parameter. /// The property is /// retrieved and is not found. /// The value for the key. virtual public object this[string key] { get { return this.properties[key]; } } IDictionary IJsonConvertible.ToJSON() { return new Dictionary { { "params", NoObjectsEncoder.Instance.Encode(properties) } }; } } }