update: 重构 FirebaseUtils, CodeReview

Signed-off-by: huyufei <yufei.hu@castbox.fm>
胡宇飞 2024-07-31 22:18:44 +08:00
parent 23c1b8f4fc
commit 3f0f13be6c
5 changed files with 54 additions and 73 deletions

View File

@ -25,7 +25,7 @@ namespace Guru
private const string VALUE_ONLY_FOR_IOS = "idfa_only_for_ios";
public static bool IsDebug { get; set; } = false;
private static bool _isGuruAnalyticInitOnce = false;
private static readonly bool _isGuruAnalyticInitOnce = false;
public static void InitGuruAnalyticService(GuruAnalyticsInitConfig initConfig, string firebaseId)
{
@ -38,7 +38,7 @@ namespace Guru
#if UNITY_EDITOR
IsDebug = true;
#else
IsDebug = isDebug;
IsDebug = initConfig.isDebug;
#endif
string appId = IPMConfig.IPM_X_APP_ID;
string deviceInfo = new DeviceInfoData().ToString();
@ -54,8 +54,8 @@ namespace Guru
if(!string.IsNullOrEmpty(firebaseId))
GuruAnalytics.Instance.SetFirebaseId(firebaseId); // 在自打点初始化之前, 需要调用一下设置 FirebaseId
GuruAnalytics.Init(appId, deviceInfo, initConfig); // Android 初始化
UpdateAllValues();
GuruAnalytics.Init(appId, deviceInfo, initConfig, OnGuruAnalyticsInitComplete); // Android 初始化
UpdateAllUserProperties();
}
catch (Exception ex)
{
@ -245,9 +245,9 @@ namespace Guru
#endif
/// <summary>
/// 获取全部ID
/// 上报中台打点的用户属性
/// </summary>
private static void UpdateAllValues()
private static void UpdateAllUserProperties()
{
Debug.Log($"{TAG} --- UpdateAllValues");
@ -296,7 +296,7 @@ namespace Guru
try
{
GuruAnalytics.Instance.SetUserProperty(key, value);
UpdateAllValues(); // 同步所有的ID
UpdateAllUserProperties(); // 同步所有的ID
}
catch (Exception ex)
{
@ -311,32 +311,6 @@ namespace Guru
}
}
/// <summary>
/// 自定义事件打点
/// </summary>
/// <param name="key"></param>
/// <param name="data"></param>
private static void TrackEventGuru(string key, Dictionary<string, dynamic> data = null, int priority = -1)
{
try
{
if (data == null) data = new Dictionary<string, dynamic>();
GuruAnalytics.Instance.LogEvent(key, data, priority);
UpdateAllValues(); // 同步所有的ID
}
catch (Exception e)
{
if (IsFirebaseReady)
{
Crashlytics.LogException(e);
}
else
{
Debug.LogWarning(e);
}
}
}
/// <summary>
/// 设置太极02阀值

View File

@ -233,6 +233,8 @@ namespace Guru
return;
}
if (eventSetting == null) eventSetting = DefaultEventSetting;
var dataStr = "";

View File

@ -2,5 +2,5 @@ using UnityEngine;
public static class NetworkUtil
{
public static bool IsNetAvaliable => Application.internetReachability != NetworkReachability.NotReachable;
public static bool IsNetAvailable => Application.internetReachability != NetworkReachability.NotReachable;
}

View File

@ -13,8 +13,6 @@ namespace Guru
private const float LOGIN_RETRY_MAX_TIME = 60; // 最大请求间隔时间
private const float LOGIN_RETRY_INTERVAL = 10; // 最大请求间隔时间
private static float _retryDelayTime = 10; // 登录重试时间
/// <summary>
@ -35,49 +33,56 @@ namespace Guru
if (string.IsNullOrEmpty(authToken)) authToken = IPMConfig.IPM_FIREBASE_TOKEN;
Log.I(LOG_TAG, $"[Auth] Firebase Token:{authToken}");
if (string.IsNullOrEmpty(authToken) || !NetworkUtil.IsNetAvaliable)
if (!string.IsNullOrEmpty(authToken) && NetworkUtil.IsNetAvailable)
{
// Token 为空 或 网络不可用
if (autoRetry)
{
// 继续重试
DelayCallFirebaseLogin(Mathf.Min(_retryDelayTime, LOGIN_RETRY_MAX_TIME), onLoginResult, authToken);
_retryDelayTime += LOGIN_RETRY_INTERVAL; // 最大重试间隔 60s
}
else
{
// 不再重试
onLoginResult?.Invoke(false);
}
LoginFirebase(authToken, autoRetry, onLoginResult);
return;
}
FirebaseAuth.DefaultInstance.SignInWithCustomTokenAsync(authToken)
.ContinueWithOnMainThread(task =>
{
// ----- Task failed -----
if (task.IsCanceled || task.IsFaulted)
{
Log.E(LOG_TAG,"[Auth] SignInWithCustomTokenAsync encountered an error: " + task.Exception);
if (autoRetry)
{
DelayCallFirebaseLogin(_retryDelayTime, onLoginResult, authToken); // 自动重试
}
else
{
onLoginResult?.Invoke(false); // 不再重试
}
return;
}
// ----- Check Result -----
bool success = FirebaseUser != null;
onLoginResult?.Invoke(success);
_retryDelayTime = LOGIN_RETRY_INTERVAL;
});
// Token 为空 或 网络不可用
if (autoRetry)
{
// 继续重试
DelayCallFirebaseLogin(Mathf.Min(_retryDelayTime, LOGIN_RETRY_MAX_TIME), onLoginResult, authToken);
_retryDelayTime += LOGIN_RETRY_INTERVAL; // 最大重试间隔 60s
}
else
{
// 不再重试
onLoginResult?.Invoke(false);
}
}
private static void LoginFirebase(string authToken = "", bool autoRetry = true, Action<bool> onLoginResult = null)
{
FirebaseAuth.DefaultInstance.SignInWithCustomTokenAsync(authToken)
.ContinueWithOnMainThread(task =>
{
// ----- Task failed -----
if (task.IsCanceled || task.IsFaulted)
{
Log.E(LOG_TAG,"[Auth] SignInWithCustomTokenAsync encountered an error: " + task.Exception);
if (autoRetry)
{
DelayCallFirebaseLogin(_retryDelayTime, onLoginResult, authToken); // 自动重试
}
else
{
onLoginResult?.Invoke(false); // 不再重试
}
return;
}
// ----- Check Result -----
bool success = FirebaseUser != null;
onLoginResult?.Invoke(success);
_retryDelayTime = LOGIN_RETRY_INTERVAL;
});
}
// TODO:P0 协程不应该用在此场景
/// <summary>
/// 延迟调用 Firebase 登录请求
/// </summary>

View File

@ -65,7 +65,7 @@ namespace Guru
private static void UploadDeviceInfo()
{
if (!NetworkUtil.IsNetAvaliable)
if (!NetworkUtil.IsNetAvailable)
{
double retryDelay = Math.Pow(2, _messageRetry);
_messageRetry++;