| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Text; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | | using Random = System.Random; |
| | 8 | |
|
| | 9 | | namespace DCL.Chat.Notifications |
| | 10 | | { |
| | 11 | | public class ChatNotificationMessageComponentView : |
| | 12 | | BaseComponentView, |
| | 13 | | IChatNotificationMessageComponentView, |
| | 14 | | IComponentModelConfig<ChatNotificationMessageComponentModel>, |
| | 15 | | IShowableNotificationView |
| | 16 | | { |
| | 17 | | private const string NEAR_BY_CHANNEL = "~nearby"; |
| | 18 | |
|
| | 19 | | [Header("Prefab References")] |
| | 20 | | [SerializeField] internal Button button; |
| | 21 | | [SerializeField] public TMP_Text notificationMessage; |
| | 22 | | [SerializeField] internal TMP_Text notificationHeader; |
| | 23 | | [SerializeField] internal TMP_Text notificationSender; |
| | 24 | | [SerializeField] internal TMP_Text notificationTimestamp; |
| | 25 | | [SerializeField] internal ImageComponentView image; |
| | 26 | | [SerializeField] internal GameObject imageContainer; |
| | 27 | | [SerializeField] internal GameObject imageBackground; |
| | 28 | | [SerializeField] internal GameObject multiNotificationBackground; |
| | 29 | | [SerializeField] internal GameObject mentionMark; |
| | 30 | | [SerializeField] internal bool isPrivate; |
| | 31 | | [SerializeField] internal RectTransform backgroundTransform; |
| | 32 | | [SerializeField] internal RectTransform messageContainerTransform; |
| | 33 | | [SerializeField] internal bool isTopNorification; |
| | 34 | |
|
| | 35 | | [Header("Configuration")] |
| | 36 | | [SerializeField] internal ChatNotificationMessageComponentModel model; |
| | 37 | | [SerializeField] private Color privateColor; |
| | 38 | | [SerializeField] private Color publicColor; |
| | 39 | | [SerializeField] private Color standardColor; |
| | 40 | | [SerializeField] private Color[] channelColors; |
| | 41 | |
|
| | 42 | | public event Action<string> OnClickedNotification; |
| 20 | 43 | | public bool shouldAnimateFocus = true; |
| | 44 | | public string notificationTargetId; |
| | 45 | | private int maxHeaderCharacters, maxSenderCharacters; |
| | 46 | | private float startingXPosition; |
| | 47 | |
|
| | 48 | | public void Configure(ChatNotificationMessageComponentModel newModel) |
| | 49 | | { |
| 2 | 50 | | model = newModel; |
| 2 | 51 | | RefreshControl(); |
| 2 | 52 | | } |
| | 53 | |
|
| | 54 | | public override void Awake() |
| | 55 | | { |
| 19 | 56 | | base.Awake(); |
| 20 | 57 | | button?.onClick.AddListener(() => OnClickedNotification?.Invoke(notificationTargetId)); |
| 19 | 58 | | startingXPosition = messageContainerTransform.anchoredPosition.x; |
| 19 | 59 | | RefreshControl(); |
| 19 | 60 | | } |
| | 61 | |
|
| | 62 | | public override void Show(bool instant = false) |
| | 63 | | { |
| 0 | 64 | | showHideAnimator.animSpeedFactor = 0.7f; |
| 0 | 65 | | base.Show(instant); |
| 0 | 66 | | ForceUIRefresh(); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public override void OnFocus() |
| | 70 | | { |
| 0 | 71 | | base.OnFocus(); |
| 0 | 72 | | if (shouldAnimateFocus) |
| 0 | 73 | | messageContainerTransform.anchoredPosition = new Vector2(startingXPosition + 5, messageContainerTransfor |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public override void OnLoseFocus() |
| | 77 | | { |
| 24 | 78 | | base.OnLoseFocus(); |
| 24 | 79 | | if (shouldAnimateFocus) |
| 24 | 80 | | messageContainerTransform.anchoredPosition = new Vector2(startingXPosition, messageContainerTransform.an |
| 24 | 81 | | } |
| | 82 | |
|
| | 83 | | public override void Hide(bool instant = false) |
| | 84 | | { |
| 0 | 85 | | showHideAnimator.animSpeedFactor = 15f; |
| 0 | 86 | | base.Hide(instant); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public override void RefreshControl() |
| | 90 | | { |
| 26 | 91 | | if (model == null) |
| 0 | 92 | | return; |
| | 93 | |
|
| 26 | 94 | | SetMaxContentCharacters(model.maxContentCharacters); |
| 26 | 95 | | SetMaxHeaderCharacters(model.maxHeaderCharacters); |
| 26 | 96 | | SetMaxSenderCharacters(model.maxSenderCharacters); |
| 26 | 97 | | SetNotificationSender(model.messageSender); |
| 26 | 98 | | SetMessage(model.message); |
| 26 | 99 | | SetTimestamp(model.time); |
| 26 | 100 | | SetIsPrivate(model.isPrivate); |
| 26 | 101 | | SetNotificationHeader(model.messageHeader); |
| 26 | 102 | | SetImage(model.imageUri); |
| 26 | 103 | | SetImageVisibility(model.isImageVisible); |
| 26 | 104 | | SetOwnPlayerMention(model.isOwnPlayerMentioned); |
| | 105 | |
|
| 26 | 106 | | if (isTopNorification) return; |
| | 107 | |
|
| 26 | 108 | | if (model.isDockedLeft) |
| 24 | 109 | | DockLeft(); |
| | 110 | | else |
| 2 | 111 | | DockRight(); |
| 2 | 112 | | } |
| | 113 | |
|
| | 114 | | public void SetMessage(string message) |
| | 115 | | { |
| 32 | 116 | | model.message = notificationMessage.ReplaceUnsupportedCharacters(message, '?'); |
| 32 | 117 | | notificationMessage.text = message; |
| | 118 | |
|
| 32 | 119 | | ForceUIRefresh(); |
| 32 | 120 | | } |
| | 121 | |
|
| | 122 | | public void SetTimestamp(string timestamp) |
| | 123 | | { |
| 32 | 124 | | model.time = timestamp; |
| 32 | 125 | | notificationTimestamp.text = timestamp; |
| | 126 | |
|
| 32 | 127 | | ForceUIRefresh(); |
| 32 | 128 | | } |
| | 129 | |
|
| | 130 | | public void SetNotificationHeader(string header) |
| | 131 | | { |
| 33 | 132 | | if (!isPrivate) |
| | 133 | | { |
| 7 | 134 | | if (header == NEAR_BY_CHANNEL) |
| 4 | 135 | | notificationHeader.color = publicColor; |
| | 136 | | else |
| 3 | 137 | | SetRandomColorForChannel(header); |
| | 138 | | } |
| | 139 | |
|
| 33 | 140 | | model.messageHeader = header; |
| 33 | 141 | | if (header.Length <= maxHeaderCharacters) |
| 30 | 142 | | notificationHeader.text = header; |
| | 143 | | else |
| 3 | 144 | | notificationHeader.text = $"{header.Substring(0, maxHeaderCharacters)}..."; |
| | 145 | |
|
| 33 | 146 | | ForceUIRefresh(); |
| 33 | 147 | | } |
| | 148 | |
|
| | 149 | |
|
| | 150 | | private void SetRandomColorForChannel(string header) |
| | 151 | | { |
| 3 | 152 | | int seed = 0; |
| 3 | 153 | | byte[] ASCIIvalues = Encoding.ASCII.GetBytes(header); |
| 66 | 154 | | foreach (var value in ASCIIvalues) |
| | 155 | | { |
| 30 | 156 | | seed += (int)value; |
| | 157 | | } |
| 3 | 158 | | Random rand1 = new Random(seed); |
| 3 | 159 | | notificationHeader.color = channelColors[rand1.Next(0, channelColors.Length)]; |
| 3 | 160 | | } |
| | 161 | |
|
| | 162 | | public void SetNotificationSender(string sender) |
| | 163 | | { |
| 31 | 164 | | model.messageSender = sender; |
| 31 | 165 | | if (sender.Length <= maxSenderCharacters) |
| 29 | 166 | | notificationSender.text = $"{sender}"; |
| | 167 | | else |
| 2 | 168 | | notificationSender.text = $"{sender.Substring(0, maxSenderCharacters)}:"; |
| | 169 | |
|
| 31 | 170 | | ForceUIRefresh(); |
| 31 | 171 | | } |
| | 172 | |
|
| | 173 | | public void SetIsMultipleNotifications() |
| | 174 | | { |
| 0 | 175 | | imageBackground.SetActive(false); |
| 0 | 176 | | multiNotificationBackground.SetActive(true); |
| 0 | 177 | | notificationHeader.color = standardColor; |
| 0 | 178 | | ForceUIRefresh(); |
| 0 | 179 | | } |
| | 180 | |
|
| | 181 | | public void SetIsPrivate(bool isPrivate) |
| | 182 | | { |
| 33 | 183 | | model.isPrivate = isPrivate; |
| 33 | 184 | | this.isPrivate = isPrivate; |
| 33 | 185 | | imageBackground.SetActive(isPrivate); |
| 33 | 186 | | if (multiNotificationBackground != null) |
| 0 | 187 | | multiNotificationBackground.SetActive(false); |
| | 188 | |
|
| 33 | 189 | | if (isPrivate) |
| 25 | 190 | | notificationHeader.color = privateColor; |
| | 191 | | else |
| 8 | 192 | | notificationHeader.color = publicColor; |
| 33 | 193 | | ForceUIRefresh(); |
| 33 | 194 | | } |
| | 195 | |
|
| | 196 | | public void SetImage(string uri) |
| | 197 | | { |
| 26 | 198 | | if (!isPrivate) |
| 4 | 199 | | return; |
| | 200 | |
|
| 22 | 201 | | image.SetImage((Sprite)null); |
| 22 | 202 | | model.imageUri = uri; |
| 22 | 203 | | image.SetImage(uri); |
| 22 | 204 | | ForceUIRefresh(); |
| 22 | 205 | | } |
| | 206 | |
|
| | 207 | | public void SetImageVisibility(bool visible) |
| | 208 | | { |
| 31 | 209 | | model.isImageVisible = visible; |
| 31 | 210 | | imageContainer.SetActive(visible); |
| 31 | 211 | | } |
| | 212 | |
|
| | 213 | | public void SetMaxContentCharacters(int maxContentCharacters) |
| | 214 | | { |
| 32 | 215 | | maxContentCharacters = Mathf.Max(0, maxContentCharacters); |
| 32 | 216 | | model.maxContentCharacters = maxContentCharacters; |
| 32 | 217 | | } |
| | 218 | |
|
| | 219 | | public void SetMaxHeaderCharacters(int maxHeaderCharacters) |
| | 220 | | { |
| 28 | 221 | | model.maxHeaderCharacters = maxHeaderCharacters; |
| 28 | 222 | | this.maxHeaderCharacters = maxHeaderCharacters; |
| 28 | 223 | | } |
| | 224 | |
|
| | 225 | | public void SetMaxSenderCharacters(int maxSenderCharacters) |
| | 226 | | { |
| 26 | 227 | | model.maxSenderCharacters = maxSenderCharacters; |
| 26 | 228 | | this.maxSenderCharacters = maxSenderCharacters; |
| 26 | 229 | | } |
| | 230 | |
|
| | 231 | | public void SetNotificationTargetId(string notificationTargetId) |
| | 232 | | { |
| 5 | 233 | | model.notificationTargetId = notificationTargetId; |
| 5 | 234 | | this.notificationTargetId = notificationTargetId; |
| 5 | 235 | | } |
| | 236 | |
|
| | 237 | | public void SetOwnPlayerMention(bool isMentioned) |
| | 238 | | { |
| 31 | 239 | | model.isOwnPlayerMentioned = isMentioned; |
| | 240 | |
|
| 31 | 241 | | if (mentionMark != null) |
| 31 | 242 | | mentionMark.SetActive(isMentioned); |
| 31 | 243 | | } |
| | 244 | |
|
| | 245 | | public void DockRight() |
| | 246 | | { |
| 4 | 247 | | model.isDockedLeft = false; |
| 4 | 248 | | DockHorizontally(1f); |
| 4 | 249 | | } |
| | 250 | |
|
| | 251 | | public void DockLeft() |
| | 252 | | { |
| 27 | 253 | | model.isDockedLeft = true; |
| 27 | 254 | | DockHorizontally(0); |
| 27 | 255 | | } |
| | 256 | |
|
| | 257 | | private void DockHorizontally(float anchor) |
| | 258 | | { |
| 31 | 259 | | messageContainerTransform.pivot = new Vector2(anchor, 0.5f); |
| 31 | 260 | | messageContainerTransform.anchorMin = new Vector2(anchor, 0.5f); |
| 31 | 261 | | messageContainerTransform.anchorMax = new Vector2(anchor, 0.5f); |
| 31 | 262 | | backgroundTransform.pivot = new Vector2(anchor, 0.5f); |
| 31 | 263 | | } |
| | 264 | |
|
| | 265 | | private void ForceUIRefresh() => |
| 183 | 266 | | backgroundTransform.ForceUpdateLayout(); |
| | 267 | | } |
| | 268 | | } |