using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using MediaBrowser.UI.Controls; using MediaBrowser.UI.ViewModels; using System; using System.Threading; using System.Windows.Controls; namespace MediaBrowser.UI.Pages { /// /// Provides a base page for all list pages /// public abstract class BaseListPage : BaseFolderPage { /// /// Gets or sets the current selection timer. /// /// The current selection timer. private Timer CurrentSelectionTimer { get; set; } /// /// Subclasses must provide the list box that holds the items /// /// The items list. protected abstract ExtendedListBox ItemsList { get; } /// /// Initializes a new instance of the class. /// /// The item id. protected BaseListPage(string itemId) : base(itemId) { } /// /// Raises the event. /// /// The instance containing the event data. protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); ItemsList.SelectionChanged += ItemsList_SelectionChanged; ItemsList.ItemInvoked += ItemsList_ItemInvoked; } /// /// The _current item /// private BaseItemDto _currentItem; /// /// Gets or sets the current selected item /// /// The current item. public BaseItemDto CurrentItem { get { return _currentItem; } set { _currentItem = value; // Update the current item index immediately UpdateCurrentItemIndex(value); // Fire notification events after a short delay // We don't want backdrops and logos reloading while the user is navigating quickly if (CurrentSelectionTimer != null) { CurrentSelectionTimer.Change(500, Timeout.Infinite); } else { CurrentSelectionTimer = new Timer(CurrentItemChangedTimerCallback, value, 500, Timeout.Infinite); } } } /// /// Fires when the current item selection timer expires /// /// The state. private void CurrentItemChangedTimerCallback(object state) { Dispatcher.InvokeAsync(() => { // Fire notification events for the UI OnPropertyChanged("CurrentItem"); // Alert subclasses OnCurrentItemChanged(); }); // Dispose the timer CurrentSelectionTimer.Dispose(); CurrentSelectionTimer = null; } /// /// Updates the current item index based on the current selection /// /// The value. private void UpdateCurrentItemIndex(BaseItemDto value) { if (value == null) { CurrentItemIndex = -1; } else { CurrentItemIndex = ItemsList.SelectedIndex; } } /// /// The _current item index /// private int _currentItemIndex; /// /// Gets of sets the index of the current item being displayed /// /// The index of the current item. public int CurrentItemIndex { get { return _currentItemIndex; } set { _currentItemIndex = value; OnPropertyChanged("CurrentItemIndex"); } } /// /// Handles the list selection changed event to update the current item /// /// The source of the event. /// The instance containing the event data. void ItemsList_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (e.AddedItems.Count > 0) { CurrentItem = (e.AddedItems[0] as DtoBaseItemViewModel).Item; } else { CurrentItem = null; } } /// /// Itemses the list_ item invoked. /// /// The sender. /// The e. /// void ItemsList_ItemInvoked(object sender, ItemEventArgs e) { var model = e.Argument as DtoBaseItemViewModel; if (model != null) { App.Instance.NavigateToItem(model.Item); } } /// /// Handles current item selection changes /// protected virtual void OnCurrentItemChanged() { if (CurrentItem != null) { SetBackdrops(CurrentItem); } } /// /// Gets called anytime a DisplayPreferences property is updated /// public override void NotifyDisplayPreferencesChanged() { base.NotifyDisplayPreferencesChanged(); // Make sure the items list has been initialized if (ItemsList != null) { if (DisplayPreferences.ScrollDirection == ScrollDirection.Horizontal) { ScrollViewer.SetHorizontalScrollBarVisibility(ItemsList, ScrollBarVisibility.Hidden); ScrollViewer.SetVerticalScrollBarVisibility(ItemsList, ScrollBarVisibility.Disabled); } else { ScrollViewer.SetHorizontalScrollBarVisibility(ItemsList, ScrollBarVisibility.Disabled); ScrollViewer.SetVerticalScrollBarVisibility(ItemsList, ScrollBarVisibility.Hidden); } } } } }