update: 重构 networkstatus 接口和逻辑
Signed-off-by: huyufei <yufei.hu@castbox.fm>
							parent
							
								
									3f0f13be6c
								
							
						
					
					
						commit
						b4fbb5d3da
					
				
										
											Binary file not shown.
										
									
								
							|  | @ -1,32 +0,0 @@ | |||
| fileFormatVersion: 2 | ||||
| guid: 32eda01e213614348899eefe856392d3 | ||||
| PluginImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   iconMap: {} | ||||
|   executionOrder: {} | ||||
|   defineConstraints: [] | ||||
|   isPreloaded: 0 | ||||
|   isOverridable: 0 | ||||
|   isExplicitlyReferenced: 0 | ||||
|   validateReferences: 1 | ||||
|   platformData: | ||||
|   - first: | ||||
|       Android: Android | ||||
|     second: | ||||
|       enabled: 1 | ||||
|       settings: {} | ||||
|   - first: | ||||
|       Any:  | ||||
|     second: | ||||
|       enabled: 0 | ||||
|       settings: {} | ||||
|   - first: | ||||
|       Editor: Editor | ||||
|     second: | ||||
|       enabled: 0 | ||||
|       settings: | ||||
|         DefaultValueInitialized: true | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
|  | @ -19,7 +19,6 @@ namespace Guru | |||
|         public string[] uploadIpAddress = null; | ||||
|         public bool enableErrorLog = false; | ||||
|         public bool isDebug = false; | ||||
|         public Action onInitComplete = null; | ||||
|     } | ||||
|      | ||||
|      | ||||
|  | @ -117,7 +116,7 @@ namespace Guru | |||
|         /// <summary> | ||||
|         /// 初始化接口 | ||||
|         /// </summary> | ||||
|         public static void Init(string appId, string deviceInfo, GuruAnalyticsInitConfig initConfig, Action onInitComplete = null) | ||||
|         public static void Init(string appId, string deviceInfo, GuruAnalyticsInitConfig initConfig, Action onInitComplete) | ||||
|         { | ||||
|             Debug.Log($"{Tag} --- Guru Analytics [{Version}] initialing..."); | ||||
|             if (_instance == null) | ||||
|  |  | |||
|  | @ -1,13 +1,17 @@ | |||
| 
 | ||||
| 
 | ||||
| namespace Guru.Network | ||||
| { | ||||
|     using Guru; | ||||
|     using System; | ||||
|     using UnityEngine; | ||||
| 
 | ||||
|     using System.Collections; | ||||
|      | ||||
|     public class NetworkStatusMonitor | ||||
|     { | ||||
|         private const string Tag = "[NET]"; | ||||
|         private const string NETWORK_STATUS_NOT_SET = "not_set"; | ||||
|          | ||||
|         private const string NETWORK_STATUS_NONE = "none"; | ||||
|         private const string NETWORK_STATUS_MOBILE = "mobile"; | ||||
|         private const string NETWORK_STATUS_WIFI = "wifi"; | ||||
|  | @ -16,6 +20,31 @@ namespace Guru.Network | |||
|         private const string NETWORK_STATUS_TETHER = "tether"; | ||||
|         private const string NETWORK_STATUS_BLUETOOTH = "bluetooth"; | ||||
|         private const string NETWORK_STATUS_OTHER = "other"; | ||||
| 
 | ||||
|         private const int CHECK_STATUS_INTERVAL_SECOND = 10; | ||||
| 
 | ||||
|         private DateTime _lastReportTime; | ||||
|         // TODO: 最好做一下持久化存储 | ||||
|         private string LastNetworkStatus | ||||
|         { | ||||
|             get => PlayerPrefs.GetString(nameof(LastNetworkStatus), NETWORK_STATUS_NOT_SET); | ||||
|             set => PlayerPrefs.SetString(nameof(LastNetworkStatus), value); | ||||
|         } | ||||
| 
 | ||||
|         private readonly Action<string> _onNetworkStatusChanged; | ||||
|         private readonly Action<string> _onReportOffline; | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 构造函数 | ||||
|         /// </summary> | ||||
|         /// <param name="onNetworkStatusChanged"></param> | ||||
|         /// <param name="onReportOffline"></param> | ||||
|         public NetworkStatusMonitor(Action<string> onNetworkStatusChanged, Action<string> onReportOffline) | ||||
|         { | ||||
|             _onNetworkStatusChanged = onNetworkStatusChanged; | ||||
|             _onReportOffline = onReportOffline; | ||||
|             CoroutineHelper.Instance.StartCoroutine(OnCheckingNetworkStatus(CHECK_STATUS_INTERVAL_SECOND)); | ||||
|         } | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 获取网络状态 | ||||
|  | @ -33,5 +62,62 @@ namespace Guru.Network | |||
|             return NETWORK_STATUS_NONE; | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 用户是否已经失去了链接 | ||||
|         /// </summary> | ||||
|         /// <returns></returns> | ||||
|         private bool IsUserOffline() => Application.internetReachability == NetworkReachability.NotReachable; | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 当前是可以打点上报 | ||||
|         /// </summary> | ||||
|         /// <returns></returns> | ||||
|         private bool CanSendReport() => DateTime.UtcNow.DayOfYear != _lastReportTime.DayOfYear && DateTime.UtcNow.Year != _lastReportTime.Year; | ||||
| 
 | ||||
|         /// <summary> | ||||
|         /// 设置打点时间 | ||||
|         /// </summary> | ||||
|         private void SetReportTime() => _lastReportTime = DateTime.UtcNow; | ||||
|          | ||||
|         private bool NeedReportOffline() | ||||
|         { | ||||
|             return IsUserOffline() && CanSendReport(); | ||||
|         } | ||||
|          | ||||
|         private void UpdateNetworkStatus() | ||||
|         { | ||||
|             var status = GetNetworkStatus(); | ||||
| #if DEBUG | ||||
|             Debug.Log($"{Tag} --- Update network status:{status}"); | ||||
| #endif | ||||
|             if (status != LastNetworkStatus) | ||||
|             { | ||||
|                 _onNetworkStatusChanged(status); | ||||
|             } | ||||
|              | ||||
|             if (NeedReportOffline()) | ||||
|             { | ||||
|                 SetReportTime(); | ||||
|                 _onReportOffline?.Invoke(LastNetworkStatus); | ||||
|                 Debug.Log($"{Tag} --- Report Offline: {LastNetworkStatus}"); | ||||
|             } | ||||
|              | ||||
|             LastNetworkStatus = status; | ||||
|         } | ||||
|          | ||||
|         /// <summary> | ||||
|         /// 每隔一段时间检测一次网络状态 | ||||
|         /// </summary> | ||||
|         /// <param name="interval"></param> | ||||
|         /// <returns></returns> | ||||
|         private IEnumerator OnCheckingNetworkStatus(float interval) | ||||
|         { | ||||
|             while (true) | ||||
|             { | ||||
|                 UpdateNetworkStatus(); | ||||
|                 yield return new WaitForSeconds(interval); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| } | ||||
		Loading…
	
		Reference in New Issue