| | 1 | | using System; |
| | 2 | | using TMPro; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.UI; |
| | 5 | | using static PrivateChatEntry; |
| | 6 | |
|
| | 7 | | public class PrivateChatEntry : BaseComponentView, IComponentModelConfig<PrivateChatEntryModel> |
| | 8 | | { |
| | 9 | | [SerializeField] internal Button openChatButton; |
| | 10 | | [SerializeField] internal PrivateChatEntryModel model; |
| | 11 | | [SerializeField] internal TMP_Text userNameLabel; |
| | 12 | | [SerializeField] internal TMP_Text lastMessageLabel; |
| | 13 | | [SerializeField] internal ImageComponentView picture; |
| | 14 | | [SerializeField] internal UnreadNotificationBadge unreadNotifications; |
| | 15 | | [SerializeField] internal Button optionsButton; |
| | 16 | | [SerializeField] internal GameObject blockedContainer; |
| | 17 | | [SerializeField] internal GameObject onlineStatusContainer; |
| | 18 | | [SerializeField] internal GameObject offlineStatusContainer; |
| | 19 | | [SerializeField] internal RectTransform userContextMenuPositionReference; |
| | 20 | |
|
| | 21 | | private UserContextMenu userContextMenu; |
| | 22 | | private IChatController chatController; |
| | 23 | |
|
| 0 | 24 | | public PrivateChatEntryModel Model => model; |
| | 25 | |
|
| | 26 | | public event Action<PrivateChatEntry> OnOpenChat; |
| | 27 | |
|
| | 28 | | public static PrivateChatEntry Create() |
| | 29 | | { |
| 7 | 30 | | return Instantiate(Resources.Load<PrivateChatEntry>("SocialBarV1/WhisperChannelElement")); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public override void Awake() |
| | 34 | | { |
| 40 | 35 | | base.Awake(); |
| 40 | 36 | | optionsButton.onClick.AddListener(() => |
| | 37 | | { |
| 0 | 38 | | userContextMenu.Show(model.userId); |
| 0 | 39 | | Dock(userContextMenu); |
| 0 | 40 | | }); |
| 42 | 41 | | openChatButton.onClick.AddListener(() => OnOpenChat?.Invoke(this)); |
| 40 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Initialize(IChatController chatController, |
| | 45 | | UserContextMenu userContextMenu) |
| | 46 | | { |
| 36 | 47 | | this.chatController = chatController; |
| 36 | 48 | | this.userContextMenu = userContextMenu; |
| 36 | 49 | | userContextMenu.OnBlock -= HandleUserBlocked; |
| 36 | 50 | | userContextMenu.OnBlock += HandleUserBlocked; |
| 36 | 51 | | } |
| | 52 | |
|
| | 53 | | public void Configure(PrivateChatEntryModel newModel) |
| | 54 | | { |
| 43 | 55 | | model = newModel; |
| 43 | 56 | | RefreshControl(); |
| 43 | 57 | | } |
| | 58 | |
|
| | 59 | | public override void RefreshControl() |
| | 60 | | { |
| 43 | 61 | | userNameLabel.text = model.userName; |
| 43 | 62 | | lastMessageLabel.text = model.lastMessage; |
| 43 | 63 | | lastMessageLabel.gameObject.SetActive(!string.IsNullOrEmpty(model.lastMessage)); |
| 43 | 64 | | SetBlockStatus(model.isBlocked); |
| 43 | 65 | | SetPresence(model.isOnline); |
| 43 | 66 | | unreadNotifications.Initialize(chatController, model.userId); |
| | 67 | |
|
| 43 | 68 | | if (model.imageFetchingEnabled) |
| 1 | 69 | | EnableAvatarSnapshotFetching(); |
| | 70 | | else |
| 42 | 71 | | DisableAvatarSnapshotFetching(); |
| 42 | 72 | | } |
| | 73 | |
|
| | 74 | | private void HandleUserBlocked(string userId, bool blocked) |
| | 75 | | { |
| 0 | 76 | | if (userId != model.userId) return; |
| 0 | 77 | | SetBlockStatus(blocked); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void SetBlockStatus(bool isBlocked) |
| | 81 | | { |
| 43 | 82 | | model.isBlocked = isBlocked; |
| 43 | 83 | | blockedContainer.SetActive(isBlocked); |
| 43 | 84 | | } |
| | 85 | |
|
| | 86 | | private void SetPresence(bool isOnline) |
| | 87 | | { |
| 43 | 88 | | model.isOnline = isOnline; |
| 43 | 89 | | onlineStatusContainer.SetActive(isOnline && !model.isBlocked); |
| 43 | 90 | | offlineStatusContainer.SetActive(!isOnline && !model.isBlocked); |
| 43 | 91 | | } |
| | 92 | |
|
| | 93 | | private void Dock(UserContextMenu userContextMenu) |
| | 94 | | { |
| 0 | 95 | | var menuTransform = (RectTransform) userContextMenu.transform; |
| 0 | 96 | | menuTransform.pivot = userContextMenuPositionReference.pivot; |
| 0 | 97 | | menuTransform.position = userContextMenuPositionReference.position; |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public bool IsVisible(RectTransform container) |
| | 101 | | { |
| 19 | 102 | | if (!gameObject.activeSelf) return false; |
| 19 | 103 | | return ((RectTransform) transform).CountCornersVisibleFrom(container) > 0; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public void EnableAvatarSnapshotFetching() |
| | 107 | | { |
| 40 | 108 | | if (model.imageFetchingEnabled) return; |
| 38 | 109 | | picture.Configure(new ImageComponentModel {uri = model.pictureUrl}); |
| 38 | 110 | | model.imageFetchingEnabled = true; |
| 38 | 111 | | } |
| | 112 | |
|
| | 113 | | public void DisableAvatarSnapshotFetching() |
| | 114 | | { |
| 85 | 115 | | if (!model.imageFetchingEnabled) return; |
| 1 | 116 | | picture.SetImage((string) null); |
| 1 | 117 | | model.imageFetchingEnabled = false; |
| 1 | 118 | | } |
| | 119 | |
|
| | 120 | | [Serializable] |
| | 121 | | public class PrivateChatEntryModel : BaseComponentModel |
| | 122 | | { |
| | 123 | | public string userId; |
| | 124 | | public string userName; |
| | 125 | | public string lastMessage; |
| | 126 | | public ulong lastMessageTimestamp; |
| | 127 | | public string pictureUrl; |
| | 128 | | public bool isBlocked; |
| | 129 | | public bool isOnline; |
| | 130 | | public bool imageFetchingEnabled; |
| | 131 | |
|
| 43 | 132 | | public PrivateChatEntryModel(string userId, string userName, string lastMessage, string pictureUrl, bool isBlock |
| | 133 | | ulong lastMessageTimestamp) |
| | 134 | | { |
| 43 | 135 | | this.userId = userId; |
| 43 | 136 | | this.userName = userName; |
| 43 | 137 | | this.lastMessage = lastMessage; |
| 43 | 138 | | this.pictureUrl = pictureUrl; |
| 43 | 139 | | this.isBlocked = isBlocked; |
| 43 | 140 | | this.isOnline = isOnline; |
| 43 | 141 | | this.lastMessageTimestamp = lastMessageTimestamp; |
| 43 | 142 | | } |
| | 143 | | } |
| | 144 | | } |