Example #1
0
        /// <summary>
        ///		Handler for ChatMessage related messages.
        /// </summary>
        /// <param name="chatMessage"> Message to handle. </param>
        /// <param name="notification"> Type of operation. </param>
        private void HandleChatMessageNotification(ChatMessage chatMessage, string notification)
        {
            // todo: set listeners to be specific to chat room
            if (chatMessage.ChatRoomId != Id)
            {
                return;
            }

            switch (notification)
            {
            case "Add":
            {
                // todo: add this to dto model
                // set user for message
                chatMessage.User = Users.SingleOrDefault(i => i.Id == chatMessage.UserId);

                DispatcherHelper.CheckBeginInvokeOnUI(() =>
                    {
                        if (ChatMessages.All(i => i.Id != chatMessage.Id))
                        {
                            ChatMessages.Add(chatMessage);
                        }
                    });
                break;
            }

            case "Remove":
            {
                var messageToRemove = ChatMessages.SingleOrDefault(i => i.Id == chatMessage.Id);

                if (messageToRemove != null)
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() => { ChatMessages.Remove(messageToRemove); });
                }

                break;
            }

            case "SubmitEdit":
                _chatMessageDataService.Edit(chatMessage);
                break;

            case "Edit":
            {
                var messageToEdit = ChatMessages.SingleOrDefault(i => i.Id == chatMessage.Id);

                if (messageToEdit != null)
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() => messageToEdit.Message = chatMessage.Message);
                }

                break;
            }
            }
        }
Example #2
0
        private void GetDataFromFirebase()
        {
            Action <Dictionary <string, ChatMessage> > onValueEvent = (Dictionary <string, ChatMessage> messages) =>
            {
                try
                {
                    System.Diagnostics.Debug.WriteLine("---> EVENT GetDataFromFirebase ");

                    Action onSetValueSuccess = () =>
                    {
                    };

                    Action <string> onSetValueError = (string errorDesc) =>
                    {
                    };

                    if (messages == null)
                    {
                    }
                    else
                    {
                        if (messages.Count != 0 && ChatMessages.Count != messages.Count)
                        {
                            foreach (var message in messages.OrderBy(m => m.Value.DateMessageTimeSpan))
                            {
                                message.Value.IsYourMessage = UserCurent.Name == message.Value.UserName;

                                if (ChatMessages.All(m => m.Id != message.Value.Id))
                                {
                                    ChatMessages.Add(message.Value);
                                    System.Diagnostics.Debug.WriteLine("---> add new -> " + message.Value.Id);
                                }
                            }

                            MessagingCenter.Send <ChatVM>(this, "ScrollToEnd");
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("---> error GetDataFromFirebase " + ex.Message);
                    throw;
                }
            };

            _firebaseDatabase.AddValueEvent("chats", onValueEvent);
        }