| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | | using MainScripts.DCL.Controllers.HotScenes; |
| | 10 | | using TMPro; |
| | 11 | | using UnityEngine.EventSystems; |
| | 12 | |
|
| | 13 | | public class PlacesSubSectionComponentView : BaseComponentView, IPlacesSubSectionComponentView |
| | 14 | | { |
| | 15 | | internal const string PLACE_CARDS_POOL_NAME = "Places_PlaceCardsPool"; |
| | 16 | | private const int PLACE_CARDS_POOL_PREWARM = 20; |
| | 17 | | private const string MOST_ACTIVE_FILTER_ID = "most_active"; |
| | 18 | | private const string MOST_ACTIVE_FILTER_TEXT = "Most active"; |
| | 19 | | private const string BEST_FILTER_ID = "like_rate"; |
| | 20 | | private const string BEST_FILTER_TEXT = "Best"; |
| | 21 | |
|
| 57 | 22 | | private readonly CancellationTokenSource disposeCts = new (); |
| 57 | 23 | | private CancellationTokenSource setPlacesCts = new (); |
| 57 | 24 | | private readonly List<PlaceCategoryButton> placeCategoryButtons = new (); |
| | 25 | |
|
| | 26 | | private List<string> poiCoords; |
| | 27 | |
|
| | 28 | | [Header("Assets References")] |
| | 29 | | [SerializeField] internal PlaceCardComponentView placeCardPrefab; |
| | 30 | | [SerializeField] internal PlaceCardComponentView placeCardModalPrefab; |
| | 31 | | [SerializeField] internal PlaceCategoryButton placeCategoryButtonPrefab; |
| | 32 | |
|
| | 33 | | [Header("Prefab References")] |
| | 34 | | [SerializeField] internal ScrollRect scrollView; |
| | 35 | | [SerializeField] internal GridContainerComponentView places; |
| | 36 | | [SerializeField] internal GameObject placesLoading; |
| | 37 | | [SerializeField] internal GameObject placesNoDataContainer; |
| | 38 | | [SerializeField] internal Color[] friendColors = null; |
| | 39 | | [SerializeField] internal GameObject showMorePlacesButtonContainer; |
| | 40 | | [SerializeField] internal ButtonComponentView showMorePlacesButton; |
| | 41 | | [SerializeField] internal DropdownComponentView sortByDropdown; |
| | 42 | | [SerializeField] internal Transform placeCategoriesGrid; |
| | 43 | | [SerializeField] internal TMP_Text totalResultsCounter; |
| | 44 | |
|
| | 45 | | [SerializeField] private Canvas canvas; |
| | 46 | |
|
| | 47 | | internal PlaceCardComponentView placeModal; |
| | 48 | | internal Pool placeCardsPool; |
| | 49 | | private Canvas placesCanvas; |
| | 50 | |
|
| 0 | 51 | | public Color[] currentFriendColors => friendColors; |
| | 52 | |
|
| 0 | 53 | | public int currentPlacesPerRow => places.currentItemsPerRow; |
| | 54 | |
|
| 0 | 55 | | public void SetAllAsLoading() => SetPlacesAsLoading(true); |
| 0 | 56 | | public void SetShowMoreButtonActive(bool isActive) => SetShowMorePlacesButtonActive(isActive); |
| | 57 | |
|
| | 58 | | public void SetPOICoords(List<string> poiList) => |
| 0 | 59 | | poiCoords = poiList; |
| | 60 | |
|
| 0 | 61 | | public int CurrentTilesPerRow => currentPlacesPerRow; |
| 0 | 62 | | public int CurrentGoingTilesPerRow { get; } |
| 149 | 63 | | public string filter { get; private set; } |
| 149 | 64 | | public string sort { get; private set; } |
| | 65 | |
|
| | 66 | | public event Action OnReady; |
| | 67 | | public event Action<PlaceCardComponentModel> OnInfoClicked; |
| | 68 | | public event Action<IHotScenesController.PlaceInfo> OnJumpInClicked; |
| | 69 | | public event Action<string, bool?> OnVoteChanged; |
| | 70 | | public event Action<string, bool> OnFavoriteClicked; |
| | 71 | | public event Action<IFriendTrackerHandler> OnFriendHandlerAdded; |
| | 72 | | public event Action OnPlacesSubSectionEnable; |
| | 73 | | public event Action OnFilterChanged; |
| | 74 | | public event Action OnSortingChanged; |
| | 75 | | public event Action OnShowMorePlacesClicked; |
| | 76 | |
|
| | 77 | | public override void Awake() |
| | 78 | | { |
| 54 | 79 | | base.Awake(); |
| 54 | 80 | | placesCanvas = places.GetComponent<Canvas>(); |
| 54 | 81 | | } |
| | 82 | |
|
| | 83 | | public void Start() |
| | 84 | | { |
| 53 | 85 | | placeModal = PlacesAndEventsCardsFactory.GetPlaceCardTemplateHiddenLazy(placeCardModalPrefab); |
| | 86 | |
|
| 53 | 87 | | places.RemoveItems(); |
| 53 | 88 | | LoadSortByDropdown(); |
| | 89 | |
|
| 53 | 90 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| 53 | 91 | | showMorePlacesButton.onClick.AddListener(() => OnShowMorePlacesClicked?.Invoke()); |
| 53 | 92 | | sortByDropdown.OnOptionSelectionChanged += SortByDropdownValueChanged; |
| 53 | 93 | | filter = ""; |
| 53 | 94 | | SetSortDropdownValue(MOST_ACTIVE_FILTER_ID, MOST_ACTIVE_FILTER_TEXT, false); |
| 53 | 95 | | OnReady?.Invoke(); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | private void LoadSortByDropdown() |
| | 99 | | { |
| 53 | 100 | | List<ToggleComponentModel> sortingMethodsToAdd = new List<ToggleComponentModel> |
| | 101 | | { |
| | 102 | | new () { id = MOST_ACTIVE_FILTER_ID, text = MOST_ACTIVE_FILTER_TEXT, isOn = true, isTextActive = true, chang |
| | 103 | | new () { id = BEST_FILTER_ID, text = BEST_FILTER_TEXT, isOn = false, isTextActive = true, changeTextColorOnS |
| | 104 | | }; |
| | 105 | |
|
| 53 | 106 | | sortByDropdown.SetTitle(sortingMethodsToAdd[0].text); |
| 53 | 107 | | sortByDropdown.SetOptions(sortingMethodsToAdd); |
| 53 | 108 | | } |
| | 109 | |
|
| | 110 | | private void SortByDropdownValueChanged(bool isOn, string optionId, string optionName) |
| | 111 | | { |
| 0 | 112 | | if (!isOn) |
| 0 | 113 | | return; |
| | 114 | |
|
| 0 | 115 | | SetSortDropdownValue(optionId, optionName, false); |
| 0 | 116 | | OnSortingChanged?.Invoke(); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public override void OnEnable() |
| | 120 | | { |
| 192 | 121 | | foreach (PlaceCategoryButton categoryBtn in placeCategoryButtons) |
| 0 | 122 | | categoryBtn.SetStatus(false); |
| | 123 | |
|
| 96 | 124 | | filter = ""; |
| 96 | 125 | | SetSortDropdownValue(MOST_ACTIVE_FILTER_ID, MOST_ACTIVE_FILTER_TEXT, false); |
| 96 | 126 | | OnPlacesSubSectionEnable?.Invoke(); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | public override void Dispose() |
| | 130 | | { |
| 116 | 131 | | base.Dispose(); |
| 116 | 132 | | disposeCts?.SafeCancelAndDispose(); |
| 116 | 133 | | setPlacesCts?.SafeCancelAndDispose(); |
| | 134 | |
|
| 116 | 135 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| | 136 | |
|
| 116 | 137 | | sortByDropdown.OnOptionSelectionChanged -= SortByDropdownValueChanged; |
| | 138 | |
|
| 116 | 139 | | places.Dispose(); |
| | 140 | |
|
| 116 | 141 | | if (placeModal != null) |
| | 142 | | { |
| 76 | 143 | | placeModal.Dispose(); |
| 76 | 144 | | Destroy(placeModal.gameObject); |
| | 145 | | } |
| 116 | 146 | | } |
| | 147 | |
|
| | 148 | | public void ConfigurePools() => |
| 17 | 149 | | placeCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(PLACE_CARDS_POOL_NAME, placeCardPrefab, PLACE_CARD |
| | 150 | |
|
| | 151 | | public override void RefreshControl() => |
| 0 | 152 | | places.RefreshControl(); |
| | 153 | |
|
| | 154 | | public void SetPlaces(List<PlaceCardComponentModel> places) |
| | 155 | | { |
| 4 | 156 | | SetPlacesAsLoading(false); |
| 4 | 157 | | placesNoDataContainer.SetActive(places.Count == 0); |
| | 158 | |
|
| 4 | 159 | | placeCardsPool.ReleaseAll(); |
| | 160 | |
|
| 4 | 161 | | this.places.ExtractItems(); |
| 4 | 162 | | this.places.RemoveItems(); |
| | 163 | |
|
| 4 | 164 | | setPlacesCts?.SafeCancelAndDispose(); |
| 4 | 165 | | setPlacesCts = CancellationTokenSource.CreateLinkedTokenSource(disposeCts.Token); |
| 4 | 166 | | AddPlacesAsync(places, setPlacesCts.Token).Forget(); |
| 4 | 167 | | } |
| | 168 | |
|
| | 169 | | public void SetPlacesAsLoading(bool isVisible) |
| | 170 | | { |
| 6 | 171 | | placesCanvas.enabled = !isVisible; |
| | 172 | |
|
| 6 | 173 | | placesLoading.SetActive(isVisible); |
| | 174 | |
|
| 6 | 175 | | if (isVisible) |
| 1 | 176 | | placesNoDataContainer.SetActive(false); |
| 6 | 177 | | } |
| | 178 | |
|
| | 179 | | public void AddPlaces(List<PlaceCardComponentModel> places) => |
| 4 | 180 | | AddPlacesAsync(places, disposeCts.Token).Forget(); |
| | 181 | |
|
| | 182 | | private async UniTask AddPlacesAsync(List<PlaceCardComponentModel> places, CancellationToken cancellationToken) |
| | 183 | | { |
| 58 | 184 | | foreach (PlaceCardComponentModel place in places) |
| | 185 | | { |
| 24 | 186 | | PlaceCardComponentView placeCard = PlacesAndEventsCardsFactory.CreateConfiguredPlaceCard(placeCardsPool, pla |
| | 187 | |
|
| 24 | 188 | | var isPoi = false; |
| | 189 | |
|
| 24 | 190 | | if (poiCoords != null) |
| | 191 | | { |
| 0 | 192 | | foreach (Vector2Int placeParcel in place.parcels) |
| | 193 | | { |
| 0 | 194 | | if (!poiCoords.Contains($"{placeParcel.x},{placeParcel.y}")) |
| | 195 | | continue; |
| | 196 | |
|
| 0 | 197 | | isPoi = true; |
| 0 | 198 | | break; |
| | 199 | | } |
| | 200 | | } |
| | 201 | |
|
| 24 | 202 | | placeCard.SetIsPOI(isPoi); |
| | 203 | |
|
| 24 | 204 | | OnFriendHandlerAdded?.Invoke(placeCard.friendsHandler); |
| | 205 | |
|
| 24 | 206 | | this.places.AddItem(placeCard); |
| | 207 | |
|
| 72 | 208 | | await UniTask.NextFrame(cancellationToken); |
| | 209 | | } |
| | 210 | |
|
| 2 | 211 | | this.places.SetItemSizeForModel(); |
| 2 | 212 | | await placeCardsPool.PrewarmAsync(places.Count, cancellationToken); |
| 2 | 213 | | } |
| | 214 | |
|
| | 215 | | public void SetActive(bool isActive) |
| | 216 | | { |
| 124 | 217 | | if (canvas.enabled == isActive) |
| 10 | 218 | | return; |
| 114 | 219 | | canvas.enabled = isActive; |
| | 220 | |
|
| 114 | 221 | | if (isActive) |
| 42 | 222 | | OnEnable(); |
| | 223 | | else |
| 72 | 224 | | OnDisable(); |
| 72 | 225 | | } |
| | 226 | |
|
| | 227 | | public void SetShowMorePlacesButtonActive(bool isActive) => |
| 2 | 228 | | showMorePlacesButtonContainer.gameObject.SetActive(isActive); |
| | 229 | |
|
| | 230 | | public void ShowPlaceModal(PlaceCardComponentModel placeInfo) |
| | 231 | | { |
| 1 | 232 | | placeModal.Show(); |
| 1 | 233 | | PlacesCardsConfigurator.Configure(placeModal, placeInfo, OnInfoClicked, OnJumpInClicked, OnVoteChanged, OnFavori |
| 1 | 234 | | } |
| | 235 | |
|
| | 236 | | public void HidePlaceModal() |
| | 237 | | { |
| 1 | 238 | | if (placeModal != null) |
| 1 | 239 | | placeModal.Hide(); |
| 1 | 240 | | } |
| | 241 | |
|
| | 242 | | public void RestartScrollViewPosition() => |
| 0 | 243 | | scrollView.verticalNormalizedPosition = 1; |
| | 244 | |
|
| | 245 | | private void SetSortDropdownValue(string id, string title, bool notify) |
| | 246 | | { |
| 149 | 247 | | sort = id; |
| 149 | 248 | | sortByDropdown.SetTitle(title); |
| 149 | 249 | | sortByDropdown.SelectOption(id, notify); |
| 149 | 250 | | } |
| | 251 | |
|
| | 252 | | private static void DeselectButtons() |
| | 253 | | { |
| 0 | 254 | | if (EventSystem.current == null) |
| 0 | 255 | | return; |
| | 256 | |
|
| 0 | 257 | | EventSystem.current.SetSelectedGameObject(null); |
| 0 | 258 | | } |
| | 259 | |
|
| | 260 | | public void SetPlaceCategories(List<(string id, string nameToShow)> placeCategories) |
| | 261 | | { |
| 0 | 262 | | foreach (var category in placeCategories) |
| | 263 | | { |
| 0 | 264 | | var categoryButton = Instantiate(placeCategoryButtonPrefab, placeCategoriesGrid); |
| 0 | 265 | | categoryButton.SetCategory(category.id, category.nameToShow); |
| 0 | 266 | | categoryButton.SetStatus(false); |
| 0 | 267 | | categoryButton.OnClick += OnCategoryButtonClicked; |
| 0 | 268 | | placeCategoryButtons.Add(categoryButton); |
| | 269 | | } |
| | 270 | |
|
| 0 | 271 | | placeModal.SetAllPlaceCategories(placeCategories); |
| 0 | 272 | | } |
| | 273 | |
|
| | 274 | | public void SetResultCounter(int totalResults) => |
| 0 | 275 | | totalResultsCounter.text = $"Result ({totalResults})"; |
| | 276 | |
|
| | 277 | | private void OnCategoryButtonClicked(string category, bool isSelected) |
| | 278 | | { |
| 0 | 279 | | foreach (PlaceCategoryButton categoryBtn in placeCategoryButtons) |
| 0 | 280 | | categoryBtn.SetStatus(category == categoryBtn.currenCategory && isSelected); |
| | 281 | |
|
| 0 | 282 | | if (isSelected) |
| | 283 | | { |
| 0 | 284 | | filter = $"categories={category}"; |
| 0 | 285 | | SetSortDropdownValue(BEST_FILTER_ID, BEST_FILTER_TEXT, false); |
| | 286 | | } |
| | 287 | | else |
| | 288 | | { |
| 0 | 289 | | filter = ""; |
| 0 | 290 | | SetSortDropdownValue(MOST_ACTIVE_FILTER_ID, MOST_ACTIVE_FILTER_TEXT, false); |
| | 291 | | } |
| | 292 | |
|
| 0 | 293 | | OnFilterChanged?.Invoke(); |
| 0 | 294 | | } |
| | 295 | | } |