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