DtoBaseItemViewModel.cs 6.2 KB

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