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