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