ChapterInfoDtoViewModel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using MediaBrowser.Model.Dto;
  2. using MediaBrowser.Model.Entities;
  3. using MediaBrowser.Model.Net;
  4. using System;
  5. using System.Linq;
  6. using System.Windows.Media.Imaging;
  7. namespace MediaBrowser.UI.ViewModels
  8. {
  9. /// <summary>
  10. /// Class ChapterInfoDtoViewModel
  11. /// </summary>
  12. public class ChapterInfoDtoViewModel : BaseViewModel
  13. {
  14. /// <summary>
  15. /// Gets or sets the image download options.
  16. /// </summary>
  17. /// <value>The image download options.</value>
  18. public ImageOptions ImageDownloadOptions { get; set; }
  19. /// <summary>
  20. /// The _image width
  21. /// </summary>
  22. private double _imageWidth;
  23. /// <summary>
  24. /// Gets or sets the width of the image.
  25. /// </summary>
  26. /// <value>The width of the image.</value>
  27. public double ImageWidth
  28. {
  29. get { return _imageWidth; }
  30. set
  31. {
  32. _imageWidth = value;
  33. OnPropertyChanged("ImageWidth");
  34. }
  35. }
  36. /// <summary>
  37. /// The _image height
  38. /// </summary>
  39. private double _imageHeight;
  40. /// <summary>
  41. /// Gets or sets the height of the image.
  42. /// </summary>
  43. /// <value>The height of the image.</value>
  44. public double ImageHeight
  45. {
  46. get { return _imageHeight; }
  47. set
  48. {
  49. _imageHeight = value;
  50. OnPropertyChanged("ImageHeight");
  51. }
  52. }
  53. /// <summary>
  54. /// The _item
  55. /// </summary>
  56. private ChapterInfoDto _chapter;
  57. /// <summary>
  58. /// Gets or sets the item.
  59. /// </summary>
  60. /// <value>The item.</value>
  61. public ChapterInfoDto Chapter
  62. {
  63. get { return _chapter; }
  64. set
  65. {
  66. _chapter = value;
  67. OnPropertyChanged("Chapter");
  68. OnPropertyChanged("TimeString");
  69. OnChapterChanged();
  70. }
  71. }
  72. /// <summary>
  73. /// The _item
  74. /// </summary>
  75. private BaseItemDto _item;
  76. /// <summary>
  77. /// Gets or sets the item.
  78. /// </summary>
  79. /// <value>The item.</value>
  80. public BaseItemDto Item
  81. {
  82. get { return _item; }
  83. set
  84. {
  85. _item = value;
  86. OnPropertyChanged("Item");
  87. }
  88. }
  89. /// <summary>
  90. /// Gets the time string.
  91. /// </summary>
  92. /// <value>The time string.</value>
  93. public string TimeString
  94. {
  95. get
  96. {
  97. var time = TimeSpan.FromTicks(Chapter.StartPositionTicks);
  98. return time.ToString(time.TotalHours < 1 ? "m':'ss" : "h':'mm':'ss");
  99. }
  100. }
  101. /// <summary>
  102. /// The _image
  103. /// </summary>
  104. private BitmapImage _image;
  105. /// <summary>
  106. /// Gets the image.
  107. /// </summary>
  108. /// <value>The image.</value>
  109. public BitmapImage Image
  110. {
  111. get { return _image; }
  112. set
  113. {
  114. _image = value;
  115. OnPropertyChanged("Image");
  116. }
  117. }
  118. /// <summary>
  119. /// Called when [item changed].
  120. /// </summary>
  121. private async void OnChapterChanged()
  122. {
  123. var options = ImageDownloadOptions ?? new ImageOptions { };
  124. options.ImageType = ImageType.ChapterImage;
  125. options.ImageIndex = Item.Chapters.IndexOf(Chapter);
  126. try
  127. {
  128. Image = await App.Instance.GetRemoteBitmapAsync(App.Instance.ApiClient.GetImageUrl(Item, options));
  129. }
  130. catch (HttpException)
  131. {
  132. }
  133. }
  134. /// <summary>
  135. /// Gets the height of the chapter image.
  136. /// </summary>
  137. /// <param name="item">The item.</param>
  138. /// <param name="height">The height.</param>
  139. /// <param name="defaultWidth">The default width.</param>
  140. /// <returns>System.Double.</returns>
  141. public static double GetChapterImageWidth(BaseItemDto item, double height, double defaultWidth)
  142. {
  143. var width = defaultWidth;
  144. if (item.MediaStreams != null)
  145. {
  146. var videoStream = item.MediaStreams.FirstOrDefault(s => s.Type == MediaStreamType.Video);
  147. if (videoStream != null)
  148. {
  149. double streamHeight = videoStream.Height ?? 0;
  150. double streamWidth = videoStream.Width ?? 0;
  151. if (streamHeight > 0 && streamWidth > 0)
  152. {
  153. var aspectRatio = streamWidth / streamHeight;
  154. width = height * aspectRatio;
  155. }
  156. }
  157. }
  158. return width;
  159. }
  160. }
  161. }