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