< Summary

Class:HighlightsSubSectionComponentController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/HighlightsSubSection/HighlightsSubSectionMenu/HighlightsSubSectionComponentController.cs
Covered lines:102
Uncovered lines:25
Coverable lines:127
Total lines:319
Line coverage:80.3% (102 of 127)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
HighlightsSubSectionComponentController(...)0%110100%
FirstLoading()0%110100%
OnExploreV2Open(...)0%6200%
RequestAllPlacesAndEvents()0%3.013091.67%
IsInShowAnimationTransitonChanged(...)0%110100%
RequestAllPlacesAndEventsFromAPI()0%110100%
OnRequestedPlacesAndEventsUpdated()0%110100%
LoadTrendingPlacesAndEvents()0%4.094082.35%
LoadFeaturedPlaces()0%5.335076.47%
LoadLiveEvents()0%330100%
Dispose()0%110100%
ShowPlaceDetailedInfo(...)0%110100%
JumpInToPlace(...)0%220100%
View_OnFriendHandlerAdded(...)0%2100%
ShowEventDetailedInfo(...)0%110100%
JumpInToEvent(...)0%220100%
SubscribeToEvent(...)0%2100%
UnsubscribeToEvent(...)0%2100%
GoToEventsSubSection()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/HighlightsSubSection/HighlightsSubSectionMenu/HighlightsSubSectionComponentController.cs

