| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Tasks; |
| | 5 | | using DCLServices.CameraReelService; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Globalization; |
| | 9 | | using System.Threading; |
| | 10 | | using TMPro; |
| | 11 | | using UnityEngine; |
| | 12 | | using UnityEngine.UI; |
| | 13 | |
|
| | 14 | | namespace DCLFeatures.CameraReel.Gallery |
| | 15 | | { |
| | 16 | | public class CameraReelGalleryView : MonoBehaviour, ICameraReelGalleryView |
| | 17 | | { |
| 0 | 18 | | private readonly SortedDictionary<DateTime, (GridContainerComponentView gridContainer, GameObject headerContaine |
| 0 | 19 | | private readonly Dictionary<CameraReelResponse, CameraReelThumbnail> thumbnails = new (); |
| 0 | 20 | | private readonly Dictionary<CameraReelThumbnail, PoolableObject> thumbnailPoolObjects = new (); |
| 0 | 21 | | private readonly Dictionary<DateTime, int> thumbnailsByMonth = new (); |
| 0 | 22 | | private readonly List<CameraReelThumbnail> thumbnailSortingBuffer = new (); |
| 0 | 23 | | private readonly HashSet<GridContainerComponentView> containersToBeSorted = new (); |
| | 24 | |
|
| | 25 | | [SerializeField] private RectTransform container; |
| | 26 | | [SerializeField] private Button showMoreButton; |
| | 27 | | [SerializeField] private RectTransform showMoreButtonPanel; |
| | 28 | | [SerializeField] private Canvas canvas; |
| | 29 | | [SerializeField] private GameObject emptyStateContainer; |
| | 30 | | [SerializeField] private RectTransform scrollMaskTransform; |
| | 31 | |
|
| | 32 | | [Header("RESOURCES")] |
| | 33 | | [SerializeField] private GameObject monthHeaderPrefab; |
| | 34 | | [SerializeField] private GridContainerComponentView monthGridContainerPrefab; |
| | 35 | | [SerializeField] private CameraReelThumbnail thumbnailPrefab; |
| | 36 | |
|
| | 37 | | private GridContainerComponentView currentMonthGridContainer; |
| | 38 | | private CancellationTokenSource updateLayoutAndSortingCancellationToken; |
| | 39 | |
|
| | 40 | | public event Action<CameraReelResponse> ScreenshotThumbnailClicked; |
| | 41 | | public event Action ShowMoreButtonClicked; |
| | 42 | |
|
| | 43 | | private void Awake() |
| | 44 | | { |
| 0 | 45 | | SwitchVisibility(isVisible: false); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | private void OnEnable() |
| | 49 | | { |
| 0 | 50 | | showMoreButton.onClick.AddListener(() => ShowMoreButtonClicked?.Invoke()); |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | private void OnDisable() |
| | 54 | | { |
| 0 | 55 | | showMoreButton.onClick.RemoveAllListeners(); |
| 0 | 56 | | } |
| | 57 | |
|
| | 58 | | public void SwitchVisibility(bool isVisible) => |
| 0 | 59 | | canvas.enabled = isVisible; |
| | 60 | |
|
| | 61 | | public void DeleteScreenshotThumbnail(CameraReelResponse reel) |
| | 62 | | { |
| 0 | 63 | | if (!thumbnails.ContainsKey(reel)) return; |
| | 64 | |
|
| 0 | 65 | | CameraReelThumbnail thumbnail = thumbnails[reel]; |
| 0 | 66 | | DateTime month = reel.metadata.GetStartOfTheMonthDate(); |
| | 67 | |
|
| 0 | 68 | | if (thumbnailPoolObjects.ContainsKey(thumbnail)) |
| | 69 | | { |
| 0 | 70 | | thumbnailPoolObjects[thumbnail].Release(); |
| 0 | 71 | | thumbnailPoolObjects.Remove(thumbnail); |
| | 72 | | } |
| | 73 | |
|
| 0 | 74 | | thumbnails.Remove(reel); |
| | 75 | |
|
| 0 | 76 | | if (thumbnailsByMonth.ContainsKey(month)) |
| 0 | 77 | | thumbnailsByMonth[month]--; |
| | 78 | |
|
| 0 | 79 | | RemoveEmptyMonthContainer(month); |
| | 80 | |
|
| 0 | 81 | | updateLayoutAndSortingCancellationToken = updateLayoutAndSortingCancellationToken.SafeRestart(); |
| 0 | 82 | | try { UpdateLayoutOnNextFrame(updateLayoutAndSortingCancellationToken.Token).Forget(); } |
| 0 | 83 | | catch (OperationCanceledException) { } |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | public void SwitchEmptyStateVisibility(bool visible) |
| | 87 | | { |
| 0 | 88 | | emptyStateContainer.SetActive(visible); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | public void SwitchShowMoreVisibility(bool visible) |
| | 92 | | { |
| 0 | 93 | | showMoreButtonPanel.gameObject.SetActive(visible); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | public void AddScreenshotThumbnail(CameraReelResponse reel, bool setAsFirst) |
| | 97 | | { |
| 0 | 98 | | DateTime month = reel.metadata.GetStartOfTheMonthDate(); |
| | 99 | |
|
| 0 | 100 | | GridContainerComponentView gridContainer = GetMonthContainer(month); |
| | 101 | |
|
| 0 | 102 | | Pool thumbnailPool = GetThumbnailEntryPool(); |
| 0 | 103 | | PoolableObject thumbnailPoolObj = thumbnailPool.Get(); |
| 0 | 104 | | CameraReelThumbnail thumbnail = thumbnailPoolObj.gameObject.GetComponent<CameraReelThumbnail>(); |
| 0 | 105 | | thumbnail.transform.SetParent(gridContainer.transform, false); |
| 0 | 106 | | thumbnail.Show(reel); |
| 0 | 107 | | thumbnail.OnClicked -= OnThumbnailClicked; |
| 0 | 108 | | thumbnail.OnClicked += OnThumbnailClicked; |
| | 109 | |
|
| 0 | 110 | | if (setAsFirst) |
| 0 | 111 | | thumbnail.transform.SetAsFirstSibling(); |
| | 112 | |
|
| 0 | 113 | | thumbnails.Add(reel, thumbnail); |
| 0 | 114 | | thumbnailPoolObjects[thumbnail] = thumbnailPoolObj; |
| | 115 | |
|
| 0 | 116 | | if (!thumbnailsByMonth.ContainsKey(month)) |
| 0 | 117 | | thumbnailsByMonth[month] = 0; |
| | 118 | |
|
| 0 | 119 | | thumbnailsByMonth[month]++; |
| | 120 | |
|
| 0 | 121 | | updateLayoutAndSortingCancellationToken = updateLayoutAndSortingCancellationToken.SafeRestart(); |
| | 122 | |
|
| 0 | 123 | | if (!containersToBeSorted.Contains(gridContainer)) |
| 0 | 124 | | containersToBeSorted.Add(gridContainer); |
| | 125 | |
|
| | 126 | | try |
| | 127 | | { |
| 0 | 128 | | SortThumbnailsByDateOnNextFrame(containersToBeSorted, updateLayoutAndSortingCancellationToken.Token) |
| 0 | 129 | | .ContinueWith(() => containersToBeSorted.Clear()) |
| | 130 | | .Forget(); |
| 0 | 131 | | UpdateLayoutOnNextFrame(updateLayoutAndSortingCancellationToken.Token).Forget(); |
| 0 | 132 | | } |
| 0 | 133 | | catch (OperationCanceledException) { } |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | private void OnThumbnailClicked(CameraReelResponse picture) => |
| 0 | 137 | | ScreenshotThumbnailClicked?.Invoke(picture); |
| | 138 | |
|
| | 139 | | private async UniTask SortThumbnailsByDateOnNextFrame(IEnumerable<GridContainerComponentView> containers, Cancel |
| | 140 | | { |
| 0 | 141 | | await UniTask.NextFrame(cancellationToken: cancellationToken); |
| | 142 | |
|
| 0 | 143 | | foreach (GridContainerComponentView container in containers) |
| 0 | 144 | | SortThumbnails(container); |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | private void SortThumbnails(GridContainerComponentView container) |
| | 148 | | { |
| 0 | 149 | | lock (thumbnailSortingBuffer) |
| | 150 | | { |
| 0 | 151 | | thumbnailSortingBuffer.Clear(); |
| | 152 | |
|
| 0 | 153 | | foreach (Transform child in container.transform) |
| | 154 | | { |
| 0 | 155 | | CameraReelThumbnail thumbnail = child.GetComponent<CameraReelThumbnail>(); |
| 0 | 156 | | if (thumbnail == null) continue; |
| 0 | 157 | | thumbnailSortingBuffer.Add(thumbnail); |
| | 158 | | } |
| | 159 | |
|
| 0 | 160 | | thumbnailSortingBuffer.Sort((t1, t2) => t1.CompareTo(t2)); |
| | 161 | |
|
| 0 | 162 | | foreach (CameraReelThumbnail thumbnail in thumbnailSortingBuffer) |
| 0 | 163 | | thumbnail.transform.SetAsFirstSibling(); |
| | 164 | | } |
| 0 | 165 | | } |
| | 166 | |
|
| | 167 | | private async UniTaskVoid UpdateLayoutOnNextFrame(CancellationToken cancellationToken) |
| | 168 | | { |
| 0 | 169 | | await UniTask.NextFrame(cancellationToken: cancellationToken); |
| 0 | 170 | | container.ForceUpdateLayout(); |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | private GridContainerComponentView GetMonthContainer(DateTime date, bool createIfEmpty = true) |
| | 174 | | { |
| | 175 | | GridContainerComponentView gridContainer; |
| | 176 | |
|
| 0 | 177 | | if (!monthContainers.ContainsKey(date) && createIfEmpty) |
| | 178 | | { |
| 0 | 179 | | gridContainer = CreateMonthContainer(date); |
| 0 | 180 | | showMoreButtonPanel.SetAsLastSibling(); |
| 0 | 181 | | SortAllMonthContainers(); |
| | 182 | | } |
| | 183 | | else |
| 0 | 184 | | gridContainer = monthContainers[date].gridContainer; |
| | 185 | |
|
| 0 | 186 | | return gridContainer; |
| | 187 | | } |
| | 188 | |
|
| | 189 | | private GridContainerComponentView CreateMonthContainer(DateTime date) |
| | 190 | | { |
| 0 | 191 | | GameObject header = Instantiate(monthHeaderPrefab, container); |
| 0 | 192 | | header.gameObject.SetActive(true); |
| 0 | 193 | | header.GetComponentInChildren<TMP_Text>().SetText(GetMonthName(date)); |
| 0 | 194 | | GridContainerComponentView gridContainer = Instantiate(monthGridContainerPrefab, container); |
| 0 | 195 | | gridContainer.gameObject.SetActive(true); |
| | 196 | |
|
| 0 | 197 | | monthContainers.Add(date, (gridContainer, header)); |
| 0 | 198 | | return gridContainer; |
| | 199 | | } |
| | 200 | |
|
| | 201 | | private void SortAllMonthContainers() |
| | 202 | | { |
| 0 | 203 | | foreach ((DateTime date, (GridContainerComponentView gridContainer, GameObject header)) in monthContainers) |
| | 204 | | { |
| 0 | 205 | | gridContainer.transform.SetAsFirstSibling(); |
| 0 | 206 | | header.transform.SetAsFirstSibling(); |
| | 207 | | } |
| 0 | 208 | | } |
| | 209 | |
|
| | 210 | | private void RemoveEmptyMonthContainer(DateTime date) |
| | 211 | | { |
| 0 | 212 | | if (!monthContainers.ContainsKey(date)) return; |
| 0 | 213 | | if (thumbnailsByMonth.ContainsKey(date) && thumbnailsByMonth[date] > 0) return; |
| | 214 | |
|
| 0 | 215 | | (GridContainerComponentView gridContainer, GameObject headerContainer) containers = monthContainers[date]; |
| 0 | 216 | | Destroy(containers.gridContainer); |
| 0 | 217 | | Destroy(containers.headerContainer); |
| 0 | 218 | | monthContainers.Remove(date); |
| 0 | 219 | | } |
| | 220 | |
|
| | 221 | | private string GetMonthName(DateTime date) => |
| 0 | 222 | | date.ToString("MMMM yyyy", CultureInfo.InvariantCulture); |
| | 223 | |
|
| | 224 | | private Pool GetThumbnailEntryPool() |
| | 225 | | { |
| | 226 | | const string POOL_ID = "CameraReelThumbnails"; |
| 0 | 227 | | var entryPool = PoolManager.i.GetPool(POOL_ID); |
| 0 | 228 | | if (entryPool != null) return entryPool; |
| | 229 | |
|
| 0 | 230 | | entryPool = PoolManager.i.AddPool( |
| | 231 | | POOL_ID, |
| | 232 | | Instantiate(thumbnailPrefab).gameObject, |
| | 233 | | maxPrewarmCount: 10, |
| | 234 | | isPersistent: true); |
| | 235 | |
|
| 0 | 236 | | return entryPool; |
| | 237 | | } |
| | 238 | | } |
| | 239 | | } |