| | 1 | | using DCL; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Shows the number of unread messages from a friend. |
| | 7 | | /// </summary> |
| | 8 | | public class UnreadNotificationBadge : MonoBehaviour |
| | 9 | | { |
| | 10 | | public TextMeshProUGUI notificationText; |
| | 11 | | public GameObject notificationContainer; |
| 91 | 12 | | public int maxNumberToShow = 9; |
| | 13 | | public GameObject ownPlayerMentionMark; |
| | 14 | |
|
| | 15 | | private IChatController chatController; |
| | 16 | | private DataStore_Mentions mentionsDataStore; |
| | 17 | | private string currentUserId; |
| | 18 | | private int currentUnreadMessagesValue; |
| | 19 | | private bool isInitialized; |
| | 20 | |
|
| | 21 | | public int CurrentUnreadMessages |
| | 22 | | { |
| 22 | 23 | | get => currentUnreadMessagesValue; |
| | 24 | | set |
| | 25 | | { |
| 159 | 26 | | currentUnreadMessagesValue = value; |
| | 27 | |
|
| 159 | 28 | | if (currentUnreadMessagesValue > 0) |
| | 29 | | { |
| 27 | 30 | | notificationContainer.SetActive(true); |
| 27 | 31 | | notificationText.text = currentUnreadMessagesValue <= maxNumberToShow ? currentUnreadMessagesValue.ToStr |
| | 32 | | } |
| | 33 | | else |
| | 34 | | { |
| 132 | 35 | | notificationContainer.SetActive(false); |
| | 36 | |
|
| 132 | 37 | | if (ownPlayerMentionMark != null) |
| 115 | 38 | | ownPlayerMentionMark.SetActive(false); |
| | 39 | | } |
| 132 | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Prepares the notification badge for listening to a specific user |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="chatController">Chat Controlled to be listened</param> |
| | 47 | | /// <param name="userId">User ID to listen to</param> |
| | 48 | | public void Initialize( |
| | 49 | | IChatController chatController, |
| | 50 | | string userId, |
| | 51 | | DataStore_Mentions mentionsDataStore) |
| | 52 | | { |
| 111 | 53 | | if (chatController == null) |
| 0 | 54 | | return; |
| | 55 | |
|
| 111 | 56 | | this.chatController = chatController; |
| 111 | 57 | | currentUserId = userId; |
| | 58 | |
|
| 111 | 59 | | UpdateUnreadMessages(); |
| | 60 | |
|
| 111 | 61 | | chatController.OnUserUnseenMessagesUpdated -= HandleUnseenMessagesUpdated; |
| 111 | 62 | | chatController.OnUserUnseenMessagesUpdated += HandleUnseenMessagesUpdated; |
| | 63 | |
|
| 111 | 64 | | chatController.OnChannelUnseenMessagesUpdated -= HandleChannelUnseenMessagesUpdated; |
| 111 | 65 | | chatController.OnChannelUnseenMessagesUpdated += HandleChannelUnseenMessagesUpdated; |
| | 66 | |
|
| 111 | 67 | | if (mentionsDataStore != null) |
| | 68 | | { |
| 111 | 69 | | this.mentionsDataStore = mentionsDataStore; |
| 111 | 70 | | mentionsDataStore.ownPlayerMentionedInChannel.OnChange -= HandleOwnPlayerMentioned; |
| 111 | 71 | | mentionsDataStore.ownPlayerMentionedInChannel.OnChange += HandleOwnPlayerMentioned; |
| 111 | 72 | | mentionsDataStore.ownPlayerMentionedInDM.OnChange -= HandleOwnPlayerMentioned; |
| 111 | 73 | | mentionsDataStore.ownPlayerMentionedInDM.OnChange += HandleOwnPlayerMentioned; |
| | 74 | | } |
| | 75 | |
|
| 111 | 76 | | isInitialized = true; |
| 111 | 77 | | } |
| | 78 | |
|
| | 79 | | private void HandleUnseenMessagesUpdated(string userId, int unseenMessages) |
| | 80 | | { |
| 18 | 81 | | if (userId != currentUserId) return; |
| 14 | 82 | | CurrentUnreadMessages = unseenMessages; |
| 14 | 83 | | } |
| | 84 | |
|
| | 85 | | private void HandleChannelUnseenMessagesUpdated(string channelId, int unseenMessages) |
| | 86 | | { |
| 19 | 87 | | if (channelId != currentUserId) return; |
| 15 | 88 | | CurrentUnreadMessages = unseenMessages; |
| 15 | 89 | | } |
| | 90 | |
|
| | 91 | | private void OnEnable() |
| | 92 | | { |
| 247 | 93 | | if (!isInitialized) return; |
| 19 | 94 | | UpdateUnreadMessages(); |
| 19 | 95 | | } |
| | 96 | |
|
| | 97 | | private void OnDestroy() |
| | 98 | | { |
| 63 | 99 | | if (chatController != null) |
| | 100 | | { |
| 59 | 101 | | chatController.OnUserUnseenMessagesUpdated -= HandleUnseenMessagesUpdated; |
| 59 | 102 | | chatController.OnChannelUnseenMessagesUpdated -= HandleChannelUnseenMessagesUpdated; |
| | 103 | | } |
| | 104 | |
|
| 63 | 105 | | if (mentionsDataStore != null) |
| 59 | 106 | | mentionsDataStore.ownPlayerMentionedInChannel.OnChange -= HandleOwnPlayerMentioned; |
| 63 | 107 | | } |
| | 108 | |
|
| | 109 | | private void UpdateUnreadMessages() => |
| 130 | 110 | | CurrentUnreadMessages = chatController.GetAllocatedUnseenMessages(currentUserId) + chatController.GetAllocatedUn |
| | 111 | |
|
| | 112 | | private void HandleOwnPlayerMentioned(string channelId, string previous) |
| | 113 | | { |
| 0 | 114 | | if (ownPlayerMentionMark == null) return; |
| 0 | 115 | | if (channelId != currentUserId) return; |
| 0 | 116 | | ownPlayerMentionMark.SetActive(true); |
| 0 | 117 | | } |
| | 118 | | } |