Пример #1
0
        private void network_MessageReceived(MeshNetwork.Peer peer, MessageItem message)
        {
            switch (message.Type)
            {
            case MessageType.Info:
                AddMessage(new ChatMessageInfoItem(message), peer.IsSelfPeer);
                break;

            case MessageType.TextMessage:
                ChatMessageTextItem textItem = new ChatMessageTextItem(peer, message);
                textItem.ForwardTo += MessageItem_ForwardTo;

                AddMessage(textItem, peer.IsSelfPeer);
                ShowPeerTypingNotification(peer.ProfileDisplayName, false);
                break;

            case MessageType.InlineImage:
            case MessageType.FileAttachment:
                ChatMessageFileItem fileItem = new ChatMessageFileItem(peer, message);
                fileItem.ForwardTo += MessageItem_ForwardTo;

                AddMessage(fileItem, peer.IsSelfPeer);
                break;
            }
        }
Пример #2
0
        private List <CustomListViewItem> ConvertToListViewItems(MessageItem[] items, bool updateLastMessageInChatItem)
        {
            MeshNetwork.Peer[] peerList = _network.GetPeers();

            List <CustomListViewItem> listItems = new List <CustomListViewItem>(items.Length);
            DateTime lastItemDate    = new DateTime();
            string   lastMessage     = null;
            DateTime lastMessageDate = new DateTime();

            foreach (MessageItem item in items)
            {
                if (lastItemDate.Date < item.MessageDate.Date)
                {
                    lastItemDate = item.MessageDate;
                    listItems.Add(new ChatMessageInfoItem(new MessageItem(lastItemDate)));
                }

                switch (item.Type)
                {
                case MessageType.Info:
                    listItems.Add(new ChatMessageInfoItem(item));
                    break;

                case MessageType.TextMessage:
                {
                    MeshNetwork.Peer sender = null;

                    foreach (MeshNetwork.Peer peer in peerList)
                    {
                        if (peer.PeerUserId.Equals(item.SenderUserId))
                        {
                            sender = peer;
                            break;
                        }
                    }

                    if (sender == null)
                    {
                        continue;
                    }

                    ChatMessageTextItem textItem = new ChatMessageTextItem(sender, item);
                    textItem.ForwardTo += MessageItem_ForwardTo;

                    listItems.Add(textItem);

                    if (sender == null)
                    {
                        lastMessage = item.SenderUserId + ": " + item.MessageText;
                    }
                    else if (sender.IsSelfPeer)
                    {
                        lastMessage = item.MessageText;
                    }
                    else
                    {
                        lastMessage = sender.ProfileDisplayName + ": " + item.MessageText;
                    }

                    lastMessageDate = item.MessageDate;
                }
                break;

                case MessageType.InlineImage:
                case MessageType.FileAttachment:
                {
                    MeshNetwork.Peer sender = null;

                    foreach (MeshNetwork.Peer peer in peerList)
                    {
                        if (peer.PeerUserId.Equals(item.SenderUserId))
                        {
                            sender = peer;
                            break;
                        }
                    }

                    if (sender == null)
                    {
                        continue;
                    }

                    ChatMessageFileItem fileItem = new ChatMessageFileItem(sender, item);
                    fileItem.ForwardTo += MessageItem_ForwardTo;

                    listItems.Add(fileItem);

                    string messageText = item.MessageText;
                    if (string.IsNullOrEmpty(messageText))
                    {
                        messageText = "file attachment";
                    }

                    if (sender == null)
                    {
                        lastMessage = item.SenderUserId + ": " + messageText;
                    }
                    else if (sender.IsSelfPeer)
                    {
                        lastMessage = messageText;
                    }
                    else
                    {
                        lastMessage = sender.ProfileDisplayName + ": " + messageText;
                    }

                    lastMessageDate = item.MessageDate;
                }
                break;
                }
            }

            if (updateLastMessageInChatItem && (lastMessage != null))
            {
                _chatItem.SetLastMessage(lastMessage, lastMessageDate, false);
            }

            return(listItems);
        }