| | 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 Environment = DCL.Environment; |
| | 9 | |
|
| | 10 | | public class EventsSubSectionComponentController : IEventsSubSectionComponentController, IPlacesAndEventsAPIRequester |
| | 11 | | { |
| | 12 | | public event Action OnCloseExploreV2; |
| | 13 | |
|
| | 14 | | private const int DEFAULT_NUMBER_OF_FEATURED_EVENTS = 3; |
| | 15 | | internal const int INITIAL_NUMBER_OF_UPCOMING_ROWS = 1; |
| | 16 | | private const int SHOW_MORE_UPCOMING_ROWS_INCREMENT = 2; |
| | 17 | | private const string EVENT_DETAIL_URL = "https://events.decentraland.org/event/?id={0}"; |
| | 18 | |
|
| | 19 | | internal readonly IEventsSubSectionComponentView view; |
| | 20 | | internal readonly IEventsAPIController eventsAPIApiController; |
| | 21 | | private readonly DataStore dataStore; |
| | 22 | | private readonly IExploreV2Analytics exploreV2Analytics; |
| | 23 | |
|
| | 24 | | internal readonly PlaceAndEventsCardsReloader cardsReloader; |
| | 25 | |
|
| 21 | 26 | | internal List<EventFromAPIModel> eventsFromAPI = new (); |
| | 27 | | internal int availableUISlots; |
| | 28 | |
|
| 21 | 29 | | public EventsSubSectionComponentController(IEventsSubSectionComponentView view, IEventsAPIController eventsAPI, IExp |
| | 30 | | { |
| 21 | 31 | | cardsReloader = new PlaceAndEventsCardsReloader(view, this, dataStore.exploreV2); |
| | 32 | |
|
| 21 | 33 | | this.view = view; |
| | 34 | |
|
| 21 | 35 | | this.view.OnReady += FirstLoading; |
| | 36 | |
|
| 21 | 37 | | this.view.OnInfoClicked += ShowEventDetailedInfo; |
| 21 | 38 | | this.view.OnJumpInClicked += OnJumpInToEvent; |
| | 39 | |
|
| 21 | 40 | | this.view.OnSubscribeEventClicked += SubscribeToEvent; |
| 21 | 41 | | this.view.OnUnsubscribeEventClicked += UnsubscribeToEvent; |
| | 42 | |
|
| 21 | 43 | | this.view.OnShowMoreUpcomingEventsClicked += ShowMoreUpcomingEvents; |
| | 44 | |
|
| 21 | 45 | | this.dataStore = dataStore; |
| 21 | 46 | | this.dataStore.channels.currentJoinChannelModal.OnChange += OnChannelToJoinChanged; |
| | 47 | |
|
| 21 | 48 | | eventsAPIApiController = eventsAPI; |
| | 49 | |
|
| 21 | 50 | | this.exploreV2Analytics = exploreV2Analytics; |
| | 51 | |
|
| 21 | 52 | | view.ConfigurePools(); |
| 21 | 53 | | } |
| | 54 | |
|
| | 55 | | public void Dispose() |
| | 56 | | { |
| 21 | 57 | | view.OnReady -= FirstLoading; |
| | 58 | |
|
| 21 | 59 | | view.OnInfoClicked -= ShowEventDetailedInfo; |
| 21 | 60 | | view.OnJumpInClicked -= OnJumpInToEvent; |
| 21 | 61 | | view.OnSubscribeEventClicked -= SubscribeToEvent; |
| 21 | 62 | | view.OnUnsubscribeEventClicked -= UnsubscribeToEvent; |
| 21 | 63 | | view.OnShowMoreUpcomingEventsClicked -= ShowMoreUpcomingEvents; |
| 21 | 64 | | view.OnEventsSubSectionEnable -= RequestAllEvents; |
| | 65 | |
|
| 21 | 66 | | dataStore.channels.currentJoinChannelModal.OnChange -= OnChannelToJoinChanged; |
| | 67 | |
|
| 21 | 68 | | cardsReloader.Dispose(); |
| 21 | 69 | | } |
| | 70 | |
|
| | 71 | | private void FirstLoading() |
| | 72 | | { |
| 0 | 73 | | view.OnEventsSubSectionEnable += RequestAllEvents; |
| 0 | 74 | | cardsReloader.Initialize(); |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | internal void RequestAllEvents() |
| | 78 | | { |
| 2 | 79 | | if (cardsReloader.CanReload()) |
| | 80 | | { |
| 2 | 81 | | availableUISlots = view.CurrentTilesPerRow * INITIAL_NUMBER_OF_UPCOMING_ROWS; |
| 2 | 82 | | view.SetShowMoreButtonActive(false); |
| | 83 | |
|
| 2 | 84 | | cardsReloader.RequestAll(); |
| | 85 | | } |
| 2 | 86 | | } |
| | 87 | |
|
| | 88 | | public void RequestAllFromAPI() |
| | 89 | | { |
| 3 | 90 | | eventsAPIApiController.GetAllEvents( |
| | 91 | | OnSuccess: OnRequestedEventsUpdated, |
| 0 | 92 | | OnFail: error => { Debug.LogError($"Error receiving events from the API: {error}"); }); |
| 3 | 93 | | } |
| | 94 | |
|
| | 95 | | internal void OnRequestedEventsUpdated(List<EventFromAPIModel> eventList) |
| | 96 | | { |
| 1 | 97 | | eventsFromAPI = eventList; |
| | 98 | |
|
| 1 | 99 | | view.SetFeaturedEvents(PlacesAndEventsCardsFactory.CreateEventsCards(FilterFeaturedEvents())); |
| 1 | 100 | | view.SetTrendingEvents(PlacesAndEventsCardsFactory.CreateEventsCards(FilterTrendingEvents())); |
| 1 | 101 | | view.SetUpcomingEvents(PlacesAndEventsCardsFactory.CreateEventsCards(FilterUpcomingEvents())); |
| 1 | 102 | | view.SetGoingEvents(PlacesAndEventsCardsFactory.CreateEventsCards(FilterGoingEvents())); |
| | 103 | |
|
| 1 | 104 | | view.SetShowMoreUpcomingEventsButtonActive(availableUISlots < eventsFromAPI.Count); |
| 1 | 105 | | } |
| | 106 | |
|
| | 107 | | internal List<EventFromAPIModel> FilterFeaturedEvents() |
| | 108 | | { |
| 6 | 109 | | List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList(); |
| | 110 | |
|
| 2 | 111 | | if (eventsFiltered.Count == 0) |
| 2 | 112 | | eventsFiltered = eventsFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_EVENTS).ToList(); |
| | 113 | |
|
| 2 | 114 | | return eventsFiltered; |
| | 115 | | } |
| 6 | 116 | | internal List<EventFromAPIModel> FilterTrendingEvents() => eventsFromAPI.Where(e => e.trending).ToList(); |
| 2 | 117 | | internal List<EventFromAPIModel> FilterUpcomingEvents() => eventsFromAPI.Take(availableUISlots).ToList(); |
| 6 | 118 | | internal List<EventFromAPIModel> FilterGoingEvents() => eventsFromAPI.Where(e => e.attending).ToList(); |
| | 119 | |
|
| | 120 | | public void ShowMoreUpcomingEvents() |
| | 121 | | { |
| 2 | 122 | | int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)availableUISlots / view.currentUpcomingEventsPerRow) * vie |
| 2 | 123 | | int numberOfItemsToAdd = (view.currentUpcomingEventsPerRow * SHOW_MORE_UPCOMING_ROWS_INCREMENT) + numberOfExtraI |
| | 124 | |
|
| 2 | 125 | | List<EventFromAPIModel> eventsFiltered = availableUISlots + numberOfItemsToAdd <= eventsFromAPI.Count |
| | 126 | | ? eventsFromAPI.GetRange(availableUISlots, numberOfItemsToAdd) |
| | 127 | | : eventsFromAPI.GetRange(availableUISlots, eventsFromAPI.Count - availableUISlots); |
| | 128 | |
|
| 2 | 129 | | view.AddUpcomingEvents(PlacesAndEventsCardsFactory.CreateEventsCards(eventsFiltered)); |
| | 130 | |
|
| 2 | 131 | | availableUISlots += numberOfItemsToAdd; |
| 2 | 132 | | if (availableUISlots > eventsFromAPI.Count) |
| 0 | 133 | | availableUISlots = eventsFromAPI.Count; |
| | 134 | |
|
| 2 | 135 | | view.SetShowMoreUpcomingEventsButtonActive(availableUISlots < eventsFromAPI.Count); |
| 2 | 136 | | } |
| | 137 | |
|
| | 138 | | internal void ShowEventDetailedInfo(EventCardComponentModel eventModel) |
| | 139 | | { |
| 1 | 140 | | view.ShowEventModal(eventModel); |
| 1 | 141 | | exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName); |
| 1 | 142 | | } |
| | 143 | |
|
| | 144 | | internal void OnJumpInToEvent(EventFromAPIModel eventFromAPI) |
| | 145 | | { |
| 1 | 146 | | JumpInToEvent(eventFromAPI); |
| 1 | 147 | | view.HideEventModal(); |
| | 148 | |
|
| 1 | 149 | | OnCloseExploreV2?.Invoke(); |
| 1 | 150 | | exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates |
| 1 | 151 | | } |
| | 152 | |
|
| | 153 | | public static void SubscribeToEvent(string eventId) |
| | 154 | | { |
| | 155 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 156 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 157 | |
|
| | 158 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re |
| | 159 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 160 | | // eventId, |
| | 161 | | // true, |
| | 162 | | // () => |
| | 163 | | // { |
| | 164 | | // // ... |
| | 165 | | // }, |
| | 166 | | // (error) => |
| | 167 | | // { |
| | 168 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 169 | | // }); |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | public static void UnsubscribeToEvent(string eventId) |
| | 173 | | { |
| | 174 | | // TODO (Santi): Remove when the RegisterAttendEvent POST is available. |
| 0 | 175 | | WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId)); |
| | 176 | |
|
| | 177 | | // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un |
| | 178 | | //eventsAPIApiController.RegisterAttendEvent( |
| | 179 | | // eventId, |
| | 180 | | // false, |
| | 181 | | // () => |
| | 182 | | // { |
| | 183 | | // // ... |
| | 184 | | // }, |
| | 185 | | // (error) => |
| | 186 | | // { |
| | 187 | | // Debug.LogError($"Error posting 'attend' message to the API: {error}"); |
| | 188 | | // }); |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | private void OnChannelToJoinChanged(string currentChannelId, string previousChannelId) |
| | 192 | | { |
| 0 | 193 | | if (!string.IsNullOrEmpty(currentChannelId)) |
| 0 | 194 | | return; |
| | 195 | |
|
| 0 | 196 | | view.HideEventModal(); |
| 0 | 197 | | OnCloseExploreV2?.Invoke(); |
| 0 | 198 | | } |
| | 199 | |
|
| | 200 | | /// <summary> |
| | 201 | | /// Makes a jump in to the event defined by the given place data from API. |
| | 202 | | /// </summary> |
| | 203 | | /// <param name="eventFromAPI">Event data from API.</param> |
| | 204 | | public static void JumpInToEvent(EventFromAPIModel eventFromAPI) |
| | 205 | | { |
| 2 | 206 | | Vector2Int coords = new Vector2Int(eventFromAPI.coordinates[0], eventFromAPI.coordinates[1]); |
| 2 | 207 | | string[] realmFromAPI = string.IsNullOrEmpty(eventFromAPI.realm) ? new[] { "", "" } : eventFromAPI.realm.Split(' |
| 2 | 208 | | string serverName = realmFromAPI[0]; |
| 2 | 209 | | string layerName = realmFromAPI[1]; |
| | 210 | |
|
| 2 | 211 | | if (string.IsNullOrEmpty(serverName)) |
| 2 | 212 | | Environment.i.world.teleportController.Teleport(coords.x, coords.y); |
| | 213 | | else |
| 0 | 214 | | Environment.i.world.teleportController.JumpIn(coords.x, coords.y, serverName, layerName); |
| 0 | 215 | | } |
| | 216 | | } |