#LineLine coverage
 1using DCL;
 2using DCL.Interface;
 3using ExploreV2Analytics;
 4using System;
 5using System.Collections.Generic;
 6using System.Linq;
 7using UnityEngine;
 8using static HotScenesController;
 9
 10public 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
 43public 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;
 2158    internal List<HotSceneInfo> placesFromAPI = new List<HotSceneInfo>();
 2159    internal List<EventFromAPIModel> eventsFromAPI = new List<EventFromAPIModel>();
 60    internal bool reloadHighlights = false;
 61    internal IExploreV2Analytics exploreV2Analytics;
 62
 2163    public HighlightsSubSectionComponentController(
 64        IHighlightsSubSectionComponentView view,
 65        IPlacesAPIController placesAPI,
 66        IEventsAPIController eventsAPI,
 67        IFriendsController friendsController,
 68        IExploreV2Analytics exploreV2Analytics)
 69    {
 2170        this.view = view;
 2171        this.view.OnReady += FirstLoading;
 2172        this.view.OnPlaceInfoClicked += ShowPlaceDetailedInfo;
 2173        this.view.OnEventInfoClicked += ShowEventDetailedInfo;
 2174        this.view.OnPlaceJumpInClicked += JumpInToPlace;
 2175        this.view.OnEventJumpInClicked += JumpInToEvent;
 2176        this.view.OnEventSubscribeEventClicked += SubscribeToEvent;
 2177        this.view.OnEventUnsubscribeEventClicked += UnsubscribeToEvent;
 2178        this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded;
 2179        this.view.OnViewAllEventsClicked += GoToEventsSubSection;
 80
 2181        placesAPIApiController = placesAPI;
 2182        eventsAPIApiController = eventsAPI;
 2183        OnPlacesAndEventsFromAPIUpdated += OnRequestedPlacesAndEventsUpdated;
 84
 2185        friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors);
 86
 2187        this.exploreV2Analytics = exploreV2Analytics;
 2188    }
 89
 90    internal void FirstLoading()
 91    {
 192        reloadHighlights = true;
 193        RequestAllPlacesAndEvents();
 94
 195        view.OnHighlightsSubSectionEnable += RequestAllPlacesAndEvents;
 196        DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open;
 197    }
 98
 99    internal void OnExploreV2Open(bool current, bool previous)
 100    {
 0101        if (current)
 0102            return;
 103
 0104        reloadHighlights = true;
 0105    }
 106
 107    public void RequestAllPlacesAndEvents()
 108    {
 2109        if (!reloadHighlights)
 0110            return;
 111
 2112        view.RestartScrollViewPosition();
 2113        view.SetTrendingPlacesAndEventsAsLoading(true);
 2114        view.SetFeaturedPlacesAsLoading(true);
 2115        view.SetLiveAsLoading(true);
 2116        RequestAllPlacesAndEventsFromAPI();
 2117        reloadHighlights = false;
 118
 2119        if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get())
 1120            RequestAllPlacesAndEventsFromAPI();
 121        else
 1122            DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged;
 1123    }
 124
 125    internal void IsInShowAnimationTransitonChanged(bool current, bool previous)
 126    {
 1127        DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged;
 1128        RequestAllPlacesAndEventsFromAPI();
 1129    }
 130
 131    internal void RequestAllPlacesAndEventsFromAPI()
 132    {
 5133        placesAPIApiController.GetAllPlaces(
 134            (placeList) =>
 135            {
 0136                placesFromAPI = placeList;
 0137                eventsAPIApiController.GetAllEvents(
 138                    (eventList) =>
 139                    {
 0140                        eventsFromAPI = eventList;
 0141                        OnPlacesAndEventsFromAPIUpdated?.Invoke();
 0142                    },
 143                    (error) =>
 144                    {
 0145                        OnPlacesAndEventsFromAPIUpdated?.Invoke();
 0146                        Debug.LogError($"Error receiving events from the API: {error}");
 0147                    });
 0148            });
 5149    }
 150
 151    internal void OnRequestedPlacesAndEventsUpdated()
 152    {
 1153        friendsTrackerController.RemoveAllHandlers();
 154
 1155        LoadTrendingPlacesAndEvents();
 1156        LoadFeaturedPlaces();
 1157        LoadLiveEvents();
 1158    }
 159
 160    public void LoadTrendingPlacesAndEvents()
 161    {
 162        // Places
 2163        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 2164        List<HotSceneInfo> placesFiltered = placesFromAPI
 165                                            .Take(DEFAULT_NUMBER_OF_TRENDING_PLACES)
 166                                            .ToList();
 167
 12168        foreach (HotSceneInfo receivedPlace in placesFiltered)
 169        {
 4170            PlaceCardComponentModel placeCardModel = ExplorePlacesHelpers.CreatePlaceCardModelFromAPIPlace(receivedPlace
 4171            places.Add(placeCardModel);
 172        }
 173
 174        // Events
 2175        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 6176        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList();
 177
 4178        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 179        {
 0180            EventCardComponentModel eventCardModel = ExploreEventsHelpers.CreateEventCardModelFromAPIEvent(receivedEvent
 0181            events.Add(eventCardModel);
 182        }
 183
 2184        view.SetTrendingPlacesAndEventsAsLoading(false);
 2185        view.SetTrendingPlacesAndEvents(places, events);
 2186    }
 187
 188    public void LoadFeaturedPlaces()
 189    {
 2190        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 191        List<HotSceneInfo> placesFiltered;
 2192        if (placesFromAPI.Count >= DEFAULT_NUMBER_OF_TRENDING_PLACES)
 193        {
 0194            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
 0198            placesFiltered = placesFromAPI
 199                             .GetRange(DEFAULT_NUMBER_OF_TRENDING_PLACES, numberOfPlaces)
 200                             .ToList();
 0201        }
 2202        else if (placesFromAPI.Count > 0)
 203        {
 2204            placesFiltered = placesFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_PLACES).ToList();
 2205        }
 206        else
 207        {
 0208            placesFiltered = new List<HotSceneInfo>();
 209        }
 210
 12211        foreach (HotSceneInfo receivedPlace in placesFiltered)
 212        {
 4213            PlaceCardComponentModel placeCardModel = ExplorePlacesHelpers.CreatePlaceCardModelFromAPIPlace(receivedPlace
 4214            places.Add(placeCardModel);
 215        }
 216
 2217        view.SetFeaturedPlaces(places);
 2218        view.SetFeaturedPlacesAsLoading(false);
 2219    }
 220
 221    public void LoadLiveEvents()
 222    {
 2223        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 6224        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(x => x.live)
 225                                                              .Take(DEFAULT_NUMBER_OF_LIVE_EVENTS)
 226                                                              .ToList();
 12227        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 228        {
 4229            EventCardComponentModel eventCardModel = ExploreEventsHelpers.CreateEventCardModelFromAPIEvent(receivedEvent
 4230            events.Add(eventCardModel);
 231        }
 232
 2233        view.SetLiveEvents(events);
 2234        view.SetLiveAsLoading(false);
 2235    }
 236
 237    public void Dispose()
 238    {
 21239        view.OnReady -= FirstLoading;
 21240        view.OnPlaceInfoClicked -= ShowPlaceDetailedInfo;
 21241        view.OnEventInfoClicked -= ShowEventDetailedInfo;
 21242        view.OnPlaceJumpInClicked -= JumpInToPlace;
 21243        view.OnEventJumpInClicked -= JumpInToEvent;
 21244        view.OnEventSubscribeEventClicked -= SubscribeToEvent;
 21245        view.OnEventUnsubscribeEventClicked -= UnsubscribeToEvent;
 21246        view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded;
 21247        view.OnViewAllEventsClicked -= GoToEventsSubSection;
 21248    }
 249
 250    internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel)
 251    {
 1252        view.ShowPlaceModal(placeModel);
 1253        exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName);
 1254    }
 255
 256    internal void JumpInToPlace(HotSceneInfo placeFromAPI)
 257    {
 1258        ExplorePlacesHelpers.JumpInToPlace(placeFromAPI);
 1259        view.HidePlaceModal();
 1260        OnCloseExploreV2?.Invoke();
 1261        exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords);
 1262    }
 263
 0264    internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends
 265
 266    internal void ShowEventDetailedInfo(EventCardComponentModel eventModel)
 267    {
 1268        view.ShowEventModal(eventModel);
 1269        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName);
 1270    }
 271
 272    internal void JumpInToEvent(EventFromAPIModel eventFromAPI)
 273    {
 1274        ExploreEventsHelpers.JumpInToEvent(eventFromAPI);
 1275        view.HideEventModal();
 1276        OnCloseExploreV2?.Invoke();
 1277        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates
 1278    }
 279
 280    internal void SubscribeToEvent(string eventId)
 281    {
 282        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0283        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        //    });
 0297    }
 298
 299    internal void UnsubscribeToEvent(string eventId)
 300    {
 301        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0302        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        //    });
 0316    }
 317
 2318    internal void GoToEventsSubSection() { OnGoToEventsSubSection?.Invoke(); }
 319}