| | 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 HighlightsSubSectionComponentView : BaseComponentView, IHighlightsSubSectionComponentView |
| | 11 | | { |
| | 12 | | private const string TRENDING_PLACE_CARDS_POOL_NAME = "Highlights_TrendingPlaceCardsPool"; |
| | 13 | | private const int TRENDING_PLACE_CARDS_POOL_PREWARM = 10; |
| | 14 | | private const string TRENDING_EVENT_CARDS_POOL_NAME = "Highlights_TrendingEventCardsPool"; |
| | 15 | | private const int TRENDING_EVENT_CARDS_POOL_PREWARM = 10; |
| | 16 | | private const string FEATURED_PLACE_CARDS_POOL_NAME = "Highlights_FeaturedPlaceCardsPool"; |
| | 17 | | private const int FEATURED_PLACE_CARDS_POOL_PREWARM = 9; |
| | 18 | | private const string LIVE_EVENT_CARDS_POOL_NAME = "Highlights_LiveEventCardsPool"; |
| | 19 | | private const int LIVE_EVENT_CARDS_POOL_PREWARM = 3; |
| | 20 | |
|
| 62 | 21 | | private readonly Queue<Func<UniTask>> cardsVisualUpdateBuffer = new (); |
| 62 | 22 | | private readonly Queue<Func<UniTask>> poolsPrewarmAsyncsBuffer = new (); |
| 62 | 23 | | private readonly CancellationTokenSource cancellationTokenSource = new (); |
| | 24 | |
|
| | 25 | | [Header("Assets References")] |
| | 26 | | [SerializeField] internal PlaceCardComponentView placeCardLongPrefab; |
| | 27 | | [SerializeField] internal EventCardComponentView eventCardLongPrefab; |
| | 28 | | [SerializeField] internal PlaceCardComponentView placeCardPrefab; |
| | 29 | | [SerializeField] internal PlaceCardComponentView placeCardModalPrefab; |
| | 30 | | [SerializeField] internal EventCardComponentView eventCardPrefab; |
| | 31 | | [SerializeField] internal EventCardComponentView eventCardModalPrefab; |
| | 32 | |
|
| | 33 | | [Header("Prefab References")] |
| | 34 | | [SerializeField] internal ScrollRect scrollView; |
| | 35 | | [SerializeField] internal CarouselComponentView trendingPlacesAndEvents; |
| | 36 | | [SerializeField] internal GameObject trendingPlacesAndEventsLoading; |
| | 37 | | [SerializeField] internal GridContainerComponentView featuredPlaces; |
| | 38 | | [SerializeField] internal GameObject featuredPlacesLoading; |
| | 39 | | [SerializeField] internal TMP_Text featuredPlacesNoDataText; |
| | 40 | | [SerializeField] internal GridContainerComponentView liveEvents; |
| | 41 | | [SerializeField] internal GameObject liveEventsLoading; |
| | 42 | | [SerializeField] internal TMP_Text liveEventsNoDataText; |
| | 43 | | [SerializeField] internal ButtonComponentView viewAllEventsButton; |
| | 44 | | [SerializeField] internal Color[] friendColors = null; |
| | 45 | |
|
| | 46 | | [SerializeField] private Canvas canvas; |
| | 47 | |
|
| | 48 | | internal PlaceCardComponentView placeModal; |
| | 49 | | internal EventCardComponentView eventModal; |
| | 50 | | internal Pool trendingPlaceCardsPool; |
| | 51 | | internal Pool trendingEventCardsPool; |
| | 52 | | internal Pool featuredPlaceCardsPool; |
| | 53 | | internal Pool liveEventCardsPool; |
| | 54 | |
|
| | 55 | | private bool isUpdatingCardsVisual; |
| | 56 | |
|
| 0 | 57 | | public Color[] currentFriendColors => friendColors; |
| | 58 | |
|
| | 59 | | public void SetAllAsLoading() |
| | 60 | | { |
| 0 | 61 | | SetTrendingPlacesAndEventsAsLoading(true); |
| 0 | 62 | | SetFeaturedPlacesAsLoading(true); |
| 0 | 63 | | SetLiveAsLoading(true); |
| 0 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | public int CurrentTilesPerRow { get; } |
| | 67 | |
|
| | 68 | | public event Action OnReady; |
| | 69 | | public event Action<PlaceCardComponentModel> OnPlaceInfoClicked; |
| | 70 | | public event Action<EventCardComponentModel> OnEventInfoClicked; |
| | 71 | | public event Action<HotScenesController.HotSceneInfo> OnPlaceJumpInClicked; |
| | 72 | | public event Action<EventFromAPIModel> OnEventJumpInClicked; |
| | 73 | | public event Action<string> OnEventSubscribeEventClicked; |
| | 74 | | public event Action<string> OnEventUnsubscribeEventClicked; |
| | 75 | | public event Action OnViewAllEventsClicked; |
| | 76 | | public event Action<FriendsHandler> OnFriendHandlerAdded; |
| | 77 | | public event Action OnHighlightsSubSectionEnable; |
| | 78 | |
|
| | 79 | | public override void Start() |
| | 80 | | { |
| 56 | 81 | | placeModal = PlacesAndEventsCardsFactory.GetPlaceCardTemplateHiddenLazy(placeCardModalPrefab); |
| 56 | 82 | | eventModal = PlacesAndEventsCardsFactory.GetEventCardTemplateHiddenLazy(eventCardModalPrefab); |
| | 83 | |
|
| 56 | 84 | | trendingPlacesAndEvents.RemoveItems(); |
| 56 | 85 | | featuredPlaces.RemoveItems(); |
| 56 | 86 | | liveEvents.RemoveItems(); |
| | 87 | |
|
| 56 | 88 | | viewAllEventsButton.onClick.AddListener(() => OnViewAllEventsClicked?.Invoke()); |
| | 89 | |
|
| 56 | 90 | | OnReady?.Invoke(); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | public override void OnEnable() |
| | 94 | | { |
| 111 | 95 | | OnHighlightsSubSectionEnable?.Invoke(); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | public override void Dispose() |
| | 99 | | { |
| 129 | 100 | | base.Dispose(); |
| | 101 | |
|
| 129 | 102 | | cancellationTokenSource.Cancel(); |
| | 103 | |
|
| 129 | 104 | | trendingPlacesAndEvents.Dispose(); |
| 129 | 105 | | featuredPlaces.Dispose(); |
| 129 | 106 | | liveEvents.Dispose(); |
| | 107 | |
|
| 129 | 108 | | if (placeModal != null) |
| | 109 | | { |
| 78 | 110 | | placeModal.Dispose(); |
| 78 | 111 | | Destroy(placeModal.gameObject); |
| | 112 | | } |
| | 113 | |
|
| 129 | 114 | | if (eventModal != null) |
| | 115 | | { |
| 78 | 116 | | eventModal.Dispose(); |
| 78 | 117 | | Destroy(eventModal.gameObject); |
| | 118 | | } |
| | 119 | |
|
| 129 | 120 | | viewAllEventsButton.onClick.RemoveAllListeners(); |
| 129 | 121 | | } |
| | 122 | |
|
| | 123 | | public void SetActive(bool isActive) |
| | 124 | | { |
| 139 | 125 | | canvas.enabled = isActive; |
| | 126 | |
|
| 139 | 127 | | if (isActive) |
| 52 | 128 | | OnEnable(); |
| | 129 | | else |
| 87 | 130 | | OnDisable(); |
| 87 | 131 | | } |
| | 132 | |
|
| | 133 | | public void ConfigurePools() |
| | 134 | | { |
| 19 | 135 | | trendingPlaceCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(TRENDING_PLACE_CARDS_POOL_NAME, placeCardL |
| 19 | 136 | | featuredPlaceCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(FEATURED_PLACE_CARDS_POOL_NAME, placeCardP |
| | 137 | |
|
| 19 | 138 | | trendingEventCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(TRENDING_EVENT_CARDS_POOL_NAME, eventCardL |
| 19 | 139 | | liveEventCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(LIVE_EVENT_CARDS_POOL_NAME, eventCardPrefab, L |
| 19 | 140 | | } |
| | 141 | |
|
| | 142 | | public override void RefreshControl() |
| | 143 | | { |
| 0 | 144 | | trendingPlacesAndEvents.RefreshControl(); |
| 0 | 145 | | featuredPlaces.RefreshControl(); |
| 0 | 146 | | liveEvents.RefreshControl(); |
| 0 | 147 | | } |
| | 148 | |
|
| | 149 | | public void SetTrendingPlacesAndEventsAsLoading(bool isVisible) |
| | 150 | | { |
| 3 | 151 | | SetTrendingPlacesAndEventsActive(!isVisible); |
| 3 | 152 | | trendingPlacesAndEventsLoading.SetActive(isVisible); |
| 3 | 153 | | } |
| | 154 | |
|
| | 155 | | internal void SetTrendingPlacesAndEventsActive(bool isActive) => |
| 5 | 156 | | trendingPlacesAndEvents.gameObject.SetActive(isActive); |
| | 157 | |
|
| | 158 | | public void SetFeaturedPlacesAsLoading(bool isVisible) |
| | 159 | | { |
| 6 | 160 | | featuredPlaces.gameObject.SetActive(!isVisible); |
| 6 | 161 | | featuredPlacesLoading.SetActive(isVisible); |
| | 162 | |
|
| 6 | 163 | | if (isVisible) |
| 1 | 164 | | featuredPlacesNoDataText.gameObject.SetActive(false); |
| 6 | 165 | | } |
| | 166 | |
|
| | 167 | | public void SetLiveAsLoading(bool isVisible) |
| | 168 | | { |
| 6 | 169 | | liveEvents.gameObject.SetActive(!isVisible); |
| 6 | 170 | | liveEventsLoading.SetActive(isVisible); |
| 6 | 171 | | viewAllEventsButton.gameObject.SetActive(!isVisible); |
| | 172 | |
|
| 6 | 173 | | if (isVisible) |
| 1 | 174 | | liveEventsNoDataText.gameObject.SetActive(false); |
| 6 | 175 | | } |
| | 176 | |
|
| | 177 | | public void ShowPlaceModal(PlaceCardComponentModel placeInfo) |
| | 178 | | { |
| 1 | 179 | | placeModal.Show(); |
| 1 | 180 | | PlacesCardsConfigurator.Configure(placeModal, placeInfo, OnPlaceInfoClicked, OnPlaceJumpInClicked); |
| 1 | 181 | | } |
| | 182 | |
|
| | 183 | | public void HidePlaceModal() |
| | 184 | | { |
| 1 | 185 | | if (placeModal != null) |
| 1 | 186 | | placeModal.Hide(); |
| 1 | 187 | | } |
| | 188 | |
|
| | 189 | | public void ShowEventModal(EventCardComponentModel eventInfo) |
| | 190 | | { |
| 1 | 191 | | eventModal.Show(); |
| 1 | 192 | | EventsCardsConfigurator.Configure(eventModal, eventInfo, OnEventInfoClicked, OnEventJumpInClicked, OnEventSubscr |
| 1 | 193 | | } |
| | 194 | |
|
| | 195 | | public void HideEventModal() |
| | 196 | | { |
| 1 | 197 | | if (eventModal != null) |
| 1 | 198 | | eventModal.Hide(); |
| 1 | 199 | | } |
| | 200 | |
|
| | 201 | | public void RestartScrollViewPosition() => |
| 0 | 202 | | scrollView.verticalNormalizedPosition = 1; |
| | 203 | |
|
| | 204 | | public void SetFeaturedPlaces(List<PlaceCardComponentModel> places) |
| | 205 | | { |
| 4 | 206 | | SetFeaturedPlacesAsLoading(false); |
| 4 | 207 | | featuredPlacesNoDataText.gameObject.SetActive(places.Count == 0); |
| | 208 | |
|
| 4 | 209 | | featuredPlaceCardsPool.ReleaseAll(); |
| | 210 | |
|
| 4 | 211 | | featuredPlaces.ExtractItems(); |
| 4 | 212 | | featuredPlaces.RemoveItems(); |
| | 213 | |
|
| 8 | 214 | | cardsVisualUpdateBuffer.Enqueue(() => SetPlacesAsync(places, cancellationTokenSource.Token)); |
| 4 | 215 | | UpdateCardsVisual(); |
| 4 | 216 | | } |
| | 217 | |
|
| | 218 | | private async UniTask SetPlacesAsync(List<PlaceCardComponentModel> places, CancellationToken cancellationToken) |
| | 219 | | { |
| 29 | 220 | | foreach (PlaceCardComponentModel place in places) |
| | 221 | | { |
| 12 | 222 | | PlaceCardComponentView placeCard = PlacesAndEventsCardsFactory.CreateConfiguredPlaceCard(featuredPlaceCardsP |
| 12 | 223 | | OnFriendHandlerAdded?.Invoke(placeCard.friendsHandler); |
| | 224 | |
|
| 12 | 225 | | this.featuredPlaces.AddItem(placeCard); |
| | 226 | |
|
| 36 | 227 | | await UniTask.NextFrame(cancellationToken); |
| | 228 | | } |
| | 229 | |
|
| 1 | 230 | | featuredPlaces.SetItemSizeForModel(); |
| 2 | 231 | | poolsPrewarmAsyncsBuffer.Enqueue(() => featuredPlaceCardsPool.PrewarmAsync(places.Count, cancellationToken)); |
| 1 | 232 | | } |
| | 233 | |
|
| | 234 | | public void SetLiveEvents(List<EventCardComponentModel> events) |
| | 235 | | { |
| 4 | 236 | | SetLiveAsLoading(false); |
| 4 | 237 | | liveEventsNoDataText.gameObject.SetActive(events.Count == 0); |
| | 238 | |
|
| 4 | 239 | | liveEventCardsPool.ReleaseAll(); |
| | 240 | |
|
| 4 | 241 | | liveEvents.ExtractItems(); |
| 4 | 242 | | liveEvents.RemoveItems(); |
| | 243 | |
|
| 8 | 244 | | cardsVisualUpdateBuffer.Enqueue(() => SetEventsAsync(events, liveEvents, liveEventCardsPool, cancellationTokenSo |
| 4 | 245 | | UpdateCardsVisual(); |
| 4 | 246 | | } |
| | 247 | |
|
| | 248 | | private async UniTask SetEventsAsync(List<EventCardComponentModel> events, GridContainerComponentView eventsGrid, Po |
| | 249 | | { |
| 29 | 250 | | foreach (EventCardComponentModel eventInfo in events) |
| | 251 | | { |
| 12 | 252 | | eventsGrid.AddItem( |
| | 253 | | PlacesAndEventsCardsFactory.CreateConfiguredEventCard(pool, eventInfo, OnEventInfoClicked, OnEventJumpIn |
| | 254 | |
|
| 36 | 255 | | await UniTask.NextFrame(cancellationToken); |
| | 256 | | } |
| | 257 | |
|
| 1 | 258 | | eventsGrid.SetItemSizeForModel(); |
| | 259 | |
|
| 2 | 260 | | poolsPrewarmAsyncsBuffer.Enqueue(() => pool.PrewarmAsync(events.Count, cancellationToken)); |
| 1 | 261 | | } |
| | 262 | |
|
| | 263 | | public void SetTrendingPlacesAndEvents(List<PlaceCardComponentModel> places, List<EventCardComponentModel> events) |
| | 264 | | { |
| 1 | 265 | | SetTrendingPlacesAndEventsAsLoading(false); |
| | 266 | |
|
| 1 | 267 | | trendingPlaceCardsPool.ReleaseAll(); |
| 1 | 268 | | trendingEventCardsPool.ReleaseAll(); |
| | 269 | |
|
| 1 | 270 | | trendingPlacesAndEvents.ExtractItems(); |
| 1 | 271 | | trendingPlacesAndEvents.RemoveItems(); |
| | 272 | |
|
| 2 | 273 | | cardsVisualUpdateBuffer.Enqueue(() => SetTrendingsAsync(places, events, cancellationTokenSource.Token)); |
| 1 | 274 | | UpdateCardsVisual(); |
| 1 | 275 | | } |
| | 276 | |
|
| | 277 | | private async UniTask SetTrendingsAsync(List<PlaceCardComponentModel> places, List<EventCardComponentModel> events, |
| | 278 | | { |
| 1 | 279 | | int eventId = 0; |
| 1 | 280 | | int placeId = 0; |
| | 281 | |
|
| 8 | 282 | | for (int i = 0; i < HighlightsSubSectionComponentController.DEFAULT_NUMBER_OF_TRENDING_PLACES; i++) |
| | 283 | | { |
| 4 | 284 | | if (i % 2 == 0 && eventId < events.Count) |
| | 285 | | { |
| 2 | 286 | | trendingPlacesAndEvents.AddItem( |
| | 287 | | PlacesAndEventsCardsFactory.CreateConfiguredEventCard(trendingEventCardsPool, events[eventId], OnEve |
| | 288 | |
|
| 2 | 289 | | eventId++; |
| | 290 | | } |
| 2 | 291 | | else if (placeId < places.Count) |
| | 292 | | { |
| 2 | 293 | | PlaceCardComponentView placeCard = PlacesAndEventsCardsFactory.CreateConfiguredPlaceCard(trendingPlaceCa |
| 2 | 294 | | OnFriendHandlerAdded?.Invoke(placeCard.friendsHandler); |
| | 295 | |
|
| 2 | 296 | | trendingPlacesAndEvents.AddItem(placeCard); |
| | 297 | |
|
| 2 | 298 | | placeId++; |
| | 299 | | } |
| | 300 | |
|
| 12 | 301 | | await UniTask.NextFrame(cancellationToken); |
| | 302 | | } |
| | 303 | |
|
| 0 | 304 | | trendingPlacesAndEvents.SetManualControlsActive(); |
| 0 | 305 | | trendingPlacesAndEvents.GenerateDotsSelector(); |
| | 306 | |
|
| 0 | 307 | | poolsPrewarmAsyncsBuffer.Enqueue(() => trendingPlaceCardsPool.PrewarmAsync(HighlightsSubSectionComponentControll |
| 0 | 308 | | } |
| | 309 | |
|
| | 310 | | private void UpdateCardsVisual() |
| | 311 | | { |
| 9 | 312 | | if (!isUpdatingCardsVisual) |
| 9 | 313 | | UpdateCardsVisualProcess().Forget(); |
| | 314 | |
|
| | 315 | | async UniTask UpdateCardsVisualProcess() |
| | 316 | | { |
| 9 | 317 | | isUpdatingCardsVisual = true; |
| | 318 | |
|
| 11 | 319 | | while (cardsVisualUpdateBuffer.Count > 0) |
| 23 | 320 | | await cardsVisualUpdateBuffer.Dequeue().Invoke(); |
| | 321 | |
|
| 4 | 322 | | while (poolsPrewarmAsyncsBuffer.Count > 0) |
| 2 | 323 | | await poolsPrewarmAsyncsBuffer.Dequeue().Invoke(); |
| | 324 | |
|
| 2 | 325 | | isUpdatingCardsVisual = false; |
| 2 | 326 | | } |
| 9 | 327 | | } |
| | 328 | | } |