| | 1 | | using DCL.Interface; |
| | 2 | | using System.Linq; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Shows the number of unread messages from a the world chat. |
| | 8 | | /// </summary> |
| | 9 | | public class UnreadWorldNotificationBadge : MonoBehaviour |
| | 10 | | { |
| | 11 | | public TextMeshProUGUI notificationText; |
| | 12 | | public GameObject notificationContainer; |
| 11 | 13 | | public int maxNumberToShow = 9; |
| | 14 | |
|
| | 15 | | private IChatController currentChatController; |
| | 16 | | private long currentTimestampReading; |
| | 17 | | private int currentUnreadMessagesValue; |
| | 18 | |
|
| | 19 | | public int currentUnreadMessages |
| | 20 | | { |
| 0 | 21 | | get => currentUnreadMessagesValue; |
| | 22 | | set |
| | 23 | | { |
| 19 | 24 | | currentUnreadMessagesValue = value; |
| 19 | 25 | | RefreshNotificationBadge(); |
| 19 | 26 | | } |
| | 27 | | } |
| | 28 | |
|
| 4 | 29 | | private void Start() { Initialize(ChatController.i); } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Prepares the notification badge for listening to the world chat |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="chatController">Chat Controlled to be listened</param> |
| | 35 | | public void Initialize(IChatController chatController) |
| | 36 | | { |
| 7 | 37 | | if (chatController == null) |
| 2 | 38 | | return; |
| | 39 | |
|
| 5 | 40 | | currentChatController = chatController; |
| 5 | 41 | | currentTimestampReading = CommonScriptableObjects.lastReadWorldChatMessages.Get(); |
| 5 | 42 | | UpdateUnreadMessages(); |
| | 43 | |
|
| 5 | 44 | | currentChatController.OnAddMessage -= ChatController_OnAddMessage; |
| 5 | 45 | | currentChatController.OnAddMessage += ChatController_OnAddMessage; |
| | 46 | |
|
| 5 | 47 | | CommonScriptableObjects.lastReadWorldChatMessages.OnChange -= LastReadWorldChatMessages_OnChange; |
| 5 | 48 | | CommonScriptableObjects.lastReadWorldChatMessages.OnChange += LastReadWorldChatMessages_OnChange; |
| 5 | 49 | | } |
| | 50 | |
|
| | 51 | | private void OnDestroy() |
| | 52 | | { |
| 9 | 53 | | if (currentChatController == null) |
| 4 | 54 | | return; |
| | 55 | |
|
| 5 | 56 | | currentChatController.OnAddMessage -= ChatController_OnAddMessage; |
| 5 | 57 | | CommonScriptableObjects.lastReadWorldChatMessages.OnChange -= LastReadWorldChatMessages_OnChange; |
| 5 | 58 | | } |
| | 59 | |
|
| | 60 | | private void ChatController_OnAddMessage(ChatMessage newMessage) |
| | 61 | | { |
| 14 | 62 | | if (newMessage.messageType == ChatMessage.Type.PUBLIC && |
| | 63 | | newMessage.sender != UserProfile.GetOwnUserProfile().userId) |
| | 64 | | { |
| | 65 | | // A new message from world is received |
| 12 | 66 | | UpdateUnreadMessages(); |
| | 67 | | } |
| 14 | 68 | | } |
| | 69 | |
|
| | 70 | | private void LastReadWorldChatMessages_OnChange(long current, long previous) |
| | 71 | | { |
| | 72 | | // The player reads the latest messages of [userId] |
| 1 | 73 | | currentTimestampReading = current; |
| 1 | 74 | | currentUnreadMessages = 0; |
| 1 | 75 | | UpdateUnreadMessages(); |
| 1 | 76 | | } |
| | 77 | |
|
| | 78 | | private void UpdateUnreadMessages() |
| | 79 | | { |
| 18 | 80 | | currentUnreadMessages = currentChatController.GetEntries() |
| | 81 | | .Count( |
| 58 | 82 | | msg => msg.messageType == ChatMessage.Type.PUBLIC && |
| | 83 | | msg.timestamp > (ulong)currentTimestampReading); |
| 18 | 84 | | } |
| | 85 | |
|
| | 86 | | private void RefreshNotificationBadge() |
| | 87 | | { |
| 19 | 88 | | if (currentUnreadMessagesValue > 0) |
| | 89 | | { |
| 12 | 90 | | notificationContainer.SetActive(true); |
| 12 | 91 | | notificationText.text = currentUnreadMessagesValue <= maxNumberToShow ? currentUnreadMessagesValue.ToString( |
| 12 | 92 | | } |
| | 93 | | else |
| | 94 | | { |
| 7 | 95 | | notificationContainer.SetActive(false); |
| | 96 | | } |
| 7 | 97 | | } |
| | 98 | | } |