using System; using System.Collections.Generic; using System.Reflection; using GoogleMobileAds.Common; namespace GoogleMobileAds.Api { public class AdLoader { public Dictionary> CustomNativeTemplateClickHandlers { get; private set; } public string AdUnitId { get; private set; } public HashSet AdTypes { get; private set; } public HashSet TemplateIds { get; private set; } public event EventHandler OnAdFailedToLoad; public event EventHandler OnCustomNativeTemplateAdLoaded; private IAdLoaderClient adLoaderClient; public class Builder { internal string AdUnitId { get; private set; } internal HashSet AdTypes { get; private set; } internal HashSet TemplateIds { get; private set; } internal Dictionary> CustomNativeTemplateClickHandlers { get; private set; } public Builder(string adUnitId) { this.AdUnitId = adUnitId; this.AdTypes = new HashSet(); this.TemplateIds = new HashSet(); this.CustomNativeTemplateClickHandlers = new Dictionary>(); } public AdLoader.Builder ForCustomNativeAd(string templateId) { this.TemplateIds.Add(templateId); this.AdTypes.Add(NativeAdType.CustomTemplate); return this; } public AdLoader.Builder ForCustomNativeAd(string templateId, Action callback) { this.TemplateIds.Add(templateId); this.CustomNativeTemplateClickHandlers[templateId] = callback; this.AdTypes.Add(NativeAdType.CustomTemplate); return this; } public AdLoader Build() { return new AdLoader(this); } } private AdLoader(AdLoader.Builder builder) { this.AdUnitId = string.Copy(builder.AdUnitId); this.CustomNativeTemplateClickHandlers = new Dictionary>(builder.CustomNativeTemplateClickHandlers); this.TemplateIds = new HashSet(builder.TemplateIds); this.AdTypes = new HashSet(builder.AdTypes); MethodInfo method = Type.GetType("GoogleMobileAds.GoogleMobileAdsClientFactory,Assembly-CSharp").GetMethod("BuildAdLoaderClient", BindingFlags.Static | BindingFlags.Public); this.adLoaderClient = (IAdLoaderClient)method.Invoke(null, new object[] { this }); Utils.CheckInitialization(); this.adLoaderClient.OnCustomNativeTemplateAdLoaded += delegate(object sender, CustomNativeEventArgs args) { this.OnCustomNativeTemplateAdLoaded(this, args); }; this.adLoaderClient.OnAdFailedToLoad += delegate(object sender, AdFailedToLoadEventArgs args) { if (this.OnAdFailedToLoad != null) { this.OnAdFailedToLoad(this, args); } }; } public void LoadAd(AdRequest request) { this.adLoaderClient.LoadAd(request); } } }