| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Chat; |
| | 6 | | using DCL.Interface; |
| | 7 | | using SocialFeaturesAnalytics; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public class PublicChatChannelController : IHUD |
| | 11 | | { |
| 0 | 12 | | public IChannelChatWindowView View { get; private set; } |
| | 13 | |
|
| | 14 | | private enum ChatWindowVisualState { NONE_VISIBLE, INPUT_MODE, PREVIEW_MODE } |
| | 15 | |
|
| | 16 | | public event Action OnBack; |
| | 17 | | public event Action OnClosed; |
| | 18 | | public event Action<bool> OnPreviewModeChanged; |
| | 19 | |
|
| | 20 | | private readonly IChatController chatController; |
| | 21 | | private readonly IUserProfileBridge userProfileBridge; |
| | 22 | | private readonly DataStore dataStore; |
| | 23 | | private readonly IProfanityFilter profanityFilter; |
| | 24 | | private readonly IMouseCatcher mouseCatcher; |
| | 25 | | private readonly InputAction_Trigger toggleChatTrigger; |
| | 26 | | private ChatHUDController chatHudController; |
| | 27 | | private double initTimeInSeconds; |
| | 28 | | private string channelId; |
| | 29 | | private ChatWindowVisualState currentState; |
| 16 | 30 | | private CancellationTokenSource deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 16 | 31 | | private CancellationTokenSource deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 32 | |
|
| 16 | 33 | | private string lastPrivateMessageRecipient = string.Empty; |
| | 34 | |
|
| 2 | 35 | | private UserProfile ownProfile => userProfileBridge.GetOwn(); |
| | 36 | |
|
| 16 | 37 | | public PublicChatChannelController(IChatController chatController, |
| | 38 | | IUserProfileBridge userProfileBridge, |
| | 39 | | DataStore dataStore, |
| | 40 | | IProfanityFilter profanityFilter, |
| | 41 | | IMouseCatcher mouseCatcher, |
| | 42 | | InputAction_Trigger toggleChatTrigger) |
| | 43 | | { |
| 16 | 44 | | this.chatController = chatController; |
| 16 | 45 | | this.userProfileBridge = userProfileBridge; |
| 16 | 46 | | this.dataStore = dataStore; |
| 16 | 47 | | this.profanityFilter = profanityFilter; |
| 16 | 48 | | this.mouseCatcher = mouseCatcher; |
| 16 | 49 | | this.toggleChatTrigger = toggleChatTrigger; |
| 16 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Initialize(IChannelChatWindowView view = null) |
| | 53 | | { |
| 16 | 54 | | view ??= PublicChatChannelComponentView.Create(); |
| 16 | 55 | | View = view; |
| 16 | 56 | | view.OnClose += HandleViewClosed; |
| 16 | 57 | | view.OnBack += HandleViewBacked; |
| 16 | 58 | | view.OnFocused += HandleViewFocused; |
| 16 | 59 | | View.OnClickOverWindow += HandleViewClicked; |
| | 60 | |
|
| | 61 | |
|
| 16 | 62 | | chatHudController = new ChatHUDController(dataStore, |
| | 63 | | userProfileBridge, |
| | 64 | | true, |
| | 65 | | profanityFilter); |
| 16 | 66 | | chatHudController.Initialize(view.ChatHUD); |
| 16 | 67 | | chatHudController.OnSendMessage += SendChatMessage; |
| 16 | 68 | | chatHudController.OnMessageUpdated += HandleMessageInputUpdated; |
| 16 | 69 | | chatHudController.OnInputFieldSelected -= HandleInputFieldSelected; |
| 16 | 70 | | chatHudController.OnInputFieldSelected += HandleInputFieldSelected; |
| 16 | 71 | | chatHudController.OnInputFieldDeselected -= HandleInputFieldDeselected; |
| 16 | 72 | | chatHudController.OnInputFieldDeselected += HandleInputFieldDeselected; |
| | 73 | |
|
| 16 | 74 | | chatController.OnAddMessage -= HandleMessageReceived; |
| 16 | 75 | | chatController.OnAddMessage += HandleMessageReceived; |
| | 76 | |
|
| 16 | 77 | | if (mouseCatcher != null) |
| 16 | 78 | | mouseCatcher.OnMouseLock += ActivatePreview; |
| | 79 | |
|
| 16 | 80 | | toggleChatTrigger.OnTriggered += HandleChatInputTriggered; |
| | 81 | |
|
| 16 | 82 | | initTimeInSeconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000.0; |
| | 83 | |
|
| 16 | 84 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 16 | 85 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| 16 | 86 | | } |
| | 87 | |
|
| | 88 | | public void Setup(string channelId) |
| | 89 | | { |
| 0 | 90 | | if (string.IsNullOrEmpty(channelId) || channelId == this.channelId) return; |
| 0 | 91 | | this.channelId = channelId; |
| | 92 | |
|
| | 93 | | // TODO: retrieve data from a channel provider |
| 0 | 94 | | View.Configure(new PublicChatChannelModel(this.channelId, ChatUtils.NEARBY_CHANNEL_ID, |
| | 95 | | "Talk to the people around you. If you move far away from someone you will lose contact. All whispers will b |
| | 96 | |
|
| 0 | 97 | | ReloadAllChats().Forget(); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public void Dispose() |
| | 101 | | { |
| 15 | 102 | | View.OnClose -= HandleViewClosed; |
| 15 | 103 | | View.OnBack -= HandleViewBacked; |
| | 104 | |
|
| 15 | 105 | | if (chatController != null) |
| 15 | 106 | | chatController.OnAddMessage -= HandleMessageReceived; |
| | 107 | |
|
| 15 | 108 | | chatHudController.OnSendMessage -= SendChatMessage; |
| 15 | 109 | | chatHudController.OnMessageUpdated -= HandleMessageInputUpdated; |
| 15 | 110 | | chatHudController.OnInputFieldSelected -= HandleInputFieldSelected; |
| 15 | 111 | | chatHudController.OnInputFieldDeselected -= HandleInputFieldDeselected; |
| | 112 | |
|
| 15 | 113 | | if (mouseCatcher != null) |
| 15 | 114 | | mouseCatcher.OnMouseLock -= ActivatePreview; |
| | 115 | |
|
| 15 | 116 | | toggleChatTrigger.OnTriggered -= HandleChatInputTriggered; |
| | 117 | |
|
| 15 | 118 | | if (View != null) |
| | 119 | | { |
| 15 | 120 | | View.OnFocused -= HandleViewFocused; |
| 15 | 121 | | View.OnClickOverWindow -= HandleViewClicked; |
| 15 | 122 | | View.Dispose(); |
| | 123 | | } |
| 15 | 124 | | } |
| | 125 | |
|
| | 126 | | private void SendChatMessage(ChatMessage message) |
| | 127 | | { |
| 3 | 128 | | var isValidMessage = !string.IsNullOrEmpty(message.body) && !string.IsNullOrWhiteSpace(message.body); |
| 3 | 129 | | var isPrivateMessage = message.messageType == ChatMessage.Type.PRIVATE; |
| | 130 | |
|
| 3 | 131 | | if (isPrivateMessage && isValidMessage) |
| 1 | 132 | | lastPrivateMessageRecipient = message.recipient; |
| | 133 | | else |
| 2 | 134 | | lastPrivateMessageRecipient = null; |
| | 135 | |
|
| 3 | 136 | | if (isValidMessage) |
| | 137 | | { |
| 2 | 138 | | chatHudController.ResetInputField(); |
| 2 | 139 | | chatHudController.FocusInputField(); |
| 2 | 140 | | } |
| | 141 | | else |
| | 142 | | { |
| 1 | 143 | | chatHudController.ResetInputField(true); |
| 1 | 144 | | ActivatePreview(); |
| 1 | 145 | | return; |
| | 146 | | } |
| | 147 | |
|
| 2 | 148 | | if (isPrivateMessage) |
| 1 | 149 | | message.body = $"/w {message.recipient} {message.body}"; |
| | 150 | |
|
| 2 | 151 | | chatController.Send(message); |
| 2 | 152 | | } |
| | 153 | |
|
| | 154 | | public void SetVisibility(bool visible, bool focusInputField) |
| | 155 | | { |
| 11 | 156 | | if (visible) |
| | 157 | | { |
| 8 | 158 | | View.Show(); |
| 8 | 159 | | MarkChatMessagesAsRead(); |
| | 160 | |
|
| 8 | 161 | | if (focusInputField) |
| 1 | 162 | | chatHudController.FocusInputField(); |
| 1 | 163 | | } |
| | 164 | | else |
| | 165 | | { |
| 3 | 166 | | chatHudController.UnfocusInputField(); |
| 3 | 167 | | View.Hide(); |
| | 168 | | } |
| 10 | 169 | | } |
| | 170 | |
|
| 9 | 171 | | public void SetVisibility(bool visible) => SetVisibility(visible, false); |
| | 172 | |
|
| | 173 | | private async UniTaskVoid ReloadAllChats() |
| | 174 | | { |
| 0 | 175 | | chatHudController.ClearAllEntries(); |
| | 176 | |
|
| | 177 | | const int entriesPerFrame = 10; |
| | 178 | | // TODO: filter entries by channelId |
| 0 | 179 | | var list = chatController.GetAllocatedEntries(); |
| 0 | 180 | | if (list.Count == 0) return; |
| | 181 | |
|
| 0 | 182 | | for (var i = list.Count - 1; i >= 0; i--) |
| | 183 | | { |
| 0 | 184 | | var message = list[i]; |
| 0 | 185 | | if (i % entriesPerFrame == 0) await UniTask.NextFrame(); |
| 0 | 186 | | HandleMessageReceived(message); |
| 0 | 187 | | } |
| 0 | 188 | | } |
| | 189 | |
|
| | 190 | | public void ActivatePreviewModeInstantly() |
| | 191 | | { |
| 3 | 192 | | deactivatePreviewCancellationToken.Cancel(); |
| 3 | 193 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 3 | 194 | | deactivateFadeOutCancellationToken.Cancel(); |
| 3 | 195 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 196 | |
|
| 3 | 197 | | View.ActivatePreviewInstantly(); |
| 3 | 198 | | chatHudController.ActivatePreview(); |
| 3 | 199 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 3 | 200 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| 3 | 201 | | OnPreviewModeChanged?.Invoke(true); |
| 2 | 202 | | } |
| | 203 | |
|
| 8 | 204 | | private void MarkChatMessagesAsRead() => chatController.MarkMessagesAsSeen(channelId); |
| | 205 | |
|
| 1 | 206 | | private void HandleViewClosed() => OnClosed?.Invoke(); |
| | 207 | |
|
| 0 | 208 | | private void HandleViewBacked() => OnBack?.Invoke(); |
| | 209 | |
|
| | 210 | | private void HandleMessageInputUpdated(string message) |
| | 211 | | { |
| 1 | 212 | | if (!string.IsNullOrEmpty(lastPrivateMessageRecipient) && message == "/r ") |
| 1 | 213 | | chatHudController.SetInputFieldText($"/w {lastPrivateMessageRecipient} "); |
| 1 | 214 | | } |
| | 215 | |
|
| | 216 | | private bool IsOldPrivateMessage(ChatMessage message) |
| | 217 | | { |
| 5 | 218 | | if (message.messageType != ChatMessage.Type.PRIVATE) return false; |
| 3 | 219 | | var timestampInSeconds = message.timestamp / 1000.0; |
| 3 | 220 | | return timestampInSeconds < initTimeInSeconds; |
| | 221 | | } |
| | 222 | |
|
| | 223 | | private void HandleMessageReceived(ChatMessage message) |
| | 224 | | { |
| 5 | 225 | | if (IsOldPrivateMessage(message)) return; |
| | 226 | |
|
| 3 | 227 | | chatHudController.AddChatMessage(message, View.IsActive); |
| | 228 | |
|
| 3 | 229 | | if (View.IsActive) |
| 0 | 230 | | MarkChatMessagesAsRead(); |
| | 231 | |
|
| 3 | 232 | | if (message.messageType == ChatMessage.Type.PRIVATE && message.recipient == ownProfile.userId) |
| 1 | 233 | | lastPrivateMessageRecipient = userProfileBridge.Get(message.sender).userName; |
| | 234 | |
|
| 3 | 235 | | deactivatePreviewCancellationToken.Cancel(); |
| 3 | 236 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 3 | 237 | | deactivateFadeOutCancellationToken.Cancel(); |
| 3 | 238 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 239 | |
|
| 3 | 240 | | if (currentState.Equals(ChatWindowVisualState.NONE_VISIBLE)) |
| | 241 | | { |
| 0 | 242 | | ActivatePreview(); |
| 0 | 243 | | } |
| 3 | 244 | | else if (currentState.Equals(ChatWindowVisualState.PREVIEW_MODE)) |
| | 245 | | { |
| 3 | 246 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| | 247 | | } |
| 3 | 248 | | } |
| | 249 | |
|
| | 250 | | private void HandleInputFieldSelected() |
| | 251 | | { |
| 1 | 252 | | deactivatePreviewCancellationToken.Cancel(); |
| 1 | 253 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 1 | 254 | | DeactivatePreview(); |
| 1 | 255 | | } |
| | 256 | |
|
| | 257 | | private void HandleInputFieldDeselected() |
| | 258 | | { |
| 1 | 259 | | if (View.IsFocused) |
| 0 | 260 | | return; |
| 1 | 261 | | WaitThenActivatePreview(deactivatePreviewCancellationToken.Token).Forget(); |
| 1 | 262 | | } |
| | 263 | |
|
| | 264 | | private void HandleViewFocused(bool focused) |
| | 265 | | { |
| 0 | 266 | | if (focused) |
| | 267 | | { |
| 0 | 268 | | deactivatePreviewCancellationToken.Cancel(); |
| 0 | 269 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 0 | 270 | | deactivateFadeOutCancellationToken.Cancel(); |
| 0 | 271 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| 0 | 272 | | if (currentState.Equals(ChatWindowVisualState.NONE_VISIBLE)) |
| | 273 | | { |
| 0 | 274 | | ActivatePreviewOnMessages(); |
| | 275 | | } |
| 0 | 276 | | } |
| | 277 | | else |
| | 278 | | { |
| 0 | 279 | | if (chatHudController.IsInputSelected) |
| 0 | 280 | | return; |
| | 281 | |
|
| 0 | 282 | | if (currentState.Equals(ChatWindowVisualState.INPUT_MODE)) |
| | 283 | | { |
| 0 | 284 | | WaitThenActivatePreview(deactivatePreviewCancellationToken.Token).Forget(); |
| 0 | 285 | | return; |
| | 286 | | } |
| | 287 | |
|
| 0 | 288 | | if (currentState.Equals(ChatWindowVisualState.PREVIEW_MODE)) |
| | 289 | | { |
| 0 | 290 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| | 291 | | } |
| | 292 | | } |
| 0 | 293 | | } |
| | 294 | |
|
| | 295 | | private void HandleViewClicked() |
| | 296 | | { |
| 0 | 297 | | if (currentState.Equals(ChatWindowVisualState.INPUT_MODE)) |
| 0 | 298 | | return; |
| 0 | 299 | | DeactivatePreview(); |
| 0 | 300 | | } |
| | 301 | |
|
| | 302 | | private async UniTaskVoid WaitThenActivatePreview(CancellationToken cancellationToken) |
| | 303 | | { |
| 3 | 304 | | await UniTask.Delay(3000, cancellationToken: cancellationToken); |
| 1 | 305 | | await UniTask.SwitchToMainThread(cancellationToken); |
| 1 | 306 | | if (cancellationToken.IsCancellationRequested) return; |
| 1 | 307 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 1 | 308 | | ActivatePreview(); |
| 1 | 309 | | } |
| | 310 | |
|
| | 311 | | private async UniTaskVoid WaitThenFadeOutMessages(CancellationToken cancellationToken) |
| | 312 | | { |
| 62 | 313 | | await UniTask.Delay(30000, cancellationToken: cancellationToken); |
| 1 | 314 | | await UniTask.SwitchToMainThread(cancellationToken); |
| 1 | 315 | | if (cancellationToken.IsCancellationRequested) return; |
| 1 | 316 | | chatHudController.FadeOutMessages(); |
| 1 | 317 | | currentState = ChatWindowVisualState.NONE_VISIBLE; |
| 1 | 318 | | } |
| | 319 | |
|
| | 320 | | public void ActivatePreview() |
| | 321 | | { |
| 4 | 322 | | View.ActivatePreview(); |
| 4 | 323 | | chatHudController.ActivatePreview(); |
| 4 | 324 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 4 | 325 | | WaitThenFadeOutMessages(deactivateFadeOutCancellationToken.Token).Forget(); |
| 4 | 326 | | OnPreviewModeChanged?.Invoke(true); |
| 4 | 327 | | } |
| | 328 | |
|
| | 329 | | public void ActivatePreviewOnMessages() |
| | 330 | | { |
| 0 | 331 | | chatHudController.ActivatePreview(); |
| 0 | 332 | | OnPreviewModeChanged?.Invoke(true); |
| 0 | 333 | | currentState = ChatWindowVisualState.PREVIEW_MODE; |
| 0 | 334 | | } |
| | 335 | |
|
| | 336 | | public void DeactivatePreview() |
| | 337 | | { |
| 3 | 338 | | deactivatePreviewCancellationToken.Cancel(); |
| 3 | 339 | | deactivatePreviewCancellationToken = new CancellationTokenSource(); |
| 3 | 340 | | deactivateFadeOutCancellationToken.Cancel(); |
| 3 | 341 | | deactivateFadeOutCancellationToken = new CancellationTokenSource(); |
| | 342 | |
|
| 3 | 343 | | View.DeactivatePreview(); |
| 3 | 344 | | chatHudController.DeactivatePreview(); |
| 3 | 345 | | OnPreviewModeChanged?.Invoke(false); |
| 3 | 346 | | currentState = ChatWindowVisualState.INPUT_MODE; |
| 3 | 347 | | } |
| | 348 | |
|
| | 349 | | private void HandleChatInputTriggered(DCLAction_Trigger action) |
| | 350 | | { |
| 0 | 351 | | if (!View.IsActive) return; |
| 0 | 352 | | chatHudController.FocusInputField(); |
| 0 | 353 | | } |
| | 354 | | } |