| | 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 | |
|
| 65 | 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 | |
|
| | 40 | | public event Action OnReady; |
| | 41 | | public event Action<PlaceCardComponentModel> OnInfoClicked; |
| | 42 | | public event Action<HotScenesController.HotSceneInfo> OnJumpInClicked; |
| | 43 | | public event Action<FriendsHandler> OnFriendHandlerAdded; |
| | 44 | | public event Action OnPlacesSubSectionEnable; |
| | 45 | | public event Action OnShowMorePlacesClicked; |
| | 46 | |
|
| | 47 | | public override void Awake() |
| | 48 | | { |
| 62 | 49 | | base.Awake(); |
| 62 | 50 | | placesCanvas = places.GetComponent<Canvas>(); |
| 62 | 51 | | } |
| | 52 | |
|
| | 53 | | public override void Start() |
| | 54 | | { |
| 58 | 55 | | placeModal = ExplorePlacesUtils.ConfigurePlaceCardModal(placeCardModalPrefab); |
| | 56 | |
|
| 58 | 57 | | places.RemoveItems(); |
| | 58 | |
|
| 58 | 59 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| 58 | 60 | | showMorePlacesButton.onClick.AddListener(() => OnShowMorePlacesClicked?.Invoke()); |
| | 61 | |
|
| 58 | 62 | | OnReady?.Invoke(); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public override void OnEnable() |
| | 66 | | { |
| 65 | 67 | | OnPlacesSubSectionEnable?.Invoke(); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | public override void Dispose() |
| | 71 | | { |
| 135 | 72 | | base.Dispose(); |
| 135 | 73 | | cancellationTokenSource.Cancel(); |
| | 74 | |
|
| 135 | 75 | | showMorePlacesButton.onClick.RemoveAllListeners(); |
| | 76 | |
|
| 135 | 77 | | places.Dispose(); |
| | 78 | |
|
| 135 | 79 | | if (placeModal != null) |
| | 80 | | { |
| 86 | 81 | | placeModal.Dispose(); |
| 86 | 82 | | Destroy(placeModal.gameObject); |
| | 83 | | } |
| 135 | 84 | | } |
| | 85 | |
|
| | 86 | | public void ConfigurePools() => |
| 17 | 87 | | ExplorePlacesUtils.ConfigurePlaceCardsPool(out placeCardsPool, PLACE_CARDS_POOL_NAME, placeCardPrefab, PLACE_CAR |
| | 88 | |
|
| | 89 | | public override void RefreshControl() => |
| 0 | 90 | | places.RefreshControl(); |
| | 91 | |
|
| | 92 | | public void SetPlaces(List<PlaceCardComponentModel> places) |
| | 93 | | { |
| 4 | 94 | | SetPlacesAsLoading(false); |
| 4 | 95 | | placesNoDataText.gameObject.SetActive(places.Count == 0); |
| | 96 | |
|
| 4 | 97 | | placeCardsPool.ReleaseAll(); |
| | 98 | |
|
| 4 | 99 | | this.places.ExtractItems(); |
| 4 | 100 | | this.places.RemoveItems(); |
| | 101 | |
|
| 4 | 102 | | SetPlacesAsync(places, cancellationTokenSource.Token).Forget(); |
| 4 | 103 | | } |
| | 104 | |
|
| | 105 | | public void SetPlacesAsLoading(bool isVisible) |
| | 106 | | { |
| 6 | 107 | | placesCanvas.enabled = !isVisible; |
| | 108 | |
|
| 6 | 109 | | placesLoading.SetActive(isVisible); |
| | 110 | |
|
| 6 | 111 | | if (isVisible) |
| 1 | 112 | | placesNoDataText.gameObject.SetActive(false); |
| 6 | 113 | | } |
| | 114 | |
|
| | 115 | | public void AddPlaces(List<PlaceCardComponentModel> places) => |
| 4 | 116 | | SetPlacesAsync(places, cancellationTokenSource.Token).Forget(); |
| | 117 | |
|
| | 118 | | private async UniTask SetPlacesAsync(List<PlaceCardComponentModel> places, CancellationToken cancellationToken) |
| | 119 | | { |
| 58 | 120 | | foreach (PlaceCardComponentModel place in places) |
| | 121 | | { |
| 24 | 122 | | this.places.AddItem( |
| | 123 | | ExplorePlacesUtils.InstantiateConfiguredPlaceCard(place, placeCardsPool, OnFriendHandlerAdded, OnInfoCli |
| 72 | 124 | | await UniTask.NextFrame(cancellationToken); |
| | 125 | | } |
| | 126 | |
|
| 2 | 127 | | this.places.SetItemSizeForModel(); |
| 2 | 128 | | await placeCardsPool.PrewarmAsync(places.Count, cancellationToken); |
| 2 | 129 | | } |
| | 130 | |
|
| | 131 | | public void SetActive(bool isActive) |
| | 132 | | { |
| 53 | 133 | | canvas.enabled = isActive; |
| | 134 | |
|
| 53 | 135 | | if (isActive) |
| 3 | 136 | | OnEnable(); |
| | 137 | | else |
| 50 | 138 | | OnDisable(); |
| 50 | 139 | | } |
| | 140 | |
|
| | 141 | | public void SetShowMorePlacesButtonActive(bool isActive) => |
| 2 | 142 | | showMorePlacesButtonContainer.gameObject.SetActive(isActive); |
| | 143 | |
|
| | 144 | | public void ShowPlaceModal(PlaceCardComponentModel placeInfo) |
| | 145 | | { |
| 1 | 146 | | placeModal.Show(); |
| 1 | 147 | | ExplorePlacesUtils.ConfigurePlaceCard(placeModal, placeInfo, OnInfoClicked, OnJumpInClicked); |
| 1 | 148 | | } |
| | 149 | |
|
| | 150 | | public void HidePlaceModal() |
| | 151 | | { |
| 1 | 152 | | if (placeModal != null) |
| 1 | 153 | | placeModal.Hide(); |
| 1 | 154 | | } |
| | 155 | |
|
| | 156 | | public void RestartScrollViewPosition() => |
| 0 | 157 | | scrollView.verticalNormalizedPosition = 1; |
| | 158 | | } |