| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | public class EventsSubSectionComponentView : BaseComponentView, IEventsSubSectionComponentView |
| | 11 | | { |
| | 12 | | internal const string FEATURED_EVENT_CARDS_POOL_NAME = "Events_FeaturedEventCardsPool"; |
| | 13 | | private const int FEATURED_EVENT_CARDS_POOL_PREWARM = 10; |
| | 14 | | private const string TRENDING_EVENT_CARDS_POOL_NAME = "Events_TrendingEventCardsPool"; |
| | 15 | | private const int TRENDING_EVENT_CARDS_POOL_PREWARM = 12; |
| | 16 | | private const string UPCOMING_EVENT_CARDS_POOL_NAME = "Events_UpcomingEventCardsPool"; |
| | 17 | | private const int UPCOMING_EVENT_CARDS_POOL_PREWARM = 9; |
| | 18 | | private const string GOING_EVENT_CARDS_POOL_NAME = "Events_FeatureGoingEventCardsPool"; |
| | 19 | | private const int GOING_EVENT_CARDS_POOL_PREWARM = 9; |
| | 20 | |
|
| 67 | 21 | | private readonly Queue<Func<UniTask>> cardsVisualUpdateBuffer = new Queue<Func<UniTask>>(); |
| 67 | 22 | | private readonly Queue<Func<UniTask>> poolsPrewarmAsyncsBuffer = new Queue<Func<UniTask>>(); |
| 67 | 23 | | private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); |
| | 24 | |
|
| | 25 | | [Header("Assets References")] |
| | 26 | | [SerializeField] internal EventCardComponentView eventCardPrefab; |
| | 27 | | [SerializeField] internal EventCardComponentView eventCardLongPrefab; |
| | 28 | | [SerializeField] internal EventCardComponentView eventCardModalPrefab; |
| | 29 | |
|
| | 30 | | [Header("Prefab References")] |
| | 31 | | [SerializeField] internal ScrollRect scrollView; |
| | 32 | | [SerializeField] internal CarouselComponentView featuredEvents; |
| | 33 | | [SerializeField] internal GameObject featuredEventsLoading; |
| | 34 | | [SerializeField] internal GridContainerComponentView trendingEvents; |
| | 35 | | [SerializeField] internal GameObject trendingEventsLoading; |
| | 36 | | [SerializeField] internal TMP_Text trendingEventsNoDataText; |
| | 37 | | [SerializeField] internal GridContainerComponentView upcomingEvents; |
| | 38 | | [SerializeField] internal GameObject upcomingEventsLoading; |
| | 39 | | [SerializeField] internal TMP_Text upcomingEventsNoDataText; |
| | 40 | | [SerializeField] internal GridContainerComponentView goingEvents; |
| | 41 | | [SerializeField] internal GameObject goingEventsLoading; |
| | 42 | | [SerializeField] internal TMP_Text goingEventsNoDataText; |
| | 43 | | [SerializeField] internal GameObject showMoreUpcomingEventsButtonContainer; |
| | 44 | | [SerializeField] internal ButtonComponentView showMoreUpcomingEventsButton; |
| | 45 | |
|
| | 46 | | [SerializeField] private Canvas canvas; |
| | 47 | |
|
| | 48 | | internal Pool featuredEventCardsPool; |
| | 49 | | internal Pool trendingEventCardsPool; |
| | 50 | | internal Pool upcomingEventCardsPool; |
| | 51 | | internal Pool goingEventCardsPool; |
| | 52 | |
|
| | 53 | | internal EventCardComponentView eventModal; |
| | 54 | |
|
| | 55 | | private Canvas trendingEventsCanvas; |
| | 56 | | private Canvas upcomingEventsCanvas; |
| | 57 | | private Canvas goingEventsCanvas; |
| | 58 | |
|
| | 59 | | private bool isUpdatingCardsVisual; |
| | 60 | |
|
| 0 | 61 | | public int currentUpcomingEventsPerRow => upcomingEvents.currentItemsPerRow; |
| | 62 | |
|
| | 63 | | public event Action OnReady; |
| | 64 | | public event Action<EventCardComponentModel> OnInfoClicked; |
| | 65 | | public event Action<EventFromAPIModel> OnJumpInClicked; |
| | 66 | | public event Action<string> OnSubscribeEventClicked; |
| | 67 | | public event Action<string> OnUnsubscribeEventClicked; |
| | 68 | | public event Action OnShowMoreUpcomingEventsClicked; |
| | 69 | | public event Action OnEventsSubSectionEnable; |
| | 70 | |
|
| | 71 | | public override void Awake() |
| | 72 | | { |
| 64 | 73 | | base.Awake(); |
| 64 | 74 | | trendingEventsCanvas = trendingEvents.GetComponent<Canvas>(); |
| 64 | 75 | | upcomingEventsCanvas = upcomingEvents.GetComponent<Canvas>(); |
| 64 | 76 | | goingEventsCanvas = goingEvents.GetComponent<Canvas>(); |
| 64 | 77 | | } |
| | 78 | |
|
| | 79 | | public override void Start() |
| | 80 | | { |
| 57 | 81 | | eventModal = ExploreEventsUtils.ConfigureEventCardModal(eventCardModalPrefab); |
| | 82 | |
|
| 57 | 83 | | featuredEvents.RemoveItems(); |
| 57 | 84 | | trendingEvents.RemoveItems(); |
| 57 | 85 | | upcomingEvents.RemoveItems(); |
| 57 | 86 | | goingEvents.RemoveItems(); |
| | 87 | |
|
| 57 | 88 | | showMoreUpcomingEventsButton.onClick.RemoveAllListeners(); |
| 57 | 89 | | showMoreUpcomingEventsButton.onClick.AddListener(() => OnShowMoreUpcomingEventsClicked?.Invoke()); |
| | 90 | |
|
| 57 | 91 | | OnReady?.Invoke(); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | public override void OnEnable() |
| | 95 | | { |
| 67 | 96 | | OnEventsSubSectionEnable?.Invoke(); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public override void Dispose() |
| | 100 | | { |
| 139 | 101 | | base.Dispose(); |
| 139 | 102 | | cancellationTokenSource.Cancel(); |
| | 103 | |
|
| 139 | 104 | | showMoreUpcomingEventsButton.onClick.RemoveAllListeners(); |
| | 105 | |
|
| 139 | 106 | | featuredEvents.Dispose(); |
| 139 | 107 | | upcomingEvents.Dispose(); |
| 139 | 108 | | trendingEvents.Dispose(); |
| 139 | 109 | | goingEvents.Dispose(); |
| | 110 | |
|
| 139 | 111 | | if (eventModal != null) |
| | 112 | | { |
| 88 | 113 | | eventModal.Dispose(); |
| 88 | 114 | | Destroy(eventModal.gameObject); |
| | 115 | | } |
| 139 | 116 | | } |
| | 117 | |
|
| | 118 | | public void SetActive(bool isActive) |
| | 119 | | { |
| 53 | 120 | | canvas.enabled = isActive; |
| | 121 | |
|
| 53 | 122 | | if (isActive) |
| 3 | 123 | | OnEnable(); |
| | 124 | | else |
| 50 | 125 | | OnDisable(); |
| 50 | 126 | | } |
| | 127 | |
|
| | 128 | | public void ConfigurePools() |
| | 129 | | { |
| 19 | 130 | | ExploreEventsUtils.ConfigureEventCardsPool(out featuredEventCardsPool, FEATURED_EVENT_CARDS_POOL_NAME, eventCard |
| 19 | 131 | | ExploreEventsUtils.ConfigureEventCardsPool(out trendingEventCardsPool, TRENDING_EVENT_CARDS_POOL_NAME, eventCard |
| 19 | 132 | | ExploreEventsUtils.ConfigureEventCardsPool(out upcomingEventCardsPool, UPCOMING_EVENT_CARDS_POOL_NAME, eventCard |
| 19 | 133 | | ExploreEventsUtils.ConfigureEventCardsPool(out goingEventCardsPool, GOING_EVENT_CARDS_POOL_NAME, eventCardPrefab |
| 19 | 134 | | } |
| | 135 | |
|
| | 136 | | public override void RefreshControl() |
| | 137 | | { |
| 0 | 138 | | featuredEvents.RefreshControl(); |
| 0 | 139 | | trendingEvents.RefreshControl(); |
| 0 | 140 | | upcomingEvents.RefreshControl(); |
| 0 | 141 | | goingEvents.RefreshControl(); |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | public void SetAllEventGroupsAsLoading() |
| | 145 | | { |
| 0 | 146 | | SetFeaturedEventsGroupAsLoading(); |
| | 147 | |
|
| 0 | 148 | | SetEventsGroupAsLoading(isVisible: true, goingEventsCanvas, goingEventsLoading); |
| 0 | 149 | | SetEventsGroupAsLoading(isVisible: true, trendingEventsCanvas, trendingEventsLoading); |
| 0 | 150 | | SetEventsGroupAsLoading(isVisible: true, upcomingEventsCanvas, upcomingEventsLoading); |
| | 151 | |
|
| 0 | 152 | | goingEventsNoDataText.gameObject.SetActive(false); |
| 0 | 153 | | trendingEventsNoDataText.gameObject.SetActive(false); |
| 0 | 154 | | upcomingEventsNoDataText.gameObject.SetActive(false); |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | internal void SetFeaturedEventsGroupAsLoading() |
| | 158 | | { |
| 1 | 159 | | featuredEvents.gameObject.SetActive(false); |
| 1 | 160 | | featuredEventsLoading.SetActive(true); |
| 1 | 161 | | } |
| | 162 | |
|
| | 163 | | public void SetEventsGroupAsLoading(bool isVisible, Canvas gridCanvas, GameObject loadingBar) |
| | 164 | | { |
| 9 | 165 | | gridCanvas.enabled = !isVisible; |
| 9 | 166 | | loadingBar.SetActive(isVisible); |
| 9 | 167 | | } |
| | 168 | |
|
| | 169 | | public void SetShowMoreUpcomingEventsButtonActive(bool isActive) => |
| 2 | 170 | | showMoreUpcomingEventsButtonContainer.gameObject.SetActive(isActive); |
| | 171 | |
|
| | 172 | | public void ShowEventModal(EventCardComponentModel eventInfo) |
| | 173 | | { |
| 1 | 174 | | eventModal.Show(); |
| 1 | 175 | | ExploreEventsUtils.ConfigureEventCard(eventModal, eventInfo, OnInfoClicked, OnJumpInClicked, OnSubscribeEventCli |
| 1 | 176 | | } |
| | 177 | |
|
| | 178 | | public void HideEventModal() |
| | 179 | | { |
| 1 | 180 | | if (eventModal != null) |
| 1 | 181 | | eventModal.Hide(); |
| 1 | 182 | | } |
| | 183 | |
|
| | 184 | | public void RestartScrollViewPosition() => |
| 0 | 185 | | scrollView.verticalNormalizedPosition = 1; |
| | 186 | |
|
| | 187 | | public void SetTrendingEvents(List<EventCardComponentModel> events) => |
| 1 | 188 | | SetEvents(events, trendingEvents, trendingEventsCanvas, trendingEventCardsPool, trendingEventsLoading, trendingE |
| | 189 | |
|
| | 190 | | public void SetGoingEvents(List<EventCardComponentModel> events) => |
| 1 | 191 | | SetEvents(events, goingEvents, goingEventsCanvas, goingEventCardsPool, goingEventsLoading, goingEventsNoDataText |
| | 192 | |
|
| | 193 | | public void SetUpcomingEvents(List<EventCardComponentModel> events) => |
| 1 | 194 | | SetEvents(events, upcomingEvents, upcomingEventsCanvas, upcomingEventCardsPool, upcomingEventsLoading, upcomingE |
| | 195 | |
|
| | 196 | | private void SetEvents(List<EventCardComponentModel> events, GridContainerComponentView eventsGrid, Canvas gridCanva |
| | 197 | | { |
| 3 | 198 | | SetEventsGroupAsLoading(false, gridCanvas, loadingBar); |
| 3 | 199 | | eventsNoDataText.gameObject.SetActive(events.Count == 0); |
| | 200 | |
|
| 3 | 201 | | eventCardsPool.ReleaseAll(); |
| | 202 | |
|
| 3 | 203 | | eventsGrid.ExtractItems(); |
| 3 | 204 | | eventsGrid.RemoveItems(); |
| | 205 | |
|
| 6 | 206 | | cardsVisualUpdateBuffer.Enqueue(() => SetEventsAsync(events, eventsGrid, eventCardsPool, cancellationTokenSource |
| 3 | 207 | | UpdateCardsVisual(); |
| 3 | 208 | | } |
| | 209 | |
|
| | 210 | | public void AddUpcomingEvents(List<EventCardComponentModel> events) |
| | 211 | | { |
| 2 | 212 | | cardsVisualUpdateBuffer.Enqueue(() => SetEventsAsync(events, upcomingEvents, upcomingEventCardsPool, cancellatio |
| 1 | 213 | | UpdateCardsVisual(); |
| 1 | 214 | | } |
| | 215 | |
|
| | 216 | | private async UniTask SetEventsAsync(List<EventCardComponentModel> events, GridContainerComponentView eventsGrid, Po |
| | 217 | | { |
| 20 | 218 | | foreach (EventCardComponentModel eventInfo in events) |
| | 219 | | { |
| 8 | 220 | | eventsGrid.AddItem( |
| | 221 | | ExploreEventsUtils.InstantiateConfiguredEventCard(eventInfo, pool, |
| | 222 | | OnInfoClicked, OnJumpInClicked, OnSubscribeEventClicked, OnUnsubscribeEventClicked)); |
| 24 | 223 | | await UniTask.NextFrame(cancellationToken); |
| | 224 | | } |
| | 225 | |
|
| 0 | 226 | | eventsGrid.SetItemSizeForModel(); |
| 0 | 227 | | poolsPrewarmAsyncsBuffer.Enqueue(() => pool.PrewarmAsync(events.Count, cancellationToken)); |
| 0 | 228 | | } |
| | 229 | |
|
| | 230 | | public void SetFeaturedEvents(List<EventCardComponentModel> events) |
| | 231 | | { |
| 1 | 232 | | featuredEventsLoading.SetActive(false); |
| 1 | 233 | | featuredEvents.gameObject.SetActive(events.Count > 0); |
| | 234 | |
|
| 1 | 235 | | featuredEventCardsPool.ReleaseAll(); |
| | 236 | |
|
| 1 | 237 | | featuredEvents.ExtractItems(); |
| | 238 | |
|
| 2 | 239 | | cardsVisualUpdateBuffer.Enqueue(() => SetFeaturedEventsAsync(events, cancellationTokenSource.Token)); |
| 1 | 240 | | UpdateCardsVisual(); |
| 1 | 241 | | } |
| | 242 | |
|
| | 243 | | private async UniTask SetFeaturedEventsAsync(List<EventCardComponentModel> events, CancellationToken cancellationTok |
| | 244 | | { |
| 5 | 245 | | foreach (EventCardComponentModel eventInfo in events) |
| | 246 | | { |
| 2 | 247 | | featuredEvents.AddItem( |
| | 248 | | ExploreEventsUtils.InstantiateConfiguredEventCard(eventInfo, featuredEventCardsPool, |
| | 249 | | OnInfoClicked, OnJumpInClicked, OnSubscribeEventClicked, OnUnsubscribeEventClicked)); |
| 6 | 250 | | await UniTask.NextFrame(cancellationToken); |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | featuredEvents.SetManualControlsActive(); |
| 0 | 254 | | featuredEvents.GenerateDotsSelector(); |
| | 255 | |
|
| 0 | 256 | | poolsPrewarmAsyncsBuffer.Enqueue(() => featuredEventCardsPool.PrewarmAsync(events.Count, cancellationToken)); |
| 0 | 257 | | } |
| | 258 | |
|
| | 259 | | private void UpdateCardsVisual() |
| | 260 | | { |
| 5 | 261 | | if (!isUpdatingCardsVisual) |
| 5 | 262 | | ShowCardsProcess().Forget(); |
| | 263 | |
|
| | 264 | | async UniTask ShowCardsProcess() |
| | 265 | | { |
| 5 | 266 | | isUpdatingCardsVisual = true; |
| | 267 | |
|
| 5 | 268 | | while (cardsVisualUpdateBuffer.Count > 0) |
| 15 | 269 | | await cardsVisualUpdateBuffer.Dequeue().Invoke(); |
| | 270 | |
|
| 0 | 271 | | while (poolsPrewarmAsyncsBuffer.Count > 0) |
| 0 | 272 | | await poolsPrewarmAsyncsBuffer.Dequeue().Invoke(); |
| | 273 | |
|
| 0 | 274 | | isUpdatingCardsVisual = false; |
| 0 | 275 | | } |
| 5 | 276 | | } |
| | 277 | | } |