MultiItemTile.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using MediaBrowser.Model.Dto;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.Model.Net;
  4. using MediaBrowser.UI;
  5. using MediaBrowser.UI.Controls;
  6. using MediaBrowser.UI.ViewModels;
  7. using Microsoft.Expression.Media.Effects;
  8. using System;
  9. using System.ComponentModel;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Media;
  13. namespace MediaBrowser.Plugins.DefaultTheme.Controls
  14. {
  15. /// <summary>
  16. /// Interaction logic for MultiItemTile.xaml
  17. /// </summary>
  18. public partial class MultiItemTile : BaseUserControl
  19. {
  20. /// <summary>
  21. /// The _image width
  22. /// </summary>
  23. private int _imageWidth;
  24. /// <summary>
  25. /// Gets or sets the width of the image.
  26. /// </summary>
  27. /// <value>The width of the image.</value>
  28. public int ImageWidth
  29. {
  30. get { return _imageWidth; }
  31. set
  32. {
  33. _imageWidth = value;
  34. mainGrid.Width = value;
  35. }
  36. }
  37. /// <summary>
  38. /// The _image height
  39. /// </summary>
  40. private int _imageHeight;
  41. /// <summary>
  42. /// Gets or sets the height of the image.
  43. /// </summary>
  44. /// <value>The height of the image.</value>
  45. public int ImageHeight
  46. {
  47. get { return _imageHeight; }
  48. set
  49. {
  50. _imageHeight = value;
  51. mainGrid.Height = value;
  52. }
  53. }
  54. /// <summary>
  55. /// The _effects
  56. /// </summary>
  57. TransitionEffect[] _effects = new TransitionEffect[]
  58. {
  59. new BlindsTransitionEffect { Orientation = BlindOrientation.Horizontal },
  60. new BlindsTransitionEffect { Orientation = BlindOrientation.Vertical },
  61. new CircleRevealTransitionEffect { },
  62. new FadeTransitionEffect { },
  63. new SlideInTransitionEffect { SlideDirection= SlideDirection.TopToBottom},
  64. new SlideInTransitionEffect { SlideDirection= SlideDirection.RightToLeft},
  65. new WipeTransitionEffect { WipeDirection = WipeDirection.RightToLeft},
  66. new WipeTransitionEffect { WipeDirection = WipeDirection.TopLeftToBottomRight}
  67. };
  68. /// <summary>
  69. /// Gets or sets the random.
  70. /// </summary>
  71. /// <value>The random.</value>
  72. private Random Random { get; set; }
  73. /// <summary>
  74. /// Gets the collection.
  75. /// </summary>
  76. /// <value>The collection.</value>
  77. public ItemCollectionViewModel Collection
  78. {
  79. get { return DataContext as ItemCollectionViewModel; }
  80. }
  81. /// <summary>
  82. /// Initializes a new instance of the <see cref="MultiItemTile" /> class.
  83. /// </summary>
  84. public MultiItemTile()
  85. {
  86. InitializeComponent();
  87. Random = new Random(Guid.NewGuid().GetHashCode());
  88. mainGrid.Width = ImageWidth;
  89. mainGrid.Height = ImageHeight;
  90. DataContextChanged += BaseItemTile_DataContextChanged;
  91. }
  92. /// <summary>
  93. /// Handles the DataContextChanged event of the BaseItemTile control.
  94. /// </summary>
  95. /// <param name="sender">The source of the event.</param>
  96. /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
  97. void BaseItemTile_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
  98. {
  99. OnCurrentItemChanged();
  100. if (Collection != null)
  101. {
  102. Collection.PropertyChanged += Collection_PropertyChanged;
  103. }
  104. }
  105. /// <summary>
  106. /// Handles the PropertyChanged event of the Collection control.
  107. /// </summary>
  108. /// <param name="sender">The source of the event.</param>
  109. /// <param name="e">The <see cref="PropertyChangedEventArgs" /> instance containing the event data.</param>
  110. void Collection_PropertyChanged(object sender, PropertyChangedEventArgs e)
  111. {
  112. if (e.PropertyName.Equals("CurrentItem"))
  113. {
  114. OnCurrentItemChanged();
  115. }
  116. }
  117. /// <summary>
  118. /// Called when [current item changed].
  119. /// </summary>
  120. private async void OnCurrentItemChanged()
  121. {
  122. if (Collection == null)
  123. {
  124. // Setting this to null doesn't seem to clear out the content
  125. transitionControl.Content = new FrameworkElement();
  126. txtName.Text = null;
  127. return;
  128. }
  129. var currentItem = Collection.CurrentItem;
  130. if (currentItem == null)
  131. {
  132. // Setting this to null doesn't seem to clear out the content
  133. transitionControl.Content = new FrameworkElement();
  134. txtName.Text = Collection.Name;
  135. return;
  136. }
  137. var img = new Image
  138. {
  139. Stretch = Stretch.Uniform,
  140. Width = ImageWidth,
  141. Height = ImageHeight
  142. };
  143. var url = GetImageSource(currentItem);
  144. if (!string.IsNullOrEmpty(url))
  145. {
  146. try
  147. {
  148. img.Source = await App.Instance.GetRemoteBitmapAsync(url);
  149. txtName.Text = Collection.Name ?? currentItem.Name;
  150. }
  151. catch (HttpException)
  152. {
  153. }
  154. }
  155. transitionControl.TransitionType = _effects[Random.Next(0, _effects.Length)];
  156. transitionControl.Content = img;
  157. }
  158. /// <summary>
  159. /// Gets the image source.
  160. /// </summary>
  161. /// <param name="item">The item.</param>
  162. /// <returns>Uri.</returns>
  163. private string GetImageSource(BaseItemDto item)
  164. {
  165. if (item != null)
  166. {
  167. if (item.BackdropCount > 0)
  168. {
  169. return App.Instance.ApiClient.GetImageUrl(item, new ImageOptions
  170. {
  171. ImageType = ImageType.Backdrop,
  172. Height = ImageHeight,
  173. Width = ImageWidth
  174. });
  175. }
  176. if (item.HasThumb)
  177. {
  178. return App.Instance.ApiClient.GetImageUrl(item, new ImageOptions
  179. {
  180. ImageType = ImageType.Thumb,
  181. Height = ImageHeight,
  182. Width = ImageWidth
  183. });
  184. }
  185. if (item.HasPrimaryImage)
  186. {
  187. return App.Instance.ApiClient.GetImageUrl(item, new ImageOptions
  188. {
  189. ImageType = ImageType.Primary,
  190. Height = ImageHeight
  191. });
  192. }
  193. }
  194. return null;
  195. }
  196. }
  197. }