| | 1 | | using DCL.Chat; |
| | 2 | | using DCL.Social.Chat; |
| | 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 int currentUnreadMessagesValue; |
| | 16 | | private IChatController chatController; |
| | 17 | |
|
| | 18 | | public int CurrentUnreadMessages |
| | 19 | | { |
| 7 | 20 | | get => currentUnreadMessagesValue; |
| | 21 | | set |
| | 22 | | { |
| 18 | 23 | | currentUnreadMessagesValue = value; |
| 18 | 24 | | if (currentUnreadMessagesValue > 0) |
| | 25 | | { |
| 12 | 26 | | notificationContainer.SetActive(true); |
| 12 | 27 | | notificationText.text = currentUnreadMessagesValue <= maxNumberToShow |
| | 28 | | ? currentUnreadMessagesValue.ToString() |
| | 29 | | : $"+{maxNumberToShow}"; |
| | 30 | | } |
| | 31 | | else |
| | 32 | | { |
| 6 | 33 | | notificationContainer.SetActive(false); |
| | 34 | | } |
| 6 | 35 | | } |
| | 36 | | } |
| | 37 | |
|
| | 38 | | private void Start() |
| | 39 | | { |
| 2 | 40 | | Initialize(ChatController.i); |
| 2 | 41 | | } |
| | 42 | |
|
| | 43 | | private void OnDestroy() |
| | 44 | | { |
| 7 | 45 | | if (chatController != null) |
| 5 | 46 | | chatController.OnTotalUnseenMessagesUpdated -= UpdateTotalUnseenMessages; |
| 7 | 47 | | } |
| | 48 | |
|
| 9 | 49 | | private void OnEnable() => UpdateTotalUnseenMessages(); |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Prepares the notification badge for listening to the world chat |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="chatController">Chat Controlled to be listened</param> |
| | 55 | | public void Initialize(IChatController chatController) |
| | 56 | | { |
| 5 | 57 | | if (chatController == null) return; |
| 5 | 58 | | this.chatController = chatController; |
| 5 | 59 | | chatController.OnTotalUnseenMessagesUpdated += UpdateTotalUnseenMessages; |
| 5 | 60 | | UpdateTotalUnseenMessages(); |
| 5 | 61 | | } |
| | 62 | |
|
| | 63 | | private void UpdateTotalUnseenMessages(int totalUnseenMessages) => |
| 13 | 64 | | CurrentUnreadMessages = totalUnseenMessages; |
| | 65 | |
|
| | 66 | | private void UpdateTotalUnseenMessages() |
| | 67 | | { |
| 23 | 68 | | if (chatController == null) return; |
| 5 | 69 | | CurrentUnreadMessages = chatController.TotalUnseenMessages; |
| 5 | 70 | | } |
| | 71 | | } |