BaseListPage.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using MediaBrowser.Model.Dto;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.UI.Controls;
  4. using MediaBrowser.UI.ViewModels;
  5. using System;
  6. using System.Threading;
  7. using System.Windows.Controls;
  8. namespace MediaBrowser.UI.Pages
  9. {
  10. /// <summary>
  11. /// Provides a base page for all list pages
  12. /// </summary>
  13. public abstract class BaseListPage : BaseFolderPage
  14. {
  15. /// <summary>
  16. /// Gets or sets the current selection timer.
  17. /// </summary>
  18. /// <value>The current selection timer.</value>
  19. private Timer CurrentSelectionTimer { get; set; }
  20. /// <summary>
  21. /// Subclasses must provide the list box that holds the items
  22. /// </summary>
  23. /// <value>The items list.</value>
  24. protected abstract ExtendedListBox ItemsList { get; }
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="BaseListPage" /> class.
  27. /// </summary>
  28. /// <param name="itemId">The item id.</param>
  29. protected BaseListPage(string itemId)
  30. : base(itemId)
  31. {
  32. }
  33. /// <summary>
  34. /// Raises the <see cref="E:Initialized" /> event.
  35. /// </summary>
  36. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  37. protected override void OnInitialized(EventArgs e)
  38. {
  39. base.OnInitialized(e);
  40. ItemsList.SelectionChanged += ItemsList_SelectionChanged;
  41. ItemsList.ItemInvoked += ItemsList_ItemInvoked;
  42. }
  43. /// <summary>
  44. /// The _current item
  45. /// </summary>
  46. private BaseItemDto _currentItem;
  47. /// <summary>
  48. /// Gets or sets the current selected item
  49. /// </summary>
  50. /// <value>The current item.</value>
  51. public BaseItemDto CurrentItem
  52. {
  53. get { return _currentItem; }
  54. set
  55. {
  56. _currentItem = value;
  57. // Update the current item index immediately
  58. UpdateCurrentItemIndex(value);
  59. // Fire notification events after a short delay
  60. // We don't want backdrops and logos reloading while the user is navigating quickly
  61. if (CurrentSelectionTimer != null)
  62. {
  63. CurrentSelectionTimer.Change(500, Timeout.Infinite);
  64. }
  65. else
  66. {
  67. CurrentSelectionTimer = new Timer(CurrentItemChangedTimerCallback, value, 500, Timeout.Infinite);
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// Fires when the current item selection timer expires
  73. /// </summary>
  74. /// <param name="state">The state.</param>
  75. private void CurrentItemChangedTimerCallback(object state)
  76. {
  77. Dispatcher.InvokeAsync(() =>
  78. {
  79. // Fire notification events for the UI
  80. OnPropertyChanged("CurrentItem");
  81. // Alert subclasses
  82. OnCurrentItemChanged();
  83. });
  84. // Dispose the timer
  85. CurrentSelectionTimer.Dispose();
  86. CurrentSelectionTimer = null;
  87. }
  88. /// <summary>
  89. /// Updates the current item index based on the current selection
  90. /// </summary>
  91. /// <param name="value">The value.</param>
  92. private void UpdateCurrentItemIndex(BaseItemDto value)
  93. {
  94. if (value == null)
  95. {
  96. CurrentItemIndex = -1;
  97. }
  98. else
  99. {
  100. CurrentItemIndex = ItemsList.SelectedIndex;
  101. }
  102. }
  103. /// <summary>
  104. /// The _current item index
  105. /// </summary>
  106. private int _currentItemIndex;
  107. /// <summary>
  108. /// Gets of sets the index of the current item being displayed
  109. /// </summary>
  110. /// <value>The index of the current item.</value>
  111. public int CurrentItemIndex
  112. {
  113. get { return _currentItemIndex; }
  114. set
  115. {
  116. _currentItemIndex = value;
  117. OnPropertyChanged("CurrentItemIndex");
  118. }
  119. }
  120. /// <summary>
  121. /// Handles the list selection changed event to update the current item
  122. /// </summary>
  123. /// <param name="sender">The source of the event.</param>
  124. /// <param name="e">The <see cref="SelectionChangedEventArgs" /> instance containing the event data.</param>
  125. void ItemsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
  126. {
  127. if (e.AddedItems.Count > 0)
  128. {
  129. CurrentItem = (e.AddedItems[0] as DtoBaseItemViewModel).Item;
  130. }
  131. else
  132. {
  133. CurrentItem = null;
  134. }
  135. }
  136. /// <summary>
  137. /// Itemses the list_ item invoked.
  138. /// </summary>
  139. /// <param name="sender">The sender.</param>
  140. /// <param name="e">The e.</param>
  141. /// <exception cref="System.NotImplementedException"></exception>
  142. void ItemsList_ItemInvoked(object sender, ItemEventArgs<object> e)
  143. {
  144. var model = e.Argument as DtoBaseItemViewModel;
  145. if (model != null)
  146. {
  147. App.Instance.NavigateToItem(model.Item);
  148. }
  149. }
  150. /// <summary>
  151. /// Handles current item selection changes
  152. /// </summary>
  153. protected virtual void OnCurrentItemChanged()
  154. {
  155. if (CurrentItem != null)
  156. {
  157. SetBackdrops(CurrentItem);
  158. }
  159. }
  160. /// <summary>
  161. /// Gets called anytime a DisplayPreferences property is updated
  162. /// </summary>
  163. public override void NotifyDisplayPreferencesChanged()
  164. {
  165. base.NotifyDisplayPreferencesChanged();
  166. // Make sure the items list has been initialized
  167. if (ItemsList != null)
  168. {
  169. if (DisplayPreferences.ScrollDirection == ScrollDirection.Horizontal)
  170. {
  171. ScrollViewer.SetHorizontalScrollBarVisibility(ItemsList, ScrollBarVisibility.Hidden);
  172. ScrollViewer.SetVerticalScrollBarVisibility(ItemsList, ScrollBarVisibility.Disabled);
  173. }
  174. else
  175. {
  176. ScrollViewer.SetHorizontalScrollBarVisibility(ItemsList, ScrollBarVisibility.Disabled);
  177. ScrollViewer.SetVerticalScrollBarVisibility(ItemsList, ScrollBarVisibility.Hidden);
  178. }
  179. }
  180. }
  181. }
  182. }