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

/ShoppingList/MainPage.xaml.cs

http://queuelessapp.codeplex.com
C# | 273 lines | 205 code | 28 blank | 40 comment | 35 complexity | e1b7a8d5e958fa6e40274ae688324297 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using Microsoft.Phone.Controls;
  13. using Microsoft.Phone.Shell;
  14. namespace ShoppingList
  15. {
  16. public partial class MainPage : PhoneApplicationPage
  17. {
  18. // Constructor
  19. public MainPage()
  20. {
  21. if (!App.ViewModel.IsDataLoaded)
  22. {
  23. App.ViewModel.LoadData();
  24. }
  25. InitializeComponent();
  26. // Set the data context of the listbox control to the sample data
  27. DataContext = App.ViewModel;
  28. this.Loaded += new RoutedEventHandler(MainPage_Loaded);
  29. }
  30. // Load data for the ViewModel Items
  31. private void MainPage_Loaded(object sender, RoutedEventArgs e)
  32. {
  33. if (!App.ViewModel.IsDataLoaded)
  34. {
  35. App.ViewModel.LoadData();
  36. }
  37. List<ListBox> listboxes = new List<ListBox>()
  38. {
  39. this.FindName("FirstListBox") as ListBox,
  40. this.FindName("SecondListBox") as ListBox
  41. };
  42. listboxes.ForEach(lb =>
  43. {
  44. if (lb != null) lb.UpdateLayout();
  45. });
  46. }
  47. private void SwitchItem_Click(object sender, RoutedEventArgs e)
  48. {
  49. var item = (sender as Button).DataContext as ItemViewModel;
  50. if (item != null)
  51. {
  52. App.ViewModel.CurrentList.TransferItem(item);
  53. Microsoft.Devices.VibrateController.Default.Start(new TimeSpan(0, 0, 0, 0, 20));
  54. }
  55. }
  56. /// <summary>
  57. /// Gets or sets the edit button.
  58. /// </summary>
  59. /// <value>The edit button.</value>
  60. public ApplicationBarIconButton EditButton
  61. {
  62. get
  63. {
  64. if (ApplicationBar.Buttons.Count >= 2)
  65. return ApplicationBar.Buttons[1] as ApplicationBarIconButton;
  66. else return null;
  67. }
  68. }
  69. /// <summary>
  70. /// Gets or sets the edit button.
  71. /// </summary>
  72. /// <value>The edit button.</value>
  73. public ApplicationBarIconButton DeleteButton
  74. {
  75. get
  76. {
  77. if (ApplicationBar.Buttons.Count >= 3)
  78. return ApplicationBar.Buttons[2] as ApplicationBarIconButton;
  79. else return null;
  80. }
  81. }
  82. private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
  83. {
  84. }
  85. private void listItemSelected(object sender, SelectionChangedEventArgs e)
  86. {
  87. if (e.AddedItems.Count > 0)
  88. {
  89. App.ViewModel.CurrentList.CurrentItem = e.AddedItems[0] as ItemViewModel;
  90. SetButtonsState();
  91. }
  92. else if (e.RemovedItems.Count > 0)
  93. {
  94. App.ViewModel.CurrentList.CurrentItem = null;
  95. SetButtonsState();
  96. }
  97. Microsoft.Devices.VibrateController.Default.Start(new TimeSpan(0, 0, 0, 0, 2));
  98. }
  99. private void SetButtonsState()
  100. {
  101. EditButton.IsEnabled = App.ViewModel.CurrentList.CurrentItem != null;
  102. DeleteButton.IsEnabled = App.ViewModel.CurrentList.CurrentItem != null;
  103. }
  104. private void ApplicationBarAddItemButton_Click(object sender, EventArgs e)
  105. {
  106. App.ViewModel.CurrentList.CurrentItem = null;
  107. FirstListBox.SelectedItem = null;
  108. SecondListBox.SelectedItem = null;
  109. List<ListBox> listboxes = new List<ListBox>()
  110. {
  111. this.FindName("FirstListBox") as ListBox,
  112. this.FindName("SecondListBox") as ListBox
  113. };
  114. listboxes.ForEach(lb =>
  115. {
  116. if (lb != null) lb.SelectedItem = null;
  117. });
  118. NavigationService.Navigate(new Uri("/EditItemPage.xaml", UriKind.Relative));
  119. }
  120. private void ApplicationBarEditItemButton_Click(object sender, EventArgs e)
  121. {
  122. NavigationService.Navigate(new Uri("/EditItemPage.xaml", UriKind.Relative));
  123. }
  124. private void ApplicationBarDeleteButton_Click(object sender, EventArgs e)
  125. {
  126. bool deleted =
  127. App.ViewModel.CurrentList.ItemsInCart.Remove(App.ViewModel.CurrentList.CurrentItem) ||
  128. App.ViewModel.CurrentList.ItemsOnList.Remove(App.ViewModel.CurrentList.CurrentItem);
  129. App.ViewModel.CurrentList.OnPropertyChanged("CartSum");
  130. App.ViewModel.CurrentList.OnPropertyChanged("ListSum");
  131. }
  132. private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  133. {
  134. //(sender as StackPanel).Children[1].Visibility = System.Windows.Visibility.Visible;
  135. //((sender as StackPanel).Children[0] as TextBlock).Style = App.Current.RootVisual.
  136. //((sender as StackPanel).Children[0] as TextBlock).TextDecorations = TextDecorations.Underline;
  137. }
  138. private void ListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  139. {
  140. //(sender as ListBox).SelectedItem = null;
  141. }
  142. private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
  143. {
  144. if (e.RemovedItems.Count > 0
  145. && (e.RemovedItems[0] as PivotItem) != null
  146. && ((e.RemovedItems[0] as PivotItem).Content as ListBox) != null)
  147. {
  148. ((e.RemovedItems[0] as PivotItem).Content as ListBox).SelectedItem = null;
  149. }
  150. //var items = new List<ItemViewModel>() {
  151. //(FirstListBox.SelectedItem as ItemViewModel),
  152. //(SecondListBox.SelectedItem as ItemViewModel)
  153. //};
  154. //items.ForEach(i => { if (i != null)i.NotifyPropertyChanged("Selected"); });
  155. var listbox = (((e.AddedItems[0] as PivotItem).Content as Grid).Children.First(c => c.GetType() == typeof(ListBox)) as ListBox);
  156. if (listbox != null)
  157. {
  158. if (listbox.Items.Contains(App.ViewModel.CurrentList.CurrentItem))
  159. {
  160. listbox.SelectedItem = App.ViewModel.CurrentList.CurrentItem;
  161. }
  162. else
  163. {
  164. var item = (listbox.SelectedItem as ItemViewModel);
  165. if (item != null)
  166. {
  167. App.ViewModel.CurrentList.CurrentItem = item;
  168. }
  169. }
  170. }
  171. //FirstListBox.SelectedIndex = -1;
  172. //SecondListBox.SelectedIndex = -1;
  173. //App.ViewModel.CurrentList.CurrentItem = null;
  174. //SetButtonsState();
  175. }
  176. private void ApplicationBarListsIconButton_Click(object sender, EventArgs e)
  177. {
  178. NavigationService.Navigate(new Uri("/Lists.xaml", UriKind.Relative));
  179. }
  180. private void ApplicationBarMenuItemHelp_Click(object sender, EventArgs e)
  181. {
  182. MessageBox.Show(ShoppingList.Resources.MainPage_Help, ShoppingList.Resources.Help, MessageBoxButton.OK);
  183. }
  184. private void btn_sendviaemail_Click(object sender, EventArgs e)
  185. {
  186. ListViewModel.MailContent content = ListViewModel.MailContent.All;
  187. if (itemspivot.SelectedItem == onlist)
  188. {
  189. content = ListViewModel.MailContent.List;
  190. }
  191. else if (itemspivot.SelectedItem == incart)
  192. {
  193. content = ListViewModel.MailContent.Cart;
  194. }
  195. App.ViewModel.CurrentList.SendViaEmail(null, content);
  196. }
  197. private void btn_sortitemsbyname_Click(object sender, EventArgs e)
  198. {
  199. SortItemsOnSelectedPage(ItemViewModel.NameComparison);
  200. //if (itemspivot.SelectedItem == onlist)
  201. //{
  202. // itemstosort = App.ViewModel.CurrentList.ItemsOnList.ToList();
  203. // itemstosort.Sort(comparison);
  204. // App.ViewModel.CurrentList.ItemsOnList = new System.Collections.ObjectModel.ObservableCollection<ItemViewModel>();
  205. // itemstosort.ForEach(i => App.ViewModel.CurrentList.ItemsOnList.Add(i));
  206. // App.ViewModel.CurrentList.OnPropertyChanged("ItemsOnList");
  207. //}
  208. //else if (itemspivot.SelectedItem == incart)
  209. //{
  210. // itemstosort = App.ViewModel.CurrentList.ItemsInCart.ToList();
  211. // itemstosort.Sort(comparison);
  212. // App.ViewModel.CurrentList.ItemsInCart = new System.Collections.ObjectModel.ObservableCollection<ItemViewModel>();
  213. // itemstosort.ForEach(i => App.ViewModel.CurrentList.ItemsInCart.Add(i));
  214. // App.ViewModel.CurrentList.OnPropertyChanged("ItemsInCart");
  215. //}
  216. }
  217. private void btn_sortitemsbyprice_Click(object sender, EventArgs e)
  218. {
  219. SortItemsOnSelectedPage(ItemViewModel.PriceComparison);
  220. }
  221. private void btn_sortitemsbyunitprice_Click(object sender, EventArgs e)
  222. {
  223. SortItemsOnSelectedPage(ItemViewModel.UnitPriceComparison);
  224. }
  225. private void SortItemsOnSelectedPage(Comparison<ItemViewModel> comparison)
  226. {
  227. bool listcase = itemspivot.SelectedItem == onlist;
  228. App.ViewModel.CurrentList.CurrentItem = null;
  229. var items = listcase ? App.ViewModel.CurrentList.ItemsOnList.ToList() :
  230. App.ViewModel.CurrentList.ItemsInCart.ToList();
  231. items.Sort(comparison);
  232. if (listcase)
  233. {
  234. App.ViewModel.CurrentList.ItemsOnList = new System.Collections.ObjectModel.ObservableCollection<ItemViewModel>();
  235. items.ForEach(i => App.ViewModel.CurrentList.ItemsOnList.Add(i));
  236. }
  237. else
  238. {
  239. App.ViewModel.CurrentList.ItemsInCart = new System.Collections.ObjectModel.ObservableCollection<ItemViewModel>();
  240. items.ForEach(i => App.ViewModel.CurrentList.ItemsInCart.Add(i));
  241. }
  242. App.ViewModel.CurrentList.OnPropertyChanged(listcase ? "ItemsOnList" : "ItemsInCart");
  243. }
  244. }
  245. }