| | 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 | |
|
| | 9 | | public interface IEventsSubSectionComponentController : IDisposable |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// It will be triggered when the sub-section want to request to close the ExploreV2 main menu. |
| | 13 | | /// </summary> |
| | 14 | | event Action OnCloseExploreV2; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Request all events from the API. |
| | 18 | | /// </summary> |
| | 19 | | void RequestAllEvents(); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Load the featured events with the last requested ones. |
| | 23 | | /// </summary> |
| | 24 | | void LoadFeaturedEvents(); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Load the trending events with the last requested ones. |
| | 28 | | /// </summary> |
| | 29 | | void LoadTrendingEvents(); |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Load the upcoming events with the last requested ones. |
| | 33 | | /// </summary> |
| | 34 | | void LoadUpcomingEvents(); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Increment the number of upcoming events loaded. |
| | 38 | | /// </summary> |
| | 39 | | void ShowMoreUpcomingEvents(); |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Load the going events with the last requested ones. |
| | 43 | | /// </summary> |
| | 44 | | void LoadGoingEvents(); |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public class EventsSubSectionComponentController : IEventsSubSectionComponentController |
| | 48 | | { |
| | 49 | | public event Action OnCloseExploreV2; |
| | 50 | | internal event Action OnEventsFromAPIUpdated; |
| | 51 | |
|
| | 52 | | internal const int DEFAULT_NUMBER_OF_FEATURED_EVENTS = 3; |
| | 53 | | internal const int INITIAL_NUMBER_OF_UPCOMING_ROWS = 1; |
| | 54 | | internal const int SHOW_MORE_UPCOMING_ROWS_INCREMENT = 2; |
| | 55 | | internal const string EVENT_DETAIL_URL = "https://events.decentraland.org/event/?id={0}"; |
| | 56 | | internal IEventsSubSectionComponentView view; |
| | 57 | | internal IEventsAPIController eventsAPIApiController; |
| 21 | 58 | | internal List<EventFromAPIModel> eventsFromAPI = new List<EventFromAPIModel>(); |
| | 59 | | internal int currentUpcomingEventsShowed = 0; |
| | 60 | | internal bool reloadEvents = false; |
| | 61 | | internal IExploreV2Analytics exploreV2Analytics; |
| | 62 | |
|
| 21 | 63 | | public EventsSubSectionComponentController( |
| | 64 | | IEventsSubSectionComponentView view, |
| | 65 | | IEventsAPIController eventsAPI, |
| | 66 | | IExploreV2Analytics exploreV2Analytics) |
| | 67 | | { |
| 21 | 68 | | this.view = view; |
| 21 | 69 | | this.view.OnReady += FirstLoading; |
| 21 | 70 | | this.view.OnInfoClicked += ShowEventDetailedInfo; |
| 21 | 71 | | this.view.OnJumpInClicked += JumpInToEvent; |
| 21 | 72 | | this.view.OnSubscribeEventClicked += SubscribeToEvent; |
| 21 | 73 | | this.view.OnUnsubscribeEventClicked += UnsubscribeToEvent; |
| 21 | 74 | | this.view.OnShowMoreUpcomingEventsClicked += ShowMoreUpcomingEvents; |
| | 75 | |
|
| 21 | 76 | | eventsAPIApiController = eventsAPI; |
| 21 | 77 | | OnEventsFromAPIUpdated += OnRequestedEventsUpdated; |
| | 78 | |
|
| 21 | 79 | | this.exploreV2Analytics = exploreV2Analytics; |
| | 80 | |
|
| 21 | 81 | | view.ConfigurePools(); |
| 21 | 82 | | } |
| | 83 | |
|
| | 84 | | internal void FirstLoading() |
| | 85 | | { |
| 1 | 86 | | reloadEvents = true; |
| 1 | 87 | | RequestAllEvents(); |
| | 88 | |
|
| 1 | 89 | | view.OnEventsSubSectionEnable += RequestAllEvents; |
| 1 | 90 | | DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open; |
| 1 | 91 | | } |
| | 92 | |
|
| | 93 | | internal void OnExploreV2Open(bool current, bool previous) |
| | 94 | | { |
| 0 | 95 | | if (current) |
| 0 | 96 | | return; |
| | 97 | |
|
| 0 | 98 | | reloadEvents = true; |
| 0 | 99 | | } |
| | 100 | |
|
| | 101 | | public void RequestAllEvents() |
| | 102 | | { |
| 2 | 103 | | if (!reloadEvents) |
| 0 | 104 | | return; |
| | 105 | |
|
| 2 | 106 | | currentUpcomingEventsShowed = view.currentUpcomingEventsPerRow * INITIAL_NUMBER_OF_UPCOMING_ROWS; |
| 2 | 107 | | view.RestartScrollViewPosition(); |
| 2 | 108 | | view.SetFeaturedEventsAsLoading(true); |
| 2 | 109 | | view.SetTrendingEventsAsLoading(true); |
| 2 | 110 | | view.SetUpcomingEventsAsLoading(true); |
| 2 | 111 | | view.SetShowMoreUpcomingEventsButtonActive(false); |
| 2 | 112 | | view.SetGoingEventsAsLoading(true); |
| 2 | 113 | | reloadEvents = false; |
| | 114 | |
|
| 2 | 115 | | if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get()) |
| 2 | 116 | | RequestAllEventsFromAPI(); |
| | 117 | | else |
| 0 | 118 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged; |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | internal void IsInShowAnimationTransitonChanged(bool current, bool previous) |
| | 122 | | { |
| 0 | 123 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged; |
| 0 | 124 | | RequestAllEventsFromAPI(); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | internal void RequestAllEventsFromAPI() |
| | 128 | | { |
| 3 | 129 | | eventsAPIApiController.GetAllEvents( |
| | 130 | | (eventList) => |
| | 131 | | { |
| 0 | 132 | | eventsFromAPI = eventList; |
| 0 | 133 | | OnEventsFromAPIUpdated?.Invoke(); |
| 0 | 134 | | }, |
| | 135 | | (error) => |
| | 136 | | { |
| 0 | 137 | | Debug.LogError($"Error receiving events from the API: {error}"); |
| 0 | 138 | | }); |
| 3 | 139 | | } |
| | 140 | |
|
| | 141 | | internal void OnRequestedEventsUpdated() |
| | 142 | | { |
| 1 | 143 | | LoadFeaturedEvents(); |
| 1 | 144 | | LoadTrendingEvents(); |
| 1 | 145 | | LoadUpcomingEvents(); |
| 1 | 146 | | LoadGoingEvents(); |
| 1 | 147 | | } |
| | 148 | |
|
| | 149 | | public void LoadFeaturedEvents() |
| | 150 | | { |
| 2 | 151 | | List<EventCardComponentModel> featuredEvents = new List<EventCardComponentModel>(); |
| 6 | 152 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList(); |
| | 153 | |
|
| 2 | 154 | | if (eventsFiltered.Count == 0) |
| 2 | 155 | | eventsFiltered = eventsFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_EVENTS).ToList(); |
| | 156 | |
|
| 12 | 157 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 158 | | { |
| 4 | 159 | | EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent); |
| 4 | 160 | | featuredEvents.Add(eventCardModel); |
| | 161 | | } |
| | 162 | |
|
| 2 | 163 | | view.SetFeaturedEvents(featuredEvents); |
| 2 | 164 | | view.SetFeaturedEventsAsLoading(false); |
| 2 | 165 | | view.SetFeaturedEventsActive(featuredEvents.Count > 0); |
| 2 | 166 | | } |
| | 167 | |
|
| | 168 | | public void LoadTrendingEvents() |
| | 169 | | { |
| 2 | 170 | | List<EventCardComponentModel> trendingEvents = new List<EventCardComponentModel>(); |
| 6 | 171 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.trending).ToList(); |
| | 172 | |
|
| 4 | 173 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 174 | | { |
| 0 | 175 | | EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent); |
| 0 | 176 | | trendingEvents.Add(eventCardModel); |
| | 177 | | } |
| | 178 | |
|
| 2 | 179 | | view.SetTrendingEvents(trendingEvents); |
| 2 | 180 | | view.SetTrendingEventsAsLoading(false); |
| 2 | 181 | | } |
| | 182 | |
|
| | 183 | | public void LoadUpcomingEvents() |
| | 184 | | { |
| 2 | 185 | | List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>(); |
| 2 | 186 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Take(currentUpcomingEventsShowed).ToList(); |
| | 187 | |
|
| 4 | 188 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 189 | | { |
| 0 | 190 | | EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent); |
| 0 | 191 | | upcomingEvents.Add(eventCardModel); |
| | 192 | | } |
| | 193 | |
|
| 2 | 194 | | view.SetUpcomingEvents(upcomingEvents); |
| 2 | 195 | | view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count); |
| 2 | 196 | | view.SetUpcomingEventsAsLoading(false); |
| 2 | 197 | | } |
| | 198 | |
|
| | 199 | | public void ShowMoreUpcomingEvents() |
| | 200 | | { |
| 2 | 201 | | List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>(); |
| 2 | 202 | | List<EventFromAPIModel> eventsFiltered = new List<EventFromAPIModel>(); |
| 2 | 203 | | int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)currentUpcomingEventsShowed / view.currentUpcomingEventsPe |
| 2 | 204 | | int numberOfItemsToAdd = view.currentUpcomingEventsPerRow * SHOW_MORE_UPCOMING_ROWS_INCREMENT + numberOfExtraIte |
| | 205 | |
|
| 2 | 206 | | if (currentUpcomingEventsShowed + numberOfItemsToAdd <= eventsFromAPI.Count) |
| 2 | 207 | | eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, numberOfItemsToAdd); |
| | 208 | | else |
| 0 | 209 | | eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, eventsFromAPI.Count - currentUpcomingEv |
| | 210 | |
|
| 4 | 211 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 212 | | { |
| 0 | 213 | | EventCardComponentModel placeCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent); |
| 0 | 214 | | upcomingEvents.Add(placeCardModel); |
| | 215 | | } |
| | 216 | |
|
| 2 | 217 | | view.AddUpcomingEvents(upcomingEvents); |
| | 218 | |
|
| 2 | 219 | | currentUpcomingEventsShowed += numberOfItemsToAdd; |
| 2 | 220 | | if (currentUpcomingEventsShowed > eventsFromAPI.Count) |
| 0 | 221 | | currentUpcomingEventsShowed = eventsFromAPI.Count; |
| | 222 | |
|
| 2 | 223 | | view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count); |
| 2 | 224 | | } |
| | 225 | |
|
| | 226 | | public void LoadGoingEvents() |
| | 227 | | { |
| 2 | 228 | | List<EventCardComponentModel> goingEvents = new List<EventCardComponentModel>(); |
| 6 | 229 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.attending).ToList(); |
| | 230 | |
|
| 4 | 231 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 232 | | { |
| 0 | 233 | | EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent); |
| 0 | 234 | | goingEvents.Add(eventCardModel); |
| | 235 | | } |
| | 236 | |
|
| 2 | 237 | | view.SetGoingEvents(goingEvents); |
| 2 | 238 | | view.SetGoingEventsAsLoading(false); |
| 2 | 239 | | } |
| | 240 | |
|
| | 241 | | public void Dispose() |
| | 242 | | { |
| 21 | 243 | | view.OnReady -= FirstLoading; |
| 21 | 244 | | view.OnInfoClicked -= ShowEventDetailedInfo; |
| 21 | 245 | | view.OnJumpInClicked -= JumpInToEvent; |
| 21 | 246 | | view.OnSubscribeEventClicked -= SubscribeToEvent; |
| 21 | 247 | | view.OnUnsubscribeEventClicked -= UnsubscribeToEvent; |
| 21 | 248 | | view.OnShowMoreUpcomingEventsClicked -= ShowMoreUpcomingEvents; |
| 21 | 249 | | view.OnEventsSubSectionEnable -= RequestAllEvents; |
| 21 | 250 | | OnEventsFromAPIUpdated -= OnRequestedEventsUpdated; |
| 21 | 251 | | DataStore.i.exploreV2.isOpen.OnChange -= OnExploreV2Open; |
| 21 | 252 | | } |
| | 253 | |
|
| | 254 | | internal void ShowEventDetailedInfo(EventCardComponentModel eventModel) |
| | 255 | | { |
| 1 | 256 | | view.ShowEventModal(eventModel); |
| 1 | 257 | | exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName); |
| 1 | 258 | | } |
| | 259 | |
|
| | 260 | | internal void JumpInToEvent(EventFromAPIModel eventFromAPI) |
| | 261 | | { |
| 1 | 262 | | ExploreEventsUtils.JumpInToEvent(eventFromAPI); |
| 1 | 263 | | view.HideEventModal(); |
| 1 | 264 | | OnCloseExploreV2?.Invoke(); |
| 1 | 265 | | exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates |
| 1 | 266 | | } |
| | 267 | |
|
| | 268 | | internal void SubscribeToEvent(string eventId) |
| | 269 | | { |
| | 270 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 271 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 272 | |
|
| | 273 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re |
| | 274 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 275 | | // eventId, |
| | 276 | | // true, |
| | 277 | | // () => |
| | 278 | | // { |
| | 279 | | // // ... |
| | 280 | | // }, |
| | 281 | | // (error) => |
| | 282 | | // { |
| | 283 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 284 | | // }); |
| 0 | 285 | | } |
| | 286 | |
|
| | 287 | | internal void UnsubscribeToEvent(string eventId) |
| | 288 | | { |
| | 289 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 290 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 291 | |
|
| | 292 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un |
| | 293 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 294 | | // eventId, |
| | 295 | | // false, |
| | 296 | | // () => |
| | 297 | | // { |
| | 298 | | // // ... |
| | 299 | | // }, |
| | 300 | | // (error) => |
| | 301 | | // { |
| | 302 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 303 | | // }); |
| 0 | 304 | | } |
| | 305 | | } |