| | 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 | |
|
| 62 | 21 | | private readonly Queue<Func<UniTask>> cardsVisualUpdateBuffer = new Queue<Func<UniTask>>(); |
| 62 | 22 | | private readonly Queue<Func<UniTask>> poolsPrewarmAsyncsBuffer = new Queue<Func<UniTask>>(); |
| 62 | 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 | |
|
| 0 | 64 | | public void SetAllAsLoading() => SetAllEventGroupsAsLoading(); |
| | 65 | |
|
| 0 | 66 | | public void SetShowMoreButtonActive(bool isActive) => SetShowMoreUpcomingEventsButtonActive(isActive); |
| | 67 | |
|
| 0 | 68 | | public int CurrentTilesPerRow => currentUpcomingEventsPerRow; |
| | 69 | |
|
| | 70 | | public event Action OnReady; |
| | 71 | | public event Action<EventCardComponentModel> OnInfoClicked; |
| | 72 | | public event Action<EventFromAPIModel> OnJumpInClicked; |
| | 73 | | public event Action<string> OnSubscribeEventClicked; |
| | 74 | | public event Action<string> OnUnsubscribeEventClicked; |
| | 75 | | public event Action OnShowMoreUpcomingEventsClicked; |
| | 76 | | public event Action OnEventsSubSectionEnable; |
| | 77 | |
|
| | 78 | | public override void Awake() |
| | 79 | | { |
| 59 | 80 | | base.Awake(); |
| 59 | 81 | | trendingEventsCanvas = trendingEvents.GetComponent<Canvas>(); |
| 59 | 82 | | upcomingEventsCanvas = upcomingEvents.GetComponent<Canvas>(); |
| 59 | 83 | | goingEventsCanvas = goingEvents.GetComponent<Canvas>(); |
| 59 | 84 | | } |
| | 85 | |
|
| | 86 | | public override void Start() |
| | 87 | | { |
| 52 | 88 | | eventModal = PlacesAndEventsCardsFactory.GetEventCardTemplateHiddenLazy(eventCardModalPrefab); |
| | 89 | |
|
| 52 | 90 | | featuredEvents.RemoveItems(); |
| 52 | 91 | | trendingEvents.RemoveItems(); |
| 52 | 92 | | upcomingEvents.RemoveItems(); |
| 52 | 93 | | goingEvents.RemoveItems(); |
| | 94 | |
|
| 52 | 95 | | showMoreUpcomingEventsButton.onClick.RemoveAllListeners(); |
| 52 | 96 | | showMoreUpcomingEventsButton.onClick.AddListener(() => OnShowMoreUpcomingEventsClicked?.Invoke()); |
| | 97 | |
|
| 52 | 98 | | OnReady?.Invoke(); |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public override void OnEnable() |
| | 102 | | { |
| 62 | 103 | | OnEventsSubSectionEnable?.Invoke(); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public override void Dispose() |
| | 107 | | { |
| 129 | 108 | | base.Dispose(); |
| 129 | 109 | | cancellationTokenSource.Cancel(); |
| | 110 | |
|
| 129 | 111 | | showMoreUpcomingEventsButton.onClick.RemoveAllListeners(); |
| | 112 | |
|
| 129 | 113 | | featuredEvents.Dispose(); |
| 129 | 114 | | upcomingEvents.Dispose(); |
| 129 | 115 | | trendingEvents.Dispose(); |
| 129 | 116 | | goingEvents.Dispose(); |
| | 117 | |
|
| 129 | 118 | | if (eventModal != null) |
| | 119 | | { |
| 78 | 120 | | eventModal.Dispose(); |
| 78 | 121 | | Destroy(eventModal.gameObject); |
| | 122 | | } |
| 129 | 123 | | } |
| | 124 | |
|
| | 125 | | public void SetActive(bool isActive) |
| | 126 | | { |
| 89 | 127 | | canvas.enabled = isActive; |
| | 128 | |
|
| 89 | 129 | | if (isActive) |
| 3 | 130 | | OnEnable(); |
| | 131 | | else |
| 86 | 132 | | OnDisable(); |
| 86 | 133 | | } |
| | 134 | |
|
| | 135 | | public void ConfigurePools() |
| | 136 | | { |
| 19 | 137 | | featuredEventCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(FEATURED_EVENT_CARDS_POOL_NAME, eventCardL |
| 19 | 138 | | trendingEventCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(TRENDING_EVENT_CARDS_POOL_NAME, eventCardP |
| 19 | 139 | | upcomingEventCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(UPCOMING_EVENT_CARDS_POOL_NAME, eventCardP |
| 19 | 140 | | goingEventCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(GOING_EVENT_CARDS_POOL_NAME, eventCardPrefab, |
| 19 | 141 | | } |
| | 142 | |
|
| | 143 | | public override void RefreshControl() |
| | 144 | | { |
| 0 | 145 | | featuredEvents.RefreshControl(); |
| 0 | 146 | | trendingEvents.RefreshControl(); |
| 0 | 147 | | upcomingEvents.RefreshControl(); |
| 0 | 148 | | goingEvents.RefreshControl(); |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | public void SetAllEventGroupsAsLoading() |
| | 152 | | { |
| 0 | 153 | | SetFeaturedEventsGroupAsLoading(); |
| | 154 | |
|
| 0 | 155 | | SetEventsGroupAsLoading(isVisible: true, goingEventsCanvas, goingEventsLoading); |
| 0 | 156 | | SetEventsGroupAsLoading(isVisible: true, trendingEventsCanvas, trendingEventsLoading); |
| 0 | 157 | | SetEventsGroupAsLoading(isVisible: true, upcomingEventsCanvas, upcomingEventsLoading); |
| | 158 | |
|
| 0 | 159 | | goingEventsNoDataText.gameObject.SetActive(false); |
| 0 | 160 | | trendingEventsNoDataText.gameObject.SetActive(false); |
| 0 | 161 | | upcomingEventsNoDataText.gameObject.SetActive(false); |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | internal void SetFeaturedEventsGroupAsLoading() |
| | 165 | | { |
| 1 | 166 | | featuredEvents.gameObject.SetActive(false); |
| 1 | 167 | | featuredEventsLoading.SetActive(true); |
| 1 | 168 | | } |
| | 169 | |
|
| | 170 | | public void SetEventsGroupAsLoading(bool isVisible, Canvas gridCanvas, GameObject loadingBar) |
| | 171 | | { |
| 9 | 172 | | gridCanvas.enabled = !isVisible; |
| 9 | 173 | | loadingBar.SetActive(isVisible); |
| 9 | 174 | | } |
| | 175 | |
|
| | 176 | | public void SetShowMoreUpcomingEventsButtonActive(bool isActive) => |
| 2 | 177 | | showMoreUpcomingEventsButtonContainer.gameObject.SetActive(isActive); |
| | 178 | |
|
| | 179 | | public void ShowEventModal(EventCardComponentModel eventInfo) |
| | 180 | | { |
| 1 | 181 | | eventModal.Show(); |
| 1 | 182 | | EventsCardsConfigurator.Configure(eventModal, eventInfo, OnInfoClicked, OnJumpInClicked, OnSubscribeEventClicked |
| 1 | 183 | | } |
| | 184 | |
|
| | 185 | | public void HideEventModal() |
| | 186 | | { |
| 1 | 187 | | if (eventModal != null) |
| 1 | 188 | | eventModal.Hide(); |
| 1 | 189 | | } |
| | 190 | |
|
| | 191 | | public void RestartScrollViewPosition() => |
| 0 | 192 | | scrollView.verticalNormalizedPosition = 1; |
| | 193 | |
|
| | 194 | | public void SetTrendingEvents(List<EventCardComponentModel> events) => |
| 1 | 195 | | SetEvents(events, trendingEvents, trendingEventsCanvas, trendingEventCardsPool, trendingEventsLoading, trendingE |
| | 196 | |
|
| | 197 | | public void SetGoingEvents(List<EventCardComponentModel> events) => |
| 1 | 198 | | SetEvents(events, goingEvents, goingEventsCanvas, goingEventCardsPool, goingEventsLoading, goingEventsNoDataText |
| | 199 | |
|
| | 200 | | public void SetUpcomingEvents(List<EventCardComponentModel> events) => |
| 1 | 201 | | SetEvents(events, upcomingEvents, upcomingEventsCanvas, upcomingEventCardsPool, upcomingEventsLoading, upcomingE |
| | 202 | |
|
| | 203 | | private void SetEvents(List<EventCardComponentModel> events, GridContainerComponentView eventsGrid, Canvas gridCanva |
| | 204 | | { |
| 3 | 205 | | SetEventsGroupAsLoading(false, gridCanvas, loadingBar); |
| 3 | 206 | | eventsNoDataText.gameObject.SetActive(events.Count == 0); |
| | 207 | |
|
| 3 | 208 | | eventCardsPool.ReleaseAll(); |
| | 209 | |
|
| 3 | 210 | | eventsGrid.ExtractItems(); |
| 3 | 211 | | eventsGrid.RemoveItems(); |
| | 212 | |
|
| 6 | 213 | | cardsVisualUpdateBuffer.Enqueue(() => SetEventsAsync(events, eventsGrid, eventCardsPool, cancellationTokenSource |
| 3 | 214 | | UpdateCardsVisual(); |
| 3 | 215 | | } |
| | 216 | |
|
| | 217 | | public void AddUpcomingEvents(List<EventCardComponentModel> events) |
| | 218 | | { |
| 2 | 219 | | cardsVisualUpdateBuffer.Enqueue(() => SetEventsAsync(events, upcomingEvents, upcomingEventCardsPool, cancellatio |
| 1 | 220 | | UpdateCardsVisual(); |
| 1 | 221 | | } |
| | 222 | |
|
| | 223 | | private async UniTask SetEventsAsync(List<EventCardComponentModel> events, GridContainerComponentView eventsGrid, Po |
| | 224 | | { |
| 20 | 225 | | foreach (EventCardComponentModel eventInfo in events) |
| | 226 | | { |
| 8 | 227 | | eventsGrid.AddItem( |
| | 228 | | PlacesAndEventsCardsFactory.CreateConfiguredEventCard(pool, eventInfo, OnInfoClicked, OnJumpInClicked, O |
| | 229 | |
|
| 24 | 230 | | await UniTask.NextFrame(cancellationToken); |
| | 231 | | } |
| | 232 | |
|
| 0 | 233 | | eventsGrid.SetItemSizeForModel(); |
| 0 | 234 | | poolsPrewarmAsyncsBuffer.Enqueue(() => pool.PrewarmAsync(events.Count, cancellationToken)); |
| 0 | 235 | | } |
| | 236 | |
|
| | 237 | | public void SetFeaturedEvents(List<EventCardComponentModel> events) |
| | 238 | | { |
| 1 | 239 | | featuredEventsLoading.SetActive(false); |
| 1 | 240 | | featuredEvents.gameObject.SetActive(events.Count > 0); |
| | 241 | |
|
| 1 | 242 | | featuredEventCardsPool.ReleaseAll(); |
| | 243 | |
|
| 1 | 244 | | featuredEvents.ExtractItems(); |
| | 245 | |
|
| 2 | 246 | | cardsVisualUpdateBuffer.Enqueue(() => SetFeaturedEventsAsync(events, cancellationTokenSource.Token)); |
| 1 | 247 | | UpdateCardsVisual(); |
| 1 | 248 | | } |
| | 249 | |
|
| | 250 | | private async UniTask SetFeaturedEventsAsync(List<EventCardComponentModel> events, CancellationToken cancellationTok |
| | 251 | | { |
| 5 | 252 | | foreach (EventCardComponentModel eventInfo in events) |
| | 253 | | { |
| 2 | 254 | | featuredEvents.AddItem( |
| | 255 | | PlacesAndEventsCardsFactory.CreateConfiguredEventCard(featuredEventCardsPool, eventInfo, OnInfoClicked, |
| | 256 | |
|
| 6 | 257 | | await UniTask.NextFrame(cancellationToken); |
| | 258 | | } |
| | 259 | |
|
| 0 | 260 | | featuredEvents.SetManualControlsActive(); |
| 0 | 261 | | featuredEvents.GenerateDotsSelector(); |
| | 262 | |
|
| 0 | 263 | | poolsPrewarmAsyncsBuffer.Enqueue(() => featuredEventCardsPool.PrewarmAsync(events.Count, cancellationToken)); |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | private void UpdateCardsVisual() |
| | 267 | | { |
| 5 | 268 | | if (!isUpdatingCardsVisual) |
| 5 | 269 | | ShowCardsProcess().Forget(); |
| | 270 | |
|
| | 271 | | async UniTask ShowCardsProcess() |
| | 272 | | { |
| 5 | 273 | | isUpdatingCardsVisual = true; |
| | 274 | |
|
| 5 | 275 | | while (cardsVisualUpdateBuffer.Count > 0) |
| 15 | 276 | | await cardsVisualUpdateBuffer.Dequeue().Invoke(); |
| | 277 | |
|
| 0 | 278 | | while (poolsPrewarmAsyncsBuffer.Count > 0) |
| 0 | 279 | | await poolsPrewarmAsyncsBuffer.Dequeue().Invoke(); |
| | 280 | |
|
| 0 | 281 | | isUpdatingCardsVisual = false; |
| 0 | 282 | | } |
| 5 | 283 | | } |
| | 284 | | } |