< Summary

Class:PlacesSubSectionComponentController
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/PlacesSubSection/PlacesSubSectionMenu/PlacesSubSectionComponentController.cs
Covered lines:92
Uncovered lines:49
Coverable lines:141
Total lines:298
Line coverage:65.2% (92 of 141)
Covered branches:0
Total branches:0
Covered methods:10
Total methods:19
Method coverage:52.6% (10 of 19)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlacesSubSectionComponentController(...)0%110100%
Dispose()0%550100%
View_OnVoteChanged(...)0%20400%
View_OnFavoritesClicked(...)0%12300%
FirstLoading()0%2100%
OpenTab()0%2100%
ApplyFilters()0%6200%
ApplySorting()0%12300%
RequestAllPlaces()0%220100%
RequestAllFromAPI()0%220100%
RequestPlaceCategoriesAsync()0%20400%
RequestAllFromAPIAsync()0%17.869052.17%
ShowMorePlaces()0%220100%
ShowMorePlacesAsync()0%3.213071.43%
ShowPlaceDetailedInfo(...)0%110100%
OnJumpInToPlace(...)0%220100%
View_OnFriendHandlerAdded(...)0%2100%
OnChannelToJoinChanged(...)0%12300%
JumpInToPlace(...)0%660100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Sections/PlacesAndEventsSection/SubSections/PlacesSubSection/PlacesSubSectionMenu/PlacesSubSectionComponentController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Helpers;
 4using DCL.Social.Friends;
 5using DCL.Tasks;
 6using DCLServices.PlacesAPIService;
 7using ExploreV2Analytics;
 8using System;
 9using System.Collections.Generic;
 10using System.Linq;
 11using UnityEngine;
 12using System.Threading;
 13using Environment = DCL.Environment;
 14using static MainScripts.DCL.Controllers.HotScenes.IHotScenesController;
 15
 16public class PlacesSubSectionComponentController : IPlacesSubSectionComponentController, IPlacesAndEventsAPIRequester
 17{
 18    public event Action OnCloseExploreV2;
 19
 20    internal const int INITIAL_NUMBER_OF_ROWS = 4;
 21    private const int PAGE_SIZE = 12;
 22    private const string MOST_ACTIVE_FILTER_ID = "most_active";
 23
 24    internal readonly IPlacesSubSectionComponentView view;
 25    internal readonly IPlacesAPIService placesAPIService;
 26    internal readonly FriendTrackerController friendsTrackerController;
 27    private readonly IExploreV2Analytics exploreV2Analytics;
 28    private readonly IPlacesAnalytics placesAnalytics;
 29    private readonly DataStore dataStore;
 30    private readonly IUserProfileBridge userProfileBridge;
 31    private IReadOnlyList<string> allPointOfInterest;
 32
 33    internal readonly PlaceAndEventsCardsReloader cardsReloader;
 34
 1735    internal readonly List<PlaceInfo> placesFromAPI = new ();
 36    internal int availableUISlots;
 37    private CancellationTokenSource getCategoriesCts;
 1738    private CancellationTokenSource getPlacesCts = new ();
 1739    private CancellationTokenSource showMoreCts = new ();
 1740    private CancellationTokenSource disposeCts = new ();
 41
 1742    public PlacesSubSectionComponentController(
 43        IPlacesSubSectionComponentView view,
 44        IPlacesAPIService placesAPI,
 45        IFriendsController friendsController,
 46        IExploreV2Analytics exploreV2Analytics,
 47        IPlacesAnalytics placesAnalytics,
 48        DataStore dataStore,
 49        IUserProfileBridge userProfileBridge)
 50    {
 1751        cardsReloader = new PlaceAndEventsCardsReloader(view, this, dataStore.exploreV2);
 52
 1753        this.view = view;
 54
 1755        this.view.OnReady += FirstLoading;
 56
 1757        this.view.OnInfoClicked += ShowPlaceDetailedInfo;
 1758        this.view.OnJumpInClicked += OnJumpInToPlace;
 1759        this.view.OnFavoriteClicked += View_OnFavoritesClicked;
 1760        this.view.OnVoteChanged += View_OnVoteChanged;
 1761        this.view.OnShowMorePlacesClicked += ShowMorePlaces;
 62
 1763        this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded;
 64
 1765        this.dataStore = dataStore;
 1766        this.dataStore.channels.currentJoinChannelModal.OnChange += OnChannelToJoinChanged;
 67
 1768        placesAPIService = placesAPI;
 69
 1770        friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors);
 71
 1772        this.exploreV2Analytics = exploreV2Analytics;
 1773        this.placesAnalytics = placesAnalytics;
 74
 1775        this.userProfileBridge = userProfileBridge;
 76
 1777        view.ConfigurePools();
 1778    }
 79
 80    public void Dispose()
 81    {
 1782        disposeCts?.SafeCancelAndDispose();
 1783        showMoreCts?.SafeCancelAndDispose();
 1784        getCategoriesCts?.SafeCancelAndDispose();
 1785        getPlacesCts?.SafeCancelAndDispose();
 86
 1787        view.OnReady -= FirstLoading;
 1788        view.OnInfoClicked -= ShowPlaceDetailedInfo;
 1789        view.OnJumpInClicked -= OnJumpInToPlace;
 1790        view.OnFavoriteClicked -= View_OnFavoritesClicked;
 1791        this.view.OnVoteChanged -= View_OnVoteChanged;
 1792        view.OnPlacesSubSectionEnable -= OpenTab;
 1793        view.OnFilterChanged -= ApplyFilters;
 1794        view.OnSortingChanged -= ApplySorting;
 1795        view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded;
 1796        view.OnShowMorePlacesClicked -= ShowMorePlaces;
 97
 1798        dataStore.channels.currentJoinChannelModal.OnChange -= OnChannelToJoinChanged;
 99
 17100        cardsReloader.Dispose();
 17101    }
 102
 103    private void View_OnVoteChanged(string placeId, bool? isUpvote)
 104    {
 0105        if (userProfileBridge.GetOwn().isGuest)
 0106            dataStore.HUDs.connectWalletModalVisible.Set(true);
 107        else
 108        {
 0109            if (isUpvote != null)
 110            {
 0111                if (isUpvote.Value)
 0112                    placesAnalytics.Like(placeId, IPlacesAnalytics.ActionSource.FromExplore);
 113                else
 0114                    placesAnalytics.Dislike(placeId, IPlacesAnalytics.ActionSource.FromExplore);
 115            }
 116            else
 0117                placesAnalytics.RemoveVote(placeId, IPlacesAnalytics.ActionSource.FromExplore);
 118
 0119            placesAPIService.SetPlaceVote(isUpvote, placeId, disposeCts.Token);
 120        }
 0121    }
 122
 123    private void View_OnFavoritesClicked(string placeUUID, bool isFavorite)
 124    {
 0125        if (userProfileBridge.GetOwn().isGuest)
 0126            dataStore.HUDs.connectWalletModalVisible.Set(true);
 127        else
 128        {
 0129            if (isFavorite)
 0130                placesAnalytics.AddFavorite(placeUUID, IPlacesAnalytics.ActionSource.FromExplore);
 131            else
 0132                placesAnalytics.RemoveFavorite(placeUUID, IPlacesAnalytics.ActionSource.FromExplore);
 133
 0134            placesAPIService.SetPlaceFavorite(placeUUID, isFavorite, disposeCts.Token);
 135        }
 0136    }
 137
 138    private void FirstLoading()
 139    {
 0140        view.OnPlacesSubSectionEnable += OpenTab;
 0141        view.OnFilterChanged += ApplyFilters;
 0142        view.OnSortingChanged += ApplySorting;
 0143        cardsReloader.Initialize();
 144
 0145        getCategoriesCts = getCategoriesCts.SafeRestart();
 0146        RequestPlaceCategoriesAsync(getCategoriesCts.Token).Forget();
 0147    }
 148
 149    private void OpenTab()
 150    {
 0151        exploreV2Analytics.SendPlacesTabOpen();
 0152        RequestAllPlaces();
 0153    }
 154
 155    private void ApplyFilters()
 156    {
 0157        if (!string.IsNullOrEmpty(view.filter))
 0158            placesAnalytics.Filter(view.filter.Replace("categories=", ""));
 159
 0160        RequestAllPlaces();
 0161    }
 162
 163    private void ApplySorting()
 164    {
 0165        placesAnalytics.Sort(view.sort == MOST_ACTIVE_FILTER_ID ? IPlacesAnalytics.SortingType.MostActive : IPlacesAnaly
 0166        RequestAllPlaces();
 0167    }
 168
 169    internal void RequestAllPlaces()
 170    {
 2171        if (cardsReloader.CanReload())
 172        {
 2173            availableUISlots = view.CurrentTilesPerRow * INITIAL_NUMBER_OF_ROWS;
 2174            view.SetShowMoreButtonActive(false);
 175
 2176            cardsReloader.RequestAll();
 177        }
 2178    }
 179
 180    public void RequestAllFromAPI()
 181    {
 2182        getPlacesCts?.SafeCancelAndDispose();
 2183        getPlacesCts = CancellationTokenSource.CreateLinkedTokenSource(disposeCts.Token);
 184
 2185        RequestAllFromAPIAsync(getPlacesCts.Token).Forget();
 2186    }
 187
 188    private async UniTaskVoid RequestPlaceCategoriesAsync(CancellationToken ct)
 189    {
 190        try
 191        {
 0192            var allCategories = await placesAPIService.GetPlaceCategories(ct);
 0193            view.SetPlaceCategories(allCategories.Select(x => (x.name, x.i18n.en)).ToList());
 0194        }
 0195        catch (OperationCanceledException) { }
 0196    }
 197
 198    private async UniTaskVoid RequestAllFromAPIAsync(CancellationToken ct)
 199    {
 200        try
 201        {
 2202            allPointOfInterest = await placesAPIService.GetPointsOfInterestCoords(ct);
 203
 2204            if (allPointOfInterest != null)
 0205                view.SetPOICoords(allPointOfInterest.ToList());
 206
 2207            (IReadOnlyList<PlaceInfo> places, int total) firstPage = await placesAPIService.GetMostActivePlaces(0, PAGE_
 2208            friendsTrackerController.RemoveAllHandlers();
 2209            placesFromAPI.Clear();
 2210            placesFromAPI.AddRange(firstPage.places);
 2211            if (firstPage.total > PAGE_SIZE)
 212            {
 0213                (IReadOnlyList<PlaceInfo> places, int total) secondPage = await placesAPIService.GetMostActivePlaces(1, 
 0214                placesFromAPI.AddRange(secondPage.places);
 215            }
 216
 2217            view.SetPlaces(PlacesAndEventsCardsFactory.ConvertPlaceResponseToModel(placesFromAPI, availableUISlots));
 2218            view.SetShowMorePlacesButtonActive(placesFromAPI.Count < firstPage.total);
 2219            view.SetResultCounter(firstPage.total);
 2220        }
 0221        catch (OperationCanceledException) { }
 2222    }
 223
 224    internal void ShowMorePlaces()
 225    {
 2226        showMoreCts?.SafeCancelAndDispose();
 2227        showMoreCts = CancellationTokenSource.CreateLinkedTokenSource(disposeCts.Token);
 2228        ShowMorePlacesAsync(showMoreCts.Token).Forget();
 2229    }
 230
 231    private async UniTask ShowMorePlacesAsync(CancellationToken ct)
 232    {
 2233        (IReadOnlyList<PlaceInfo> places, int total) = await placesAPIService.GetMostActivePlaces((placesFromAPI.Count/P
 234
 2235        placesFromAPI.AddRange(places);
 2236        view.AddPlaces(PlacesAndEventsCardsFactory.ConvertPlaceResponseToModel(places));
 2237        view.SetShowMorePlacesButtonActive(placesFromAPI.Count < total);
 2238    }
 239
 240    internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel)
 241    {
 1242        view.ShowPlaceModal(placeModel);
 1243        exploreV2Analytics.SendClickOnPlaceInfo(placeModel.placeInfo.id, placeModel.placeName);
 244
 1245        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.Places);
 1246    }
 247
 248    internal void OnJumpInToPlace(PlaceInfo placeFromAPI)
 249    {
 1250        JumpInToPlace(placeFromAPI);
 1251        view.HidePlaceModal();
 252
 1253        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None);
 1254        OnCloseExploreV2?.Invoke();
 1255        exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.title, Utils.ConvertStringToVector(placeFromA
 1256    }
 257
 258    private void View_OnFriendHandlerAdded(IFriendTrackerHandler friendsHandler) =>
 0259        friendsTrackerController.AddHandler(friendsHandler);
 260
 261    private void OnChannelToJoinChanged(string currentChannelId, string previousChannelId)
 262    {
 0263        if (!string.IsNullOrEmpty(currentChannelId))
 0264            return;
 265
 0266        view.HidePlaceModal();
 0267        dataStore.exploreV2.currentVisibleModal.Set(ExploreV2CurrentModal.None);
 0268        OnCloseExploreV2?.Invoke();
 0269    }
 270
 271    /// <summary>
 272    /// Makes a jump in to the place defined by the given place data from API.
 273    /// </summary>
 274    /// <param name="placeFromAPI">Place data from API.</param>
 275    public static void JumpInToPlace(PlaceInfo placeFromAPI)
 276    {
 2277        PlaceInfo.Realm realm = new PlaceInfo.Realm() { layer = null, serverName = null };
 5278        placeFromAPI.realms_detail = placeFromAPI.realms_detail.OrderByDescending(x => x.usersCount).ToArray();
 279
 8280        for (int i = 0; i < placeFromAPI.realms_detail.Length; i++)
 281        {
 3282            bool isArchipelagoRealm = string.IsNullOrEmpty(placeFromAPI.realms_detail[i].layer);
 283
 3284            if (isArchipelagoRealm || placeFromAPI.realms_detail[i].usersCount < placeFromAPI.realms_detail[i].maxUsers)
 285            {
 1286                realm = placeFromAPI.realms_detail[i];
 1287                break;
 288            }
 289        }
 290
 2291        Vector2Int position = Utils.ConvertStringToVector(placeFromAPI.base_position);
 292
 2293        if (string.IsNullOrEmpty(realm.serverName))
 1294            Environment.i.world.teleportController.Teleport(position.x, position.y);
 295        else
 1296            Environment.i.world.teleportController.JumpIn(position.x, position.y, realm.serverName, realm.layer);
 1297    }
 298}