| | 1 | | using DCL; |
| | 2 | | using ExploreV2Analytics; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | | using static HotScenesController; |
| | 8 | |
|
| | 9 | | public interface IPlacesSubSectionComponentController : 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 places from the API. |
| | 18 | | /// </summary> |
| | 19 | | void RequestAllPlaces(); |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Load the places with the last requested ones. |
| | 23 | | /// </summary> |
| | 24 | | void LoadPlaces(); |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Increment the number of places loaded. |
| | 28 | | /// </summary> |
| | 29 | | void ShowMorePlaces(); |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public class PlacesSubSectionComponentController : IPlacesSubSectionComponentController |
| | 33 | | { |
| | 34 | | public event Action OnCloseExploreV2; |
| | 35 | | internal event Action OnPlacesFromAPIUpdated; |
| | 36 | |
|
| | 37 | | internal const int INITIAL_NUMBER_OF_ROWS = 5; |
| | 38 | | internal const int SHOW_MORE_ROWS_INCREMENT = 3; |
| | 39 | | internal IPlacesSubSectionComponentView view; |
| | 40 | | internal IPlacesAPIController placesAPIApiController; |
| | 41 | | internal FriendTrackerController friendsTrackerController; |
| 18 | 42 | | internal List<HotSceneInfo> placesFromAPI = new List<HotSceneInfo>(); |
| | 43 | | internal int currentPlacesShowed = 0; |
| | 44 | | internal bool reloadPlaces = false; |
| | 45 | | internal IExploreV2Analytics exploreV2Analytics; |
| | 46 | |
|
| 18 | 47 | | public PlacesSubSectionComponentController( |
| | 48 | | IPlacesSubSectionComponentView view, |
| | 49 | | IPlacesAPIController placesAPI, |
| | 50 | | IFriendsController friendsController, |
| | 51 | | IExploreV2Analytics exploreV2Analytics) |
| | 52 | | { |
| 18 | 53 | | this.view = view; |
| 18 | 54 | | this.view.OnReady += FirstLoading; |
| 18 | 55 | | this.view.OnInfoClicked += ShowPlaceDetailedInfo; |
| 18 | 56 | | this.view.OnJumpInClicked += JumpInToPlace; |
| 18 | 57 | | this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded; |
| 18 | 58 | | this.view.OnShowMorePlacesClicked += ShowMorePlaces; |
| | 59 | |
|
| 18 | 60 | | placesAPIApiController = placesAPI; |
| 18 | 61 | | OnPlacesFromAPIUpdated += OnRequestedPlacesUpdated; |
| | 62 | |
|
| 18 | 63 | | friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors); |
| | 64 | |
|
| 18 | 65 | | this.exploreV2Analytics = exploreV2Analytics; |
| | 66 | |
|
| 18 | 67 | | view.ConfigurePools(); |
| 18 | 68 | | } |
| | 69 | |
|
| | 70 | | internal void FirstLoading() |
| | 71 | | { |
| 1 | 72 | | reloadPlaces = true; |
| 1 | 73 | | RequestAllPlaces(); |
| | 74 | |
|
| 1 | 75 | | view.OnPlacesSubSectionEnable += RequestAllPlaces; |
| 1 | 76 | | DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open; |
| 1 | 77 | | } |
| | 78 | |
|
| | 79 | | internal void OnExploreV2Open(bool current, bool previous) |
| | 80 | | { |
| 0 | 81 | | if (current) |
| 0 | 82 | | return; |
| | 83 | |
|
| 0 | 84 | | reloadPlaces = true; |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | public void RequestAllPlaces() |
| | 88 | | { |
| 2 | 89 | | if (!reloadPlaces) |
| 0 | 90 | | return; |
| | 91 | |
|
| 2 | 92 | | currentPlacesShowed = view.currentPlacesPerRow * INITIAL_NUMBER_OF_ROWS; |
| 2 | 93 | | view.RestartScrollViewPosition(); |
| 2 | 94 | | view.SetPlacesAsLoading(true); |
| 2 | 95 | | view.SetShowMorePlacesButtonActive(false); |
| 2 | 96 | | reloadPlaces = false; |
| | 97 | |
|
| 2 | 98 | | if (!DataStore.i.exploreV2.isInShowAnimationTransiton.Get()) |
| 2 | 99 | | RequestAllPlacesFromAPI(); |
| | 100 | | else |
| 0 | 101 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange += IsInShowAnimationTransitonChanged; |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | internal void IsInShowAnimationTransitonChanged(bool current, bool previous) |
| | 105 | | { |
| 0 | 106 | | DataStore.i.exploreV2.isInShowAnimationTransiton.OnChange -= IsInShowAnimationTransitonChanged; |
| 0 | 107 | | RequestAllPlacesFromAPI(); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | internal void RequestAllPlacesFromAPI() |
| | 111 | | { |
| 3 | 112 | | placesAPIApiController.GetAllPlaces( |
| | 113 | | (placeList) => |
| | 114 | | { |
| 0 | 115 | | placesFromAPI = placeList; |
| 0 | 116 | | OnPlacesFromAPIUpdated?.Invoke(); |
| 0 | 117 | | }); |
| 3 | 118 | | } |
| | 119 | |
|
| 2 | 120 | | internal void OnRequestedPlacesUpdated() { LoadPlaces(); } |
| | 121 | |
|
| | 122 | | public void LoadPlaces() |
| | 123 | | { |
| 2 | 124 | | friendsTrackerController.RemoveAllHandlers(); |
| | 125 | |
|
| 2 | 126 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| 2 | 127 | | List<HotSceneInfo> placesFiltered = placesFromAPI.Take(currentPlacesShowed).ToList(); |
| 4 | 128 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 129 | | { |
| 0 | 130 | | PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 0 | 131 | | places.Add(placeCardModel); |
| | 132 | | } |
| | 133 | |
|
| 2 | 134 | | view.SetPlaces(places); |
| 2 | 135 | | view.SetShowMorePlacesButtonActive(currentPlacesShowed < placesFromAPI.Count); |
| 2 | 136 | | view.SetPlacesAsLoading(false); |
| 2 | 137 | | } |
| | 138 | |
|
| | 139 | | public void ShowMorePlaces() |
| | 140 | | { |
| 2 | 141 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| 2 | 142 | | List<HotSceneInfo> placesFiltered = new List<HotSceneInfo>(); |
| 2 | 143 | | int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)currentPlacesShowed / view.currentPlacesPerRow) * view.cur |
| 2 | 144 | | int numberOfItemsToAdd = view.currentPlacesPerRow * SHOW_MORE_ROWS_INCREMENT + numberOfExtraItemsToAdd; |
| | 145 | |
|
| 2 | 146 | | if (currentPlacesShowed + numberOfItemsToAdd <= placesFromAPI.Count) |
| 2 | 147 | | placesFiltered = placesFromAPI.GetRange(currentPlacesShowed, numberOfItemsToAdd); |
| | 148 | | else |
| 0 | 149 | | placesFiltered = placesFromAPI.GetRange(currentPlacesShowed, placesFromAPI.Count - currentPlacesShowed); |
| | 150 | |
|
| 4 | 151 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 152 | | { |
| 0 | 153 | | PlaceCardComponentModel placeCardModel = ExplorePlacesUtils.CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 0 | 154 | | places.Add(placeCardModel); |
| | 155 | | } |
| | 156 | |
|
| 2 | 157 | | view.AddPlaces(places); |
| | 158 | |
|
| 2 | 159 | | currentPlacesShowed += numberOfItemsToAdd; |
| 2 | 160 | | if (currentPlacesShowed > placesFromAPI.Count) |
| 0 | 161 | | currentPlacesShowed = placesFromAPI.Count; |
| | 162 | |
|
| 2 | 163 | | view.SetShowMorePlacesButtonActive(currentPlacesShowed < placesFromAPI.Count); |
| 2 | 164 | | } |
| | 165 | |
|
| | 166 | | public void Dispose() |
| | 167 | | { |
| 18 | 168 | | view.OnReady -= FirstLoading; |
| 18 | 169 | | view.OnInfoClicked -= ShowPlaceDetailedInfo; |
| 18 | 170 | | view.OnJumpInClicked -= JumpInToPlace; |
| 18 | 171 | | view.OnPlacesSubSectionEnable -= RequestAllPlaces; |
| 18 | 172 | | view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded; |
| 18 | 173 | | view.OnShowMorePlacesClicked -= ShowMorePlaces; |
| 18 | 174 | | OnPlacesFromAPIUpdated -= OnRequestedPlacesUpdated; |
| 18 | 175 | | DataStore.i.exploreV2.isOpen.OnChange -= OnExploreV2Open; |
| 18 | 176 | | } |
| | 177 | |
|
| | 178 | | internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel) |
| | 179 | | { |
| 1 | 180 | | view.ShowPlaceModal(placeModel); |
| 1 | 181 | | exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName); |
| 1 | 182 | | } |
| | 183 | |
|
| | 184 | | internal void JumpInToPlace(HotSceneInfo placeFromAPI) |
| | 185 | | { |
| 1 | 186 | | ExplorePlacesUtils.JumpInToPlace(placeFromAPI); |
| 1 | 187 | | view.HidePlaceModal(); |
| 1 | 188 | | OnCloseExploreV2?.Invoke(); |
| 1 | 189 | | exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords); |
| 1 | 190 | | } |
| | 191 | |
|
| 0 | 192 | | internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends |
| | 193 | | } |