aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.UI/ViewModels/DtoBaseItemViewModel.cs
blob: f8194e04b84b2c54e49a526c5c5c11c89b91f64d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.UI.Pages;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace MediaBrowser.UI.ViewModels
{
    /// <summary>
    /// Class DtoBaseItemViewModel
    /// </summary>
    public class DtoBaseItemViewModel : BaseViewModel
    {
        /// <summary>
        /// The _average primary image aspect ratio
        /// </summary>
        private double _averagePrimaryImageAspectRatio;
        /// <summary>
        /// Gets the aspect ratio that should be used if displaying the primary image
        /// </summary>
        /// <value>The average primary image aspect ratio.</value>
        public double AveragePrimaryImageAspectRatio
        {
            get { return _averagePrimaryImageAspectRatio; }

            set
            {
                _averagePrimaryImageAspectRatio = value;
                OnPropertyChanged("AveragePrimaryImageAspectRatio");
            }
        }

        /// <summary>
        /// The _parent display preferences
        /// </summary>
        private DisplayPreferences _parentDisplayPreferences;
        /// <summary>
        /// Gets of sets the current DisplayPreferences
        /// </summary>
        /// <value>The parent display preferences.</value>
        public DisplayPreferences ParentDisplayPreferences
        {
            get { return _parentDisplayPreferences; }

            set
            {
                _parentDisplayPreferences = value;
                NotifyDisplayPreferencesChanged();
            }
        }

        /// <summary>
        /// The _item
        /// </summary>
        private BaseItemDto _item;
        /// <summary>
        /// Gets or sets the item.
        /// </summary>
        /// <value>The item.</value>
        public BaseItemDto Item
        {
            get { return _item; }

            set
            {
                _item = value;
                OnPropertyChanged("Item");
            }
        }

        /// <summary>
        /// Notifies the display preferences changed.
        /// </summary>
        public void NotifyDisplayPreferencesChanged()
        {
            OnPropertyChanged("DisplayPreferences");
        }

        /// <summary>
        /// Gets an image url that can be used to download an image from the api
        /// </summary>
        /// <param name="imageType">The type of image requested</param>
        /// <param name="imageIndex">The image index, if there are multiple. Currently only applies to backdrops. Supply null or 0 for first backdrop.</param>
        /// <returns>System.String.</returns>
        public string GetImageUrl(ImageType imageType, int? imageIndex = null)
        {
            var height = ParentDisplayPreferences.PrimaryImageHeight;

            var averageAspectRatio = BaseFolderPage.GetAspectRatio(imageType, AveragePrimaryImageAspectRatio);

            var width = height * averageAspectRatio;

            var imageOptions = new ImageOptions
            {
                ImageType = imageType,
                ImageIndex = imageIndex,
                Height = height
            };

            if (imageType == ImageType.Primary)
            {
                var currentAspectRatio = imageType == ImageType.Primary ? Item.PrimaryImageAspectRatio ?? width / height : width / height;

                // Preserve the exact AR if it deviates from the average significantly
                var preserveExactAspectRatio = Math.Abs(currentAspectRatio - averageAspectRatio) > .10;

                if (!preserveExactAspectRatio)
                {
                    imageOptions.Width = Convert.ToInt32(width);
                }
            }

            return App.Instance.ApiClient.GetImageUrl(Item, imageOptions);
        }

        /// <summary>
        /// Gets the average primary image aspect ratio.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <returns>System.Double.</returns>
        /// <exception cref="System.ArgumentNullException">items</exception>
        public static double GetAveragePrimaryImageAspectRatio(IEnumerable<BaseItemDto> items)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            double total = 0;
            var count = 0;

            foreach (var child in items)
            {
                var ratio = child.PrimaryImageAspectRatio ?? 0;

                if (ratio.Equals(0))
                {
                    continue;
                }

                total += ratio;
                count++;
            }

            return count == 0 ? 1 : total / count;
        }

        /// <summary>
        /// Gets the observable items.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <returns>ObservableCollection{DtoBaseItemViewModel}.</returns>
        public static ObservableCollection<DtoBaseItemViewModel> GetObservableItems(BaseItemDto[] items)
        {
            return GetObservableItems(items, GetAveragePrimaryImageAspectRatio(items));
        }
        
        /// <summary>
        /// Gets the observable items.
        /// </summary>
        /// <param name="items">The items.</param>
        /// <param name="averagePrimaryImageAspectRatio">The average primary image aspect ratio.</param>
        /// <param name="parentDisplayPreferences">The parent display preferences.</param>
        /// <returns>ObservableCollection{DtoBaseItemViewModel}.</returns>
        /// <exception cref="System.ArgumentNullException">items</exception>
        public static ObservableCollection<DtoBaseItemViewModel> GetObservableItems(IEnumerable<BaseItemDto> items, double averagePrimaryImageAspectRatio, DisplayPreferences parentDisplayPreferences = null)
        {
            if (items == null)
            {
                throw new ArgumentNullException("items");
            }

            return new ObservableCollection<DtoBaseItemViewModel>(items.Select(i => new DtoBaseItemViewModel
            {
                Item = i,
                ParentDisplayPreferences = parentDisplayPreferences,
                AveragePrimaryImageAspectRatio = averagePrimaryImageAspectRatio
            }));
        }
    }
}