< Summary

Class:EventsSubSectionComponentController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/EventsSubSection/EventsSubSectionMenu/EventsSubSectionComponentController.cs
Covered lines:97
Uncovered lines:29
Coverable lines:126
Total lines:305
Line coverage:76.9% (97 of 126)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EventsSubSectionComponentController(...)0%110100%
FirstLoading()0%110100%
OnExploreV2Open(...)0%6200%
RequestAllEvents()0%3.093078.57%
IsInShowAnimationTransitonChanged(...)0%2100%
RequestAllEventsFromAPI()0%220100%
OnRequestedEventsUpdated()0%110100%
LoadFeaturedEvents()0%440100%
LoadTrendingEvents()0%3.243070%
LoadUpcomingEvents()0%2.082072.73%
ShowMoreUpcomingEvents()0%4.344072.22%
LoadGoingEvents()0%3.243070%
Dispose()0%110100%
ShowEventDetailedInfo(...)0%110100%
JumpInToEvent(...)0%220100%
SubscribeToEvent(...)0%2100%
UnsubscribeToEvent(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/EventsSubSection/EventsSubSectionMenu/EventsSubSectionComponentController.cs

#LineLine coverage
 1using DCL;
 2using DCL.Interface;
 3using ExploreV2Analytics;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using UnityEngine;
 8
 9public 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
 47public 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;
 2158    internal List<EventFromAPIModel> eventsFromAPI = new List<EventFromAPIModel>();
 59    internal int currentUpcomingEventsShowed = 0;
 60    internal bool reloadEvents = false;
 61    internal IExploreV2Analytics exploreV2Analytics;
 62
 2163    public EventsSubSectionComponentController(
 64        IEventsSubSectionComponentView view,
 65        IEventsAPIController eventsAPI,
 66        IExploreV2Analytics exploreV2Analytics)
 67    {
 2168        this.view = view;
 2169        this.view.OnReady += FirstLoading;
 2170        this.view.OnInfoClicked += ShowEventDetailedInfo;
 2171        this.view.OnJumpInClicked += JumpInToEvent;
 2172        this.view.OnSubscribeEventClicked += SubscribeToEvent;
 2173        this.view.OnUnsubscribeEventClicked += UnsubscribeToEvent;
 2174        this.view.OnShowMoreUpcomingEventsClicked += ShowMoreUpcomingEvents;
 75
 2176        eventsAPIApiController = eventsAPI;
 2177        OnEventsFromAPIUpdated += OnRequestedEventsUpdated;
 78
 2179        this.exploreV2Analytics = exploreV2Analytics;
 80
 2181        view.ConfigurePools();
 2182    }
 83
 84    internal void FirstLoading()
 85    {
 186        reloadEvents = true;
 187        RequestAllEvents();
 88
 189        view.OnEventsSubSectionEnable += RequestAllEvents;
 190        DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open;
 191    }
 92
 93    internal void OnExploreV2Open(bool current, bool previous)
 94    {
 095        if (current)
 096            return;
 97
 098        reloadEvents = true;
 099    }
 100
 101    public void RequestAllEvents()
 102    {
 2103        if (!reloadEvents)
 0104            return;
 105
 2106        currentUpcomingEventsShowed = view.currentUpcomingEventsPerRow * INITIAL_NUMBER_OF_UPCOMING_ROWS;
 2107        view.RestartScrollViewPosition();
 2108        view.SetFeaturedEventsAsLoading(true);
 2109        view.SetTrendingEventsAsLoading(true);
 2110        view.SetUpcomingEventsAsLoading(true);
 2111        view.SetShowMoreUpcomingEventsButtonActive(false);
 2112        view.SetGoingEventsAsLoading(true);
 2113        reloadEvents = false;
 114
 2115        if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get())
 2116            RequestAllEventsFromAPI();
 117        else
 0118            DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged;
 0119    }
 120
 121    internal void IsInShowAnimationTransitonChanged(bool current, bool previous)
 122    {
 0123        DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged;
 0124        RequestAllEventsFromAPI();
 0125    }
 126
 127    internal void RequestAllEventsFromAPI()
 128    {
 3129        eventsAPIApiController.GetAllEvents(
 130            (eventList) =>
 131            {
 0132                eventsFromAPI = eventList;
 0133                OnEventsFromAPIUpdated?.Invoke();
 0134            },
 135            (error) =>
 136            {
 0137                Debug.LogError($"Error receiving events from the API: {error}");
 0138            });
 3139    }
 140
 141    internal void OnRequestedEventsUpdated()
 142    {
 1143        LoadFeaturedEvents();
 1144        LoadTrendingEvents();
 1145        LoadUpcomingEvents();
 1146        LoadGoingEvents();
 1147    }
 148
 149    public void LoadFeaturedEvents()
 150    {
 2151        List<EventCardComponentModel> featuredEvents = new List<EventCardComponentModel>();
 6152        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList();
 153
 2154        if (eventsFiltered.Count == 0)
 2155            eventsFiltered = eventsFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_EVENTS).ToList();
 156
 12157        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 158        {
 4159            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 4160            featuredEvents.Add(eventCardModel);
 161        }
 162
 2163        view.SetFeaturedEvents(featuredEvents);
 2164        view.SetFeaturedEventsAsLoading(false);
 2165        view.SetFeaturedEventsActive(featuredEvents.Count > 0);
 2166    }
 167
 168    public void LoadTrendingEvents()
 169    {
 2170        List<EventCardComponentModel> trendingEvents = new List<EventCardComponentModel>();
 6171        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.trending).ToList();
 172
 4173        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 174        {
 0175            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0176            trendingEvents.Add(eventCardModel);
 177        }
 178
 2179        view.SetTrendingEvents(trendingEvents);
 2180        view.SetTrendingEventsAsLoading(false);
 2181    }
 182
 183    public void LoadUpcomingEvents()
 184    {
 2185        List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>();
 2186        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Take(currentUpcomingEventsShowed).ToList();
 187
 4188        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 189        {
 0190            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0191            upcomingEvents.Add(eventCardModel);
 192        }
 193
 2194        view.SetUpcomingEvents(upcomingEvents);
 2195        view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count);
 2196        view.SetUpcomingEventsAsLoading(false);
 2197    }
 198
 199    public void ShowMoreUpcomingEvents()
 200    {
 2201        List<EventCardComponentModel> upcomingEvents = new List<EventCardComponentModel>();
 2202        List<EventFromAPIModel> eventsFiltered = new List<EventFromAPIModel>();
 2203        int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)currentUpcomingEventsShowed / view.currentUpcomingEventsPe
 2204        int numberOfItemsToAdd = view.currentUpcomingEventsPerRow * SHOW_MORE_UPCOMING_ROWS_INCREMENT + numberOfExtraIte
 205
 2206        if (currentUpcomingEventsShowed + numberOfItemsToAdd <= eventsFromAPI.Count)
 2207            eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, numberOfItemsToAdd);
 208        else
 0209            eventsFiltered = eventsFromAPI.GetRange(currentUpcomingEventsShowed, eventsFromAPI.Count - currentUpcomingEv
 210
 4211        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 212        {
 0213            EventCardComponentModel placeCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0214            upcomingEvents.Add(placeCardModel);
 215        }
 216
 2217        view.AddUpcomingEvents(upcomingEvents);
 218
 2219        currentUpcomingEventsShowed += numberOfItemsToAdd;
 2220        if (currentUpcomingEventsShowed > eventsFromAPI.Count)
 0221            currentUpcomingEventsShowed = eventsFromAPI.Count;
 222
 2223        view.SetShowMoreUpcomingEventsButtonActive(currentUpcomingEventsShowed < eventsFromAPI.Count);
 2224    }
 225
 226    public void LoadGoingEvents()
 227    {
 2228        List<EventCardComponentModel> goingEvents = new List<EventCardComponentModel>();
 6229        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.attending).ToList();
 230
 4231        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 232        {
 0233            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0234            goingEvents.Add(eventCardModel);
 235        }
 236
 2237        view.SetGoingEvents(goingEvents);
 2238        view.SetGoingEventsAsLoading(false);
 2239    }
 240
 241    public void Dispose()
 242    {
 21243        view.OnReady -= FirstLoading;
 21244        view.OnInfoClicked -= ShowEventDetailedInfo;
 21245        view.OnJumpInClicked -= JumpInToEvent;
 21246        view.OnSubscribeEventClicked -= SubscribeToEvent;
 21247        view.OnUnsubscribeEventClicked -= UnsubscribeToEvent;
 21248        view.OnShowMoreUpcomingEventsClicked -= ShowMoreUpcomingEvents;
 21249        view.OnEventsSubSectionEnable -= RequestAllEvents;
 21250        OnEventsFromAPIUpdated -= OnRequestedEventsUpdated;
 21251        DataStore.i.exploreV2.isOpen.OnChange -= OnExploreV2Open;
 21252    }
 253
 254    internal void ShowEventDetailedInfo(EventCardComponentModel eventModel)
 255    {
 1256        view.ShowEventModal(eventModel);
 1257        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName);
 1258    }
 259
 260    internal void JumpInToEvent(EventFromAPIModel eventFromAPI)
 261    {
 1262        ExploreEventsUtils.JumpInToEvent(eventFromAPI);
 1263        view.HideEventModal();
 1264        OnCloseExploreV2?.Invoke();
 1265        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates
 1266    }
 267
 268    internal void SubscribeToEvent(string eventId)
 269    {
 270        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0271        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        //    });
 0285    }
 286
 287    internal void UnsubscribeToEvent(string eventId)
 288    {
 289        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0290        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        //    });
 0304    }
 305}