private void SetHubConnectionHandlerToRecieveLeftBehindMessages(string yourPhoneNumber, Page chatPage, Friend friendChattingWith) { ScrollView scrollViewChatMsgArea = (ScrollView)chatPage.FindByName("scrollViewChatMsgArea"); StackLayout chat_messages_area = (StackLayout)chatPage.FindByName("chat_messages_area"); hubConnection.On <List <ChatMessage> >(yourPhoneNumber + "-left_behind", async(leftBehindChatMessages) => { List <PrimaryKeyLeftBehindMsg> listOfPrimaryKeysForLeftBehindMsgs = new List <PrimaryKeyLeftBehindMsg>(); //Used to tell chatchub that messages have been received foreach (ChatMessage leftBehindChatMsg in leftBehindChatMessages) { if ((leftBehindChatMsg.SeenByRecipient).Equals("no")) { //PLACE THE LEFT BEHIND MESSAGE IN YOUR CHAT AREA leftBehindChatMsg.Writer = "Friend"; ChatMessageLabelGenerator.CreateAndDisplayChatMessageLabel(leftBehindChatMsg, friendChattingWith, chat_messages_area); scrollViewChatMsgArea.ScrollToAsync(chat_messages_area, ScrollToPosition.End, false); //PLACE THE MESSAGE IN YOUR DATABASE await PlaceChatMessageInDatabase(leftBehindChatMsg.Msg, "Friend", friendChattingWith); //PLACE THE PRIMARY KEY OF THIS MESSAGE IN A LIST PrimaryKeyLeftBehindMsg primaryKey = new PrimaryKeyLeftBehindMsg(); primaryKey.MsgID = leftBehindChatMsg.MsgID; primaryKey.PhoneNumWriter = leftBehindChatMsg.PhoneNumOfWriter; listOfPrimaryKeysForLeftBehindMsgs.Add(primaryKey); } } //TELL CHAT HUB THAT THESE LEFT BEHIND MESSAGES HAVE BEEN RECEIVED await hubConnection.InvokeAsync("MessagesReceived", yourPhoneNumber, listOfPrimaryKeysForLeftBehindMsgs); }); }
private void SetHubConnectionhandlerToRecieveChatMsg(string yourPhoneNumber, Page chatPage, Friend friendChattingWith) { hubConnection.On <ChatMessage>(yourPhoneNumber, async(chatMessage) => { //Friend friendChattingWith = (Friend)BindingContext; if (friendChattingWith.ID == chatMessage.PhoneNumOfWriter) //if the person's phone # that sent me the message is = to the phone # of the person i started the chat with { ScrollView scrollViewChatMsgArea = (ScrollView)chatPage.FindByName("scrollViewChatMsgArea"); StackLayout chat_messages_area = (StackLayout)chatPage.FindByName("chat_messages_area"); //CREATE AND DISPLAY THE CHAT MESSAGE THAT WAS RECIEVED chatMessage.Writer = "Friend"; ChatMessageLabelGenerator.CreateAndDisplayChatMessageLabel(chatMessage, friendChattingWith, chat_messages_area); //SCROLL TO THE END POSITION OF THE SCROLL VIEW SO WE CAN SEE THE MESSAGE ADDED scrollViewChatMsgArea.ScrollToAsync(chat_messages_area, ScrollToPosition.End, false); //PLACE THE MESSAGE RECEIVED IN THE DATABASE await PlaceChatMessageInDatabase(chatMessage.Msg, "Friend", friendChattingWith); //TELL THE HUB I RECEIVED THE MESSAGE List <PrimaryKeyLeftBehindMsg> listOfPrimaryKeysForLeftBehindMsgs = new List <PrimaryKeyLeftBehindMsg>(); PrimaryKeyLeftBehindMsg primaryKey = new PrimaryKeyLeftBehindMsg(); primaryKey.MsgID = chatMessage.MsgID; primaryKey.PhoneNumWriter = chatMessage.PhoneNumOfWriter; listOfPrimaryKeysForLeftBehindMsgs.Add(primaryKey); await hubConnection.InvokeAsync("MessagesReceived", yourPhoneNumber, listOfPrimaryKeysForLeftBehindMsgs); } }); }
private async Task RetrieveAndDisplayChatMessagesForFriend() { Friend friendChattingWith = (Friend)BindingContext; List <ChatMessage> chatMessages = await App.Database.GetAllChatMessagesForGivenFriend(friendChattingWith); foreach (ChatMessage chatMessage in chatMessages) { //OutputChatMessage(chatMessage); ChatMessageLabelGenerator.CreateAndDisplayChatMessageLabel(chatMessage, friendChattingWith, chat_messages_area); } }
async void Chat_Entry_Completed(object sender, EventArgs e) { //PLACE THE MESSAGE IN YOUR DATABASE ChatMessage chatMessage = await PlaceChatMessageInDatabase(((Entry)sender).Text, "You"); ChatMessageLabelGenerator.CreateAndDisplayChatMessageLabel(chatMessage, (Friend)BindingContext, chat_messages_area); //SEND THE MESSAGE TO YOUR FRIEND Friend friendChattingWith = (Friend)BindingContext; await SendMessage(friendChattingWith.ID, chatMessage); //clear chat entry box chat_entry_box.Text = ""; //set scroll position to bottom await scrollViewChatMsgArea.ScrollToAsync(chat_messages_area, ScrollToPosition.End, false); }