| | 1 | | using System.Collections; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.EventSystems; |
| | 9 | | using DCL; |
| | 10 | |
|
| | 11 | | public class FriendsTabViewBase : MonoBehaviour, IPointerDownHandler |
| | 12 | | { |
| | 13 | | [System.Serializable] |
| | 14 | | public class LastFriendTimestampModel |
| | 15 | | { |
| | 16 | | public string userId; |
| | 17 | | public ulong lastMessageTimestamp; |
| | 18 | | } |
| | 19 | |
|
| | 20 | | [System.Serializable] |
| | 21 | | public class EntryList |
| | 22 | | { |
| | 23 | | public string toggleTextPrefix; |
| | 24 | | public GameObject toggleButton; |
| | 25 | | public TextMeshProUGUI toggleText; |
| | 26 | | public Transform container; |
| 92 | 27 | | private Dictionary<string, FriendEntryBase> entries = new Dictionary<string, FriendEntryBase>(); |
| | 28 | |
|
| | 29 | | // This list store each friendId with the greatest timestamp from his related messages |
| 92 | 30 | | private List<LastFriendTimestampModel> latestTimestampsOrdered = new List<LastFriendTimestampModel>(); |
| | 31 | |
|
| 0 | 32 | | public int Count() { return entries.Count; } |
| | 33 | |
|
| | 34 | | public void Add(string userId, FriendEntryBase entry) |
| | 35 | | { |
| 31 | 36 | | if (entries.ContainsKey(userId)) |
| 0 | 37 | | return; |
| | 38 | |
|
| 31 | 39 | | entries.Add(userId, entry); |
| | 40 | |
|
| 31 | 41 | | entry.transform.SetParent(container, false); |
| 31 | 42 | | entry.transform.localScale = Vector3.one; |
| | 43 | |
|
| 31 | 44 | | UpdateToggle(); |
| 31 | 45 | | ReorderingFriendEntries(); |
| 31 | 46 | | } |
| | 47 | |
|
| | 48 | | public FriendEntryBase Remove(string userId) |
| | 49 | | { |
| 39 | 50 | | if (!entries.ContainsKey(userId)) |
| 29 | 51 | | return null; |
| | 52 | |
|
| 10 | 53 | | var entry = entries[userId]; |
| | 54 | |
|
| 10 | 55 | | entries.Remove(userId); |
| | 56 | |
|
| 10 | 57 | | UpdateToggle(); |
| | 58 | |
|
| 10 | 59 | | return entry; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | void UpdateToggle() |
| | 63 | | { |
| 41 | 64 | | toggleText.text = $"{toggleTextPrefix} ({Count()})"; |
| 41 | 65 | | toggleButton.SetActive(Count() != 0); |
| 41 | 66 | | } |
| | 67 | |
|
| | 68 | | public void AddOrUpdateLastTimestamp(LastFriendTimestampModel timestamp, bool reorderFriendEntries = true) |
| | 69 | | { |
| 19 | 70 | | if (timestamp == null) |
| 19 | 71 | | return; |
| | 72 | |
|
| 0 | 73 | | LastFriendTimestampModel existingTimestamp = latestTimestampsOrdered.FirstOrDefault(t => t.userId == timesta |
| 0 | 74 | | if (existingTimestamp == null) |
| | 75 | | { |
| 0 | 76 | | latestTimestampsOrdered.Add(timestamp); |
| 0 | 77 | | } |
| 0 | 78 | | else if (timestamp.lastMessageTimestamp > existingTimestamp.lastMessageTimestamp) |
| | 79 | | { |
| 0 | 80 | | existingTimestamp.lastMessageTimestamp = timestamp.lastMessageTimestamp; |
| | 81 | | } |
| | 82 | |
|
| 0 | 83 | | if (reorderFriendEntries) |
| | 84 | | { |
| 0 | 85 | | latestTimestampsOrdered = latestTimestampsOrdered.OrderByDescending(f => f.lastMessageTimestamp).ToList( |
| 0 | 86 | | ReorderingFriendEntries(); |
| | 87 | | } |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public LastFriendTimestampModel RemoveLastTimestamp(string userId) |
| | 91 | | { |
| 27 | 92 | | LastFriendTimestampModel timestampToRemove = latestTimestampsOrdered.FirstOrDefault(t => t.userId == userId) |
| 27 | 93 | | if (timestampToRemove == null) |
| 27 | 94 | | return null; |
| | 95 | |
|
| 0 | 96 | | latestTimestampsOrdered.Remove(timestampToRemove); |
| | 97 | |
|
| 0 | 98 | | return timestampToRemove; |
| | 99 | | } |
| | 100 | |
|
| | 101 | | private void ReorderingFriendEntries() |
| | 102 | | { |
| 62 | 103 | | foreach (var item in latestTimestampsOrdered) |
| | 104 | | { |
| 0 | 105 | | if (entries.ContainsKey(item.userId)) |
| 0 | 106 | | entries[item.userId].transform.SetAsLastSibling(); |
| | 107 | | } |
| 31 | 108 | | } |
| | 109 | | } |
| | 110 | |
|
| | 111 | | private const string FRIEND_ENTRIES_POOL_NAME = "FriendEntriesPool_"; |
| | 112 | | [SerializeField] protected GameObject entryPrefab; |
| | 113 | | [SerializeField] protected GameObject emptyListImage; |
| | 114 | |
|
| | 115 | | protected RectTransform rectTransform; |
| | 116 | | protected FriendsHUDView owner; |
| | 117 | |
|
| | 118 | | public UserContextMenu contextMenuPanel; |
| | 119 | | public UserContextConfirmationDialog confirmationDialog; |
| | 120 | |
|
| 46 | 121 | | protected Dictionary<string, FriendEntryBase> entries = new Dictionary<string, FriendEntryBase>(); |
| 46 | 122 | | protected Dictionary<string, PoolableObject> instantiatedFriendEntries = new Dictionary<string, PoolableObject>(); |
| | 123 | | protected Pool friendEntriesPool; |
| | 124 | | protected bool layoutIsDirty = false; |
| | 125 | |
|
| | 126 | | private Coroutine updateRoutine = null; |
| | 127 | |
|
| | 128 | | private void Awake() |
| | 129 | | { |
| | 130 | | //Use a coroutine instead of an Update method to load the entries in the background while the gameobject is disa |
| 23 | 131 | | updateRoutine = CoroutineStarter.Start(UpdateCoroutine()); |
| 23 | 132 | | } |
| | 133 | |
|
| 0 | 134 | | internal List<FriendEntryBase> GetAllEntries() { return entries.Values.ToList(); } |
| | 135 | |
|
| | 136 | | internal FriendEntryBase GetEntry(string userId) |
| | 137 | | { |
| 88 | 138 | | if (!entries.ContainsKey(userId)) |
| 32 | 139 | | return null; |
| | 140 | |
|
| 56 | 141 | | return entries[userId]; |
| | 142 | | } |
| | 143 | |
|
| | 144 | | protected virtual void OnEnable() |
| | 145 | | { |
| 25 | 146 | | if (rectTransform == null) |
| 22 | 147 | | rectTransform = transform as RectTransform; |
| | 148 | |
|
| 25 | 149 | | layoutIsDirty = true; |
| 25 | 150 | | } |
| | 151 | |
|
| | 152 | | protected virtual void OnDisable() |
| | 153 | | { |
| 25 | 154 | | confirmationDialog.Hide(); |
| 25 | 155 | | contextMenuPanel.Hide(); |
| 25 | 156 | | } |
| | 157 | |
|
| | 158 | | protected virtual void UpdateLayout() |
| | 159 | | { |
| 66 | 160 | | if (layoutIsDirty) |
| | 161 | | { |
| 30 | 162 | | layoutIsDirty = false; |
| 30 | 163 | | rectTransform.ForceUpdateLayout(); |
| | 164 | | } |
| 66 | 165 | | } |
| | 166 | |
|
| | 167 | | private IEnumerator UpdateCoroutine() |
| | 168 | | { |
| 43 | 169 | | while (true) |
| | 170 | | { |
| 66 | 171 | | UpdateLayout(); |
| 66 | 172 | | yield return null; |
| | 173 | | } |
| | 174 | | } |
| | 175 | |
|
| | 176 | | public void OnPointerDown(PointerEventData eventData) |
| | 177 | | { |
| 0 | 178 | | if (eventData.pointerPressRaycast.gameObject == null || eventData.pointerPressRaycast.gameObject.layer != Physic |
| 0 | 179 | | contextMenuPanel.Hide(); |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | public virtual void Initialize(FriendsHUDView owner, int preinstantiatedEntries) |
| | 183 | | { |
| 44 | 184 | | this.owner = owner; |
| | 185 | |
|
| 44 | 186 | | friendEntriesPool = PoolManager.i.GetPool(FRIEND_ENTRIES_POOL_NAME + this.name + this.GetInstanceID()); |
| 44 | 187 | | if (friendEntriesPool == null) |
| | 188 | | { |
| 44 | 189 | | friendEntriesPool = PoolManager.i.AddPool( |
| | 190 | | FRIEND_ENTRIES_POOL_NAME + this.name + this.GetInstanceID(), |
| | 191 | | Instantiate(entryPrefab), |
| | 192 | | maxPrewarmCount: preinstantiatedEntries, |
| | 193 | | isPersistent: true); |
| 44 | 194 | | friendEntriesPool.ForcePrewarm(); |
| | 195 | | } |
| | 196 | |
|
| 44 | 197 | | rectTransform = transform as RectTransform; |
| | 198 | |
|
| 44 | 199 | | contextMenuPanel.OnBlock += OnPressBlockButton; |
| 44 | 200 | | contextMenuPanel.OnUnfriend += OnPressDeleteButton; |
| 44 | 201 | | } |
| | 202 | |
|
| | 203 | | public virtual void OnDestroy() |
| | 204 | | { |
| 23 | 205 | | contextMenuPanel.OnBlock -= OnPressBlockButton; |
| 23 | 206 | | contextMenuPanel.OnUnfriend -= OnPressDeleteButton; |
| 23 | 207 | | if (updateRoutine != null) |
| | 208 | | { |
| 23 | 209 | | CoroutineStarter.Stop(updateRoutine); |
| 23 | 210 | | updateRoutine = null; |
| | 211 | | } |
| 23 | 212 | | } |
| | 213 | |
|
| 0 | 214 | | protected virtual void OnPressDeleteButton(string userId) { } |
| | 215 | |
|
| | 216 | | protected virtual void OnPressBlockButton(string userId, bool blockUser) |
| | 217 | | { |
| 0 | 218 | | FriendEntryBase friendEntryToBlock = GetEntry(userId); |
| 0 | 219 | | if (friendEntryToBlock != null) |
| | 220 | | { |
| 0 | 221 | | friendEntryToBlock.model.blocked = blockUser; |
| 0 | 222 | | friendEntryToBlock.Populate(friendEntryToBlock.model); |
| | 223 | | } |
| 0 | 224 | | } |
| | 225 | |
|
| | 226 | | protected virtual bool CreateEntry(string userId) |
| | 227 | | { |
| 30 | 228 | | if (entries.ContainsKey(userId)) |
| 0 | 229 | | return false; |
| | 230 | |
|
| 30 | 231 | | PoolableObject newFriendEntry = friendEntriesPool.Get(); |
| 30 | 232 | | instantiatedFriendEntries.Add(userId, newFriendEntry); |
| 30 | 233 | | var entry = newFriendEntry.gameObject.GetComponent<FriendEntryBase>(); |
| 30 | 234 | | entries.Add(userId, entry); |
| | 235 | |
|
| 30 | 236 | | entry.OnMenuToggle += (x) => |
| | 237 | | { |
| 4 | 238 | | contextMenuPanel.transform.position = entry.menuPositionReference.position; |
| 4 | 239 | | contextMenuPanel.Show(userId); |
| 4 | 240 | | }; |
| | 241 | |
|
| 30 | 242 | | UpdateEmptyListObjects(); |
| 30 | 243 | | return true; |
| | 244 | | } |
| | 245 | |
|
| | 246 | | public virtual bool UpdateEntry(string userId, FriendEntryBase.Model model) |
| | 247 | | { |
| 31 | 248 | | if (!entries.ContainsKey(userId)) |
| 0 | 249 | | return false; |
| | 250 | |
|
| 31 | 251 | | var entry = entries[userId]; |
| | 252 | |
|
| 31 | 253 | | entry.Populate(model); |
| 31 | 254 | | entry.userId = userId; |
| | 255 | |
|
| 31 | 256 | | layoutIsDirty = true; |
| 31 | 257 | | return true; |
| | 258 | | } |
| | 259 | |
|
| | 260 | | public virtual bool RemoveEntry(string userId) |
| | 261 | | { |
| 21 | 262 | | if (!entries.ContainsKey(userId)) |
| 11 | 263 | | return false; |
| | 264 | |
|
| 10 | 265 | | if (instantiatedFriendEntries.TryGetValue(userId, out PoolableObject go)) |
| | 266 | | { |
| 10 | 267 | | friendEntriesPool.Release(go); |
| 10 | 268 | | instantiatedFriendEntries.Remove(userId); |
| | 269 | | } |
| 10 | 270 | | entries.Remove(userId); |
| | 271 | |
|
| 10 | 272 | | UpdateEmptyListObjects(); |
| | 273 | |
|
| 10 | 274 | | layoutIsDirty = true; |
| 10 | 275 | | return true; |
| | 276 | | } |
| | 277 | |
|
| 80 | 278 | | protected virtual void UpdateEmptyListObjects() { emptyListImage.SetActive(entries.Count == 0); } |
| | 279 | | } |