| | 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 WorldsSubSectionComponentView : BaseComponentView, IWorldsSubSectionComponentView, IPointerClickHandler |
| | 14 | | { |
| | 15 | | private const string WORLD_CARDS_POOL_NAME = "Worlds_WorldCardsPool"; |
| | 16 | | private const int WORLD_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 | |
|
| 52 | 22 | | private readonly CancellationTokenSource disposeCts = new (); |
| 52 | 23 | | private CancellationTokenSource setWorldsCts = new (); |
| | 24 | |
|
| | 25 | | private List<string> poiCoords; |
| | 26 | |
|
| | 27 | | [Header("Assets References")] |
| | 28 | | [SerializeField] internal PlaceCardComponentView worldCardPrefab; |
| | 29 | | [SerializeField] internal PlaceCardComponentView worldCardModalPrefab; |
| | 30 | |
|
| | 31 | | [Header("Prefab References")] |
| | 32 | | [SerializeField] internal ScrollRect scrollView; |
| | 33 | | [SerializeField] internal GridContainerComponentView worlds; |
| | 34 | | [SerializeField] internal GameObject worldsLoading; |
| | 35 | | [SerializeField] internal GameObject worldsNoDataContainer; |
| | 36 | | [SerializeField] internal Color[] friendColors = null; |
| | 37 | | [SerializeField] internal GameObject showMoreWorldsButtonContainer; |
| | 38 | | [SerializeField] internal ButtonComponentView showMoreWorldsButton; |
| | 39 | | [SerializeField] internal DropdownComponentView sortByDropdown; |
| | 40 | | [SerializeField] internal Button findOutMoreButton; |
| | 41 | | [SerializeField] internal TMP_Text textComponent; |
| | 42 | |
|
| | 43 | | [SerializeField] private Canvas canvas; |
| | 44 | |
|
| | 45 | | internal PlaceCardComponentView worldModal; |
| | 46 | | internal Pool worldCardsPool; |
| | 47 | | private Canvas worldsCanvas; |
| | 48 | |
|
| 0 | 49 | | public Color[] currentFriendColors => friendColors; |
| | 50 | |
|
| 0 | 51 | | public int currentWorldsPerRow => worlds.currentItemsPerRow; |
| | 52 | |
|
| 0 | 53 | | public void SetAllAsLoading() => SetWorldsAsLoading(true); |
| 0 | 54 | | public void SetShowMoreButtonActive(bool isActive) => SetShowMoreWorldsButtonActive(isActive); |
| | 55 | |
|
| | 56 | | public void SetPOICoords(List<string> poiList) => |
| 0 | 57 | | poiCoords = poiList; |
| | 58 | |
|
| 0 | 59 | | public int CurrentTilesPerRow => currentWorldsPerRow; |
| 97 | 60 | | public string filter { get; private set; } |
| 97 | 61 | | public string sort { get; private set; } |
| | 62 | |
|
| | 63 | | public event Action OnReady; |
| | 64 | | public event Action<PlaceCardComponentModel> OnInfoClicked; |
| | 65 | | public event Action<IHotScenesController.PlaceInfo> OnJumpInClicked; |
| | 66 | | public event Action<string, bool?> OnVoteChanged; |
| | 67 | | public event Action<string, bool> OnFavoriteClicked; |
| | 68 | | public event Action<IFriendTrackerHandler> OnFriendHandlerAdded; |
| | 69 | | public event Action OnWorldsSubSectionEnable; |
| | 70 | | public event Action OnSortingChanged; |
| | 71 | | public event Action OnShowMoreWorldsClicked; |
| | 72 | | public event Action OnOpenWorldsInfo; |
| | 73 | | public event Action OnOpenWorldsDaoProposal; |
| | 74 | |
|
| | 75 | | public override void Awake() |
| | 76 | | { |
| 49 | 77 | | base.Awake(); |
| 49 | 78 | | worldsCanvas = worlds.GetComponent<Canvas>(); |
| 49 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Start() |
| | 82 | | { |
| 48 | 83 | | worldModal = PlacesAndEventsCardsFactory.GetWorldCardTemplateHiddenLazy(worldCardModalPrefab); |
| | 84 | |
|
| 48 | 85 | | worlds.RemoveItems(); |
| 48 | 86 | | LoadSortByDropdown(); |
| 48 | 87 | | findOutMoreButton.onClick.RemoveAllListeners(); |
| 48 | 88 | | findOutMoreButton.onClick.AddListener(()=>OnOpenWorldsInfo?.Invoke()); |
| | 89 | |
|
| 48 | 90 | | showMoreWorldsButton.onClick.RemoveAllListeners(); |
| 48 | 91 | | showMoreWorldsButton.onClick.AddListener(() => OnShowMoreWorldsClicked?.Invoke()); |
| 48 | 92 | | sortByDropdown.OnOptionSelectionChanged += SortByDropdownValueChanged; |
| 48 | 93 | | filter = ""; |
| 48 | 94 | | SetSortDropdownValue(MOST_ACTIVE_FILTER_ID, MOST_ACTIVE_FILTER_TEXT, false); |
| 48 | 95 | | OnReady?.Invoke(); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | private void LoadSortByDropdown() |
| | 99 | | { |
| 48 | 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 | |
|
| 48 | 106 | | sortByDropdown.SetTitle(sortingMethodsToAdd[0].text); |
| 48 | 107 | | sortByDropdown.SetOptions(sortingMethodsToAdd); |
| 48 | 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 | | { |
| 49 | 121 | | base.OnEnable(); |
| | 122 | |
|
| 49 | 123 | | filter = ""; |
| 49 | 124 | | SetSortDropdownValue(MOST_ACTIVE_FILTER_ID, MOST_ACTIVE_FILTER_TEXT, false); |
| 49 | 125 | | OnWorldsSubSectionEnable?.Invoke(); |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | public override void Dispose() |
| | 129 | | { |
| 106 | 130 | | base.Dispose(); |
| 106 | 131 | | disposeCts?.SafeCancelAndDispose(); |
| 106 | 132 | | setWorldsCts?.SafeCancelAndDispose(); |
| | 133 | |
|
| 106 | 134 | | showMoreWorldsButton.onClick.RemoveAllListeners(); |
| | 135 | |
|
| 106 | 136 | | sortByDropdown.OnOptionSelectionChanged -= SortByDropdownValueChanged; |
| | 137 | |
|
| 106 | 138 | | worlds.Dispose(); |
| | 139 | |
|
| 106 | 140 | | if (worldModal != null) |
| | 141 | | { |
| 70 | 142 | | worldModal.Dispose(); |
| 70 | 143 | | Destroy(worldModal.gameObject); |
| | 144 | | } |
| 106 | 145 | | } |
| | 146 | |
|
| | 147 | | public void ConfigurePools() => |
| 12 | 148 | | worldCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(WORLD_CARDS_POOL_NAME, worldCardPrefab, WORLD_CARD |
| | 149 | |
|
| | 150 | | public override void RefreshControl() => |
| 0 | 151 | | worlds.RefreshControl(); |
| | 152 | |
|
| | 153 | | public void SetWorlds(List<PlaceCardComponentModel> worlds) |
| | 154 | | { |
| 4 | 155 | | SetWorldsAsLoading(false); |
| 4 | 156 | | worldsNoDataContainer.SetActive(worlds.Count == 0); |
| | 157 | |
|
| 4 | 158 | | worldCardsPool.ReleaseAll(); |
| | 159 | |
|
| 4 | 160 | | this.worlds.ExtractItems(); |
| 4 | 161 | | this.worlds.RemoveItems(); |
| | 162 | |
|
| 4 | 163 | | setWorldsCts?.SafeCancelAndDispose(); |
| 4 | 164 | | setWorldsCts = CancellationTokenSource.CreateLinkedTokenSource(disposeCts.Token); |
| 4 | 165 | | AddWorldsAsync(worlds, setWorldsCts.Token).Forget(); |
| 4 | 166 | | } |
| | 167 | |
|
| | 168 | | public void SetWorldsAsLoading(bool isVisible) |
| | 169 | | { |
| 6 | 170 | | worldsCanvas.enabled = !isVisible; |
| | 171 | |
|
| 6 | 172 | | worldsLoading.SetActive(isVisible); |
| | 173 | |
|
| 6 | 174 | | if (isVisible) |
| 1 | 175 | | worldsNoDataContainer.SetActive(false); |
| 6 | 176 | | } |
| | 177 | |
|
| | 178 | | public void AddWorlds(List<PlaceCardComponentModel> worlds) => |
| 4 | 179 | | AddWorldsAsync(worlds, disposeCts.Token).Forget(); |
| | 180 | |
|
| | 181 | | private async UniTask AddWorldsAsync(List<PlaceCardComponentModel> worlds, CancellationToken cancellationToken) |
| | 182 | | { |
| 58 | 183 | | foreach (PlaceCardComponentModel world in worlds) |
| | 184 | | { |
| 24 | 185 | | PlaceCardComponentView worldCard = PlacesAndEventsCardsFactory.CreateConfiguredPlaceCard(worldCardsPool, wor |
| | 186 | |
|
| 24 | 187 | | var isPoi = false; |
| | 188 | |
|
| 24 | 189 | | if (poiCoords != null) |
| | 190 | | { |
| 0 | 191 | | foreach (Vector2Int worldParcel in world.parcels) |
| | 192 | | { |
| 0 | 193 | | if (!poiCoords.Contains($"{worldParcel.x},{worldParcel.y}")) |
| | 194 | | continue; |
| | 195 | |
|
| 0 | 196 | | isPoi = true; |
| 0 | 197 | | break; |
| | 198 | | } |
| | 199 | | } |
| | 200 | |
|
| 24 | 201 | | worldCard.SetIsPOI(isPoi); |
| | 202 | |
|
| 24 | 203 | | OnFriendHandlerAdded?.Invoke(worldCard.friendsHandler); |
| | 204 | |
|
| 24 | 205 | | this.worlds.AddItem(worldCard); |
| | 206 | |
|
| 72 | 207 | | await UniTask.NextFrame(cancellationToken); |
| | 208 | | } |
| | 209 | |
|
| 2 | 210 | | this.worlds.SetItemSizeForModel(); |
| 2 | 211 | | await worldCardsPool.PrewarmAsync(worlds.Count, cancellationToken); |
| 2 | 212 | | } |
| | 213 | |
|
| | 214 | | public void SetActive(bool isActive) |
| | 215 | | { |
| 78 | 216 | | if (canvas.enabled == isActive) |
| 42 | 217 | | return; |
| 36 | 218 | | canvas.enabled = isActive; |
| | 219 | |
|
| 36 | 220 | | if (isActive) |
| 0 | 221 | | OnEnable(); |
| | 222 | | else |
| 36 | 223 | | OnDisable(); |
| 36 | 224 | | } |
| | 225 | |
|
| | 226 | | public void SetShowMoreWorldsButtonActive(bool isActive) => |
| 2 | 227 | | showMoreWorldsButtonContainer.gameObject.SetActive(isActive); |
| | 228 | |
|
| | 229 | | public void ShowWorldModal(PlaceCardComponentModel worldInfo) |
| | 230 | | { |
| 0 | 231 | | worldModal.Show(); |
| 0 | 232 | | PlacesCardsConfigurator.Configure(worldModal, worldInfo, OnInfoClicked, OnJumpInClicked, OnVoteChanged, OnFavori |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | public void HideWorldModal() |
| | 236 | | { |
| 0 | 237 | | if (worldModal != null) |
| 0 | 238 | | worldModal.Hide(); |
| 0 | 239 | | } |
| | 240 | |
|
| | 241 | | public void RestartScrollViewPosition() => |
| 0 | 242 | | scrollView.verticalNormalizedPosition = 1; |
| | 243 | |
|
| | 244 | | private void SetSortDropdownValue(string id, string title, bool notify) |
| | 245 | | { |
| 97 | 246 | | sort = id; |
| 97 | 247 | | sortByDropdown.SetTitle(title); |
| 97 | 248 | | sortByDropdown.SelectOption(id, notify); |
| 97 | 249 | | } |
| | 250 | |
|
| | 251 | | public void OnPointerClick(PointerEventData eventData) |
| | 252 | | { |
| 0 | 253 | | int linkIndex = TMP_TextUtilities.FindIntersectingLink(textComponent, eventData.position, textComponent.canvas.w |
| | 254 | |
|
| 0 | 255 | | if (linkIndex == -1) return; |
| | 256 | |
|
| 0 | 257 | | TMP_LinkInfo linkInfo = textComponent.textInfo.linkInfo[linkIndex]; |
| | 258 | |
|
| 0 | 259 | | if (linkInfo.GetLinkID() == "dao") |
| 0 | 260 | | OnOpenWorldsDaoProposal?.Invoke(); |
| 0 | 261 | | } |
| | 262 | | } |