< 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:113
Uncovered lines:28
Coverable lines:141
Total lines:350
Line coverage:80.1% (113 of 141)
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%220100%
RequestAllPlacesAndEvents()0%4.054085.71%
IsInShowAnimationTransitonChanged(...)0%110100%
RequestAllPlacesAndEventsFromAPI()0%110100%
OnRequestedPlacesAndEventsUpdated()0%110100%
LoadTrendingPlacesAndEvents()0%4.114081.25%
LoadFeaturedPlaces()0%5.255078.57%
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%
OnChannelToJoinChanged(...)0%12300%

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 DCL.Social.Friends;
 4using ExploreV2Analytics;
 5using System;
 6using System.Collections.Generic;
 7using System.Linq;
 8using UnityEngine;
 9using static HotScenesController;
 10
 11public interface IHighlightsSubSectionComponentController : IDisposable
 12{
 13    /// <summary>
 14    /// It will be triggered when the sub-section want to request to close the ExploreV2 main menu.
 15    /// </summary>
 16    event Action OnCloseExploreV2;
 17
 18    /// <summary>
 19    /// It will be triggered when the sub-section want to request to go to the Events sub-section.
 20    /// </summary>
 21    event Action OnGoToEventsSubSection;
 22
 23    /// <summary>
 24    /// Request all places and events from the API.
 25    /// </summary>
 26    void RequestAllPlacesAndEvents();
 27
 28    /// <summary>
 29    /// Load the trending places and events with the last requested ones.
 30    /// </summary>
 31    void LoadTrendingPlacesAndEvents();
 32
 33    /// <summary>
 34    /// Load the featured places with the last requested ones.
 35    /// </summary>
 36    void LoadFeaturedPlaces();
 37
 38    /// <summary>
 39    /// Load the live events with the last requested ones.
 40    /// </summary>
 41    void LoadLiveEvents();
 42}
 43
 44public class HighlightsSubSectionComponentController : IHighlightsSubSectionComponentController
 45{
 46    public event Action OnCloseExploreV2;
 47    public event Action OnGoToEventsSubSection;
 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    internal float lastTimeAPIChecked = 0;
 63    private DataStore dataStore;
 64
 2165    public HighlightsSubSectionComponentController(
 66        IHighlightsSubSectionComponentView view,
 67        IPlacesAPIController placesAPI,
 68        IEventsAPIController eventsAPI,
 69        IFriendsController friendsController,
 70        IExploreV2Analytics exploreV2Analytics,
 71        DataStore dataStore)
 72    {
 2173        this.view = view;
 2174        this.view.OnReady += FirstLoading;
 2175        this.view.OnPlaceInfoClicked += ShowPlaceDetailedInfo;
 2176        this.view.OnEventInfoClicked += ShowEventDetailedInfo;
 2177        this.view.OnPlaceJumpInClicked += JumpInToPlace;
 2178        this.view.OnEventJumpInClicked += JumpInToEvent;
 2179        this.view.OnEventSubscribeEventClicked += SubscribeToEvent;
 2180        this.view.OnEventUnsubscribeEventClicked += UnsubscribeToEvent;
 2181        this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded;
 2182        this.view.OnViewAllEventsClicked += GoToEventsSubSection;
 83
 2184        this.dataStore = dataStore;
 2185        this.dataStore.channels.currentJoinChannelModal.OnChange += OnChannelToJoinChanged;
 86
 2187        placesAPIApiController = placesAPI;
 2188        eventsAPIApiController = eventsAPI;
 89
 2190        friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors);
 91
 2192        this.exploreV2Analytics = exploreV2Analytics;
 93
 2194        view.ConfigurePools();
 2195    }
 96
 97    internal void FirstLoading()
 98    {
 199        reloadHighlights = true;
 1100        lastTimeAPIChecked = Time.realtimeSinceStartup - PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK_API
 1101        RequestAllPlacesAndEvents();
 102
 1103        view.OnHighlightsSubSectionEnable += RequestAllPlacesAndEvents;
 1104        dataStore.exploreV2.isOpen.OnChange += OnExploreV2Open;
 1105    }
 106
 107    internal void OnExploreV2Open(bool current, bool previous)
 108    {
 2109        if (current)
 1110            return;
 111
 1112        reloadHighlights = true;
 1113    }
 114
 115    public void RequestAllPlacesAndEvents()
 116    {
 2117        if (!reloadHighlights)
 0118            return;
 119
 2120        view.RestartScrollViewPosition();
 121
 2122        if (Time.realtimeSinceStartup < lastTimeAPIChecked + PlacesAndEventsSectionComponentController.MIN_TIME_TO_CHECK
 0123            return;
 124
 2125        view.SetTrendingPlacesAndEventsAsLoading(true);
 2126        view.SetFeaturedPlacesAsLoading(true);
 2127        view.SetLiveAsLoading(true);
 128
 2129        reloadHighlights = false;
 2130        lastTimeAPIChecked = Time.realtimeSinceStartup;
 131
 2132        if (!dataStore.exploreV2.isInShowAnimationTransiton.Get())
 1133            RequestAllPlacesAndEventsFromAPI();
 134        else
 1135            dataStore.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged;
 1136    }
 137
 138    internal void IsInShowAnimationTransitonChanged(bool current, bool previous)
 139    {
 1140        dataStore.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged;
 1141        RequestAllPlacesAndEventsFromAPI();
 1142    }
 143
 144    internal void RequestAllPlacesAndEventsFromAPI()
 145    {
 3146        placesAPIApiController.GetAllPlaces(
 147            (placeList) =>
 148            {
 0149                placesFromAPI = placeList;
 0150                eventsAPIApiController.GetAllEvents(
 151                    (eventList) =>
 152                    {
 0153                        eventsFromAPI = eventList;
 0154                        OnRequestedPlacesAndEventsUpdated();
 0155                    },
 156                    (error) =>
 157                    {
 0158                        OnRequestedPlacesAndEventsUpdated();
 0159                        Debug.LogError($"Error receiving events from the API: {error}");
 0160                    });
 0161            });
 3162    }
 163
 164    internal void OnRequestedPlacesAndEventsUpdated()
 165    {
 1166        friendsTrackerController.RemoveAllHandlers();
 167
 1168        LoadTrendingPlacesAndEvents();
 1169        LoadFeaturedPlaces();
 1170        LoadLiveEvents();
 1171    }
 172
 173    public void LoadTrendingPlacesAndEvents()
 174    {
 175        // Places
 2176        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 2177        List<HotSceneInfo> placesFiltered = placesFromAPI
 178                                            .Take(DEFAULT_NUMBER_OF_TRENDING_PLACES)
 179                                            .ToList();
 180
 12181        foreach (HotSceneInfo receivedPlace in placesFiltered)
 182        {
 4183            PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace);
 4184            places.Add(placeCardModel);
 185        }
 186
 187        // Events
 2188        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 2189        List<EventFromAPIModel> eventsFiltered = eventsFromAPI
 4190            .Where(e => e.highlighted)
 191            .Take(placesFiltered.Count)
 192            .ToList();
 193
 4194        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 195        {
 0196            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 0197            events.Add(eventCardModel);
 198        }
 199
 2200        view.SetTrendingPlacesAndEvents(places, events);
 2201    }
 202
 203    public void LoadFeaturedPlaces()
 204    {
 2205        List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>();
 206        List<HotSceneInfo> placesFiltered;
 2207        if (placesFromAPI.Count >= DEFAULT_NUMBER_OF_TRENDING_PLACES)
 208        {
 0209            int numberOfPlaces = placesFromAPI.Count >= (DEFAULT_NUMBER_OF_TRENDING_PLACES + DEFAULT_NUMBER_OF_FEATURED_
 210                ? DEFAULT_NUMBER_OF_FEATURED_PLACES
 211                : placesFromAPI.Count - DEFAULT_NUMBER_OF_TRENDING_PLACES;
 212
 0213            placesFiltered = placesFromAPI
 214                             .GetRange(DEFAULT_NUMBER_OF_TRENDING_PLACES, numberOfPlaces)
 215                             .ToList();
 216        }
 2217        else if (placesFromAPI.Count > 0)
 218        {
 2219            placesFiltered = placesFromAPI.Take(DEFAULT_NUMBER_OF_FEATURED_PLACES).ToList();
 220        }
 221        else
 222        {
 0223            placesFiltered = new List<HotSceneInfo>();
 224        }
 225
 12226        foreach (HotSceneInfo receivedPlace in placesFiltered)
 227        {
 4228            PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace);
 4229            places.Add(placeCardModel);
 230        }
 231
 2232        view.SetFeaturedPlaces(places);
 2233    }
 234
 235    public void LoadLiveEvents()
 236    {
 2237        List<EventCardComponentModel> events = new List<EventCardComponentModel>();
 6238        List<EventFromAPIModel> eventsFiltered = eventsFromAPI.Where(x => x.live)
 239                                                              .Take(DEFAULT_NUMBER_OF_LIVE_EVENTS)
 240                                                              .ToList();
 12241        foreach (EventFromAPIModel receivedEvent in eventsFiltered)
 242        {
 4243            EventCardComponentModel eventCardModel = ExploreEventsUtils.CreateEventCardModelFromAPIEvent(receivedEvent);
 4244            events.Add(eventCardModel);
 245        }
 246
 2247        view.SetLiveEvents(events);
 2248    }
 249
 250    public void Dispose()
 251    {
 21252        view.OnReady -= FirstLoading;
 21253        view.OnPlaceInfoClicked -= ShowPlaceDetailedInfo;
 21254        view.OnEventInfoClicked -= ShowEventDetailedInfo;
 21255        view.OnPlaceJumpInClicked -= JumpInToPlace;
 21256        view.OnEventJumpInClicked -= JumpInToEvent;
 21257        view.OnEventSubscribeEventClicked -= SubscribeToEvent;
 21258        view.OnEventUnsubscribeEventClicked -= UnsubscribeToEvent;
 21259        view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded;
 21260        view.OnViewAllEventsClicked -= GoToEventsSubSection;
 261
 21262        dataStore.exploreV2.isOpen.OnChange -= OnExploreV2Open;
 21263        dataStore.channels.currentJoinChannelModal.OnChange -= OnChannelToJoinChanged;
 21264    }
 265
 266    internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel)
 267    {
 1268        view.ShowPlaceModal(placeModel);
 1269        exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName);
 1270        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.Places);
 1271    }
 272
 273    internal void JumpInToPlace(HotSceneInfo placeFromAPI)
 274    {
 1275        ExplorePlacesUtils.JumpInToPlace(placeFromAPI);
 1276        view.HidePlaceModal();
 1277        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None);
 1278        OnCloseExploreV2?.Invoke();
 1279        exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords);
 1280    }
 281
 0282    internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends
 283
 284    internal void ShowEventDetailedInfo(EventCardComponentModel eventModel)
 285    {
 1286        view.ShowEventModal(eventModel);
 1287        exploreV2Analytics.SendClickOnEventInfo(eventModel.eventId, eventModel.eventName);
 1288        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.Events);
 1289    }
 290
 291    internal void JumpInToEvent(EventFromAPIModel eventFromAPI)
 292    {
 1293        ExploreEventsUtils.JumpInToEvent(eventFromAPI);
 1294        view.HideEventModal();
 1295        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None);
 1296        OnCloseExploreV2?.Invoke();
 1297        exploreV2Analytics.SendEventTeleport(eventFromAPI.id, eventFromAPI.name, new Vector2Int(eventFromAPI.coordinates
 1298    }
 299
 300    internal void SubscribeToEvent(string eventId)
 301    {
 302        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0303        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 304
 305        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to re
 306        //eventsAPIApiController.RegisterAttendEvent(
 307        //    eventId,
 308        //    true,
 309        //    () =>
 310        //    {
 311        //        // ...
 312        //    },
 313        //    (error) =>
 314        //    {
 315        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 316        //    });
 0317    }
 318
 319    internal void UnsubscribeToEvent(string eventId)
 320    {
 321        // TODO (Santi): Remove when the RegisterAttendEvent POST is available.
 0322        WebInterface.OpenURL(string.Format(EVENT_DETAIL_URL, eventId));
 323
 324        // TODO (Santi): Waiting for the new version of the Events API where we will be able to send a signed POST to un
 325        //eventsAPIApiController.RegisterAttendEvent(
 326        //    eventId,
 327        //    false,
 328        //    () =>
 329        //    {
 330        //        // ...
 331        //    },
 332        //    (error) =>
 333        //    {
 334        //        Debug.LogError($"Error posting 'attend' message to the API: {error}");
 335        //    });
 0336    }
 337
 2338    internal void GoToEventsSubSection() { OnGoToEventsSubSection?.Invoke(); }
 339
 340    private void OnChannelToJoinChanged(string currentChannelId, string previousChannelId)
 341    {
 0342        if (!string.IsNullOrEmpty(currentChannelId))
 0343            return;
 344
 0345        view.HidePlaceModal();
 0346        view.HideEventModal();
 0347        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None);
 0348        OnCloseExploreV2?.Invoke();
 0349    }
 350}