| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Social.Chat |
| | 8 | | { |
| | 9 | | public class ChatMentionSuggestionComponentView : MonoBehaviour |
| | 10 | | { |
| | 11 | | private const string POOL_NAME_PREFIX = "ChatMentionSuggestions"; |
| | 12 | |
|
| | 13 | | [SerializeField] internal ChatMentionSuggestionEntryComponentView mentionSuggestionPrefab; |
| | 14 | | [SerializeField] internal RectTransform entryContainer; |
| | 15 | | [SerializeField] internal RectTransform layout; |
| | 16 | |
|
| 38 | 17 | | private readonly Dictionary<ChatMentionSuggestionEntryComponentView, PoolableObject> pooledObjects = new (); |
| | 18 | |
|
| | 19 | | public event Action<ChatMentionSuggestionModel> OnEntrySubmit; |
| | 20 | |
|
| 2 | 21 | | public bool IsVisible => gameObject.activeInHierarchy; |
| | 22 | |
|
| | 23 | | public void Clear() |
| | 24 | | { |
| 4 | 25 | | foreach ((ChatMentionSuggestionEntryComponentView view, PoolableObject poolObj) in pooledObjects) |
| 0 | 26 | | poolObj.Release(); |
| 2 | 27 | | pooledObjects.Clear(); |
| 2 | 28 | | } |
| | 29 | |
|
| | 30 | | public void Show() => |
| 3 | 31 | | gameObject.SetActive(true); |
| | 32 | |
|
| | 33 | | public void Hide() => |
| 4 | 34 | | gameObject.SetActive(false); |
| | 35 | |
|
| | 36 | | public void Set(List<ChatMentionSuggestionModel> suggestions) |
| | 37 | | { |
| 12 | 38 | | foreach (ChatMentionSuggestionModel suggestion in suggestions) |
| | 39 | | { |
| 4 | 40 | | ChatMentionSuggestionEntryComponentView entry = GetEntryFromPool(); |
| 4 | 41 | | entry.transform.SetParent(entryContainer, false); |
| 4 | 42 | | entry.SetModel(suggestion); |
| 4 | 43 | | entry.Show(true); |
| 4 | 44 | | entry.OnSubmitted -= HandleEntrySubmit; |
| 4 | 45 | | entry.OnSubmitted += HandleEntrySubmit; |
| | 46 | | } |
| | 47 | |
|
| 2 | 48 | | layout.ForceUpdateLayout(); |
| 2 | 49 | | } |
| | 50 | |
|
| | 51 | | public void SelectFirstEntry() => |
| 2 | 52 | | pooledObjects.Keys.FirstOrDefault()?.Select(); |
| | 53 | |
|
| | 54 | | public void SelectNextEntry() |
| | 55 | | { |
| 0 | 56 | | var selectionFound = false; |
| 0 | 57 | | ChatMentionSuggestionEntryComponentView[] views = pooledObjects.Keys.ToArray(); |
| | 58 | |
|
| 0 | 59 | | for (var i = 0; i < views.Length; i++) |
| | 60 | | { |
| 0 | 61 | | ChatMentionSuggestionEntryComponentView view = views[i]; |
| 0 | 62 | | if (!view.IsSelected) continue; |
| 0 | 63 | | int nextIndex = (i + 1) % views.Length; |
| 0 | 64 | | view.Deselect(); |
| 0 | 65 | | views[nextIndex].Select(); |
| 0 | 66 | | selectionFound = true; |
| 0 | 67 | | break; |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | if (!selectionFound) |
| 0 | 71 | | SelectFirstEntry(); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public void SelectPreviousEntry() |
| | 75 | | { |
| 0 | 76 | | var selectionFound = false; |
| 0 | 77 | | ChatMentionSuggestionEntryComponentView[] views = pooledObjects.Keys.ToArray(); |
| | 78 | |
|
| 0 | 79 | | for (var i = 0; i < views.Length; i++) |
| | 80 | | { |
| 0 | 81 | | ChatMentionSuggestionEntryComponentView view = views[i]; |
| 0 | 82 | | if (!view.IsSelected) continue; |
| 0 | 83 | | int nextIndex = i - 1 < 0 ? views.Length - 1 : i - 1; |
| 0 | 84 | | view.Deselect(); |
| 0 | 85 | | views[nextIndex].Select(); |
| 0 | 86 | | selectionFound = true; |
| 0 | 87 | | break; |
| | 88 | | } |
| | 89 | |
|
| 0 | 90 | | if (!selectionFound) |
| 0 | 91 | | SelectFirstEntry(); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | private void HandleEntrySubmit(ChatMentionSuggestionModel model) => |
| 1 | 95 | | OnEntrySubmit?.Invoke(model); |
| | 96 | |
|
| | 97 | | private ChatMentionSuggestionEntryComponentView GetEntryFromPool() |
| | 98 | | { |
| 4 | 99 | | var pool = GetPool(mentionSuggestionPrefab); |
| 4 | 100 | | var pooledObj = pool.Get(); |
| 4 | 101 | | var entry = pooledObj.gameObject.GetComponent<ChatMentionSuggestionEntryComponentView>(); |
| 4 | 102 | | pooledObjects[entry] = pooledObj; |
| 4 | 103 | | return entry; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | private Pool GetPool(ChatMentionSuggestionEntryComponentView prefab) |
| | 107 | | { |
| 4 | 108 | | string poolId = POOL_NAME_PREFIX + GetInstanceID(); |
| 4 | 109 | | Pool entryPool = PoolManager.i.GetPool(poolId); |
| 6 | 110 | | if (entryPool != null) return entryPool; |
| | 111 | |
|
| 2 | 112 | | entryPool = PoolManager.i.AddPool( |
| | 113 | | poolId, |
| | 114 | | Instantiate(prefab).gameObject, |
| | 115 | | maxPrewarmCount: 0, |
| | 116 | | isPersistent: true); |
| 2 | 117 | | entryPool.ForcePrewarm(); |
| | 118 | |
|
| 2 | 119 | | return entryPool; |
| | 120 | | } |
| | 121 | |
|
| | 122 | | public void SubmitSelectedEntry() |
| | 123 | | { |
| 0 | 124 | | ChatMentionSuggestionEntryComponentView view = pooledObjects.Keys.FirstOrDefault(view => view.IsSelected); |
| 0 | 125 | | view?.OnSubmit(null); |
| 0 | 126 | | } |
| | 127 | | } |
| | 128 | | } |