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