diff --git a/Runtime/Code/Config/GuruSDKInitConfig.cs b/Runtime/Code/Config/GuruSDKInitConfig.cs index 24c8300..a291ef8 100644 --- a/Runtime/Code/Config/GuruSDKInitConfig.cs +++ b/Runtime/Code/Config/GuruSDKInitConfig.cs @@ -7,7 +7,7 @@ namespace Guru /// /// 启动参数配置 /// - public partial class GuruSDKInitConfig + public class GuruSDKInitConfig { /// /// 使用自定义的ConsentFlow启动流程 @@ -58,6 +58,10 @@ namespace Guru /// 启用 AdjustDeeplink /// public Action OnAdjustDeeplinkCallback = null; + /// + /// 自打点启动参数 + /// + public GuruAnalyticsInitConfig CustomAnalyticsInitConfig = null; /// /// 支付初始化Keys @@ -65,51 +69,6 @@ namespace Guru public byte[] GoogleKeys; // 数据取自 GooglePlayTangle.Data(); public byte[] AppleRootCerts; // 数据取自 AppleTangle.Data(); - #region Initialization - - /// - /// 构建启动配置 - /// - /// - public static GuruSDKInitConfig Build( - bool useCustomConsent = false, - bool autoLoadAds = true, - bool iapEnabled = true, - bool autoRecordFinishedLevels = true, - bool isBuyNoAds = false, - string bannerBackgroundColor = "#00000000", - bool debugMode = false, - Action onAdjustDeeplinkCallback = null, - Dictionary defaultRemoteData = null, - byte[] googleKeys = null, - byte[] appleRootCerts = null, - bool debugEnableEventLog = false) - { - // 创建启动用参数 - GuruSDKInitConfig config = new GuruSDKInitConfig() - { - UseCustomConsent = useCustomConsent, - AutoLoadWhenAdsReady = autoLoadAds, - IAPEnabled = iapEnabled, - AutoRecordFinishedLevels = autoRecordFinishedLevels, - IsBuyNoAds = isBuyNoAds, - BannerBackgroundColor = bannerBackgroundColor, - DebugMode = debugMode, - OnAdjustDeeplinkCallback = onAdjustDeeplinkCallback, - GoogleKeys = googleKeys, - AppleRootCerts = appleRootCerts, - DefaultRemoteData = defaultRemoteData ?? new Dictionary(), - EnableDebugLogEvent = debugEnableEventLog, - }; -#if UNITY_EDITOR - config.DebugMode = true; -#endif - return config; - } - - - #endregion - #region Print public override string ToString() @@ -134,4 +93,100 @@ namespace Guru #endregion } + + + + /// + /// 构建器 + /// + public class GuruSDKInitConfigBuilder + { + + private GuruSDKInitConfig _config = new GuruSDKInitConfig(); + + /// + /// 构建配置 + /// + /// + public GuruSDKInitConfig Build() + { + return _config; + } + + public GuruSDKInitConfigBuilder SetUseCustomConsent(bool value) + { + _config.UseCustomConsent = value; + return this; + } + public GuruSDKInitConfigBuilder SetAutoLoadWhenAdsReady(bool value) + { + _config.AutoLoadWhenAdsReady = value; + return this; + } + public GuruSDKInitConfigBuilder SetIAPEnabled(bool value) + { + _config.IAPEnabled = value; + return this; + } + public GuruSDKInitConfigBuilder SetAutoRecordFinishedLevels(bool value) + { + _config.AutoRecordFinishedLevels = value; + return this; + } + public GuruSDKInitConfigBuilder SetIsBuyNoAds(bool value) + { + _config.IsBuyNoAds = value; + return this; + } + public GuruSDKInitConfigBuilder SetBannerBackgroundColor(string value) + { + _config.BannerBackgroundColor = value; + return this; + } + public GuruSDKInitConfigBuilder SetDebugMode(bool value) + { + _config.DebugMode = value; + return this; + } + public GuruSDKInitConfigBuilder SetOnAdjustDeeplinkCallback(Action callback) + { + _config.OnAdjustDeeplinkCallback = callback; + return this; + } + public GuruSDKInitConfigBuilder SetGoogleKeys(byte[] value) + { + _config.GoogleKeys = value; + return this; + } + public GuruSDKInitConfigBuilder SetAppleRootCerts(byte[] value) + { + _config.AppleRootCerts = value; + return this; + } + public GuruSDKInitConfigBuilder SetDefaultRemoteData(Dictionary value) + { + _config.DefaultRemoteData = value; + return this; + } + public GuruSDKInitConfigBuilder SetEnableDebugLogEvent(bool value) + { + _config.EnableDebugLogEvent = value; + return this; + } + public GuruSDKInitConfigBuilder SetCustomAnalyticsInitConfig(GuruAnalyticsInitConfig value) + { + _config.CustomAnalyticsInitConfig = value; + return this; + } + public GuruSDKInitConfigBuilder SetCustomServiceKey(string value) + { + _config.CustomServiceKey = value; + return this; + } + public GuruSDKInitConfigBuilder SetAutoNotificationPermission(bool value) + { + _config.AutoNotificationPermission = value; + return this; + } + } } \ No newline at end of file diff --git a/Runtime/Code/SDK/GuruSDK.Callbacks.cs b/Runtime/Code/SDK/GuruSDK.Callbacks.cs index 1950be5..68326ba 100644 --- a/Runtime/Code/SDK/GuruSDK.Callbacks.cs +++ b/Runtime/Code/SDK/GuruSDK.Callbacks.cs @@ -334,25 +334,25 @@ namespace Guru } private static Action _onUserAuthResult; - public static event Action OnUserAuthResult + public static event Action OnGuruUserAuthResult { add => _onUserAuthResult += value; remove => _onUserAuthResult -= value; } - internal static void InvokeOnUserAuthResult(bool success) + internal static void InvokeOnGuruUserAuthResult(bool success) { _onUserAuthResult?.Invoke(success); } - private static Action _onFirebaseAuthResult; - public static event Action OnFirebaseAuthResult + private static Action _onFirebaseUserAuthResult; + public static event Action OnFirebaseUserAuthResult { - add => _onFirebaseAuthResult += value; - remove => _onFirebaseAuthResult -= value; + add => _onFirebaseUserAuthResult += value; + remove => _onFirebaseUserAuthResult -= value; } internal static void InvokeOnFirebaseAuthResult(bool success) { - _onFirebaseAuthResult?.Invoke(success); + _onFirebaseUserAuthResult?.Invoke(success); } // DeepLink 回调 diff --git a/Runtime/Code/SDK/GuruSDK.cs b/Runtime/Code/SDK/GuruSDK.cs index 6574708..8b0a43c 100644 --- a/Runtime/Code/SDK/GuruSDK.cs +++ b/Runtime/Code/SDK/GuruSDK.cs @@ -89,33 +89,10 @@ namespace Guru } // TODO : 下个版本需要将 整个 GuruSDK 做功能性的拆分 - // GuruSDk.Callbacks -> GuruSDkCallbacks 所有的内置回调改为成员变量, - // 去掉所有的内部类, 去掉所有的 Static - // Static 只用于常量 - // TODO: 下一个版本改为标准的 Builder 模式 - public static GuruSDKInitConfig BuildConfig( - bool useCustomConsent = false, - bool autoLoadAds = true, - bool iapEnabled = true, - bool autoRecordFinishedLevels = true, - bool debugMode = false, - bool isBuyNoAds = false, - Action onAdjustDeeplinkCallback = null, - string bannerColor = "#00000000", - Dictionary defaultRemoteData = null, - byte[] googleKeys = null, - byte[] appleRootCerts = null, - bool debugEnableEventLog = false) - { - var config = GuruSDKInitConfig.Build(useCustomConsent, autoLoadAds, iapEnabled, - autoRecordFinishedLevels, isBuyNoAds, bannerColor, - debugMode, onAdjustDeeplinkCallback, defaultRemoteData, googleKeys, appleRootCerts, debugEnableEventLog); - return config; - } - + public static void Init(Action onComplete) { - Init(GuruSDKInitConfig.Build(), onComplete); + Init(new GuruSDKInitConfigBuilder().Build(), onComplete); } public static void Init(GuruSDKInitConfig config, Action onComplete) @@ -155,12 +132,11 @@ namespace Guru private void InitServices() { - Analytics.InitAnalytics(); // 打点提前初始化 + Analytics.Init(); // 打点提前初始化 //---- Start All tools ---- LogI($"#2 --- InitFirebase ---"); //---------- Start Firebase ------------ StartFirebaseService(); - LogI($"#2.1 --- InitFacebook ---"); //---------- Start Facebook ------------ FBService.Instance.StartService(Analytics.OnFBInitComplete); @@ -168,75 +144,6 @@ namespace Guru IsInitialSuccess = true; } - - - /// - /// 启动 Firebase 服务 - /// - private void StartFirebaseService() - { - FirebaseUtil.onInitComplete += OnFirebaseReady; - FirebaseUtil.OnUserAuthResult += OnUserAuthResult; - FirebaseUtil.OnFirebaseAuthResult += OnFirebaseAuthResult; - - if (InitConfig.OnAdjustDeeplinkCallback != null) - { - //TODO: 下个版本 AdjustService 和 Firebase 解耦 - FirebaseUtil.OnAdjustDeeplinkCallback = InitConfig.OnAdjustDeeplinkCallback; // 挂载 Deeplink 的回调 - } - - FirebaseUtil.InitFirebase(Analytics.OnFirebaseInitCompleted); // 确保所有的逻辑提前被调用到 - } - - private void OnUserAuthResult(bool success) - { - - if (success && string.IsNullOrEmpty(IPMConfig.IPM_UID)) - { - success = false; - } - Callbacks.SDK.InvokeOnUserAuthResult(success); - - if (success) - { - Model.UserId = IPMConfig.IPM_UID; - if (GuruIAP.Instance != null) - { - GuruIAP.Instance.SetUID(UID); - GuruIAP.Instance.SetUUID(UUID); - } - - SetUID(UID); - } - } - - private void OnFirebaseAuthResult(bool success) - { - Callbacks.SDK.InvokeOnFirebaseAuthResult(success); - } - - /// - /// 开始各种组件初始化 - /// - private void OnFirebaseReady(bool success) - { - FirebaseUtil.onInitComplete -= OnFirebaseReady; - LogI($"#3 --- On FirebaseDeps: {success} ---"); - IsFirebaseReady = success; - Callbacks.SDK.InvokeOnFirebaseReady(success); - // LogFirebaseDeps(success); - - LogI($"#3.5 --- Call InitRemoteConfig ---"); - // 开始Remote Manager初始化 - RemoteConfigManager.Init(BuildDefaultRemoteData(_initConfig.DefaultRemoteData)); - RemoteConfigManager.OnFetchCompleted += OnFetchRemoteCallback; - - LogI($"#4 --- Apply remote services config ---"); - // 根据缓存的云控配置来初始化参数 - InitAllGuruServices(); - } - - /// /// 注入云控参数基础数据 /// @@ -264,8 +171,6 @@ namespace Guru Callbacks.Remote.InvokeOnRemoteFetchComplete(success); } - - private void Update() { UpdateAllUpdates(); // 驱动所有的更新器 @@ -274,6 +179,8 @@ namespace Guru #endregion + + #region App Remote Update /// @@ -798,6 +705,132 @@ namespace Guru } + #endregion + + #region Firebase 服务 + + /// + /// 启动 Firebase 服务 + /// + private void StartFirebaseService() + { + FirebaseUtil.Init(OnFirebaseDepsCheckResult, + OnGetFirebaseId, + OnGetGuruUID, + OnFirebaseLoginResult); // 确保所有的逻辑提前被调用到 + } + + private void OnGetGuruUID(bool success) + { + Callbacks.SDK.InvokeOnGuruUserAuthResult(success); + + if (success) + { + Model.UserId = IPMConfig.IPM_UID; + if (GuruIAP.Instance != null) + { + GuruIAP.Instance.SetUID(UID); + GuruIAP.Instance.SetUUID(UUID); + } + SetUID(UID); + } + } + + + + + private void OnGetFirebaseId(string fid) + { + // 初始化 Adjust 服务 + InitAdjustService(fid, InitConfig.OnAdjustDeeplinkCallback); + + // 初始化自打点 + var config = InitConfig.CustomAnalyticsInitConfig; // 获取外置的启动配置 + if (config == null) + { + // 创建默认的配置 + config = new GuruAnalyticsInitConfig(); + } + + Analytics.InitGuruAnalyticService(config, fid); + + } + + private void OnFirebaseLoginResult(bool success) + { + Callbacks.SDK.InvokeOnFirebaseAuthResult(success); + } + + /// + /// 开始各种组件初始化 + /// + private void OnFirebaseDepsCheckResult(bool success) + { + LogI($"#3 --- On FirebaseDeps: {success} ---"); + IsFirebaseReady = success; + Callbacks.SDK.InvokeOnFirebaseReady(success); + + Analytics.OnFirebaseInitCompleted(); + + LogI($"#3.5 --- Call InitRemoteConfig ---"); + // 开始Remote Manager初始化 + RemoteConfigManager.Init(BuildDefaultRemoteData(_initConfig.DefaultRemoteData)); + RemoteConfigManager.OnFetchCompleted += OnFetchRemoteCallback; + + LogI($"#4 --- Apply remote services config ---"); + // 根据缓存的云控配置来初始化参数 + InitAllGuruServices(); + } + + #endregion + + #region Adjust服务 + + /// + /// 启动 Adjust 服务 + /// + private static void InitAdjustService(string firebaseId, Action onDeeplinkCallback = null) + { + // 启动 AdjustService + string appToken = GuruSettings.Instance.AdjustSetting?.GetAppToken() ?? ""; + string fbAppId = GuruSettings.Instance.IPMSetting.FacebookAppId; + + if (!string.IsNullOrEmpty(IPMConfig.ADJUST_ID)) + ReportAdjustId(IPMConfig.ADJUST_ID); // 二次启动后,若有值则立即上报属性 + + AdjustService.StartService(appToken, fbAppId, firebaseId, DeviceId, + OnGetAdjustId, onDeeplinkCallback); + } + + private static void OnGetAdjustId(string adjustId) + { + // 获取 ADID + if (string.IsNullOrEmpty(adjustId)) + { + adjustId = "not_set"; + } + else + { + IPMConfig.ADJUST_ID = adjustId; + } + ReportAdjustId(adjustId); + + Analytics.OnAdjustInitComplete(); + } + + + private static void ReportAdjustId(string adjustId) + { + SetUserProperty("adjust_id", adjustId); + Debug.Log($"[SDK] --- Firebase + Adjust ID: {adjustId}"); + } + + + + + + + #endregion } } \ No newline at end of file