| | | 1 | | using System; |
| | | 2 | | using Jellyfin.Data.Enums; |
| | | 3 | | using MediaBrowser.Controller.Entities; |
| | | 4 | | using MediaBrowser.Controller.Sorting; |
| | | 5 | | using MediaBrowser.Model.Entities; |
| | | 6 | | using MediaBrowser.Model.Globalization; |
| | | 7 | | |
| | | 8 | | namespace Emby.Server.Implementations.Sorting; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Class providing comparison for official ratings. |
| | | 12 | | /// </summary> |
| | | 13 | | public class OfficialRatingComparer : IBaseItemComparer |
| | | 14 | | { |
| | | 15 | | private readonly ILocalizationManager _localizationManager; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="OfficialRatingComparer"/> class. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="localizationManager">Instance of the <see cref="ILocalizationManager"/> interface.</param> |
| | | 21 | | public OfficialRatingComparer(ILocalizationManager localizationManager) |
| | | 22 | | { |
| | 21 | 23 | | _localizationManager = localizationManager; |
| | 21 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the name. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <value>The name.</value> |
| | 1 | 30 | | public ItemSortBy Type => ItemSortBy.OfficialRating; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Compares the specified x. |
| | | 34 | | /// </summary> |
| | | 35 | | /// <param name="x">The x.</param> |
| | | 36 | | /// <param name="y">The y.</param> |
| | | 37 | | /// <returns>System.Int32.</returns> |
| | | 38 | | public int Compare(BaseItem? x, BaseItem? y) |
| | | 39 | | { |
| | 0 | 40 | | ArgumentNullException.ThrowIfNull(x); |
| | 0 | 41 | | ArgumentNullException.ThrowIfNull(y); |
| | 0 | 42 | | var zeroRating = new ParentalRatingScore(0, 0); |
| | | 43 | | |
| | 0 | 44 | | var ratingX = string.IsNullOrEmpty(x.OfficialRating) ? zeroRating : _localizationManager.GetRatingScore(x.Offici |
| | 0 | 45 | | var ratingY = string.IsNullOrEmpty(y.OfficialRating) ? zeroRating : _localizationManager.GetRatingScore(y.Offici |
| | 0 | 46 | | var scoreCompare = ratingX.Score.CompareTo(ratingY.Score); |
| | 0 | 47 | | if (scoreCompare is 0) |
| | | 48 | | { |
| | 0 | 49 | | return (ratingX.SubScore ?? 0).CompareTo(ratingY.SubScore ?? 0); |
| | | 50 | | } |
| | | 51 | | |
| | 0 | 52 | | return scoreCompare; |
| | | 53 | | } |
| | | 54 | | } |