using MediaBrowser.Model.DTO;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Net;
using MediaBrowser.UI;
using MediaBrowser.UI.Controls;
using MediaBrowser.UI.ViewModels;
using Microsoft.Expression.Media.Effects;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MediaBrowser.Plugins.DefaultTheme.Controls
{
///
/// Interaction logic for MultiItemTile.xaml
///
public partial class MultiItemTile : BaseUserControl
{
///
/// The _image width
///
private int _imageWidth;
///
/// Gets or sets the width of the image.
///
/// The width of the image.
public int ImageWidth
{
get { return _imageWidth; }
set
{
_imageWidth = value;
mainGrid.Width = value;
}
}
///
/// The _image height
///
private int _imageHeight;
///
/// Gets or sets the height of the image.
///
/// The height of the image.
public int ImageHeight
{
get { return _imageHeight; }
set
{
_imageHeight = value;
mainGrid.Height = value;
}
}
///
/// The _effects
///
TransitionEffect[] _effects = new TransitionEffect[]
{
new BlindsTransitionEffect { Orientation = BlindOrientation.Horizontal },
new BlindsTransitionEffect { Orientation = BlindOrientation.Vertical },
new CircleRevealTransitionEffect { },
new FadeTransitionEffect { },
new SlideInTransitionEffect { SlideDirection= SlideDirection.TopToBottom},
new SlideInTransitionEffect { SlideDirection= SlideDirection.RightToLeft},
new WipeTransitionEffect { WipeDirection = WipeDirection.RightToLeft},
new WipeTransitionEffect { WipeDirection = WipeDirection.TopLeftToBottomRight}
};
///
/// Gets or sets the random.
///
/// The random.
private Random Random { get; set; }
///
/// Gets the collection.
///
/// The collection.
public ItemCollectionViewModel Collection
{
get { return DataContext as ItemCollectionViewModel; }
}
///
/// Initializes a new instance of the class.
///
public MultiItemTile()
{
InitializeComponent();
Random = new Random(Guid.NewGuid().GetHashCode());
mainGrid.Width = ImageWidth;
mainGrid.Height = ImageHeight;
DataContextChanged += BaseItemTile_DataContextChanged;
}
///
/// Handles the DataContextChanged event of the BaseItemTile control.
///
/// The source of the event.
/// The instance containing the event data.
void BaseItemTile_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
OnCurrentItemChanged();
if (Collection != null)
{
Collection.PropertyChanged += Collection_PropertyChanged;
}
}
///
/// Handles the PropertyChanged event of the Collection control.
///
/// The source of the event.
/// The instance containing the event data.
void Collection_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("CurrentItem"))
{
OnCurrentItemChanged();
}
}
///
/// Called when [current item changed].
///
private async void OnCurrentItemChanged()
{
if (Collection == null)
{
// Setting this to null doesn't seem to clear out the content
transitionControl.Content = new FrameworkElement();
txtName.Text = null;
return;
}
var currentItem = Collection.CurrentItem;
if (currentItem == null)
{
// Setting this to null doesn't seem to clear out the content
transitionControl.Content = new FrameworkElement();
txtName.Text = Collection.Name;
return;
}
var img = new Image
{
Stretch = Stretch.Uniform,
Width = ImageWidth,
Height = ImageHeight
};
var url = GetImageSource(currentItem);
if (!string.IsNullOrEmpty(url))
{
try
{
img.Source = await App.Instance.GetRemoteBitmapAsync(url);
txtName.Text = Collection.Name ?? currentItem.Name;
}
catch (HttpException)
{
}
}
transitionControl.TransitionType = _effects[Random.Next(0, _effects.Length)];
transitionControl.Content = img;
}
///
/// Gets the image source.
///
/// The item.
/// Uri.
private string GetImageSource(DtoBaseItem item)
{
if (item != null)
{
if (item.BackdropCount > 0)
{
return App.Instance.ApiClient.GetImageUrl(item, new ImageOptions
{
ImageType = ImageType.Backdrop,
Height = ImageHeight,
Width = ImageWidth
});
}
if (item.HasThumb)
{
return App.Instance.ApiClient.GetImageUrl(item, new ImageOptions
{
ImageType = ImageType.Thumb,
Height = ImageHeight,
Width = ImageWidth
});
}
if (item.HasPrimaryImage)
{
return App.Instance.ApiClient.GetImageUrl(item, new ImageOptions
{
ImageType = ImageType.Primary,
Height = ImageHeight
});
}
}
return null;
}
}
}