| | 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; |
| 21 | 80 | | } |
| | 81 | |
|
| | 82 | | internal void FirstLoading() |
| | 83 | | { |
| 1 | 84 | | reloadEvents = true; |
| 1 | 85 | | RequestAllEvents(); |
| | 86 | |
|
| 1 | 87 | | view.OnEventsSubSectionEnable += RequestAllEvents; |
| 1 | 88 | | DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open; |
| 1 | 89 | | } |
| | 90 | |
|
| | 91 | | internal void OnExploreV2Open(bool current, bool previous) |
| | 92 | | { |
| 0 | 93 | | if (current) |
| 0 | 94 | | return; |
| | 95 | |
|
| 0 | 96 | | reloadEvents = true; |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public void RequestAllEvents() |
| | 100 | | { |
| 2 | 101 | | if (!reloadEvents) |
| 0 | 102 | | return; |
| | 103 | |
|
| 2 | 104 | | currentUpcomingEventsShowed = view.currentUpcomingEventsPerRow * INITIAL_NUMBER_OF_UPCOMING_ROWS; |
| 2 | 105 | | view.RestartScrollViewPosition(); |
| 2 | 106 | | view.SetFeaturedEventsAsLoading(true); |
| 2 | 107 | | view.SetTrendingEventsAsLoading(true); |
| 2 | 108 | | view.SetUpcomingEventsAsLoading(true); |
| 2 | 109 | | view.SetShowMoreUpcomingEventsButtonActive(false); |
| 2 | 110 | | view.SetGoingEventsAsLoading(true); |
| 2 | 111 | | reloadEvents = false; |
| | 112 | |
|
| 2 | 113 | | if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get()) |
| 2 | 114 | | RequestAllEventsFromAPI(); |
| | 115 | | else |
| 0 | 116 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged; |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | internal void IsInShowAnimationTransitonChanged(bool current, bool previous) |
| | 120 | | { |
| 0 | 121 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged; |
| 0 | 122 | | RequestAllEventsFromAPI(); |
| 0 | 123 | | } |
| | 124 | |
|
| | 125 | | internal void RequestAllEventsFromAPI() |
| | 126 | | { |
| 3 | 127 | | eventsAPIApiController.GetAllEvents( |
| | 128 | | (eventList) => |
| | 129 | | { |
| 0 | 130 | | eventsFromAPI = eventList; |
| 0 | 131 | | OnEventsFromAPIUpdated?.Invoke(); |
| 0 | 132 | | }, |
| | 133 | | (error) => |
| | 134 | | { |
| 0 | 135 | | Debug.LogError($"Error receiving events from the API: {error}"); |
| 0 | 136 | | }); |
| 3 | 137 | | } |
| | 138 | |
|
| | 139 | | internal void OnRequestedEventsUpdated() |
| | 140 | | { |
| 1 | 141 | | LoadFeaturedEvents(); |
| 1 | 142 | | LoadTrendingEvents(); |
| 1 | 143 | | LoadUpcomingEvents(); |
| 1 | 144 | | LoadGoingEvents(); |
| 1 | 145 | | } |
| | 146 | |
|
| | 147 | | public void LoadFeaturedEvents() |
| | 148 | | { |
| 2 | 149 | | List<EventCardComponentModel> featuredEvents = new List<EventCardComponentModel>(); |
| 6 | 150 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList(); |
| | 151 | |
|
| 2 | 152 | | if (eventsFiltered.Count == 0) |
| 2 | 153 | | eventsFiltered = eventsFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_EVENTS).ToList(); |
| | 154 | |
|
| 12 | 155 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 156 | | { |
| 4 | 157 | | EventCardComponentModel eventCardModel = ExploreEventsHelpers.CreateEventCardModelFromAPIEvent(receivedEvent |
| 4 | 158 | | featuredEvents.Add(eventCardModel); |
| | 159 | | } |
| | 160 | |
|
| 2 | 161 | | view.SetFeaturedEvents(featuredEvents); |
| 2 | 162 | | view.SetFeaturedEventsAsLoading(false); |
| 2 | 163 | | view.SetFeaturedEventsActive(featuredEvents.Count > 0); |
| 2 | 164 | | } |
| | 165 | |
|
| | 166 | | public void LoadTrendingEvents() |
| | 167 | | { |
| 2 | 168 | | List<EventCardComponentModel> trendingEvents = new List<EventCardComponentModel>(); |
| 6 | 169 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.trending).ToList(); |
| | 170 | |
|
| 4 | 171 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 172 | | { |
| 0 | 173 | | EventCardComponentModel eventCardModel = ExploreEventsHelpers.CreateEventCardModelFromAPIEvent(receivedEvent |
| 0 | 174 | | trendingEvents.Add(eventCardModel); |
| | 175 | | } |
| | 176 | |
|
| 2 | 177 | | view.SetTrendingEvents(trendingEvents); |
| 2 | 178 | | view.SetTrendingEventsAsLoading(false); |
| 2 | 179 | | } |
| | 180 | |
|
| | 181 | | public void LoadUpcomingEvents() |
| | 182 | | { |
| 2 | 183 | | List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>(); |
| 2 | 184 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Take(currentUpcomingEventsShowed).ToList(); |
| | 185 | |
|
| 4 | 186 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 187 | | { |
| 0 | 188 | | EventCardComponentModel eventCardModel = ExploreEventsHelpers.CreateEventCardModelFromAPIEvent(receivedEvent |
| 0 | 189 | | upcomingEvents.Add(eventCardModel); |
| | 190 | | } |
| | 191 | |
|
| 2 | 192 | | view.SetUpcomingEvents(upcomingEvents); |
| 2 | 193 | | view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count); |
| 2 | 194 | | view.SetUpcomingEventsAsLoading(false); |
| 2 | 195 | | } |
| | 196 | |
|
| | 197 | | public void ShowMoreUpcomingEvents() |
| | 198 | | { |
| 2 | 199 | | List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>(); |
| 2 | 200 | | List<EventFromAPIModel> eventsFiltered = new List<EventFromAPIModel>(); |
| 2 | 201 | | int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)currentUpcomingEventsShowed / view.currentUpcomingEventsPe |
| 2 | 202 | | int numberOfItemsToAdd = view.currentUpcomingEventsPerRow * SHOW_MORE_UPCOMING_ROWS_INCREMENT + numberOfExtraIte |
| | 203 | |
|
| 2 | 204 | | if (currentUpcomingEventsShowed + numberOfItemsToAdd <= eventsFromAPI.Count) |
| 2 | 205 | | eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, numberOfItemsToAdd); |
| | 206 | | else |
| 0 | 207 | | eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, eventsFromAPI.Count - currentUpcomingEv |
| | 208 | |
|
| 4 | 209 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 210 | | { |
| 0 | 211 | | EventCardComponentModel placeCardModel = ExploreEventsHelpers.CreateEventCardModelFromAPIEvent(receivedEvent |
| 0 | 212 | | upcomingEvents.Add(placeCardModel); |
| | 213 | | } |
| | 214 | |
|
| 2 | 215 | | view.AddUpcomingEvents(upcomingEvents); |
| | 216 | |
|
| 2 | 217 | | currentUpcomingEventsShowed += numberOfItemsToAdd; |
| 2 | 218 | | if (currentUpcomingEventsShowed > eventsFromAPI.Count) |
| 0 | 219 | | currentUpcomingEventsShowed = eventsFromAPI.Count; |
| | 220 | |
|
| 2 | 221 | | view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count); |
| 2 | 222 | | } |
| | 223 | |
|
| | 224 | | public void LoadGoingEvents() |
| | 225 | | { |
| 2 | 226 | | List<EventCardComponentModel> goingEvents = new List<EventCardComponentModel>(); |
| 6 | 227 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.attending).ToList(); |
| | 228 | |
|
| 4 | 229 | | foreach (EventFromAPIModel receivedEvent in eventsFiltered) |
| | 230 | | { |
| 0 | 231 | | EventCardComponentModel eventCardModel = ExploreEventsHelpers.CreateEventCardModelFromAPIEvent(receivedEvent |
| 0 | 232 | | goingEvents.Add(eventCardModel); |
| | 233 | | } |
| | 234 | |
|
| 2 | 235 | | view.SetGoingEvents(goingEvents); |
| 2 | 236 | | view.SetGoingEventsAsLoading(false); |
| 2 | 237 | | } |
| | 238 | |
|
| | 239 | | public void Dispose() |
| | 240 | | { |
| 21 | 241 | | view.OnReady -= FirstLoading; |
| 21 | 242 | | view.OnInfoClicked -= ShowEventDetailedInfo; |
| 21 | 243 | | view.OnJumpInClicked -= JumpInToEvent; |
| 21 | 244 | | view.OnSubscribeEventClicked -= SubscribeToEvent; |
| 21 | 245 | | view.OnUnsubscribeEventClicked -= UnsubscribeToEvent; |
| 21 | 246 | | view.OnShowMoreUpcomingEventsClicked -= ShowMoreUpcomingEvents; |
| 21 | 247 | | view.OnEventsSubSectionEnable -= RequestAllEvents; |
| 21 | 248 | | OnEventsFromAPIUpdated -= OnRequestedEventsUpdated; |
| 21 | 249 | | DataStore.i.exploreV2.isOpen.OnChange -= OnExploreV2Open; |
| 21 | 250 | | } |
| | 251 | |
|
| | 252 | | internal void ShowEventDetailedInfo(EventCardComponentModel eventModel) |
| | 253 | | { |
| 1 | 254 | | view.ShowEventModal(eventModel); |
| 1 | 255 | | exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName); |
| 1 | 256 | | } |
| | 257 | |
|
| | 258 | | internal void JumpInToEvent(EventFromAPIModel eventFromAPI) |
| | 259 | | { |
| 1 | 260 | | ExploreEventsHelpers.JumpInToEvent(eventFromAPI); |
| 1 | 261 | | view.HideEventModal(); |
| 1 | 262 | | OnCloseExploreV2?.Invoke(); |
| 1 | 263 | | exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates |
| 1 | 264 | | } |
| | 265 | |
|
| | 266 | | internal void SubscribeToEvent(string eventId) |
| | 267 | | { |
| | 268 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 269 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 270 | |
|
| | 271 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re |
| | 272 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 273 | | // eventId, |
| | 274 | | // true, |
| | 275 | | // () => |
| | 276 | | // { |
| | 277 | | // // ... |
| | 278 | | // }, |
| | 279 | | // (error) => |
| | 280 | | // { |
| | 281 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 282 | | // }); |
| 0 | 283 | | } |
| | 284 | |
|
| | 285 | | internal void UnsubscribeToEvent(string eventId) |
| | 286 | | { |
| | 287 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 288 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 289 | |
|
| | 290 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un |
| | 291 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 292 | | // eventId, |
| | 293 | | // false, |
| | 294 | | // () => |
| | 295 | | // { |
| | 296 | | // // ... |
| | 297 | | // }, |
| | 298 | | // (error) => |
| | 299 | | // { |
| | 300 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 301 | | // }); |
| 0 | 302 | | } |
| | 303 | | } |