update: 优化 NetworkStatusMonitor 的代码

--story=1021173 --user=yufei.hu 【中台】【BI】上报用户离线打点 guru_offline https://www.tapd.cn/33527076/s/1161347

Signed-off-by: huyufei <yufei.hu@castbox.fm>
胡宇飞 2024-08-01 17:33:35 +08:00
parent 719be42b59
commit 9cf0dd5801
2 changed files with 16 additions and 14 deletions

View File

@ -19,8 +19,9 @@ namespace Guru
private static bool _hasGotUid; // 已取得UID
private static DateTime _lastReportRateDate; //上次上报信息的日期
private static double _reportSuccessInterval; // 上报频率
// private const string VALUE_NOT_FOR_IOS = "not_support_for_ios";
#if UNITY_IOS
private const string VALUE_NOT_FOR_IOS = "not_support_for_ios";
#endif
// private const string VALUE_ONLY_FOR_IOS = "idfa_only_for_ios";
private static bool _isGuruAnalyticInitOnce = false;

View File

@ -22,7 +22,7 @@ namespace Guru.Network
private const int CHECK_STATUS_INTERVAL_SECOND = 10;
private DateTime LastReportDate
private DateTime LastTriggerDate
{
get => _saveData.LastReportDate;
set => _saveData.LastReportDate = value;
@ -35,21 +35,22 @@ namespace Guru.Network
}
private readonly Action<string> _onNetworkStatusChanged;
private readonly Action<string> _onReportOffline;
private readonly Action<string> _onFirstOfflineToday;
private readonly NetworkMonitorData _saveData;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="onNetworkStatusChanged"></param>
/// <param name="onReportOffline"></param>
public NetworkStatusMonitor(Action<string> onNetworkStatusChanged, Action<string> onReportOffline)
/// <param name="onFirstOfflineToday"></param>
public NetworkStatusMonitor(Action<string> onNetworkStatusChanged, Action<string> onFirstOfflineToday)
{
_onNetworkStatusChanged = onNetworkStatusChanged;
_onReportOffline = onReportOffline;
_onFirstOfflineToday = onFirstOfflineToday;
_saveData = new NetworkMonitorData(); // 读取数据
//TODO: 此处不应使用协程实现, 协程只用在 UI 上。
CoroutineHelper.Instance.StartCoroutine(OnCheckingNetworkStatus(CHECK_STATUS_INTERVAL_SECOND));
}
@ -80,16 +81,16 @@ namespace Guru.Network
/// 当前是可以打点上报
/// </summary>
/// <returns></returns>
private bool CanSendReport() => DateTime.UtcNow.DayOfYear != LastReportDate.DayOfYear && DateTime.UtcNow.Year != LastReportDate.Year;
private bool IsSameDay(DateTime date) => DateTime.UtcNow.DayOfYear != date.DayOfYear && DateTime.UtcNow.Year != date.Year;
/// <summary>
/// 设置打点时间
/// </summary>
private void SetReportTime() => LastReportDate = DateTime.UtcNow;
private void SetTriggerFirstOfflineDate() => LastTriggerDate = DateTime.UtcNow;
private bool NeedReportOffline()
private bool ShouldTriggerFirstOffline()
{
return IsUserOffline() && CanSendReport();
return IsUserOffline() && IsSameDay(LastTriggerDate);
}
private void UpdateNetworkStatus()
@ -103,11 +104,11 @@ namespace Guru.Network
_onNetworkStatusChanged(status);
}
if (NeedReportOffline())
if (ShouldTriggerFirstOffline())
{
Debug.Log($"{Tag} --- Report Offline: {LastNetworkStatus}");
_onReportOffline?.Invoke(LastNetworkStatus);
SetReportTime();
_onFirstOfflineToday?.Invoke(LastNetworkStatus);
SetTriggerFirstOfflineDate();
}
LastNetworkStatus = status;