| | 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 | | internal event Action OnPlacesAndEventsFromAPIUpdated; |
| | 48 | |
|
| | 49 | | internal const int DEFAULT_NUMBER_OF_TRENDING_PLACES = 10; |
| | 50 | | internal const int DEFAULT_NUMBER_OF_FEATURED_PLACES = 9; |
| | 51 | | internal const int DEFAULT_NUMBER_OF_LIVE_EVENTS = 3; |
| | 52 | | internal const string EVENT_DETAIL_URL = "https://events.decentraland.org/event/?id={0}"; |
| | 53 | |
|
| | 54 | | internal IHighlightsSubSectionComponentView view; |
| | 55 | | internal IPlacesAPIController placesAPIApiController; |
| | 56 | | internal IEventsAPIController eventsAPIApiController; |
| | 57 | | internal FriendTrackerController friendsTrackerController; |
| 21 | 58 | | internal List<HotSceneInfo> placesFromAPI = new List<HotSceneInfo>(); |
| 21 | 59 | | internal List<EventFromAPIModel> eventsFromAPI = new List<EventFromAPIModel>(); |
| | 60 | | internal bool reloadHighlights = false; |
| | 61 | | internal IExploreV2Analytics exploreV2Analytics; |
| | 62 | |
|
| 21 | 63 | | public HighlightsSubSectionComponentController( |
| | 64 | | IHighlightsSubSectionComponentView view, |
| | 65 | | IPlacesAPIController placesAPI, |
| | 66 | | IEventsAPIController eventsAPI, |
| | 67 | | IFriendsController friendsController, |
| | 68 | | IExploreV2Analytics exploreV2Analytics) |
| | 69 | | { |
| 21 | 70 | | this.view = view; |
| 21 | 71 | | this.view.OnReady += FirstLoading; |
| 21 | 72 | | this.view.OnPlaceInfoClicked += ShowPlaceDetailedInfo; |
| 21 | 73 | | this.view.OnEventInfoClicked += ShowEventDetailedInfo; |
| 21 | 74 | | this.view.OnPlaceJumpInClicked += JumpInToPlace; |
| 21 | 75 | | this.view.OnEventJumpInClicked += JumpInToEvent; |
| 21 | 76 | | this.view.OnEventSubscribeEventClicked += SubscribeToEvent; |
| 21 | 77 | | this.view.OnEventUnsubscribeEventClicked += UnsubscribeToEvent; |
| 21 | 78 | | this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded; |
| 21 | 79 | | this.view.OnViewAllEventsClicked += GoToEventsSubSection; |
| | 80 | |
|
| 21 | 81 | | placesAPIApiController = placesAPI; |
| 21 | 82 | | eventsAPIApiController = eventsAPI; |
| 21 | 83 | | OnPlacesAndEventsFromAPIUpdated += OnRequestedPlacesAndEventsUpdated; |
| | 84 | |
|
| 21 | 85 | | friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors); |
| | 86 | |
|
| 21 | 87 | | this.exploreV2Analytics = exploreV2Analytics; |
| | 88 | |
|
| 21 | 89 | | view.ConfigurePools(); |
| 21 | 90 | | } |
| | 91 | |
|
| | 92 | | internal void FirstLoading() |
| | 93 | | { |
| 1 | 94 | | reloadHighlights = true; |
| 1 | 95 | | RequestAllPlacesAndEvents(); |
| | 96 | |
|
| 1 | 97 | | view.OnHighlightsSubSectionEnable += RequestAllPlacesAndEvents; |
| 1 | 98 | | DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open; |
| 1 | 99 | | } |
| | 100 | |
|
| | 101 | | internal void OnExploreV2Open(bool current, bool previous) |
| | 102 | | { |
| 0 | 103 | | if (current) |
| 0 | 104 | | return; |
| | 105 | |
|
| 0 | 106 | | reloadHighlights = true; |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public void RequestAllPlacesAndEvents() |
| | 110 | | { |
| 2 | 111 | | if (!reloadHighlights) |
| 0 | 112 | | return; |
| | 113 | |
|
| 2 | 114 | | view.RestartScrollViewPosition(); |
| 2 | 115 | | view.SetTrendingPlacesAndEventsAsLoading(true); |
| 2 | 116 | | view.SetFeaturedPlacesAsLoading(true); |
| 2 | 117 | | view.SetLiveAsLoading(true); |
| 2 | 118 | | RequestAllPlacesAndEventsFromAPI(); |
| 2 | 119 | | reloadHighlights = false; |
| | 120 | |
|
| 2 | 121 | | if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get()) |
| 1 | 122 | | RequestAllPlacesAndEventsFromAPI(); |
| | 123 | | else |
| 1 | 124 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged; |
| 1 | 125 | | } |
| | 126 | |
|
| | 127 | | internal void IsInShowAnimationTransitonChanged(bool current, bool previous) |
| | 128 | | { |
| 1 | 129 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged; |
| 1 | 130 | | RequestAllPlacesAndEventsFromAPI(); |
| 1 | 131 | | } |
| | 132 | |
|
| | 133 | | internal void RequestAllPlacesAndEventsFromAPI() |
| | 134 | | { |
| 5 | 135 | | placesAPIApiController.GetAllPlaces( |
| | 136 | | (placeList) => |
| | 137 | | { |
| 0 | 138 | | placesFromAPI = placeList; |
| 0 | 139 | | eventsAPIApiController.GetAllEvents( |
| | 140 | | (eventList) => |
| | 141 | | { |
| 0 | 142 | | eventsFromAPI = eventList; |
| 0 | 143 | | OnPlacesAndEventsFromAPIUpdated?.Invoke(); |
| 0 | 144 | | }, |
| | 145 | | (error) => |
| | 146 | | { |
| 0 | 147 | | OnPlacesAndEventsFromAPIUpdated?.Invoke(); |
| 0 | 148 | | Debug.LogError($"Error receiving events from the API: {error}"); |
| 0 | 149 | | }); |
| 0 | 150 | | }); |
| 5 | 151 | | } |
| | 152 | |
|
| | 153 | | internal void OnRequestedPlacesAndEventsUpdated() |
| | 154 | | { |
| 1 | 155 | | friendsTrackerController.RemoveAllHandlers(); |
| | 156 | |
|
| 1 | 157 | | LoadTrendingPlacesAndEvents(); |
| 1 | 158 | | LoadFeaturedPlaces(); |
| 1 | 159 | | LoadLiveEvents(); |
| 1 | 160 | | } |
| | 161 | |
|
| | 162 | | public void LoadTrendingPlacesAndEvents() |
| | 163 | | { |
| | 164 | | // Places |
| 2 | 165 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| 2 | 166 | | List<HotSceneInfo> placesFiltered = placesFromAPI |
| | 167 | | .Take(DEFAULT_NUMBER_OF_TRENDING_PLACES) |
| | 168 | | .ToList(); |
| | 169 | |
|
| 12 | 170 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 171 | | { |
| 4 | 172 | | PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 4 | 173 | | places.Add(placeCardModel); |
| | 174 | | } |
| | 175 | |
|
| | 176 | | // Events |
| 2 | 177 | | List<EventCardComponentModel> events = new List<EventCardComponentModel>(); |
| 6 | 178 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList(); |
| | 179 | |
|
| 4 | 180 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 181 | | { |
| 0 | 182 | | EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent); |
| 0 | 183 | | events.Add(eventCardModel); |
| | 184 | | } |
| | 185 | |
|
| 2 | 186 | | view.SetTrendingPlacesAndEventsAsLoading(false); |
| 2 | 187 | | view.SetTrendingPlacesAndEvents(places, events); |
| 2 | 188 | | } |
| | 189 | |
|
| | 190 | | public void LoadFeaturedPlaces() |
| | 191 | | { |
| 2 | 192 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| | 193 | | List<HotSceneInfo> placesFiltered; |
| 2 | 194 | | if (placesFromAPI.Count >= DEFAULT_NUMBER_OF_TRENDING_PLACES) |
| | 195 | | { |
| 0 | 196 | | int numberOfPlaces = placesFromAPI.Count >= (DEFAULT_NUMBER_OF_TRENDING_PLACES + DEFAULT_NUMBER_OF_FEATURED_ |
| | 197 | | ? DEFAULT_NUMBER_OF_FEATURED_PLACES |
| | 198 | | : placesFromAPI.Count - DEFAULT_NUMBER_OF_TRENDING_PLACES; |
| | 199 | |
|
| 0 | 200 | | placesFiltered = placesFromAPI |
| | 201 | | .GetRange(DEFAULT_NUMBER_OF_TRENDING_PLACES, numberOfPlaces) |
| | 202 | | .ToList(); |
| 0 | 203 | | } |
| 2 | 204 | | else if (placesFromAPI.Count > 0) |
| | 205 | | { |
| 2 | 206 | | placesFiltered = placesFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_PLACES).ToList(); |
| 2 | 207 | | } |
| | 208 | | else |
| | 209 | | { |
| 0 | 210 | | placesFiltered = new List<HotSceneInfo>(); |
| | 211 | | } |
| | 212 | |
|
| 12 | 213 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 214 | | { |
| 4 | 215 | | PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 4 | 216 | | places.Add(placeCardModel); |
| | 217 | | } |
| | 218 | |
|
| 2 | 219 | | view.SetFeaturedPlaces(places); |
| 2 | 220 | | view.SetFeaturedPlacesAsLoading(false); |
| 2 | 221 | | } |
| | 222 | |
|
| | 223 | | public void LoadLiveEvents() |
| | 224 | | { |
| 2 | 225 | | List<EventCardComponentModel> events = new List<EventCardComponentModel>(); |
| 6 | 226 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(x => x.live) |
| | 227 | | .Take(DEFAULT_NUMBER_OF_LIVE_EVENTS) |
| | 228 | | .ToList(); |
| 12 | 229 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 230 | | { |
| 4 | 231 | | EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent); |
| 4 | 232 | | events.Add(eventCardModel); |
| | 233 | | } |
| | 234 | |
|
| 2 | 235 | | view.SetLiveEvents(events); |
| 2 | 236 | | view.SetLiveAsLoading(false); |
| 2 | 237 | | } |
| | 238 | |
|
| | 239 | | public void Dispose() |
| | 240 | | { |
| 21 | 241 | | view.OnReady -= FirstLoading; |
| 21 | 242 | | view.OnPlaceInfoClicked -= ShowPlaceDetailedInfo; |
| 21 | 243 | | view.OnEventInfoClicked -= ShowEventDetailedInfo; |
| 21 | 244 | | view.OnPlaceJumpInClicked -= JumpInToPlace; |
| 21 | 245 | | view.OnEventJumpInClicked -= JumpInToEvent; |
| 21 | 246 | | view.OnEventSubscribeEventClicked -= SubscribeToEvent; |
| 21 | 247 | | view.OnEventUnsubscribeEventClicked -= UnsubscribeToEvent; |
| 21 | 248 | | view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded; |
| 21 | 249 | | view.OnViewAllEventsClicked -= GoToEventsSubSection; |
| 21 | 250 | | } |
| | 251 | |
|
| | 252 | | internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel) |
| | 253 | | { |
| 1 | 254 | | view.ShowPlaceModal(placeModel); |
| 1 | 255 | | exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName); |
| 1 | 256 | | } |
| | 257 | |
|
| | 258 | | internal void JumpInToPlace(HotSceneInfo placeFromAPI) |
| | 259 | | { |
| 1 | 260 | | ExplorePlacesUtils.JumpInToPlace(placeFromAPI); |
| 1 | 261 | | view.HidePlaceModal(); |
| 1 | 262 | | OnCloseExploreV2?.Invoke(); |
| 1 | 263 | | exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords); |
| 1 | 264 | | } |
| | 265 | |
|
| 0 | 266 | | internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends |
| | 267 | |
|
| | 268 | | internal void ShowEventDetailedInfo(EventCardComponentModel eventModel) |
| | 269 | | { |
| 1 | 270 | | view.ShowEventModal(eventModel); |
| 1 | 271 | | exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName); |
| 1 | 272 | | } |
| | 273 | |
|
| | 274 | | internal void JumpInToEvent(EventFromAPIModel eventFromAPI) |
| | 275 | | { |
| 1 | 276 | | ExploreEventsUtils.JumpInToEvent(eventFromAPI); |
| 1 | 277 | | view.HideEventModal(); |
| 1 | 278 | | OnCloseExploreV2?.Invoke(); |
| 1 | 279 | | exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates |
| 1 | 280 | | } |
| | 281 | |
|
| | 282 | | internal void SubscribeToEvent(string eventId) |
| | 283 | | { |
| | 284 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 285 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 286 | |
|
| | 287 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re |
| | 288 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 289 | | // eventId, |
| | 290 | | // true, |
| | 291 | | // () => |
| | 292 | | // { |
| | 293 | | // // ... |
| | 294 | | // }, |
| | 295 | | // (error) => |
| | 296 | | // { |
| | 297 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 298 | | // }); |
| 0 | 299 | | } |
| | 300 | |
|
| | 301 | | internal void UnsubscribeToEvent(string eventId) |
| | 302 | | { |
| | 303 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 304 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 305 | |
|
| | 306 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un |
| | 307 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 308 | | // eventId, |
| | 309 | | // false, |
| | 310 | | // () => |
| | 311 | | // { |
| | 312 | | // // ... |
| | 313 | | // }, |
| | 314 | | // (error) => |
| | 315 | | // { |
| | 316 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 317 | | // }); |
| 0 | 318 | | } |
| | 319 | |
|
| 2 | 320 | | internal void GoToEventsSubSection() { OnGoToEventsSubSection?.Invoke(); } |
| | 321 | | } |