| | 1 | | using System; |
| | 2 | | using Jellyfin.Data.Enums; |
| | 3 | | using MediaBrowser.Controller.Entities; |
| | 4 | | using MediaBrowser.Controller.Sorting; |
| | 5 | | using MediaBrowser.Model.Querying; |
| | 6 | |
|
| | 7 | | namespace Emby.Server.Implementations.Sorting |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Class PremiereDateComparer. |
| | 11 | | /// </summary> |
| | 12 | | public class PremiereDateComparer : IBaseItemComparer |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Gets the name. |
| | 16 | | /// </summary> |
| | 17 | | /// <value>The name.</value> |
| 1 | 18 | | public ItemSortBy Type => ItemSortBy.PremiereDate; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Compares the specified x. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="x">The x.</param> |
| | 24 | | /// <param name="y">The y.</param> |
| | 25 | | /// <returns>System.Int32.</returns> |
| | 26 | | public int Compare(BaseItem? x, BaseItem? y) |
| | 27 | | { |
| 14 | 28 | | return GetDate(x).CompareTo(GetDate(y)); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Gets the date. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="x">The x.</param> |
| | 35 | | /// <returns>DateTime.</returns> |
| | 36 | | private static DateTime GetDate(BaseItem? x) |
| | 37 | | { |
| 28 | 38 | | if (x is null) |
| | 39 | | { |
| 0 | 40 | | return DateTime.MinValue; |
| | 41 | | } |
| | 42 | |
|
| 28 | 43 | | if (x.PremiereDate.HasValue) |
| | 44 | | { |
| 18 | 45 | | return x.PremiereDate.Value; |
| | 46 | | } |
| | 47 | |
|
| 10 | 48 | | if (x.ProductionYear.HasValue) |
| | 49 | | { |
| | 50 | | try |
| | 51 | | { |
| 8 | 52 | | return new DateTime(x.ProductionYear.Value, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
| | 53 | | } |
| 2 | 54 | | catch (ArgumentOutOfRangeException) |
| | 55 | | { |
| | 56 | | // Don't blow up if the item has a bad ProductionYear, just return MinValue |
| 2 | 57 | | } |
| | 58 | | } |
| | 59 | |
|
| 4 | 60 | | return DateTime.MinValue; |
| 6 | 61 | | } |
| | 62 | | } |
| | 63 | | } |