public void UpdatePreviewFull()
        {
            Message message = new Message()
            {
                MyTurn            = MessageListBox.Items.Count > 0 ? (MessageListBox.Items[MessageListBox.Items.Count - 1] as MessageUiForm).MyTurn : false,
                DoesRead          = false,
                SendDateTime      = DateTime.Now.ToString(),
                MessageText       = this.MyMsg.Text.Trim(),
                SenderName        = this.ChatTopName_TextBlock.Text,
                MessageContentUrl = null
            };

            XmlFunctions.UpdatePreviewByMsg(GetPreviewSerList(CurrentChatID, message));

            foreach (var item in PreviewsPanel.Children)
            {
                if (item is UserDialogPreviewButton)
                {
                    var s = (item as UserDialogPreviewButton);
                    if (s.ID == CurrentChatID)
                    {
                        s.TextPreview       = message.MessageText;
                        s.DateTimePreviewer = message.SendDateTime;
                        s.MyTurn            = message.MyTurn;
                        s.Icon = message.MyTurn ? 1244 : 3695;
                    }
                }
            }
        }
        private void MessageListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.MessageListBox.SelectedIndex != -1)
            {
                var           dialog  = new EditMessageDialog();
                MessageUiForm tmp_msg = (this.MessageListBox.Items[this.MessageListBox.SelectedIndex] as MessageUiForm);
                dialog.MsgTextBox.Text            = tmp_msg.MessageText;
                dialog.MsgDatePicker.SelectedDate = DateTime.Parse(tmp_msg.SendDateTime);
                dialog.MsgTimePicker.SelectedTime = DateTime.Parse(tmp_msg.SendDateTime);
                dialog.MessageContentUrl          = tmp_msg.MessageContentUrl;
                dialog.MyTurnToggle.IsChecked     = tmp_msg.MyTurn;
                dialog.ShowDialog();

                if (dialog.DoesExecuted)
                {
                    var new_msg = new Message()
                    {
                        MyTurn            = (bool)dialog.MyTurnToggle.IsChecked,
                        MessageText       = dialog.MsgTextBox.Text,
                        SendDateTime      = DateTime.Parse(dialog.MsgDatePicker.SelectedDate.ToString()).ToShortDateString() + " " + DateTime.Parse(dialog.MsgTimePicker.SelectedTime.ToString()).ToLongTimeString(),
                        SenderName        = this.ChatTopName_TextBlock.Text,
                        MessageContentUrl = dialog.MessageContentUrl
                    };
                    XmlFunctions.WriteDayJournal(new_msg, CurrentChatID, (this.MessageListBox.Items[this.MessageListBox.SelectedIndex] as MessageUiForm).SendDateTime);

                    ClearCurrentDialog();

                    LoadChatFromPrev(GetCurrentChatButtonInstance(), new RoutedEventArgs());
                }
            }
        }
        private void SendMsgBtn_Click(object sender, RoutedEventArgs e)
        {
            if (this.MyMsg.Text != "")
            {//НЕ ПУСТОЕ СООБЩЕНИЕ
                var msg = new MessageUiForm(this.MyMsg.Text.Trim(), DateTime.Now.ToString(), this.ChatTopName_TextBlock.Text);

                this.MessageListBox.Items.Add(msg);

                MoveChatScrollToDownEnd();

                UpdatePreviewFull();

                XmlFunctions.UpdateDayJournal(msg, CurrentChatID);

                this.MyMsg.Text = "";

                SortPrevsByDate();

                //List<MessageUiForm> listMsg = new List<MessageUiForm>();
                //foreach (var item in this.MessageListBox.Items) {
                //    listMsg.Add(item as MessageUiForm);
                //}

                //listMsg.Sort()

                //this.MessageListBox.Items.Clear();

                //foreach (var item in this.MessageListBox.Items)
                //    this.MessageListBox.Items.Add(item);
            }
        }
        private void MainWindow1_Loaded(object sender, RoutedEventArgs e)
        {
            if (File.Exists(Properties.Resources.PreviewSavePath))
            {
                foreach (var item in XmlFunctions.GetPreviewListInfo().userPreviewSerializables)
                {
                    this.PreviewsPanel.Children.Add(new UserDialogPreviewButton(item.UserName)
                    {
                        ID = item.ID, PhoneNumber = item.PhoneNumber, PictureURL = item.PictureURL, TextPreview = item.LastMSG.MessageText, DateTimePreviewer = item.LastMSG.SendDateTime, MyTurn = item.LastMSG.MyTurn, Icon = item.LastMSG.MyTurn ? 1244 : 3695
                    });
                }

                BumpPreviewCollection = new StackPanel();
                this.BumpPreviewCollection.HorizontalAlignment = HorizontalAlignment.Stretch;
                this.BumpPreviewCollection.VerticalAlignment   = VerticalAlignment.Stretch;
                Grid.SetColumn(BumpPreviewCollection, 1);
                Grid.SetRow(BumpPreviewCollection, 1);

                foreach (var item in XmlFunctions.GetPreviewListInfo().userPreviewSerializables)
                {
                    BumpPreviewCollection.Children.Add(new UserDialogPreviewButton(item.UserName)
                    {
                        ID = item.ID, PhoneNumber = item.PhoneNumber, PictureURL = item.PictureURL
                    });
                }
            }

            GC.Collect();
        }
        private void LoadChatMsgs()
        {
            var s = XmlFunctions.GetDayJournal(CurrentChatID, LoadLastChatFile().ToShortDateString());

            if (s != null)
            {
                foreach (var item in s.Messages)
                {
                    MessageListBox.Items.Add(new MessageUiForm(item.MessageText, item.SendDateTime, item.SenderName, item.DoesRead, item.DoesRead, item.MessageContentUrl)
                    {
                        DoesRead          = item.DoesRead,
                        MessageContentUrl = item.MessageContentUrl,
                        MyTurn            = item.MyTurn,
                        SenderName        = item.SenderName,
                        SendDateTime      = item.SendDateTime,
                    });
                }
                MoveChatScrollToDownEnd();
                //LastMSG = s.Messages.Last();

                //LastDateToLoad = DateTime.Parse(DateTime.Parse(s.Messages.First().SendDateTime).ToShortDateString());
            }

            GC.Collect();
        }
        private void ChatScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (this.ChatGrid.RowDefinitions[0].Height != new GridLength(0))
            {
                if (this.ChatScrollViewer.VerticalOffset == 0 /*&&   this.ChatScrollViewer.ScrollableHeight != 0*/)
                {
                    if (ChatScrollViewerVerticalOffsetZeroPointerFixer)
                    {
                        DayMessageJournalSerializable s = XmlFunctions.GetDayJournal(CurrentChatID, LoadPrevLastChatFile());

                        if (s != null)
                        {
                            int index = 0;
                            foreach (var item in s.Messages)
                            {
                                var tmp_msg = new MessageUiForm(item.MessageText, item.SendDateTime, item.SenderName, item.DoesRead, item.DoesRead, item.MessageContentUrl)
                                {
                                    DoesRead          = item.DoesRead,
                                    MessageContentUrl = item.MessageContentUrl,
                                    MyTurn            = item.MyTurn,
                                    SenderName        = item.SenderName,
                                    SendDateTime      = item.SendDateTime
                                };

                                bool test_first_msg = false;
                                foreach (var i in this.MessageListBox.Items)
                                {
                                    if ((i as MessageUiForm).SendDateTime == tmp_msg.SendDateTime)
                                    {
                                        test_first_msg = true;
                                    }
                                }

                                if (!test_first_msg)
                                {
                                    MessageListBox.Items.Insert(index, tmp_msg);
                                    // MessageBox.Show($"{tmp_msg.MessageText}");
                                }
                                index++;
                            }
                            this.ChatScrollViewer.LineDown();
                            this.ChatScrollViewer.LineDown();
                            this.ChatScrollViewer.LineDown();
                            this.ChatScrollViewer.LineDown();
                            this.ChatScrollViewer.LineDown();
                            //MessageBox.Show(this.ChatScrollViewer.VerticalOffset.ToString());

                            GC.Collect();
                            ChatScrollViewerVerticalOffsetZeroPointerFixer = false;
                        }
                        //MessageBox.Show(CurrentLoadedDate.ToString());
                    }
                    else
                    {
                        ChatScrollViewerVerticalOffsetZeroPointerFixer = true;
                    }
                }
            }
        }
        private void SendMsgBtnToMe_Click(object sender, RoutedEventArgs e)
        {
            if (this.MyMsg.Text != "")
            {//НЕ ПУСТОЕ СООБЩЕНИЕ
                var msg = new MessageUiForm(this.MyMsg.Text.Trim(), DateTime.Now.ToString(), this.ChatTopName_TextBlock.Text, false);

                this.MessageListBox.Items.Add(msg);

                MoveChatScrollToDownEnd();

                UpdatePreviewFull();

                XmlFunctions.UpdateDayJournal(msg, CurrentChatID);

                this.MyMsg.Text = "";

                SortPrevsByDate();
            }
        }
        private void SendImgMsgBtn_Click(object sender, RoutedEventArgs e)
        {
            string CurrentPathToPict = "";

            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "Image Files|*.jpg; *.jpeg; *.png";

            if (openFileDialog.ShowDialog() == true)
            {
                CurrentPathToPict = openFileDialog.FileName;
            }

            Directory.CreateDirectory(Properties.Resources.UserDataDirPath + "\\" + CurrentChatID);

            if ((!File.Exists(Properties.Resources.UserDataDirPath + "\\" + CurrentChatID + "\\" + System.IO.Path.GetFileName(openFileDialog.FileName)) && File.Exists(openFileDialog.FileName)))
            {
                string destionation = Properties.Resources.UserDataDirPath + "\\" + CurrentChatID + "\\" + System.IO.Path.GetFileName(openFileDialog.FileName);
                File.Copy(openFileDialog.FileName, destionation);

                #region MessageInUi

                var msg = new MessageUiForm("IMAGE_CONTENT", DateTime.Now.ToString(), this.ChatTopName_TextBlock.Text, true, true, destionation);

                this.MessageListBox.Items.Add(msg);

                MoveChatScrollToDownEnd();

                UpdatePreviewFull();

                XmlFunctions.UpdateDayJournal(msg, CurrentChatID);

                this.MyMsg.Text = "";

                SortPrevsByDate();

                #endregion MessageInUi
            }
        }
