/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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Input;
  6. using CommunicationSystemClient.ViewModel.SocialModule.SocialModuleInterface;
  7. using CommunicationSystemClient.Model.ModelInterface;
  8. using CommunicationSystemClient.Model;
  9. using System.Windows.Forms;
  10. namespace CommunicationSystemClient.ViewModel.SocialModule
  11. {
  12. class UserChatWindowViewModel : ViewModelBase
  13. {
  14. private KeyValuePair<int,string> userInfo;
  15. private ChatModelInterface model;
  16. private int myId;
  17. private bool isHistoryShown;
  18. public UserChatWindowViewModel(KeyValuePair<int,string> userInfo)
  19. {
  20. this.userInfo = userInfo;
  21. this.Alias = userInfo.Value;
  22. this.DisplayName = userInfo.Value;
  23. this.CanSendMessage = true;
  24. this.Hint = "";
  25. model = ModelFactory.getModelFactory().getChatModel();
  26. myId = ModelFactory.getModelFactory().getUserModel().GetMe().Id;
  27. this.WindowWidth = 310;
  28. this.HistoryPanelWidth = 330;
  29. this.isHistoryShown = false;
  30. this.ShowHistoryButton = "History >>";
  31. }
  32. private double _windowWidth;
  33. public double WindowWidth
  34. {
  35. get
  36. {
  37. return _windowWidth;
  38. }
  39. set
  40. {
  41. _windowWidth = value;
  42. this.OnPropertyChanged("WindowWidth");
  43. }
  44. }
  45. public string Alias { get; private set; }
  46. #region Message
  47. private string _messages;
  48. public string Messages
  49. {
  50. get
  51. {
  52. return _messages;
  53. }
  54. set
  55. {
  56. _messages = value;
  57. this.OnPropertyChanged("Messages");
  58. }
  59. }
  60. private string _myMessage;
  61. public string MyMessage
  62. {
  63. get
  64. {
  65. return _myMessage;
  66. }
  67. set
  68. {
  69. _myMessage = value;
  70. this.OnPropertyChanged("MyMessage");
  71. }
  72. }
  73. private bool _canSendMessage;
  74. public bool CanSendMessage
  75. {
  76. get
  77. {
  78. return _canSendMessage;
  79. }
  80. set
  81. {
  82. _canSendMessage = value;
  83. this.OnPropertyChanged("CanSendMessage");
  84. }
  85. }
  86. private string _hint;
  87. public string Hint
  88. {
  89. get
  90. {
  91. return _hint;
  92. }
  93. set
  94. {
  95. _hint = value;
  96. this.OnPropertyChanged("Hint");
  97. }
  98. }
  99. private RelayCommand _sendMessageCommand;
  100. public ICommand SendMessageCommand
  101. {
  102. get
  103. {
  104. if (_sendMessageCommand == null)
  105. {
  106. _sendMessageCommand = new RelayCommand(param => this.OnRequestSendMessage());
  107. }
  108. return _sendMessageCommand;
  109. }
  110. }
  111. public void OnRequestSendMessage()
  112. {
  113. if (this.MyMessage == null || this.MyMessage.Length == 0)
  114. {
  115. this.Hint = "Content can't be empty!!!";
  116. }
  117. else
  118. {
  119. this.showMessage("Me", this.MyMessage);
  120. this.Hint = "";
  121. if (!model.TalkTo(userInfo.Key, this.MyMessage))
  122. {
  123. this.CanSendMessage = false;
  124. this.Hint = "The user is off line.";
  125. }
  126. this.MyMessage = "";
  127. }
  128. }
  129. public event EventHandler RequestMoveToBottom;
  130. public void showMessage(string name,string message)
  131. {
  132. string str = name + ":\n" + message + "\n";
  133. this.Messages += str;
  134. model.SaveMessage(this.userInfo.Key, str);
  135. if (RequestMoveToBottom != null)
  136. {
  137. RequestMoveToBottom(this, EventArgs.Empty);
  138. }
  139. }
  140. #endregion
  141. #region History
  142. private RelayCommand _historyPanelCommand;
  143. public ICommand HistoryPanelCommand
  144. {
  145. get
  146. {
  147. if (_historyPanelCommand == null)
  148. {
  149. _historyPanelCommand = new RelayCommand(param => this.ShowHistoryPanel());
  150. }
  151. return _historyPanelCommand;
  152. }
  153. }
  154. public void ShowHistoryPanel()
  155. {
  156. if (this.isHistoryShown)
  157. {
  158. this.HistoryPanelWidth = 0;
  159. this.ShowHistoryButton = "History >>";
  160. }
  161. else
  162. {
  163. this.HistoryPanelWidth = 330;
  164. this.ShowHistoryButton = "History <<";
  165. }
  166. this.WindowWidth = 310 + this.HistoryPanelWidth;
  167. this.isHistoryShown = !this.isHistoryShown;
  168. }
  169. private string _showHistoryButton;
  170. public string ShowHistoryButton
  171. {
  172. get
  173. {
  174. return _showHistoryButton;
  175. }
  176. set
  177. {
  178. _showHistoryButton = value;
  179. this.OnPropertyChanged("ShowHistoryButton");
  180. }
  181. }
  182. private double _historyPanelWidth;
  183. public double HistoryPanelWidth
  184. {
  185. get
  186. {
  187. return _historyPanelWidth;
  188. }
  189. set
  190. {
  191. _historyPanelWidth = value;
  192. this.OnPropertyChanged("HistoryPanelWidth");
  193. }
  194. }
  195. private string _date;
  196. public string Date
  197. {
  198. get
  199. {
  200. return _date;
  201. }
  202. set
  203. {
  204. _date = value;
  205. this.OnPropertyChanged("Date");
  206. }
  207. }
  208. private DateTime _selectedDate;
  209. public DateTime SelectedDate
  210. {
  211. get
  212. {
  213. return _selectedDate;
  214. }
  215. set
  216. {
  217. _selectedDate = value;
  218. this.Date = value.ToString("MM/dd/yyyy");
  219. this.ShowHistory(value);
  220. this.OnPropertyChanged("SelectedDate");
  221. }
  222. }
  223. private void ShowHistory(DateTime date)
  224. {
  225. ICollection<string> messages = model.GetSavedMessage(this.userInfo.Key, date);
  226. if (messages != null && messages.Count != 0)
  227. {
  228. string str = "";
  229. foreach (string s in messages)
  230. {
  231. str += s;
  232. }
  233. this.History = str;
  234. this.SaveHint = "";
  235. }
  236. else
  237. {
  238. this.History = "";
  239. this.SaveHint = "Sorry, no history is available!";
  240. }
  241. }
  242. private RelayCommand _fileCommand;
  243. public ICommand FileCommand
  244. {
  245. get
  246. {
  247. if (_fileCommand == null)
  248. {
  249. _fileCommand = new RelayCommand(param => this.OnRequestChooseFile());
  250. }
  251. return _fileCommand;
  252. }
  253. }
  254. public void OnRequestChooseFile()
  255. {
  256. FolderBrowserDialog dlg = new FolderBrowserDialog();
  257. DialogResult flag = dlg.ShowDialog();
  258. if (flag == DialogResult.OK)
  259. {
  260. FilePath = dlg.SelectedPath;
  261. }
  262. else
  263. {
  264. this.SaveHint = "The path of the directory is not valid!";
  265. }
  266. }
  267. private string _filePath;
  268. public string FilePath
  269. {
  270. get
  271. {
  272. return _filePath;
  273. }
  274. set
  275. {
  276. _filePath = value;
  277. this.OnPropertyChanged("FilePath");
  278. }
  279. }
  280. private RelayCommand _saveCommand;
  281. public ICommand SaveCommand
  282. {
  283. get
  284. {
  285. if (_saveCommand == null)
  286. {
  287. _saveCommand = new RelayCommand(param => this.OnRequestSave());
  288. }
  289. return _saveCommand;
  290. }
  291. }
  292. public void OnRequestSave()
  293. {
  294. if (this.History == null || this.History.Length == 0)
  295. {
  296. this.SaveHint = "No message is available!";
  297. return;
  298. }
  299. else if (this.FilePath == null || this.FilePath.Length == 0)
  300. {
  301. this.SaveHint = "The path of the file directory is not valid!";
  302. return;
  303. }
  304. else
  305. {
  306. if (model.ExportSavedMessage(this.userInfo.Key, this.SelectedDate, this.FilePath))
  307. {
  308. this.SaveHint = "Export successfully!";
  309. }
  310. else
  311. {
  312. this.SaveHint = "Export not successfully!";
  313. }
  314. }
  315. }
  316. private string _saveHint;
  317. public string SaveHint
  318. {
  319. get
  320. {
  321. return _saveHint;
  322. }
  323. set
  324. {
  325. _saveHint = value;
  326. this.OnPropertyChanged("SaveHint");
  327. }
  328. }
  329. private string _history;
  330. public string History
  331. {
  332. get
  333. {
  334. return _history;
  335. }
  336. set
  337. {
  338. _history = value;
  339. this.OnPropertyChanged("History");
  340. }
  341. }
  342. #endregion
  343. }
  344. }