update: NetworkStatusMonitor 代码重构和 Review

--story=1020899 --user=yufei.hu 【BI】guru 埋点增加网络相关用户属性 https://www.tapd.cn/58098289/s/1160727

Signed-off-by: huyufei <yufei.hu@castbox.fm>
胡宇飞 2024-07-30 22:53:48 +08:00
parent ada48d7c0e
commit 742d81eab8
2 changed files with 7 additions and 86 deletions

View File

@ -162,7 +162,7 @@ namespace Guru
}
/// <summary>
/// Firebase上报用户属性
/// 设置用户属性
/// </summary>
public static void SetUserProperty(string key, string value)
{

View File

@ -1,10 +1,8 @@
namespace Guru.Network
{
using Guru;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class NetworkStatusMonitor
@ -19,98 +17,21 @@ namespace Guru.Network
private const string NETWORK_STATUS_BLUETOOTH = "bluetooth";
private const string NETWORK_STATUS_OTHER = "other";
private bool _isReady = false;
private INetworkStatusProxy _proxy;
private INetworkStatusProxy Proxy
{
get
{
if (_proxy == null)
{
#if UNITY_EDITOR
_proxy = new NetworkStatusEditor();
#elif UNITY_ANDROID
_proxy = new NetworkStatusAndroid();
#elif UNITY_IOS
_proxy = new NetworkStatusIOS();
#endif
}
if (_proxy == null)
{
// 如果未实现则报异常
throw new NotImplementedException("Can't find NetworkStatusProxy instance!!");
}
return _proxy;
}
}
/// <summary>
/// 网络状态列表
/// </summary>
private List<string> _statusNameList = new List<string>()
{
NETWORK_STATUS_VPN,
NETWORK_STATUS_TETHER,
NETWORK_STATUS_MOBILE,
NETWORK_STATUS_WIFI,
NETWORK_STATUS_ETHERNET,
NETWORK_STATUS_BLUETOOTH,
NETWORK_STATUS_OTHER,
NETWORK_STATUS_NONE,
};
public void Init(Action<bool> onInitComplete = null)
{
Proxy.Init(success =>
{
_isReady = success;
onInitComplete?.Invoke(success);
});
}
/// <summary>
/// 获取网络状态
/// </summary>
/// <returns></returns>
public string GetNetworkStatus()
{
if (_isReady)
switch (Application.internetReachability)
{
var status = Proxy.GetNetworkStatus();
if (status != null && status.Length > 0)
{
string statusName = "";
for (int i = 0; i < _statusNameList.Count; i++)
{
// 根据名称优先级, 先获取到的优先上报
statusName = _statusNameList[i];
if (status.Contains(statusName))
{
Debug.Log($"{Tag} --- GetNetworkStatus: [{string.Join(",", status)}]");
return statusName;
case NetworkReachability.ReachableViaLocalAreaNetwork:
return NETWORK_STATUS_WIFI;
case NetworkReachability.ReachableViaCarrierDataNetwork:
return NETWORK_STATUS_MOBILE;
}
}
}
}
Debug.LogWarning($"{Tag} --- Network Monitor is not ready!");
return NETWORK_STATUS_NONE;
}
// private void OnProxyInitComplete(bool success)
// {
// if (success)
// {
// Debug.Log($"{Tag} --- NetworkStatusMonitor init success");
// }
// else
// {
// Debug.LogError($"{Tag} --- NetworkStatusMonitor init failed");
// }
// }
}
}