| | 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 FavoritesSubSectionComponentView : BaseComponentView, IFavoritesSubSectionComponentView |
| | 11 | | { |
| | 12 | | internal const string FAVORITE_CARDS_POOL_NAME = "Places_FavoriteCardsPool"; |
| | 13 | | private const int FAVORITE_CARDS_POOL_PREWARM = 20; |
| | 14 | |
|
| 43 | 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 favorites; |
| | 24 | | [SerializeField] internal GameObject favoritesLoading; |
| | 25 | | [SerializeField] internal GameObject noFavoritesPrompt; |
| | 26 | | [SerializeField] internal Color[] friendColors = null; |
| | 27 | | [SerializeField] internal GameObject showMoreFavoritesButtonContainer; |
| | 28 | | [SerializeField] internal ButtonComponentView showMoreFavoritesButton; |
| | 29 | |
|
| | 30 | | [SerializeField] private Canvas canvas; |
| | 31 | |
|
| | 32 | | internal PlaceCardComponentView placeModal; |
| | 33 | | internal Pool favoritesCardsPool; |
| | 34 | | private Canvas favoritesCanvas; |
| | 35 | |
|
| 0 | 36 | | public Color[] currentFriendColors => friendColors; |
| | 37 | |
|
| 0 | 38 | | public int currentFavoritePlacesPerRow => favorites.currentItemsPerRow; |
| | 39 | |
|
| 0 | 40 | | public void SetAllAsLoading() => SetFavoritesAsLoading(true); |
| 0 | 41 | | public void SetShowMoreButtonActive(bool isActive) => SetShowMoreFavoritesButtonActive(isActive); |
| 0 | 42 | | public int CurrentTilesPerRow => currentFavoritePlacesPerRow; |
| | 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 OnFavoriteSubSectionEnable; |
| | 49 | | public event Action OnShowMoreFavoritesClicked; |
| | 50 | |
|
| | 51 | | public override void Awake() |
| | 52 | | { |
| 40 | 53 | | base.Awake(); |
| 40 | 54 | | favoritesCanvas = favorites.GetComponent<Canvas>(); |
| 40 | 55 | | } |
| | 56 | |
|
| | 57 | | public override void Start() |
| | 58 | | { |
| 28 | 59 | | placeModal = PlacesAndEventsCardsFactory.GetPlaceCardTemplateHiddenLazy(placeCardModalPrefab); |
| | 60 | |
|
| 28 | 61 | | favorites.RemoveItems(); |
| | 62 | |
|
| 28 | 63 | | showMoreFavoritesButton.onClick.RemoveAllListeners(); |
| 28 | 64 | | showMoreFavoritesButton.onClick.AddListener(() => OnShowMoreFavoritesClicked?.Invoke()); |
| | 65 | |
|
| 28 | 66 | | OnReady?.Invoke(); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public override void OnEnable() |
| | 70 | | { |
| 40 | 71 | | OnFavoriteSubSectionEnable?.Invoke(); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public override void Dispose() |
| | 75 | | { |
| 91 | 76 | | base.Dispose(); |
| 91 | 77 | | cancellationTokenSource.Cancel(); |
| | 78 | |
|
| 91 | 79 | | showMoreFavoritesButton.onClick.RemoveAllListeners(); |
| | 80 | |
|
| 91 | 81 | | favorites.Dispose(); |
| | 82 | |
|
| 91 | 83 | | if (placeModal != null) |
| | 84 | | { |
| 56 | 85 | | placeModal.Dispose(); |
| 56 | 86 | | Destroy(placeModal.gameObject); |
| | 87 | | } |
| 91 | 88 | | } |
| | 89 | |
|
| | 90 | | public void ConfigurePools() => |
| 0 | 91 | | favoritesCardsPool = PlacesAndEventsCardsFactory.GetCardsPoolLazy(FAVORITE_CARDS_POOL_NAME, placeCardPrefab, FAV |
| | 92 | |
|
| | 93 | | public override void RefreshControl() => |
| 0 | 94 | | favorites.RefreshControl(); |
| | 95 | |
|
| | 96 | | public void SetFavorites(List<PlaceCardComponentModel> places) |
| | 97 | | { |
| 0 | 98 | | SetFavoritesAsLoading(false); |
| 0 | 99 | | noFavoritesPrompt.gameObject.SetActive(places.Count == 0); |
| | 100 | |
|
| 0 | 101 | | favoritesCardsPool.ReleaseAll(); |
| | 102 | |
|
| 0 | 103 | | this.favorites.ExtractItems(); |
| 0 | 104 | | this.favorites.RemoveItems(); |
| | 105 | |
|
| 0 | 106 | | SetFavoritesAsync(places, cancellationTokenSource.Token).Forget(); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public void SetFavoritesAsLoading(bool isVisible) |
| | 110 | | { |
| 0 | 111 | | favoritesCanvas.enabled = !isVisible; |
| | 112 | |
|
| 0 | 113 | | favoritesLoading.SetActive(isVisible); |
| | 114 | |
|
| 0 | 115 | | if (isVisible) |
| 0 | 116 | | noFavoritesPrompt.gameObject.SetActive(false); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public void AddFavorites(List<PlaceCardComponentModel> places) => |
| 0 | 120 | | SetFavoritesAsync(places, cancellationTokenSource.Token).Forget(); |
| | 121 | |
|
| | 122 | | private async UniTask SetFavoritesAsync(List<PlaceCardComponentModel> places, CancellationToken cancellationToken) |
| | 123 | | { |
| 0 | 124 | | foreach (PlaceCardComponentModel place in places) |
| | 125 | | { |
| 0 | 126 | | PlaceCardComponentView placeCard = PlacesAndEventsCardsFactory.CreateConfiguredPlaceCard(favoritesCardsPool, |
| 0 | 127 | | OnFriendHandlerAdded?.Invoke(placeCard.friendsHandler); |
| | 128 | |
|
| 0 | 129 | | this.favorites.AddItem(placeCard); |
| | 130 | |
|
| 0 | 131 | | await UniTask.NextFrame(cancellationToken); |
| | 132 | | } |
| | 133 | |
|
| 0 | 134 | | this.favorites.SetItemSizeForModel(); |
| 0 | 135 | | await favoritesCardsPool.PrewarmAsync(places.Count, cancellationToken); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | public void SetActive(bool isActive) |
| | 139 | | { |
| 86 | 140 | | canvas.enabled = isActive; |
| | 141 | |
|
| 86 | 142 | | if (isActive) |
| 0 | 143 | | OnEnable(); |
| | 144 | | else |
| 86 | 145 | | OnDisable(); |
| 86 | 146 | | } |
| | 147 | |
|
| | 148 | | public void SetShowMoreFavoritesButtonActive(bool isActive) => |
| 0 | 149 | | showMoreFavoritesButtonContainer.gameObject.SetActive(isActive); |
| | 150 | |
|
| | 151 | | public void ShowPlaceModal(PlaceCardComponentModel placeInfo) |
| | 152 | | { |
| 0 | 153 | | placeModal.Show(); |
| 0 | 154 | | PlacesCardsConfigurator.Configure(placeModal, placeInfo, OnInfoClicked, OnJumpInClicked); |
| 0 | 155 | | } |
| | 156 | |
|
| | 157 | | public void HidePlaceModal() |
| | 158 | | { |
| 0 | 159 | | if (placeModal != null) |
| 0 | 160 | | placeModal.Hide(); |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | public void RestartScrollViewPosition() => |
| 0 | 164 | | scrollView.verticalNormalizedPosition = 1; |
| | 165 | | } |