| | 1 | | using DCL; |
| | 2 | | using DCL.Interface; |
| | 3 | | using ExploreV2Analytics; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | | using static HotScenesController; |
| | 9 | |
|
| | 10 | | public interface IHighlightsSubSectionComponentController : IDisposable |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// It will be triggered when the sub-section want to request to close the ExploreV2 main menu. |
| | 14 | | /// </summary> |
| | 15 | | event Action OnCloseExploreV2; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// It will be triggered when the sub-section want to request to go to the Events sub-section. |
| | 19 | | /// </summary> |
| | 20 | | event Action OnGoToEventsSubSection; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Request all places and events from the API. |
| | 24 | | /// </summary> |
| | 25 | | void RequestAllPlacesAndEvents(); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Load the trending places and events with the last requested ones. |
| | 29 | | /// </summary> |
| | 30 | | void LoadTrendingPlacesAndEvents(); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Load the featured places with the last requested ones. |
| | 34 | | /// </summary> |
| | 35 | | void LoadFeaturedPlaces(); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Load the live events with the last requested ones. |
| | 39 | | /// </summary> |
| | 40 | | void LoadLiveEvents(); |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public class HighlightsSubSectionComponentController : IHighlightsSubSectionComponentController |
| | 44 | | { |
| | 45 | | public event Action OnCloseExploreV2; |
| | 46 | | public event Action OnGoToEventsSubSection; |
| | 47 | |
|
| | 48 | | internal const int DEFAULT_NUMBER_OF_TRENDING_PLACES = 10; |
| | 49 | | internal const int DEFAULT_NUMBER_OF_FEATURED_PLACES = 9; |
| | 50 | | internal const int DEFAULT_NUMBER_OF_LIVE_EVENTS = 3; |
| | 51 | | internal const string EVENT_DETAIL_URL = "https://events.decentraland.org/event/?id={0}"; |
| | 52 | |
|
| | 53 | | internal IHighlightsSubSectionComponentView view; |
| | 54 | | internal IPlacesAPIController placesAPIApiController; |
| | 55 | | internal IEventsAPIController eventsAPIApiController; |
| | 56 | | internal FriendTrackerController friendsTrackerController; |
| 21 | 57 | | internal List<HotSceneInfo> placesFromAPI = new List<HotSceneInfo>(); |
| 21 | 58 | | internal List<EventFromAPIModel> eventsFromAPI = new List<EventFromAPIModel>(); |
| | 59 | | internal bool reloadHighlights = false; |
| | 60 | | internal IExploreV2Analytics exploreV2Analytics; |
| | 61 | | internal float lastTimeAPIChecked = 0; |
| | 62 | | private DataStore dataStore; |
| | 63 | |
|
| 21 | 64 | | public HighlightsSubSectionComponentController( |
| | 65 | | IHighlightsSubSectionComponentView view, |
| | 66 | | IPlacesAPIController placesAPI, |
| | 67 | | IEventsAPIController eventsAPI, |
| | 68 | | IFriendsController friendsController, |
| | 69 | | IExploreV2Analytics exploreV2Analytics, |
| | 70 | | DataStore dataStore) |
| | 71 | | { |
| 21 | 72 | | this.view = view; |
| 21 | 73 | | this.view.OnReady += FirstLoading; |
| 21 | 74 | | this.view.OnPlaceInfoClicked += ShowPlaceDetailedInfo; |
| 21 | 75 | | this.view.OnEventInfoClicked += ShowEventDetailedInfo; |
| 21 | 76 | | this.view.OnPlaceJumpInClicked += JumpInToPlace; |
| 21 | 77 | | this.view.OnEventJumpInClicked += JumpInToEvent; |
| 21 | 78 | | this.view.OnEventSubscribeEventClicked += SubscribeToEvent; |
| 21 | 79 | | this.view.OnEventUnsubscribeEventClicked += UnsubscribeToEvent; |
| 21 | 80 | | this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded; |
| 21 | 81 | | this.view.OnViewAllEventsClicked += GoToEventsSubSection; |
| | 82 | |
|
| 21 | 83 | | this.dataStore = dataStore; |
| 21 | 84 | | this.dataStore.channels.currentJoinChannelModal.OnChange += OnChannelToJoinChanged; |
| | 85 | |
|
| 21 | 86 | | placesAPIApiController = placesAPI; |
| 21 | 87 | | eventsAPIApiController = eventsAPI; |
| | 88 | |
|
| 21 | 89 | | friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors); |
| | 90 | |
|
| 21 | 91 | | this.exploreV2Analytics = exploreV2Analytics; |
| | 92 | |
|
| 21 | 93 | | view.ConfigurePools(); |
| 21 | 94 | | } |
| | 95 | |
|
| | 96 | | internal void FirstLoading() |
| | 97 | | { |
| 1 | 98 | | reloadHighlights = true; |
| 1 | 99 | | lastTimeAPIChecked = Time.realtimeSinceStartup - PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK_API |
| 1 | 100 | | RequestAllPlacesAndEvents(); |
| | 101 | |
|
| 1 | 102 | | view.OnHighlightsSubSectionEnable += RequestAllPlacesAndEvents; |
| 1 | 103 | | dataStore.exploreV2.isOpen.OnChange += OnExploreV2Open; |
| 1 | 104 | | } |
| | 105 | |
|
| | 106 | | internal void OnExploreV2Open(bool current, bool previous) |
| | 107 | | { |
| 0 | 108 | | if (current) |
| 0 | 109 | | return; |
| | 110 | |
|
| 0 | 111 | | reloadHighlights = true; |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | public void RequestAllPlacesAndEvents() |
| | 115 | | { |
| 2 | 116 | | if (!reloadHighlights) |
| 0 | 117 | | return; |
| | 118 | |
|
| 2 | 119 | | view.RestartScrollViewPosition(); |
| | 120 | |
|
| 2 | 121 | | if (Time.realtimeSinceStartup < lastTimeAPIChecked + PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK |
| 0 | 122 | | return; |
| | 123 | |
|
| 2 | 124 | | view.SetTrendingPlacesAndEventsAsLoading(true); |
| 2 | 125 | | view.SetFeaturedPlacesAsLoading(true); |
| 2 | 126 | | view.SetLiveAsLoading(true); |
| | 127 | |
|
| 2 | 128 | | reloadHighlights = false; |
| 2 | 129 | | lastTimeAPIChecked = Time.realtimeSinceStartup; |
| | 130 | |
|
| 2 | 131 | | if (!dataStore.exploreV2.isInShowAnimationTransiton.Get()) |
| 1 | 132 | | RequestAllPlacesAndEventsFromAPI(); |
| | 133 | | else |
| 1 | 134 | | dataStore.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged; |
| 1 | 135 | | } |
| | 136 | |
|
| | 137 | | internal void IsInShowAnimationTransitonChanged(bool current, bool previous) |
| | 138 | | { |
| 1 | 139 | | dataStore.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged; |
| 1 | 140 | | RequestAllPlacesAndEventsFromAPI(); |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | internal void RequestAllPlacesAndEventsFromAPI() |
| | 144 | | { |
| 3 | 145 | | placesAPIApiController.GetAllPlaces( |
| | 146 | | (placeList) => |
| | 147 | | { |
| 0 | 148 | | placesFromAPI = placeList; |
| 0 | 149 | | eventsAPIApiController.GetAllEvents( |
| | 150 | | (eventList) => |
| | 151 | | { |
| 0 | 152 | | eventsFromAPI = eventList; |
| 0 | 153 | | OnRequestedPlacesAndEventsUpdated(); |
| 0 | 154 | | }, |
| | 155 | | (error) => |
| | 156 | | { |
| 0 | 157 | | OnRequestedPlacesAndEventsUpdated(); |
| 0 | 158 | | Debug.LogError($"Error receiving events from the API: {error}"); |
| 0 | 159 | | }); |
| 0 | 160 | | }); |
| 3 | 161 | | } |
| | 162 | |
|
| | 163 | | internal void OnRequestedPlacesAndEventsUpdated() |
| | 164 | | { |
| 1 | 165 | | friendsTrackerController.RemoveAllHandlers(); |
| | 166 | |
|
| 1 | 167 | | LoadTrendingPlacesAndEvents(); |
| 1 | 168 | | LoadFeaturedPlaces(); |
| 1 | 169 | | LoadLiveEvents(); |
| 1 | 170 | | } |
| | 171 | |
|
| | 172 | | public void LoadTrendingPlacesAndEvents() |
| | 173 | | { |
| | 174 | | // Places |
| 2 | 175 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| 2 | 176 | | List<HotSceneInfo> placesFiltered = placesFromAPI |
| | 177 | | .Take(DEFAULT_NUMBER_OF_TRENDING_PLACES) |
| | 178 | | .ToList(); |
| | 179 | |
|
| 12 | 180 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 181 | | { |
| 4 | 182 | | PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 4 | 183 | | places.Add(placeCardModel); |
| | 184 | | } |
| | 185 | |
|
| | 186 | | // Events |
| 2 | 187 | | List<EventCardComponentModel> events = new List<EventCardComponentModel>(); |
| 2 | 188 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI |
| 4 | 189 | | .Where(e => e.highlighted) |
| | 190 | | .Take(placesFiltered.Count) |
| | 191 | | .ToList(); |
| | 192 | |
|
| 4 | 193 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 194 | | { |
| 0 | 195 | | EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent); |
| 0 | 196 | | events.Add(eventCardModel); |
| | 197 | | } |
| | 198 | |
|
| 2 | 199 | | view.SetTrendingPlacesAndEvents(places, events); |
| 2 | 200 | | } |
| | 201 | |
|
| | 202 | | public void LoadFeaturedPlaces() |
| | 203 | | { |
| 2 | 204 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| | 205 | | List<HotSceneInfo> placesFiltered; |
| 2 | 206 | | if (placesFromAPI.Count >= DEFAULT_NUMBER_OF_TRENDING_PLACES) |
| | 207 | | { |
| 0 | 208 | | int numberOfPlaces = placesFromAPI.Count >= (DEFAULT_NUMBER_OF_TRENDING_PLACES + DEFAULT_NUMBER_OF_FEATURED_ |
| | 209 | | ? DEFAULT_NUMBER_OF_FEATURED_PLACES |
| | 210 | | : placesFromAPI.Count - DEFAULT_NUMBER_OF_TRENDING_PLACES; |
| | 211 | |
|
| 0 | 212 | | placesFiltered = placesFromAPI |
| | 213 | | .GetRange(DEFAULT_NUMBER_OF_TRENDING_PLACES, numberOfPlaces) |
| | 214 | | .ToList(); |
| 0 | 215 | | } |
| 2 | 216 | | else if (placesFromAPI.Count > 0) |
| | 217 | | { |
| 2 | 218 | | placesFiltered = placesFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_PLACES).ToList(); |
| 2 | 219 | | } |
| | 220 | | else |
| | 221 | | { |
| 0 | 222 | | placesFiltered = new List<HotSceneInfo>(); |
| | 223 | | } |
| | 224 | |
|
| 12 | 225 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 226 | | { |
| 4 | 227 | | PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 4 | 228 | | places.Add(placeCardModel); |
| | 229 | | } |
| | 230 | |
|
| 2 | 231 | | view.SetFeaturedPlaces(places); |
| 2 | 232 | | } |
| | 233 | |
|
| | 234 | | public void LoadLiveEvents() |
| | 235 | | { |
| 2 | 236 | | List<EventCardComponentModel> events = new List<EventCardComponentModel>(); |
| 6 | 237 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(x => x.live) |
| | 238 | | .Take(DEFAULT_NUMBER_OF_LIVE_EVENTS) |
| | 239 | | .ToList(); |
| 12 | 240 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 241 | | { |
| 4 | 242 | | EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent); |
| 4 | 243 | | events.Add(eventCardModel); |
| | 244 | | } |
| | 245 | |
|
| 2 | 246 | | view.SetLiveEvents(events); |
| 2 | 247 | | } |
| | 248 | |
|
| | 249 | | public void Dispose() |
| | 250 | | { |
| 21 | 251 | | view.OnReady -= FirstLoading; |
| 21 | 252 | | view.OnPlaceInfoClicked -= ShowPlaceDetailedInfo; |
| 21 | 253 | | view.OnEventInfoClicked -= ShowEventDetailedInfo; |
| 21 | 254 | | view.OnPlaceJumpInClicked -= JumpInToPlace; |
| 21 | 255 | | view.OnEventJumpInClicked -= JumpInToEvent; |
| 21 | 256 | | view.OnEventSubscribeEventClicked -= SubscribeToEvent; |
| 21 | 257 | | view.OnEventUnsubscribeEventClicked -= UnsubscribeToEvent; |
| 21 | 258 | | view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded; |
| 21 | 259 | | view.OnViewAllEventsClicked -= GoToEventsSubSection; |
| | 260 | |
|
| 21 | 261 | | dataStore.exploreV2.isOpen.OnChange -= OnExploreV2Open; |
| 21 | 262 | | dataStore.channels.currentJoinChannelModal.OnChange -= OnChannelToJoinChanged; |
| 21 | 263 | | } |
| | 264 | |
|
| | 265 | | internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel) |
| | 266 | | { |
| 1 | 267 | | view.ShowPlaceModal(placeModel); |
| 1 | 268 | | exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName); |
| 1 | 269 | | dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.Places); |
| 1 | 270 | | } |
| | 271 | |
|
| | 272 | | internal void JumpInToPlace(HotSceneInfo placeFromAPI) |
| | 273 | | { |
| 1 | 274 | | ExplorePlacesUtils.JumpInToPlace(placeFromAPI); |
| 1 | 275 | | view.HidePlaceModal(); |
| 1 | 276 | | dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None); |
| 1 | 277 | | OnCloseExploreV2?.Invoke(); |
| 1 | 278 | | exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords); |
| 1 | 279 | | } |
| | 280 | |
|
| 0 | 281 | | internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends |
| | 282 | |
|
| | 283 | | internal void ShowEventDetailedInfo(EventCardComponentModel eventModel) |
| | 284 | | { |
| 1 | 285 | | view.ShowEventModal(eventModel); |
| 1 | 286 | | exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName); |
| 1 | 287 | | dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.Events); |
| 1 | 288 | | } |
| | 289 | |
|
| | 290 | | internal void JumpInToEvent(EventFromAPIModel eventFromAPI) |
| | 291 | | { |
| 1 | 292 | | ExploreEventsUtils.JumpInToEvent(eventFromAPI); |
| 1 | 293 | | view.HideEventModal(); |
| 1 | 294 | | dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None); |
| 1 | 295 | | OnCloseExploreV2?.Invoke(); |
| 1 | 296 | | exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates |
| 1 | 297 | | } |
| | 298 | |
|
| | 299 | | internal void SubscribeToEvent(string eventId) |
| | 300 | | { |
| | 301 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 302 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 303 | |
|
| | 304 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re |
| | 305 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 306 | | // eventId, |
| | 307 | | // true, |
| | 308 | | // () => |
| | 309 | | // { |
| | 310 | | // // ... |
| | 311 | | // }, |
| | 312 | | // (error) => |
| | 313 | | // { |
| | 314 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 315 | | // }); |
| 0 | 316 | | } |
| | 317 | |
|
| | 318 | | internal void UnsubscribeToEvent(string eventId) |
| | 319 | | { |
| | 320 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 321 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 322 | |
|
| | 323 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un |
| | 324 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 325 | | // eventId, |
| | 326 | | // false, |
| | 327 | | // () => |
| | 328 | | // { |
| | 329 | | // // ... |
| | 330 | | // }, |
| | 331 | | // (error) => |
| | 332 | | // { |
| | 333 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 334 | | // }); |
| 0 | 335 | | } |
| | 336 | |
|
| 2 | 337 | | internal void GoToEventsSubSection() { OnGoToEventsSubSection?.Invoke(); } |
| | 338 | |
|
| | 339 | | private void OnChannelToJoinChanged(string currentChannelId, string previousChannelId) |
| | 340 | | { |
| 0 | 341 | | if (!string.IsNullOrEmpty(currentChannelId)) |
| 0 | 342 | | return; |
| | 343 | |
|
| 0 | 344 | | view.HidePlaceModal(); |
| 0 | 345 | | view.HideEventModal(); |
| 0 | 346 | | dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None); |
| 0 | 347 | | OnCloseExploreV2?.Invoke(); |
| 0 | 348 | | } |
| | 349 | | } |