private void AddMessageToList(Microsoft.Bot.Connector.DirectLine.Activity message) { MessagesList.Insert(0, message); Adapter.NotifyItemInserted(0); MessagesRecycler.ScrollToPosition(0); }
private void RbInstantMessaging_MessageReceived(object sender, Rainbow.Events.MessageEventArgs e) { if (e.ConversationId == this.conversationId) { log.Debug("[RbInstantMessaging_MessageReceived] - FromJId:[{0}] - ToJid:[{1}] - CarbonCopy:[{2}] - Message.Id:[{3}] - Message.ReplaceId:[{4}]", e.Message.FromJid, e.Message.ToJid, e.CarbonCopy, e.Message.Id, e.Message.ReplaceId); InstantMessaging.Model.Message newMsg = GetMessageFromRBMessage(e.Message, rbConversation.Type); if (newMsg == null) { log.Warn("[RbInstantMessaging_MessageReceived] - Impossible to have Model.Message from XMPP Message - Message.Id:[{3}]", e.Message.Id); return; } // Since message is not null, set the Avatar Source newMsg.AvatarSource = Helper.GetContactAvatarImageSource(newMsg.PeerId); // Manage incoming REPLACE message if (!String.IsNullOrEmpty(e.Message.ReplaceId)) { InstantMessaging.Model.Message previousMessage = GetMessageByMessageId(e.Message.Id); if (previousMessage == null) { // We do nothing in this case ... Message not in the cache, so not visible ... return; } previousMessage.Body = newMsg.Body; previousMessage.BodyIsVisible = String.IsNullOrEmpty(previousMessage.Body) ? "False" : "True"; previousMessage.EditedIsVisible = "True"; } // Manage incoming NEW message else { // It's a new message to add in the list newMessageAdded = true; lock (lockObservableMessagesList) { // Do we have already some message ? if (MessagesList.Count == 0) { MessagesList.Add(newMsg); } else { // Add to the list but need to check date InstantMessaging.Model.Message storedMsg; bool newMsgInserted = false; int nb = MessagesList.Count - 1; for (int i = nb; i > 0; i--) { storedMsg = MessagesList[i]; if (newMsg.MessageDateTime > storedMsg.MessageDateTime) { if (i == nb) { MessagesList.Add(newMsg); } else { MessagesList.Insert(i, newMsg); } newMsgInserted = true; break; } } // If we don't have already added the new message, we insert it to the first place if (!newMsgInserted) { MessagesList.Insert(0, newMsg); } } } } // Mark the message as read if (XamarinApplication.CurrentConversationId == this.conversationId) { XamarinApplication.RbInstantMessaging.MarkMessageAsRead(this.conversationId, newMsg.Id, null); } } }