Exemple #9
0
        public static void WriteDayJournal(Message message, long ID, string OldMsgSendTime)
        {
            try {
                string        local_tmp_path = Properties.Resources.UserDataDirPath + "\\" + ID + "\\" + DateTime.Parse(message.SendDateTime).ToShortDateString() + Properties.Resources.SaveFormatter;
                XmlSerializer xmlSerializer  = new XmlSerializer(typeof(DayMessageJournalSerializable));

                //MessageBox.Show($"OLD SEND DATE TIME {OldMsgSendTime}\n NEW DATE TIME {message.SendDateTime}");
                string path_to_existing_file_remove = Properties.Resources.UserDataDirPath + "\\" + ID + "\\" + DateTime.Parse(OldMsgSendTime).ToShortDateString() + Properties.Resources.SaveFormatter;
                if (File.Exists(local_tmp_path))
                {
                    #region Creating and editing new journal

                    DayMessageJournalSerializable new_chat_journal = XmlFunctions.GetDayJournal(ID, DateTime.Parse(message.SendDateTime).ToShortDateString());
                    File.Delete(local_tmp_path);
                    new_chat_journal.Messages.Add(message);
                    new_chat_journal.Messages.Sort((x, y) => DateTime.Parse((x.SendDateTime)).CompareTo(DateTime.Parse((y.SendDateTime))));
                    //new_chat_journal.Messages.Reverse();

                    using (FileStream fs = new FileStream(CreatePathToJournal(ID, new_chat_journal.CurrentDate), FileMode.OpenOrCreate)) {
                        xmlSerializer.Serialize(fs, new_chat_journal);
                    }
                    //MessageBox.Show(message.MyTurn.ToString());

                    #endregion Creating and editing new journal
                }
                else
                {
                    DayMessageJournalSerializable new_chat_journal = new DayMessageJournalSerializable();
                    new_chat_journal.Messages.Add(new Message(message));
                    new_chat_journal.CurrentDate = DateTime.Parse(message.SendDateTime).ToShortDateString();

                    XmlSerializer formatter = new XmlSerializer(typeof(DayMessageJournalSerializable));

                    using (FileStream fs = new FileStream(CreatePathToJournal(ID, DateTime.Parse(message.SendDateTime).ToShortDateString()), FileMode.OpenOrCreate)) {
                        formatter.Serialize(fs, new_chat_journal);
                    }
                }

                #region Editing old journal

                if (File.Exists(path_to_existing_file_remove))
                {
                    var oldChat = GetDayJournal(ID, DateTime.Parse(OldMsgSendTime).ToShortDateString());

                    oldChat.Messages.Remove(oldChat.Messages.Find(x => x.SendDateTime == OldMsgSendTime));

                    if (File.Exists(path_to_existing_file_remove))
                    {
                        File.Delete(path_to_existing_file_remove);
                    }

                    if (oldChat.Messages.Count != 0)
                    {
                        using (FileStream fs = new FileStream(CreatePathToJournal(ID, oldChat.CurrentDate), FileMode.OpenOrCreate)) {
                            xmlSerializer.Serialize(fs, oldChat);
                        }
                    }
                }

                #endregion Editing old journal
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }

            GC.Collect();
        }