DtoBaseItemViewModel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using MediaBrowser.Model.Dto;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.UI.Pages;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Linq;
  8. namespace MediaBrowser.UI.ViewModels
  9. {
  10. /// <summary>
  11. /// Class DtoBaseItemViewModel
  12. /// </summary>
  13. public class DtoBaseItemViewModel : BaseViewModel
  14. {
  15. /// <summary>
  16. /// The _average primary image aspect ratio
  17. /// </summary>
  18. private double _averagePrimaryImageAspectRatio;
  19. /// <summary>
  20. /// Gets the aspect ratio that should be used if displaying the primary image
  21. /// </summary>
  22. /// <value>The average primary image aspect ratio.</value>
  23. public double AveragePrimaryImageAspectRatio
  24. {
  25. get { return _averagePrimaryImageAspectRatio; }
  26. set
  27. {
  28. _averagePrimaryImageAspectRatio = value;
  29. OnPropertyChanged("AveragePrimaryImageAspectRatio");
  30. }
  31. }
  32. /// <summary>
  33. /// The _parent display preferences
  34. /// </summary>
  35. private DisplayPreferences _parentDisplayPreferences;
  36. /// <summary>
  37. /// Gets of sets the current DisplayPreferences
  38. /// </summary>
  39. /// <value>The parent display preferences.</value>
  40. public DisplayPreferences ParentDisplayPreferences
  41. {
  42. get { return _parentDisplayPreferences; }
  43. set
  44. {
  45. _parentDisplayPreferences = value;
  46. NotifyDisplayPreferencesChanged();
  47. }
  48. }
  49. /// <summary>
  50. /// The _item
  51. /// </summary>
  52. private BaseItemDto _item;
  53. /// <summary>
  54. /// Gets or sets the item.
  55. /// </summary>
  56. /// <value>The item.</value>
  57. public BaseItemDto Item
  58. {
  59. get { return _item; }
  60. set
  61. {
  62. _item = value;
  63. OnPropertyChanged("Item");
  64. }
  65. }
  66. /// <summary>
  67. /// Notifies the display preferences changed.
  68. /// </summary>
  69. public void NotifyDisplayPreferencesChanged()
  70. {
  71. OnPropertyChanged("DisplayPreferences");
  72. }
  73. /// <summary>
  74. /// Gets an image url that can be used to download an image from the api
  75. /// </summary>
  76. /// <param name="imageType">The type of image requested</param>
  77. /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
  78. /// <returns>System.String.</returns>
  79. public string GetImageUrl(ImageType imageType, int? imageIndex = null)
  80. {
  81. var height = ParentDisplayPreferences.PrimaryImageHeight;
  82. var averageAspectRatio = BaseFolderPage.GetAspectRatio(imageType, AveragePrimaryImageAspectRatio);
  83. var width = height * averageAspectRatio;
  84. var imageOptions = new ImageOptions
  85. {
  86. ImageType = imageType,
  87. ImageIndex = imageIndex,
  88. Height = height
  89. };
  90. if (imageType == ImageType.Primary)
  91. {
  92. var currentAspectRatio = imageType == ImageType.Primary ? Item.PrimaryImageAspectRatio ?? width / height : width / height;
  93. // Preserve the exact AR if it deviates from the average significantly
  94. var preserveExactAspectRatio = Math.Abs(currentAspectRatio - averageAspectRatio) > .10;
  95. if (!preserveExactAspectRatio)
  96. {
  97. imageOptions.Width = Convert.ToInt32(width);
  98. }
  99. }
  100. return App.Instance.ApiClient.GetImageUrl(Item, imageOptions);
  101. }
  102. /// <summary>
  103. /// Gets the average primary image aspect ratio.
  104. /// </summary>
  105. /// <param name="items">The items.</param>
  106. /// <returns>System.Double.</returns>
  107. /// <exception cref="System.ArgumentNullException">items</exception>
  108. public static double GetAveragePrimaryImageAspectRatio(IEnumerable<BaseItemDto> items)
  109. {
  110. if (items == null)
  111. {
  112. throw new ArgumentNullException("items");
  113. }
  114. double total = 0;
  115. var count = 0;
  116. foreach (var child in items)
  117. {
  118. var ratio = child.PrimaryImageAspectRatio ?? 0;
  119. if (ratio.Equals(0))
  120. {
  121. continue;
  122. }
  123. total += ratio;
  124. count++;
  125. }
  126. return count == 0 ? 1 : total / count;
  127. }
  128. /// <summary>
  129. /// Gets the observable items.
  130. /// </summary>
  131. /// <param name="items">The items.</param>
  132. /// <returns>ObservableCollection{DtoBaseItemViewModel}.</returns>
  133. public static ObservableCollection<DtoBaseItemViewModel> GetObservableItems(BaseItemDto[] items)
  134. {
  135. return GetObservableItems(items, GetAveragePrimaryImageAspectRatio(items));
  136. }
  137. /// <summary>
  138. /// Gets the observable items.
  139. /// </summary>
  140. /// <param name="items">The items.</param>
  141. /// <param name="averagePrimaryImageAspectRatio">The average primary image aspect ratio.</param>
  142. /// <param name="parentDisplayPreferences">The parent display preferences.</param>
  143. /// <returns>ObservableCollection{DtoBaseItemViewModel}.</returns>
  144. /// <exception cref="System.ArgumentNullException">items</exception>
  145. public static ObservableCollection<DtoBaseItemViewModel> GetObservableItems(IEnumerable<BaseItemDto> items, double averagePrimaryImageAspectRatio, DisplayPreferences parentDisplayPreferences = null)
  146. {
  147. if (items == null)
  148. {
  149. throw new ArgumentNullException("items");
  150. }
  151. return new ObservableCollection<DtoBaseItemViewModel>(items.Select(i => new DtoBaseItemViewModel
  152. {
  153. Item = i,
  154. ParentDisplayPreferences = parentDisplayPreferences,
  155. AveragePrimaryImageAspectRatio = averagePrimaryImageAspectRatio
  156. }));
  157. }
  158. }
  159. }