| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL.Interface; |
| | 5 | | using System.Collections; |
| | 6 | | using UpdateMode = DCL.MapGlobalUsersPositionMarkerController.UpdateMode; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Handle the fetch of hot scenes at intervals. |
| | 12 | | /// Interval time may change accordingly if set to update in background or foregraound |
| | 13 | | /// </summary> |
| | 14 | | internal class FetchScenesHandler : IDisposable |
| | 15 | | { |
| | 16 | | public event Action<List<HotScenesController.HotSceneInfo>> OnScenesFetched; |
| | 17 | |
|
| | 18 | | float initialIntevalTime; |
| | 19 | | float backgroundIntervalTime; |
| | 20 | | float foregroundIntervalTime; |
| | 21 | |
|
| | 22 | | Coroutine updateCoroutine; |
| | 23 | | UpdateMode updateMode; |
| | 24 | |
|
| | 25 | | internal bool isFirstFetch; |
| | 26 | | internal float updateInterval; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Constructor |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="initialIntevalTime">seconds for interval until the first populated scenes are fetched</param> |
| | 32 | | /// <param name="foregroundIntervalTime">seconds for interval when update mode is in FOREGROUND</param> |
| | 33 | | /// <param name="backgroundIntervalTime">seconds for interval when update mode is in BACKGROUND</param> |
| 11 | 34 | | public FetchScenesHandler(float initialIntevalTime, float foregroundIntervalTime, float backgroundIntervalTime) |
| | 35 | | { |
| 11 | 36 | | this.initialIntevalTime = initialIntevalTime; |
| 11 | 37 | | this.backgroundIntervalTime = backgroundIntervalTime; |
| 11 | 38 | | this.foregroundIntervalTime = foregroundIntervalTime; |
| 11 | 39 | | this.updateInterval = initialIntevalTime; |
| 11 | 40 | | this.isFirstFetch = true; |
| 11 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Initialize fetch intervals |
| | 45 | | /// </summary> |
| | 46 | | public void Init() |
| | 47 | | { |
| 10 | 48 | | if (updateCoroutine != null) |
| 0 | 49 | | return; |
| | 50 | |
|
| 10 | 51 | | updateCoroutine = CoroutineStarter.Start(UpdateCoroutine()); |
| 10 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Set update mode. Scene's fetch intervals will smaller when updating in FOREGROUND than when updating in BACK |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="mode">update mode</param> |
| | 58 | | public void SetUpdateMode(UpdateMode mode) |
| | 59 | | { |
| 13 | 60 | | updateMode = mode; |
| 13 | 61 | | if (isFirstFetch) |
| 11 | 62 | | return; |
| | 63 | |
|
| 2 | 64 | | switch (updateMode) |
| | 65 | | { |
| | 66 | | case UpdateMode.BACKGROUND: |
| 1 | 67 | | updateInterval = backgroundIntervalTime; |
| 1 | 68 | | break; |
| | 69 | | case UpdateMode.FOREGROUND: |
| 1 | 70 | | updateInterval = foregroundIntervalTime; |
| | 71 | | break; |
| | 72 | | } |
| 1 | 73 | | } |
| | 74 | |
|
| | 75 | | public void Dispose() |
| | 76 | | { |
| 12 | 77 | | CoroutineStarter.Stop(updateCoroutine); |
| | 78 | |
|
| 12 | 79 | | if (HotScenesController.i) |
| 0 | 80 | | HotScenesController.i.OnHotSceneListFinishUpdating -= OnHotSceneListFinishUpdating; |
| 12 | 81 | | } |
| | 82 | |
|
| | 83 | | private IEnumerator UpdateCoroutine() |
| | 84 | | { |
| 0 | 85 | | while (true) |
| | 86 | | { |
| 10 | 87 | | float time = Time.realtimeSinceStartup; |
| | 88 | |
|
| 12 | 89 | | while (Time.realtimeSinceStartup - time < updateInterval) |
| | 90 | | { |
| 12 | 91 | | yield return null; |
| | 92 | | } |
| | 93 | |
|
| 0 | 94 | | if (HotScenesController.i.timeSinceLastUpdate > updateInterval) |
| | 95 | | { |
| 0 | 96 | | HotScenesController.i.OnHotSceneListFinishUpdating += OnHotSceneListFinishUpdating; |
| 0 | 97 | | WebInterface.FetchHotScenes(); |
| 0 | 98 | | } |
| | 99 | | else |
| | 100 | | { |
| 0 | 101 | | OnHotSceneListFinishUpdating(); |
| | 102 | | } |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|
| 0 | 106 | | private void OnHotSceneListFinishUpdating() { OnHotScenesFetched(HotScenesController.i.hotScenesList); } |
| | 107 | |
|
| | 108 | | private void OnHotScenesFetched(List<HotScenesController.HotSceneInfo> scenes) |
| | 109 | | { |
| 0 | 110 | | HotScenesController.i.OnHotSceneListFinishUpdating -= OnHotSceneListFinishUpdating; |
| | 111 | |
|
| 0 | 112 | | bool fetchSuccess = scenes.Count > 0 && scenes[0].usersTotalCount > 0; |
| | 113 | |
|
| 0 | 114 | | if (!fetchSuccess) |
| 0 | 115 | | return; |
| | 116 | |
|
| 0 | 117 | | if (isFirstFetch) |
| | 118 | | { |
| 0 | 119 | | isFirstFetch = false; |
| 0 | 120 | | SetUpdateMode(updateMode); |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | OnScenesFetched?.Invoke(scenes); |
| 0 | 124 | | } |
| | 125 | | } |
| | 126 | | } |