< 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:103
Uncovered lines:25
Coverable lines:128
Total lines:321
Line coverage:80.4% (103 of 128)
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;
 88
 2189        view.ConfigurePools();
 2190    }
 91
 92    internal void FirstLoading()
 93    {
 194        reloadHighlights = true;
 195        RequestAllPlacesAndEvents();
 96
 197        view.OnHighlightsSubSectionEnable += RequestAllPlacesAndEvents;
 198        DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open;
 199    }
 100
 101    internal void OnExploreV2Open(bool current, bool previous)
 102    {
 0103        if (current)
 0104            return;
 105
 0106        reloadHighlights = true;
 0107    }
 108
 109    public void RequestAllPlacesAndEvents()
 110    {
 2111        if (!reloadHighlights)
 0112            return;
 113
 2114        view.RestartScrollViewPosition();
 2115        view.SetTrendingPlacesAndEventsAsLoading(true);
 2116        view.SetFeaturedPlacesAsLoading(true);
 2117        view.SetLiveAsLoading(true);
 2118        RequestAllPlacesAndEventsFromAPI();
 2119        reloadHighlights = false;
 120
 2121        if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get())
 1122            RequestAllPlacesAndEventsFromAPI();
 123        else
 1124            DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged;
 1125    }
 126
 127    internal void IsInShowAnimationTransitonChanged(bool current, bool previous)
 128    {
 1129        DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged;
 1130        RequestAllPlacesAndEventsFromAPI();
 1131    }
 132
 133    internal void RequestAllPlacesAndEventsFromAPI()
 134    {
 5135        placesAPIApiController.GetAllPlaces(
 136            (placeList) =>
 137            {
 0138                placesFromAPI = placeList;
 0139                eventsAPIApiController.GetAllEvents(
 140                    (eventList) =>
 141                    {
 0142                        eventsFromAPI = eventList;
 0143                        OnPlacesAndEventsFromAPIUpdated?.Invoke();
 0144                    },
 145                    (error) =>
 146                    {
 0147                        OnPlacesAndEventsFromAPIUpdated?.Invoke();
 0148                        Debug.LogError($"Error receiving events from the API: {error}");
 0149                    });
 0150            });
 5151    }
 152
 153    internal void OnRequestedPlacesAndEventsUpdated()
 154    {
 1155        friendsTrackerController.RemoveAllHandlers();
 156
 1157        LoadTrendingPlacesAndEvents();
 1158        LoadFeaturedPlaces();
 1159        LoadLiveEvents();
 1160    }
 161
 162    public void LoadTrendingPlacesAndEvents()
 163    {
 164        // Places
 2165        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 2166        List<HotSceneInfo> placesFiltered = placesFromAPI
 167                                            .Take(DEFAULT_NUMBER_OF_TRENDING_PLACES)
 168                                            .ToList();
 169
 12170        foreach (HotSceneInfo receivedPlace in placesFiltered)
 171        {
 4172            PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace);
 4173            places.Add(placeCardModel);
 174        }
 175
 176        // Events
 2177        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 6178        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(e => e.highlighted).ToList();
 179
 4180        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 181        {
 0182            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0183            events.Add(eventCardModel);
 184        }
 185
 2186        view.SetTrendingPlacesAndEventsAsLoading(false);
 2187        view.SetTrendingPlacesAndEvents(places, events);
 2188    }
 189
 190    public void LoadFeaturedPlaces()
 191    {
 2192        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 193        List<HotSceneInfo> placesFiltered;
 2194        if (placesFromAPI.Count >= DEFAULT_NUMBER_OF_TRENDING_PLACES)
 195        {
 0196            int numberOfPlaces = placesFromAPI.Count >= (DEFAULT_NUMBER_OF_TRENDING_PLACES + DEFAULT_NUMBER_OF_FEATURED_
 197                ? DEFAULT_NUMBER_OF_FEATURED_PLACES
 198                : placesFromAPI.Count - DEFAULT_NUMBER_OF_TRENDING_PLACES;
 199
 0200            placesFiltered = placesFromAPI
 201                             .GetRange(DEFAULT_NUMBER_OF_TRENDING_PLACES, numberOfPlaces)
 202                             .ToList();
 0203        }
 2204        else if (placesFromAPI.Count > 0)
 205        {
 2206            placesFiltered = placesFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_PLACES).ToList();
 2207        }
 208        else
 209        {
 0210            placesFiltered = new List<HotSceneInfo>();
 211        }
 212
 12213        foreach (HotSceneInfo receivedPlace in placesFiltered)
 214        {
 4215            PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace);
 4216            places.Add(placeCardModel);
 217        }
 218
 2219        view.SetFeaturedPlaces(places);
 2220        view.SetFeaturedPlacesAsLoading(false);
 2221    }
 222
 223    public void LoadLiveEvents()
 224    {
 2225        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 6226        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(x => x.live)
 227                                                              .Take(DEFAULT_NUMBER_OF_LIVE_EVENTS)
 228                                                              .ToList();
 12229        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 230        {
 4231            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 4232            events.Add(eventCardModel);
 233        }
 234
 2235        view.SetLiveEvents(events);
 2236        view.SetLiveAsLoading(false);
 2237    }
 238
 239    public void Dispose()
 240    {
 21241        view.OnReady -= FirstLoading;
 21242        view.OnPlaceInfoClicked -= ShowPlaceDetailedInfo;
 21243        view.OnEventInfoClicked -= ShowEventDetailedInfo;
 21244        view.OnPlaceJumpInClicked -= JumpInToPlace;
 21245        view.OnEventJumpInClicked -= JumpInToEvent;
 21246        view.OnEventSubscribeEventClicked -= SubscribeToEvent;
 21247        view.OnEventUnsubscribeEventClicked -= UnsubscribeToEvent;
 21248        view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded;
 21249        view.OnViewAllEventsClicked -= GoToEventsSubSection;
 21250    }
 251
 252    internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel)
 253    {
 1254        view.ShowPlaceModal(placeModel);
 1255        exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName);
 1256    }
 257
 258    internal void JumpInToPlace(HotSceneInfo placeFromAPI)
 259    {
 1260        ExplorePlacesUtils.JumpInToPlace(placeFromAPI);
 1261        view.HidePlaceModal();
 1262        OnCloseExploreV2?.Invoke();
 1263        exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords);
 1264    }
 265
 0266    internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends
 267
 268    internal void ShowEventDetailedInfo(EventCardComponentModel eventModel)
 269    {
 1270        view.ShowEventModal(eventModel);
 1271        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName);
 1272    }
 273
 274    internal void JumpInToEvent(EventFromAPIModel eventFromAPI)
 275    {
 1276        ExploreEventsUtils.JumpInToEvent(eventFromAPI);
 1277        view.HideEventModal();
 1278        OnCloseExploreV2?.Invoke();
 1279        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates
 1280    }
 281
 282    internal void SubscribeToEvent(string eventId)
 283    {
 284        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0285        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 286
 287        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re
 288        //eventsAPIApiController.RegisterAttendEvent(
 289        //    eventId,
 290        //    true,
 291        //    () =>
 292        //    {
 293        //        // ...
 294        //    },
 295        //    (error) =>
 296        //    {
 297        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 298        //    });
 0299    }
 300
 301    internal void UnsubscribeToEvent(string eventId)
 302    {
 303        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0304        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 305
 306        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un
 307        //eventsAPIApiController.RegisterAttendEvent(
 308        //    eventId,
 309        //    false,
 310        //    () =>
 311        //    {
 312        //        // ...
 313        //    },
 314        //    (error) =>
 315        //    {
 316        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 317        //    });
 0318    }
 319
 2320    internal void GoToEventsSubSection() { OnGoToEventsSubSection?.Invoke(); }
 321}