| | 1 | | using DCL; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | public class PlacesSubSectionComponentView : BaseComponentView, IPlacesSubSectionComponentView |
| | 11 | | { |
| | 12 | | internal const string PLACE_CARDS_POOL_NAME = "Places_PlaceCardsPool"; |
| | 13 | | private const int PLACE_CARDS_POOL_PREWARM = 20; |
| | 14 | |
|
| 60 | 15 | | private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); |
| | 16 | |
|
| | 17 | | [Header("Assets References")] |
| | 18 | | [SerializeField] internal PlaceCardComponentView placeCardPrefab; |
| | 19 | | [SerializeField] internal PlaceCardComponentView placeCardModalPrefab; |
| | 20 | |
|
| | 21 | | [Header("Prefab References")] |
| | 22 | | [SerializeField] internal ScrollRect scrollView; |
| | 23 | | [SerializeField] internal GridContainerComponentView places; |
| | 24 | | [SerializeField] internal GameObject placesLoading; |
| | 25 | | [SerializeField] internal TMP_Text placesNoDataText; |
| | 26 | | [SerializeField] internal Color[] friendColors = null; |
| | 27 | | [SerializeField] internal GameObject showMorePlacesButtonContainer; |
| | 28 | | [SerializeField] internal ButtonComponentView showMorePlacesButton; |
| | 29 | |
|
| | 30 | | [SerializeField] private Canvas canvas; |
| | 31 | |
|
| | 32 | | internal PlaceCardComponentView placeModal; |
| | 33 | | internal Pool placeCardsPool; |
| | 34 | | private Canvas placesCanvas; |
| | 35 | |
|
| 0 | 36 | | public Color[] currentFriendColors => friendColors; |
| | 37 | |
|
| 0 | 38 | | public int currentPlacesPerRow => places.currentItemsPerRow; |
| | 39 | |
|
| 0 | 40 | | public void SetAllAsLoading() => SetPlacesAsLoading(true); |
| 0 | 41 | | public void SetShowMoreButtonActive(bool isActive) => SetShowMorePlacesButtonActive(isActive); |
| 0 | 42 | | public int CurrentTilesPerRow => currentPlacesPerRow; |
| | 43 | |
|
| | 44 | | public event Action OnReady; |
| | 45 | | public event Action<PlaceCardComponentModel> OnInfoClicked; |
| | 46 | | public event Action<HotScenesController.HotSceneInfo> OnJumpInClicked; |
| | 47 | | public event Action<FriendsHandler> OnFriendHandlerAdded; |
| | 48 | | public event Action OnPlacesSubSectionEnable; |
| | 49 | | public event Action OnShowMorePlacesClicked; |
| | 50 | |
|
| | 51 | | public override void Awake() |
| | 52 | | { |
| 57 | 53 | | base.Awake(); |
| 57 | 54 | | placesCanvas = places.GetComponent<Canvas>(); |
| 57 | 55 | | } |
| | 56 | |
|
| | 57 | | public override void Start() |
| | 58 | | { |
| 53 | 59 | | placeModal = PlacesAndEventsCardsFactory.GetPlaceCardTemplateHiddenLazy(placeCardModalPrefab); |
| | 60 | |
|
| 53 | 61 | | places.RemoveItems(); |
| | 62 | |
|
| 53 | 63 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| 53 | 64 | | showMorePlacesButton.onClick.AddListener(() => OnShowMorePlacesClicked?.Invoke()); |
| | 65 | |
|
| 53 | 66 | | OnReady?.Invoke(); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public override void OnEnable() |
| | 70 | | { |
| 60 | 71 | | OnPlacesSubSectionEnable?.Invoke(); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public override void Dispose() |
| | 75 | | { |
| 125 | 76 | | base.Dispose(); |
| 125 | 77 | | cancellationTokenSource.Cancel(); |
| | 78 | |
|
| 125 | 79 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| | 80 | |
|
| 125 | 81 | | places.Dispose(); |
| | 82 | |
|
| 125 | 83 | | if (placeModal != null) |
| | 84 | | { |
| 76 | 85 | | placeModal.Dispose(); |
| 76 | 86 | | Destroy(placeModal.gameObject); |
| | 87 | | } |
| 125 | 88 | | } |
| | 89 | |
|
| | 90 | | public void ConfigurePools() => |
| 17 | 91 | | placeCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(PLACE_CARDS_POOL_NAME, placeCardPrefab, PLACE_CARD |
| | 92 | |
|
| | 93 | | public override void RefreshControl() => |
| 0 | 94 | | places.RefreshControl(); |
| | 95 | |
|
| | 96 | | public void SetPlaces(List<PlaceCardComponentModel> places) |
| | 97 | | { |
| 4 | 98 | | SetPlacesAsLoading(false); |
| 4 | 99 | | placesNoDataText.gameObject.SetActive(places.Count == 0); |
| | 100 | |
|
| 4 | 101 | | placeCardsPool.ReleaseAll(); |
| | 102 | |
|
| 4 | 103 | | this.places.ExtractItems(); |
| 4 | 104 | | this.places.RemoveItems(); |
| | 105 | |
|
| 4 | 106 | | SetPlacesAsync(places, cancellationTokenSource.Token).Forget(); |
| 4 | 107 | | } |
| | 108 | |
|
| | 109 | | public void SetPlacesAsLoading(bool isVisible) |
| | 110 | | { |
| 6 | 111 | | placesCanvas.enabled = !isVisible; |
| | 112 | |
|
| 6 | 113 | | placesLoading.SetActive(isVisible); |
| | 114 | |
|
| 6 | 115 | | if (isVisible) |
| 1 | 116 | | placesNoDataText.gameObject.SetActive(false); |
| 6 | 117 | | } |
| | 118 | |
|
| | 119 | | public void AddPlaces(List<PlaceCardComponentModel> places) => |
| 4 | 120 | | SetPlacesAsync(places, cancellationTokenSource.Token).Forget(); |
| | 121 | |
|
| | 122 | | private async UniTask SetPlacesAsync(List<PlaceCardComponentModel> places, CancellationToken cancellationToken) |
| | 123 | | { |
| 58 | 124 | | foreach (PlaceCardComponentModel place in places) |
| | 125 | | { |
| 24 | 126 | | PlaceCardComponentView placeCard = PlacesAndEventsCardsFactory.CreateConfiguredPlaceCard(placeCardsPool, pla |
| 24 | 127 | | OnFriendHandlerAdded?.Invoke(placeCard.friendsHandler); |
| | 128 | |
|
| 24 | 129 | | this.places.AddItem(placeCard); |
| | 130 | |
|
| 72 | 131 | | await UniTask.NextFrame(cancellationToken); |
| | 132 | | } |
| | 133 | |
|
| 2 | 134 | | this.places.SetItemSizeForModel(); |
| 2 | 135 | | await placeCardsPool.PrewarmAsync(places.Count, cancellationToken); |
| 2 | 136 | | } |
| | 137 | |
|
| | 138 | | public void SetActive(bool isActive) |
| | 139 | | { |
| 89 | 140 | | canvas.enabled = isActive; |
| | 141 | |
|
| 89 | 142 | | if (isActive) |
| 3 | 143 | | OnEnable(); |
| | 144 | | else |
| 86 | 145 | | OnDisable(); |
| 86 | 146 | | } |
| | 147 | |
|
| | 148 | | public void SetShowMorePlacesButtonActive(bool isActive) => |
| 2 | 149 | | showMorePlacesButtonContainer.gameObject.SetActive(isActive); |
| | 150 | |
|
| | 151 | | public void ShowPlaceModal(PlaceCardComponentModel placeInfo) |
| | 152 | | { |
| 1 | 153 | | placeModal.Show(); |
| 1 | 154 | | PlacesCardsConfigurator.Configure(placeModal, placeInfo, OnInfoClicked, OnJumpInClicked); |
| 1 | 155 | | } |
| | 156 | |
|
| | 157 | | public void HidePlaceModal() |
| | 158 | | { |
| 1 | 159 | | if (placeModal != null) |
| 1 | 160 | | placeModal.Hide(); |
| 1 | 161 | | } |
| | 162 | |
|
| | 163 | | public void RestartScrollViewPosition() => |
| 0 | 164 | | scrollView.verticalNormalizedPosition = 1; |
| | 165 | | } |