FeedPivot.xaml.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright © 2011-2012 Nokia Corporation. All rights reserved.
  3. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
  4. * Other product and company names mentioned herein may be trademarks
  5. * or trade names of their respective owners.
  6. * See LICENSE.TXT for license information.
  7. */
  8. using System;
  9. using System.Linq;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Navigation;
  13. using Microsoft.Phone.Controls;
  14. using RSSReader.Model;
  15. namespace RSSReader.Views
  16. {
  17. /// <summary>
  18. /// Page that shows the feed pivot
  19. /// </summary>
  20. public partial class FeedPivot : PhoneApplicationPage
  21. {
  22. /// <summary>
  23. /// True when this object instance has been just created, otherwise false
  24. /// </summary>
  25. private bool isNewInstance = true;
  26. /// <summary>
  27. /// Feed currently shown to the user
  28. /// </summary>
  29. int feedId;
  30. /// <summary>
  31. /// Feed that was shown to the user originally when he/she entered the view
  32. /// </summary>
  33. int originalFeedId;
  34. /// <summary>
  35. /// Id of the page shown in the pivot
  36. /// </summary>
  37. int pageId;
  38. /// <summary>
  39. /// Instance of the page shown in the pivot
  40. /// </summary>
  41. RSSPage page;
  42. /// <summary>
  43. /// Constructor
  44. /// </summary>
  45. public FeedPivot()
  46. {
  47. InitializeComponent();
  48. }
  49. /// <summary>
  50. /// Overridden OnNavigatedTo handler
  51. /// </summary>
  52. protected override void OnNavigatedTo(NavigationEventArgs e)
  53. {
  54. base.OnNavigatedTo(e);
  55. if (isNewInstance)
  56. {
  57. originalFeedId = feedId = int.Parse(NavigationContext.QueryString["id"]);
  58. pageId = int.Parse(NavigationContext.QueryString["page"]);
  59. page = RSSService.GetRSSPage(pageId);
  60. InitializePivotViews(page);
  61. // Switch to the previously selected PivotItem, if it has been stored
  62. if (State.ContainsKey("feedPivotIndex"))
  63. {
  64. RSSItemsPivot.SelectedIndex = (int)State["feedPivotIndex"];
  65. }
  66. isNewInstance = false;
  67. }
  68. }
  69. /// <summary>
  70. /// Overridden OnNavigatedFrom handler
  71. /// </summary>
  72. protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
  73. {
  74. if (e.NavigationMode != NavigationMode.Back)
  75. {
  76. // Remember the PivotItem we were on
  77. State["feedPivotIndex"] = RSSItemsPivot.SelectedIndex;
  78. }
  79. }
  80. /// <summary>
  81. /// Method that initializes all pivot items
  82. /// </summary>
  83. /// <param name="page"></param>
  84. private void InitializePivotViews(RSSPage page)
  85. {
  86. // Perform initialization only on the 1st time
  87. if (RSSItemsPivot.Items.Count == 0)
  88. {
  89. // Initialize PivotItems
  90. // Initialize them starting from the one that was tapped
  91. // so that when the PivotItems show up, the active one
  92. // is already the tapped one. Otherwise we'd have to wait
  93. // for all of the PivotItems to load and then change the
  94. // SelectedIndex, which would result in animation in the
  95. // Pivot control.
  96. for (int i = 0; i < page.Feeds.Count; i++)
  97. {
  98. int j = originalFeedId + i;
  99. if (j >= page.Feeds.Count)
  100. {
  101. j = j % page.Feeds.Count;
  102. }
  103. CreatePivotItem(page.Feeds[j]);
  104. }
  105. }
  106. }
  107. /// <summary>
  108. /// Method that initializes a single pivot item
  109. /// </summary>
  110. /// <param name="f">Feed to initialize the pivot item from</param>
  111. private void CreatePivotItem(RSSFeed f)
  112. {
  113. if (f.IsVisible)
  114. {
  115. // Programmatically create the pivot item
  116. // Note that most of the time you'd want to do this with XAML and data binding,
  117. // or at least use a data template for the content of the pivot item. However in
  118. // this case it's better to create everything manually because we are updating the state
  119. // of pivot items in LoadItems(). This means that we have to access the pivot
  120. // item controls directly, which is very difficult to do in Silverlight if the
  121. // controls are being created through data template (this is because FindName()
  122. // method of FrameworkTemplate is not available in Silverlight). It would probably be
  123. // possible to go around the need of accessing controls directly through data-binding but
  124. // that would most likely lead to more code lines than the 16 we need here.
  125. PivotItem pivotItem = new PivotItem();
  126. pivotItem.Header = f.Title;
  127. pivotItem.Margin = new Thickness(0, 0, 0, 0);
  128. Grid grid = new Grid();
  129. grid.Margin = new Thickness(25, 0, 0, 0);
  130. // Add a progressbar for each pivot item, initially hidden
  131. ProgressBar progressBar = new ProgressBar();
  132. progressBar.IsIndeterminate = false;
  133. progressBar.Visibility = Visibility.Collapsed;
  134. grid.Children.Add(progressBar);
  135. ListBox listBox = new ListBox();
  136. listBox.ItemTemplate = (DataTemplate)Resources["RSSItemTemplate"];
  137. listBox.SelectionChanged += ItemSelectionChanged;
  138. // Enable tilt effect for the listbox.
  139. // TilEffect is part of Silverlight Toolkit which does not ship as part of this example. Please see
  140. // release notes for instructions how to install and use Silverlight Toolkit.
  141. TiltEffect.SetIsTiltEnabled(listBox, true);
  142. grid.Children.Add(listBox);
  143. pivotItem.Content = grid;
  144. RSSItemsPivot.Items.Add(pivotItem);
  145. }
  146. }
  147. /// <summary>
  148. /// Pivot selection changed, load feed items
  149. /// </summary>
  150. /// <param name="sender"></param>
  151. /// <param name="e"></param>
  152. private void FeedSelectionChanged(object sender, SelectionChangedEventArgs e)
  153. {
  154. UpdateFeedId((sender as Pivot).SelectedIndex);
  155. LoadItems(true);
  156. }
  157. /// <summary>
  158. /// Helper method for updating pivot selection
  159. /// </summary>
  160. /// <param name="selectedIndex"></param>
  161. private void UpdateFeedId(int selectedIndex)
  162. {
  163. feedId = originalFeedId + selectedIndex;
  164. if (feedId >= page.Feeds.Count)
  165. {
  166. feedId = feedId % page.Feeds.Count;
  167. }
  168. }
  169. /// <summary>
  170. /// Item selected, go to ItemPage
  171. /// </summary>
  172. /// <param name="sender"></param>
  173. /// <param name="e"></param>
  174. private void ItemSelectionChanged(object sender, SelectionChangedEventArgs e)
  175. {
  176. if (e.AddedItems.Count == 1)
  177. {
  178. RSSItem item = (RSSItem)e.AddedItems[0];
  179. RSSService.SelectedItem = item;
  180. ListBox listBox = (sender as ListBox);
  181. int selected = listBox.SelectedIndex;
  182. if (selected != App.NO_SELECTION)
  183. {
  184. // Deselect the selected item
  185. listBox.SelectedIndex = App.NO_SELECTION;
  186. NavigationService.Navigate(new Uri("/Views/ItemPage.xaml?item=" + selected.ToString() + "&feed=" + feedId.ToString() + "&page=" + pageId.ToString(), UriKind.Relative));
  187. }
  188. }
  189. }
  190. /// <summary>
  191. /// Handler for refresh button
  192. /// </summary>
  193. private void AppBarRefresh(object sender, EventArgs e)
  194. {
  195. LoadItems(false);
  196. }
  197. /// <summary>
  198. /// Handler for search button
  199. /// </summary>
  200. private void AppBarSearch(object sender, EventArgs e)
  201. {
  202. NavigationService.Navigate(new Uri("/Views/SearchPage.xaml?page=" + pageId.ToString() + "&feed=" + feedId.ToString(), UriKind.Relative));
  203. }
  204. /// <summary>
  205. /// Loads feed's items to view
  206. /// </summary>
  207. /// <param name="useCache">True if cache should be used, otherwise false</param>
  208. private void LoadItems(bool useCache)
  209. {
  210. PivotItem selectedItem = RSSItemsPivot.SelectedItem as PivotItem;
  211. Grid contentGrid = selectedItem.Content as Grid;
  212. ProgressBar progressBar = contentGrid.Children.First() as ProgressBar;
  213. ListBox listBox = contentGrid.Children.Last() as ListBox;
  214. listBox.Visibility = Visibility.Collapsed;
  215. progressBar.Visibility = Visibility.Visible;
  216. progressBar.IsIndeterminate = true;
  217. RSSService.GetRSSItems(
  218. pageId,
  219. feedId,
  220. useCache,
  221. (rssItems) =>
  222. {
  223. listBox.ItemsSource = rssItems;
  224. listBox.Visibility = Visibility.Visible;
  225. progressBar.Visibility = Visibility.Collapsed;
  226. progressBar.IsIndeterminate = false;
  227. },
  228. (exception) =>
  229. {
  230. MessageBox.Show("Application failed to retrieve content from server. Please check your network connectivity.");
  231. }
  232. );
  233. }
  234. }
  235. }