using MediaBrowser.Model.Dto; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Net; using MediaBrowser.Plugins.DefaultTheme.Resources; using MediaBrowser.UI; using MediaBrowser.UI.Controls; using MediaBrowser.UI.Pages; using MediaBrowser.UI.ViewModels; using System.Collections.Generic; using System.Threading.Tasks; namespace MediaBrowser.Plugins.DefaultTheme.Pages { /// /// Interaction logic for HomePage.xaml /// public partial class HomePage : BaseHomePage { /// /// Initializes a new instance of the class. /// public HomePage() { InitializeComponent(); lstCollectionFolders.ItemInvoked += lstCollectionFolders_ItemInvoked; } /// /// The _favorite items /// private ItemCollectionViewModel _favoriteItems; /// /// Gets or sets the favorite items. /// /// The favorite items. public ItemCollectionViewModel FavoriteItems { get { return _favoriteItems; } set { _favoriteItems = value; OnPropertyChanged("FavoriteItems"); } } /// /// The _resumable items /// private ItemCollectionViewModel _resumableItems; /// /// Gets or sets the resumable items. /// /// The resumable items. public ItemCollectionViewModel ResumableItems { get { return _resumableItems; } set { _resumableItems = value; OnPropertyChanged("ResumableItems"); } } /// /// The _recently added items /// private ItemCollectionViewModel _recentlyAddedItems; /// /// Gets or sets the recently added items. /// /// The recently added items. public ItemCollectionViewModel RecentlyAddedItems { get { return _recentlyAddedItems; } set { _recentlyAddedItems = value; OnPropertyChanged("RecentlyAddedItems"); } } /// /// The _recently played items /// private ItemCollectionViewModel _recentlyPlayedItems; /// /// Gets or sets the recently played items. /// /// The recently played items. public ItemCollectionViewModel RecentlyPlayedItems { get { return _recentlyPlayedItems; } set { _recentlyPlayedItems = value; OnPropertyChanged("RecentlyPlayedItems"); } } /// /// The _spotlight items /// private ItemCollectionViewModel _spotlightItems; /// /// Gets or sets the spotlight items. /// /// The spotlight items. public ItemCollectionViewModel SpotlightItems { get { return _spotlightItems; } set { _spotlightItems = value; OnPropertyChanged("SpotlightItems"); } } /// /// The _top picks /// private ItemCollectionViewModel _topPicks; /// /// Gets or sets the top picks. /// /// The top picks. public ItemCollectionViewModel TopPicks { get { return _topPicks; } set { _topPicks = value; OnPropertyChanged("TopPicks"); } } /// /// LSTs the collection folders_ item invoked. /// /// The sender. /// The e. void lstCollectionFolders_ItemInvoked(object sender, ItemEventArgs e) { var model = e.Argument as DtoBaseItemViewModel; if (model != null) { App.Instance.NavigateToItem(model.Item); } } /// /// Called when [loaded]. /// protected override void OnLoaded() { base.OnLoaded(); AppResources.Instance.SetDefaultPageTitle(); } /// /// Gets called anytime the Folder gets refreshed /// protected override void OnFolderChanged() { base.OnFolderChanged(); Task.Run(() => RefreshSpecialItems()); } /// /// Refreshes the special items. /// /// Task. private async Task RefreshSpecialItems() { var tasks = new List(); tasks.Add(RefreshFavoriteItemsAsync()); // In-Progress Items if (Folder.ResumableItemCount > 0) { tasks.Add(RefreshResumableItemsAsync()); } else { SetResumableItems(new BaseItemDto[] { }); } // Recently Added Items if (Folder.RecentlyAddedItemCount > 0) { tasks.Add(RefreshRecentlyAddedItemsAsync()); } else { SetRecentlyAddedItems(new BaseItemDto[] { }); } // Recently Played Items if (Folder.RecentlyPlayedItemCount > 0) { tasks.Add(RefreshRecentlyPlayedItemsAsync()); } else { SetRecentlyPlayedItems(new BaseItemDto[] { }); } tasks.Add(RefreshTopPicksAsync()); tasks.Add(RefreshSpotlightItemsAsync()); await Task.WhenAll(tasks).ConfigureAwait(false); } /// /// Refreshes the favorite items async. /// /// Task. private async Task RefreshFavoriteItemsAsync() { var query = new ItemQuery { Filters = new[] { ItemFilter.IsFavorite }, ImageTypes = new[] { ImageType.Backdrop, ImageType.Thumb }, UserId = App.Instance.CurrentUser.Id, ParentId = Folder.Id, Limit = 10, SortBy = new[] { ItemSortBy.Random }, Recursive = true }; try { var result = await App.Instance.ApiClient.GetItemsAsync(query).ConfigureAwait(false); SetFavoriteItems(result.Items); } catch (HttpException) { // Already logged in lower levels // Don't allow the entire screen to fail } } /// /// Refreshes the resumable items async. /// /// Task. private async Task RefreshResumableItemsAsync() { var query = new ItemQuery { Filters = new[] { ItemFilter.IsResumable }, ImageTypes = new[] { ImageType.Backdrop, ImageType.Thumb }, UserId = App.Instance.CurrentUser.Id, ParentId = Folder.Id, Limit = 10, SortBy = new[] { ItemSortBy.DatePlayed }, SortOrder = SortOrder.Descending, Recursive = true }; try { var result = await App.Instance.ApiClient.GetItemsAsync(query).ConfigureAwait(false); SetResumableItems(result.Items); } catch (HttpException) { // Already logged in lower levels // Don't allow the entire screen to fail } } /// /// Refreshes the recently played items async. /// /// Task. private async Task RefreshRecentlyPlayedItemsAsync() { var query = new ItemQuery { Filters = new[] { ItemFilter.IsRecentlyPlayed }, ImageTypes = new[] { ImageType.Backdrop, ImageType.Thumb }, UserId = App.Instance.CurrentUser.Id, ParentId = Folder.Id, Limit = 10, SortBy = new[] { ItemSortBy.DatePlayed }, SortOrder = SortOrder.Descending, Recursive = true }; try { var result = await App.Instance.ApiClient.GetItemsAsync(query).ConfigureAwait(false); SetRecentlyPlayedItems(result.Items); } catch (HttpException) { // Already logged in lower levels // Don't allow the entire screen to fail } } /// /// Refreshes the recently added items async. /// /// Task. private async Task RefreshRecentlyAddedItemsAsync() { var query = new ItemQuery { Filters = new[] { ItemFilter.IsRecentlyAdded, ItemFilter.IsNotFolder }, ImageTypes = new[] { ImageType.Backdrop, ImageType.Thumb }, UserId = App.Instance.CurrentUser.Id, ParentId = Folder.Id, Limit = 10, SortBy = new[] { ItemSortBy.DateCreated }, SortOrder = SortOrder.Descending, Recursive = true }; try { var result = await App.Instance.ApiClient.GetItemsAsync(query).ConfigureAwait(false); SetRecentlyAddedItems(result.Items); } catch (HttpException) { // Already logged in lower levels // Don't allow the entire screen to fail } } /// /// Refreshes the top picks async. /// /// Task. private async Task RefreshTopPicksAsync() { var query = new ItemQuery { ImageTypes = new[] { ImageType.Backdrop, ImageType.Thumb }, Filters = new[] { ItemFilter.IsRecentlyAdded, ItemFilter.IsNotFolder }, UserId = App.Instance.CurrentUser.Id, ParentId = Folder.Id, Limit = 10, SortBy = new[] { ItemSortBy.Random }, SortOrder = SortOrder.Descending, Recursive = true }; try { var result = await App.Instance.ApiClient.GetItemsAsync(query).ConfigureAwait(false); TopPicks = new ItemCollectionViewModel { Items = result.Items, Name = "Top Picks" }; } catch (HttpException) { // Already logged in lower levels // Don't allow the entire screen to fail } } /// /// Refreshes the spotlight items async. /// /// Task. private async Task RefreshSpotlightItemsAsync() { var query = new ItemQuery { ImageTypes = new[] { ImageType.Backdrop }, ExcludeItemTypes = new[] { "Season" }, UserId = App.Instance.CurrentUser.Id, ParentId = Folder.Id, Limit = 10, SortBy = new[] { ItemSortBy.Random }, Recursive = true }; try { var result = await App.Instance.ApiClient.GetItemsAsync(query).ConfigureAwait(false); SpotlightItems = new ItemCollectionViewModel(rotationPeriodMs: 6000, rotationDevaiationMs: 1000) { Items = result.Items }; } catch (HttpException) { // Already logged in lower levels // Don't allow the entire screen to fail } } /// /// Sets the favorite items. /// /// The items. private void SetFavoriteItems(BaseItemDto[] items) { FavoriteItems = new ItemCollectionViewModel { Items = items, Name = "Favorites" }; } /// /// Sets the resumable items. /// /// The items. private void SetResumableItems(BaseItemDto[] items) { ResumableItems = new ItemCollectionViewModel { Items = items, Name = "Resume" }; } /// /// Sets the recently played items. /// /// The items. private void SetRecentlyPlayedItems(BaseItemDto[] items) { RecentlyPlayedItems = new ItemCollectionViewModel { Items = items, Name = "Recently Played" }; } /// /// Sets the recently added items. /// /// The items. private void SetRecentlyAddedItems(BaseItemDto[] items) { RecentlyAddedItems = new ItemCollectionViewModel { Items = items, Name = "Recently Added" }; } } }