SearchPage.xaml.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.Collections.ObjectModel;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Navigation;
  13. using Microsoft.Phone.Controls;
  14. using RSSReader.Model;
  15. namespace RSSReader.Views
  16. {
  17. /// <summary>
  18. /// Page for filtering RSS items based on a search string
  19. /// </summary>
  20. public partial class SearchPage : 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. /// Id of the feed containing RSS items
  28. /// </summary>
  29. private int feedId;
  30. /// <summary>
  31. /// Id of the page containing the RSS feed
  32. /// </summary>
  33. private int pageId;
  34. /// <summary>
  35. /// RSS items contained in the view
  36. /// </summary>
  37. public ObservableCollection<RSSItem> Items { get; set; }
  38. /// <summary>
  39. /// ViewSource used to show Items collection in a listbox
  40. /// </summary>
  41. public CollectionViewSource ItemsView { get; set; }
  42. /// <summary>
  43. /// A flag to prevent premature ItemSelectionChanged calls while still setting up the listbox
  44. /// </summary>
  45. private bool isLoadingFinished = false;
  46. /// <summary>
  47. /// Constructor
  48. /// </summary>
  49. public SearchPage()
  50. {
  51. InitializeComponent();
  52. Loaded += new System.Windows.RoutedEventHandler(SearchPage_Loaded);
  53. }
  54. /// <summary>
  55. /// Handler used to open virtual keyboard upon Loaded event
  56. /// </summary>
  57. /// <param name="sender"></param>
  58. /// <param name="e"></param>
  59. void SearchPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
  60. {
  61. // Set the focus to the TextBox to get the keyboard open
  62. SearchBox.Focus();
  63. }
  64. /// <summary>
  65. /// Loads the selected RSSFeed to the ListBox when
  66. /// navigated to this page
  67. /// </summary>
  68. /// <param name="e"></param>
  69. protected override void OnNavigatedTo(NavigationEventArgs e)
  70. {
  71. base.OnNavigatedTo(e);
  72. if (isNewInstance)
  73. {
  74. Items = new ObservableCollection<RSSItem>();
  75. ItemsView = new CollectionViewSource();
  76. feedId = int.Parse(NavigationContext.QueryString["feed"]);
  77. pageId = int.Parse(NavigationContext.QueryString["page"]);
  78. RSSFeed feed = RSSService.GetRSSFeed(pageId, feedId);
  79. foreach (RSSItem item in feed.Items)
  80. {
  81. Items.Add(item);
  82. }
  83. ItemsView.Source = Items;
  84. ItemListBox.ItemsSource = ItemsView.View;
  85. ItemListBox.SelectedIndex = App.NO_SELECTION;
  86. isLoadingFinished = true;
  87. isNewInstance = false;
  88. }
  89. }
  90. /// <summary>
  91. /// Handles moving to ItemPage after an RSSItem has been selected
  92. /// </summary>
  93. /// <param name="sender"></param>
  94. /// <param name="e"></param>
  95. private void ItemSelectionChanged(object sender, SelectionChangedEventArgs e)
  96. {
  97. if (e.AddedItems.Count == 1)
  98. {
  99. RSSItem item = (RSSItem)e.AddedItems[0];
  100. RSSService.SelectedItem = item;
  101. ListBox listBox = (sender as ListBox);
  102. int selected = listBox.SelectedIndex;
  103. if (selected != App.NO_SELECTION && isLoadingFinished)
  104. {
  105. // Deselect the selected item
  106. listBox.SelectedIndex = App.NO_SELECTION;
  107. isLoadingFinished = false;
  108. NavigationService.Navigate(new Uri("/Views/ItemPage.xaml?item=" + selected.ToString() +
  109. "&feed=" + feedId.ToString() + "&page=" + pageId.ToString(), UriKind.Relative));
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// Fired whenever search text has changed. Filters RSSItems
  115. /// </summary>
  116. /// <param name="sender"></param>
  117. /// <param name="e"></param>
  118. private void SearchTextChanged(object sender, TextChangedEventArgs e)
  119. {
  120. TextBox searchBox = (TextBox)sender;
  121. ItemsView.View.Filter = i =>
  122. {
  123. if (i == null) return true;
  124. RSSItem item = (RSSItem)i;
  125. return (item.Title.ToLower().Contains(searchBox.Text.ToLower()));
  126. };
  127. }
  128. }
  129. }