PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/1.7/WordPress/ModerateCommentPage.xaml.cs

https://bitbucket.org/sendhil/wordpress-windowsphone
C# | 401 lines | 313 code | 77 blank | 11 comment | 22 complexity | ff39a6c0b94ca0dd2a10fb5515c7cabe MD5 | raw file
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Media;
  5. using System.Windows.Media.Animation;
  6. using System.Windows.Input;
  7. using Microsoft.Phone.Controls;
  8. using Microsoft.Phone.Shell;
  9. using WordPress.Localization;
  10. using WordPress.Model;
  11. using Microsoft.Phone.Tasks;
  12. namespace WordPress
  13. {
  14. public partial class ModerateCommentPage : PhoneApplicationPage
  15. {
  16. #region member variables
  17. private string COMMENTKEY_VALUE = "comment";
  18. private string REPLYPANELVISIBLE_VALUE = "replyPanelVisible";
  19. private string REPLYTEXTBOXTEXT_VALUE = "replyTextBoxText";
  20. private ApplicationBarIconButton _deleteIconButton;
  21. private ApplicationBarIconButton _replyIconButton;
  22. private ApplicationBarIconButton _spamIconButton;
  23. private ApplicationBarIconButton _approveIconButton;
  24. private ApplicationBarIconButton _unapproveIconButton;
  25. private StringTable _localizedStrings;
  26. #endregion
  27. #region constructor
  28. public ModerateCommentPage()
  29. {
  30. InitializeComponent();
  31. _localizedStrings = App.Current.Resources["StringTable"] as StringTable;
  32. replyPanel.Visibility = Visibility.Collapsed;
  33. ApplicationBar = new ApplicationBar();
  34. ApplicationBar.BackgroundColor = (Color)App.Current.Resources["AppbarBackgroundColor"];
  35. ApplicationBar.ForegroundColor = (Color)App.Current.Resources["WordPressGrey"];
  36. _deleteIconButton = new ApplicationBarIconButton(new Uri("/Images/appbar.delete.png", UriKind.Relative));
  37. _deleteIconButton.Text = _localizedStrings.ControlsText.Delete;
  38. _deleteIconButton.Click += OnDeleteIconButtonClick;
  39. _replyIconButton = new ApplicationBarIconButton(new Uri("/Images/appbar.edit.png", UriKind.Relative));
  40. _replyIconButton.Text = _localizedStrings.ControlsText.Reply;
  41. _replyIconButton.Click += OnReplyIconButtonClick;
  42. _spamIconButton = new ApplicationBarIconButton(new Uri("/Images/appbar.spam.png", UriKind.Relative));
  43. _spamIconButton.Text = _localizedStrings.ControlsText.Spam;
  44. _spamIconButton.Click += OnSpamIconButtonClick;
  45. _approveIconButton = new ApplicationBarIconButton(new Uri("/Images/appbar.approve.png", UriKind.Relative));
  46. _approveIconButton.Text = _localizedStrings.ControlsText.Approve;
  47. _approveIconButton.Click += OnApproveIconButtonClick;
  48. _unapproveIconButton = new ApplicationBarIconButton(new Uri("/Images/appbar.unapprove.png", UriKind.Relative));
  49. _unapproveIconButton.Text = _localizedStrings.ControlsText.Unapprove;
  50. _unapproveIconButton.Click += OnUnapproveIconButtonClick;
  51. authorEmailTextBlock.MouseLeftButtonDown += new MouseButtonEventHandler(authorEmailTextBlock_MouseLeftButtonDown);
  52. authorURLTextBlock.MouseLeftButtonDown += new MouseButtonEventHandler(authorURLTextBlock_MouseLeftButtonDown);
  53. Loaded += OnPageLoaded;
  54. }
  55. #endregion
  56. #region methods
  57. private void OnPageLoaded(object sender, EventArgs args)
  58. {
  59. App.WaitIndicationService.RootVisualElement = LayoutRoot;
  60. BlogName.Text = App.MasterViewModel.CurrentBlog.BlogNameUpper;
  61. ChangeApplicationBarAppearance();
  62. //now that the application bar is in the right visual state, check for any
  63. //stored data for a reply
  64. if (State.ContainsKey(REPLYPANELVISIBLE_VALUE))
  65. {
  66. if (State.ContainsKey(REPLYTEXTBOXTEXT_VALUE))
  67. {
  68. replyTextBox.Text = State[REPLYTEXTBOXTEXT_VALUE] as string;
  69. }
  70. ShowReplyPanel();
  71. }
  72. }
  73. protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
  74. {
  75. if (Visibility.Visible == replyPanel.Visibility)
  76. {
  77. HideReplyPanel();
  78. e.Cancel = true;
  79. }
  80. else
  81. {
  82. base.OnBackKeyPress(e);
  83. }
  84. }
  85. private void OnReplyIconButtonClick(object sender, EventArgs e)
  86. {
  87. ShowReplyPanel();
  88. }
  89. private void ShowReplyPanel()
  90. {
  91. replyPanel.Visibility = Visibility.Visible;
  92. ApplicationBar.IsVisible = false;
  93. Storyboard fadeInStoryboard = AnimationHelper.CreateEaseInAnimationStoryBoard(replyPanel, Grid.OpacityProperty, 0.0, 0.97, TimeSpan.FromMilliseconds(250));
  94. fadeInStoryboard.Begin();
  95. }
  96. private void HideReplyPanel()
  97. {
  98. Storyboard fadeOutStoryboard = AnimationHelper.CreateEaseOutAnimationStoryBoard(replyPanel, Grid.OpacityProperty, 0.97, 0, TimeSpan.FromMilliseconds(250));
  99. fadeOutStoryboard.BeginTime = TimeSpan.FromMilliseconds(250);
  100. fadeOutStoryboard.Completed += OnFadeOutStoryboardCompleted;
  101. fadeOutStoryboard.Begin();
  102. }
  103. private void OnFadeOutStoryboardCompleted(object sender, EventArgs e)
  104. {
  105. ApplicationBar.IsVisible = true;
  106. replyPanel.Visibility = Visibility.Collapsed;
  107. replyTextBox.Text = string.Empty;
  108. Storyboard storyboard = sender as Storyboard;
  109. storyboard.Completed -= OnFadeOutStoryboardCompleted;
  110. }
  111. private void OnSpamIconButtonClick(object sender, EventArgs e)
  112. {
  113. string prompt = _localizedStrings.Prompts.ConfirmMarkSpamComment;
  114. MessageBoxResult result = MessageBox.Show(prompt, _localizedStrings.Prompts.Confirm, MessageBoxButton.OKCancel);
  115. if (result == MessageBoxResult.OK)
  116. {
  117. Comment comment = DataContext as Comment;
  118. comment.CommentStatus = eCommentStatus.spam;
  119. EditCommentRPC rpc = new EditCommentRPC(App.MasterViewModel.CurrentBlog, comment);
  120. rpc.Completed += OnEditCommentRPCCompleted;
  121. rpc.ExecuteAsync();
  122. App.WaitIndicationService.ShowIndicator(_localizedStrings.Messages.MarkingAsSpam);
  123. }
  124. else
  125. {
  126. return;
  127. }
  128. }
  129. private void OnUnapproveIconButtonClick(object sender, EventArgs e)
  130. {
  131. Comment comment = DataContext as Comment;
  132. comment.CommentStatus = eCommentStatus.hold;
  133. EditCommentRPC rpc = new EditCommentRPC(App.MasterViewModel.CurrentBlog, comment);
  134. rpc.Completed += OnEditCommentRPCCompleted;
  135. rpc.ExecuteAsync();
  136. App.WaitIndicationService.ShowIndicator(_localizedStrings.Messages.UnapprovingComment);
  137. }
  138. private void OnApproveIconButtonClick(object sender, EventArgs e)
  139. {
  140. Comment comment = DataContext as Comment;
  141. comment.CommentStatus = eCommentStatus.approve;
  142. EditCommentRPC rpc = new EditCommentRPC(App.MasterViewModel.CurrentBlog, comment);
  143. rpc.Completed += OnEditCommentRPCCompleted;
  144. rpc.ExecuteAsync();
  145. App.WaitIndicationService.ShowIndicator(_localizedStrings.Messages.ApprovingComment);
  146. }
  147. private void OnEditCommentRPCCompleted(object sender, XMLRPCCompletedEventArgs<Comment> args)
  148. {
  149. EditCommentRPC rpc = sender as EditCommentRPC;
  150. rpc.Completed -= OnEditCommentRPCCompleted;
  151. if (null == args.Error)
  152. {
  153. NavigationService.GoBack();
  154. }
  155. else
  156. {
  157. this.HandleException(args.Error);
  158. }
  159. App.WaitIndicationService.HideIndicator();
  160. ChangeApplicationBarAppearance();
  161. }
  162. protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
  163. {
  164. base.OnNavigatedFrom(e);
  165. //store transient data in the State dictionary
  166. if (State.ContainsKey(COMMENTKEY_VALUE))
  167. {
  168. State.Remove(COMMENTKEY_VALUE);
  169. }
  170. Comment comment = DataContext as Comment;
  171. State.Add(COMMENTKEY_VALUE, comment);
  172. //save reply data if it is active
  173. if (Visibility.Visible == replyPanel.Visibility)
  174. {
  175. State.Add(REPLYPANELVISIBLE_VALUE, Visibility.Visible);
  176. State.Add(REPLYTEXTBOXTEXT_VALUE, replyTextBox.Text);
  177. }
  178. }
  179. protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
  180. {
  181. base.OnNavigatedTo(e);
  182. Comment comment = null;
  183. //check for transient data in the State dictionary
  184. if (State.ContainsKey(COMMENTKEY_VALUE))
  185. {
  186. comment = (Comment)State[COMMENTKEY_VALUE];
  187. DataContext = comment;
  188. }
  189. else
  190. {
  191. DataContext = App.MasterViewModel.CurrentComment;
  192. }
  193. }
  194. private void ChangeApplicationBarAppearance()
  195. {
  196. //change the available options based on the status of the comment
  197. Comment comment = DataContext as Comment;
  198. ApplicationBar.Buttons.Clear();
  199. ApplicationBar.Buttons.Add(_deleteIconButton);
  200. ApplicationBar.Buttons.Add(_replyIconButton);
  201. switch (comment.CommentStatus)
  202. {
  203. case eCommentStatus.approve:
  204. ApplicationBar.Buttons.Add(_spamIconButton);
  205. ApplicationBar.Buttons.Add(_unapproveIconButton);
  206. break;
  207. case eCommentStatus.hold:
  208. ApplicationBar.Buttons.Add(_spamIconButton);
  209. ApplicationBar.Buttons.Add(_approveIconButton);
  210. break;
  211. case eCommentStatus.spam:
  212. ApplicationBar.Buttons.Add(_approveIconButton);
  213. break;
  214. }
  215. }
  216. private void OnDeleteIconButtonClick(object sender, EventArgs e)
  217. {
  218. string prompt = _localizedStrings.Prompts.ConfirmDeleteComment;
  219. MessageBoxResult result = MessageBox.Show(prompt, _localizedStrings.Prompts.Confirm, MessageBoxButton.OKCancel);
  220. if (result == MessageBoxResult.OK)
  221. {
  222. Comment comment = DataContext as Comment;
  223. DeleteCommentRPC rpc = new DeleteCommentRPC(App.MasterViewModel.CurrentBlog, comment);
  224. rpc.Completed += OnDeleteCommentRPCCompleted;
  225. rpc.ExecuteAsync();
  226. App.WaitIndicationService.ShowIndicator(_localizedStrings.Messages.DeletingComment);
  227. }
  228. else
  229. {
  230. return;
  231. }
  232. }
  233. private void OnDeleteCommentRPCCompleted(object sender, XMLRPCCompletedEventArgs<Comment> args)
  234. {
  235. DeleteCommentRPC rpc = sender as DeleteCommentRPC;
  236. rpc.Completed -= OnDeleteCommentRPCCompleted;
  237. if (null == args.Error)
  238. {
  239. //remove the comment from the store--saves us a web call
  240. App.MasterViewModel.Comments.Remove(args.Items[0]);
  241. NavigationService.GoBack();
  242. }
  243. else
  244. {
  245. this.HandleException(args.Error);
  246. }
  247. App.WaitIndicationService.HideIndicator();
  248. }
  249. private void OnReplyButtonClick(object sender, RoutedEventArgs e)
  250. {
  251. ReplyToComment();
  252. }
  253. private void ReplyToComment()
  254. {
  255. if (string.IsNullOrEmpty(replyTextBox.Text))
  256. {
  257. MessageBox.Show(_localizedStrings.Messages.MissingReply);
  258. replyTextBox.Focus();
  259. return;
  260. }
  261. Blog currentBlog = App.MasterViewModel.CurrentBlog;
  262. Comment comment = DataContext as Comment;
  263. Comment reply = new Comment()
  264. {
  265. Author = currentBlog.Username,
  266. Parent = comment.CommentId,
  267. Content = replyTextBox.Text
  268. };
  269. NewCommentRPC rpc = new NewCommentRPC(currentBlog, comment, reply);
  270. rpc.Completed += new XMLRPCCompletedEventHandler<Comment>(OnNewCommentRPCCompleted);
  271. rpc.ExecuteAsync();
  272. App.WaitIndicationService.ShowIndicator(_localizedStrings.Messages.ReplyingToComment);
  273. }
  274. private void OnNewCommentRPCCompleted(object sender, XMLRPCCompletedEventArgs<Comment> args)
  275. {
  276. NewCommentRPC rpc = sender as NewCommentRPC;
  277. rpc.Completed -= OnNewCommentRPCCompleted;
  278. if (null == args.Error)
  279. {
  280. //fire off a request for the latest comment so we can get our comment updated
  281. //with the latest from the server.
  282. DataService.Current.FetchCurrentBlogCommentsAsync();
  283. NavigationService.GoBack();
  284. }
  285. else
  286. {
  287. this.HandleException(args.Error);
  288. }
  289. }
  290. private void authorEmailTextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  291. {
  292. Comment comment = DataContext as Comment;
  293. if (comment.AuthorEmail.Equals(""))
  294. {
  295. return; // No email address so don't show the compose task.
  296. }
  297. EmailComposeTask emailcomposer = new EmailComposeTask();
  298. emailcomposer.To = comment.AuthorEmail;
  299. emailcomposer.Subject = String.Format("Re: {0}", comment.PostTitle);
  300. emailcomposer.Body = String.Format("{0} {1},", _localizedStrings.Messages.Hello, comment.Author);
  301. emailcomposer.Show();
  302. }
  303. private void authorURLTextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  304. {
  305. Comment comment = DataContext as Comment;
  306. if (comment.AuthorUrl.Equals(""))
  307. {
  308. return; // do nothing.
  309. }
  310. WebBrowserTask wbTask = new WebBrowserTask();
  311. try
  312. {
  313. // A url that is not a good URI will throw an exception.
  314. wbTask.Uri = new Uri(comment.AuthorUrl);
  315. wbTask.Show();
  316. }
  317. catch (Exception ex)
  318. {
  319. // Fail silently
  320. }
  321. }
  322. #endregion
  323. }
  324. }