/Code/CommunicationSystem/CommunicationSystemClient/ViewModel/SocialModule/UserChatWindowViewModel.cs
http://communicatinsystem.googlecode.com/ · C# · 384 lines · 344 code · 40 blank · 0 comment · 32 complexity · f0f6521be940709adb5c0ed7b240985b MD5 · raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Input;
- using CommunicationSystemClient.ViewModel.SocialModule.SocialModuleInterface;
- using CommunicationSystemClient.Model.ModelInterface;
- using CommunicationSystemClient.Model;
- using System.Windows.Forms;
-
- namespace CommunicationSystemClient.ViewModel.SocialModule
- {
- class UserChatWindowViewModel : ViewModelBase
- {
- private KeyValuePair<int,string> userInfo;
-
- private ChatModelInterface model;
-
- private int myId;
-
- private bool isHistoryShown;
-
- public UserChatWindowViewModel(KeyValuePair<int,string> userInfo)
- {
- this.userInfo = userInfo;
- this.Alias = userInfo.Value;
- this.DisplayName = userInfo.Value;
- this.CanSendMessage = true;
- this.Hint = "";
- model = ModelFactory.getModelFactory().getChatModel();
- myId = ModelFactory.getModelFactory().getUserModel().GetMe().Id;
- this.WindowWidth = 310;
- this.HistoryPanelWidth = 330;
- this.isHistoryShown = false;
- this.ShowHistoryButton = "History >>";
- }
-
- private double _windowWidth;
- public double WindowWidth
- {
- get
- {
- return _windowWidth;
- }
- set
- {
- _windowWidth = value;
- this.OnPropertyChanged("WindowWidth");
- }
- }
-
- public string Alias { get; private set; }
-
- #region Message
- private string _messages;
- public string Messages
- {
- get
- {
- return _messages;
- }
-
- set
- {
- _messages = value;
- this.OnPropertyChanged("Messages");
- }
- }
-
- private string _myMessage;
- public string MyMessage
- {
- get
- {
- return _myMessage;
- }
-
- set
- {
- _myMessage = value;
- this.OnPropertyChanged("MyMessage");
- }
- }
-
- private bool _canSendMessage;
- public bool CanSendMessage
- {
- get
- {
- return _canSendMessage;
- }
- set
- {
- _canSendMessage = value;
- this.OnPropertyChanged("CanSendMessage");
- }
- }
-
- private string _hint;
- public string Hint
- {
- get
- {
- return _hint;
- }
- set
- {
- _hint = value;
- this.OnPropertyChanged("Hint");
- }
- }
-
- private RelayCommand _sendMessageCommand;
-
- public ICommand SendMessageCommand
- {
- get
- {
- if (_sendMessageCommand == null)
- {
- _sendMessageCommand = new RelayCommand(param => this.OnRequestSendMessage());
- }
-
- return _sendMessageCommand;
- }
- }
-
- public void OnRequestSendMessage()
- {
- if (this.MyMessage == null || this.MyMessage.Length == 0)
- {
- this.Hint = "Content can't be empty!!!";
- }
- else
- {
- this.showMessage("Me", this.MyMessage);
- this.Hint = "";
- if (!model.TalkTo(userInfo.Key, this.MyMessage))
- {
- this.CanSendMessage = false;
- this.Hint = "The user is off line.";
- }
- this.MyMessage = "";
- }
- }
-
- public event EventHandler RequestMoveToBottom;
- public void showMessage(string name,string message)
- {
- string str = name + ":\n" + message + "\n";
- this.Messages += str;
- model.SaveMessage(this.userInfo.Key, str);
-
- if (RequestMoveToBottom != null)
- {
- RequestMoveToBottom(this, EventArgs.Empty);
- }
- }
- #endregion
-
-
- #region History
-
- private RelayCommand _historyPanelCommand;
- public ICommand HistoryPanelCommand
- {
- get
- {
- if (_historyPanelCommand == null)
- {
- _historyPanelCommand = new RelayCommand(param => this.ShowHistoryPanel());
- }
- return _historyPanelCommand;
- }
- }
-
- public void ShowHistoryPanel()
- {
- if (this.isHistoryShown)
- {
- this.HistoryPanelWidth = 0;
- this.ShowHistoryButton = "History >>";
- }
- else
- {
- this.HistoryPanelWidth = 330;
- this.ShowHistoryButton = "History <<";
- }
-
- this.WindowWidth = 310 + this.HistoryPanelWidth;
- this.isHistoryShown = !this.isHistoryShown;
- }
-
- private string _showHistoryButton;
- public string ShowHistoryButton
- {
- get
- {
- return _showHistoryButton;
- }
- set
- {
- _showHistoryButton = value;
- this.OnPropertyChanged("ShowHistoryButton");
- }
- }
-
- private double _historyPanelWidth;
- public double HistoryPanelWidth
- {
- get
- {
- return _historyPanelWidth;
- }
- set
- {
- _historyPanelWidth = value;
- this.OnPropertyChanged("HistoryPanelWidth");
- }
- }
-
- private string _date;
- public string Date
- {
- get
- {
- return _date;
- }
- set
- {
- _date = value;
- this.OnPropertyChanged("Date");
- }
- }
-
- private DateTime _selectedDate;
- public DateTime SelectedDate
- {
- get
- {
- return _selectedDate;
- }
- set
- {
- _selectedDate = value;
- this.Date = value.ToString("MM/dd/yyyy");
- this.ShowHistory(value);
- this.OnPropertyChanged("SelectedDate");
- }
- }
-
- private void ShowHistory(DateTime date)
- {
- ICollection<string> messages = model.GetSavedMessage(this.userInfo.Key, date);
- if (messages != null && messages.Count != 0)
- {
- string str = "";
- foreach (string s in messages)
- {
- str += s;
- }
- this.History = str;
- this.SaveHint = "";
- }
- else
- {
- this.History = "";
- this.SaveHint = "Sorry, no history is available!";
- }
- }
-
- private RelayCommand _fileCommand;
-
- public ICommand FileCommand
- {
- get
- {
- if (_fileCommand == null)
- {
- _fileCommand = new RelayCommand(param => this.OnRequestChooseFile());
- }
- return _fileCommand;
- }
- }
-
- public void OnRequestChooseFile()
- {
- FolderBrowserDialog dlg = new FolderBrowserDialog();
-
- DialogResult flag = dlg.ShowDialog();
- if (flag == DialogResult.OK)
- {
- FilePath = dlg.SelectedPath;
- }
- else
- {
- this.SaveHint = "The path of the directory is not valid!";
- }
- }
-
- private string _filePath;
- public string FilePath
- {
- get
- {
- return _filePath;
- }
- set
- {
- _filePath = value;
- this.OnPropertyChanged("FilePath");
- }
- }
-
- private RelayCommand _saveCommand;
-
- public ICommand SaveCommand
- {
- get
- {
- if (_saveCommand == null)
- {
- _saveCommand = new RelayCommand(param => this.OnRequestSave());
- }
- return _saveCommand;
- }
- }
-
- public void OnRequestSave()
- {
- if (this.History == null || this.History.Length == 0)
- {
- this.SaveHint = "No message is available!";
- return;
- }
- else if (this.FilePath == null || this.FilePath.Length == 0)
- {
- this.SaveHint = "The path of the file directory is not valid!";
- return;
- }
- else
- {
- if (model.ExportSavedMessage(this.userInfo.Key, this.SelectedDate, this.FilePath))
- {
- this.SaveHint = "Export successfully!";
- }
- else
- {
- this.SaveHint = "Export not successfully!";
- }
- }
- }
-
- private string _saveHint;
- public string SaveHint
- {
- get
- {
- return _saveHint;
- }
- set
- {
- _saveHint = value;
- this.OnPropertyChanged("SaveHint");
- }
- }
-
- private string _history;
- public string History
- {
- get
- {
- return _history;
- }
- set
- {
- _history = value;
- this.OnPropertyChanged("History");
- }
- }
-
- #endregion
- }
- }