using System; using System.Collections; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Net.Http; using System.Text; using BITKit; using BITKit.StateMachine; using BITKit.UX; using Cysharp.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Net.BITKit.Chat; using Newtonsoft.Json; using UnityEngine; using UnityEngine.UIElements; namespace Net.Project.B.PDA.App { [Serializable] public struct Chat : IApp { public string PackageName => "com.bndroid.chat"; public string AppName => "聊天"; } public class AppChat : PDAApp { private readonly IChatService _chatService; private readonly IServiceProvider _serviceProvider; protected override string DocumentPath => "ui_app_chat"; [UXBindPath("chat-panel")] private VisualElement _chatPanel; [UXBindPath("chat_list-container")] private VisualElement _chatListContainer; [UXBindPath("chat-container")] private VisualElement _chatContainer; [UXBindPath("message-field")] private TextField _messageField; [UXBindPath("send-button")] private Button _sendButton; [UXBindPath("chat-scroll")] private ScrollView _chatScroll; [UXBindPath("chat_user-label")] private Label _chatUserLabel; [UXBindPath("is_typing-label")] private Label _isTypingLabel; private VisualTreeAsset _chatTemplate; private VisualTreeAsset _chatListTemplate; private readonly ValidHandle _userTyping = new(); private readonly Dictionary _chatList = new(); private string _currentUserId = string.Empty; public AppChat(IServiceProvider serviceProvider, IChatService chatService) { _serviceProvider = serviceProvider; _chatService = chatService; } public override void Initialize() { base.Initialize(); _chatTemplate = _chatContainer.Q().templateSource; _chatListTemplate = _chatListContainer.Q().templateSource; _messageField.RegisterValueChangedCallback(x => _sendButton.SetActive(string.IsNullOrEmpty(x.newValue) is false)); _sendButton.clicked += SendMessage; _messageField.value = string.Empty; _messageField.BlinkingCursor(); _messageField.RegisterCallback(OnKeyDown); _userTyping.AddListener(_isTypingLabel.SetActive); _userTyping.Invoke(); _chatListContainer.Clear() ; _chatContainer.Clear(); _chatService.OnMessageReceived += OnMessageReceived; _chatPanel.SetActive(false); } private void OnMessageReceived(ChatMessage obj) { if (obj.FromUserId != Environment.UserName && obj.ToUserId != Environment.UserName) return; _serviceProvider.GetRequiredService().Badge(App.PackageName); var chatList = GetOrCreateChatList(obj); if (obj.FromUserId != _currentUserId && obj.ToUserId != _currentUserId) { chatList.AddToClassList("allow-badge"); return; } CreateMessage(obj); } public override async UniTask OnStateEntryAsync(IState old) { await base.OnStateEntryAsync(old); _chatListContainer.Clear(); _chatList.Clear(); foreach (var chatMessage in await _chatService.GetChatList(Environment.UserName)) { GetOrCreateChatList(chatMessage); } } private VisualElement GetOrCreateChatList(ChatMessage chatMessage) { var userName = string.Equals(chatMessage.FromUserId, Environment.UserName) ? chatMessage.ToUserId : chatMessage.FromUserId; if (_chatList.TryGetValue(userName, out var container)) { container.Get