| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using MediaBrowser.Controller.Entities; |
| | | 4 | | |
| | | 5 | | namespace MediaBrowser.Controller.Library |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Single definition of "which alternate version was most recently played" shared by the resume tile |
| | | 9 | | /// (<see cref="IUserDataManager.GetResumeUserData"/>), the media-source default ordering and Next Up. |
| | | 10 | | /// Each call site declares its own eligibility rule so the intentional differences (resumable-only vs. |
| | | 11 | | /// resumable-or-completed) are visible in one place instead of being re-implemented divergently. |
| | | 12 | | /// The SQL resume query keeps its own translation of the same rule. |
| | | 13 | | /// </summary> |
| | | 14 | | public static class VersionPlaybackSelector |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Selects the entry whose user data has the greatest <see cref="UserItemData.LastPlayedDate"/>, |
| | | 18 | | /// considering only entries that satisfy <paramref name="isEligible"/>. On an exact tie the first |
| | | 19 | | /// encountered entry wins. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <typeparam name="T">The candidate type (e.g. a version item or a media source).</typeparam> |
| | | 22 | | /// <param name="items">The candidates to choose from.</param> |
| | | 23 | | /// <param name="dataSelector">Resolves the user data for a candidate, or <c>null</c> when it has none.</param> |
| | | 24 | | /// <param name="isEligible">Whether a candidate's user data makes it a valid winner.</param> |
| | | 25 | | /// <returns>The most recently played eligible candidate, or <c>default</c> when none qualify.</returns> |
| | | 26 | | public static T? SelectMostRecentlyPlayed<T>( |
| | | 27 | | IEnumerable<T> items, |
| | | 28 | | Func<T, UserItemData?> dataSelector, |
| | | 29 | | Func<UserItemData, bool> isEligible) |
| | | 30 | | { |
| | 3 | 31 | | ArgumentNullException.ThrowIfNull(items); |
| | 3 | 32 | | ArgumentNullException.ThrowIfNull(dataSelector); |
| | 3 | 33 | | ArgumentNullException.ThrowIfNull(isEligible); |
| | | 34 | | |
| | 3 | 35 | | T? winner = default; |
| | 3 | 36 | | var winnerDate = DateTime.MinValue; |
| | 3 | 37 | | var hasWinner = false; |
| | | 38 | | |
| | 24 | 39 | | foreach (var item in items) |
| | | 40 | | { |
| | 9 | 41 | | var data = dataSelector(item); |
| | 9 | 42 | | if (data is null || !isEligible(data)) |
| | | 43 | | { |
| | | 44 | | continue; |
| | | 45 | | } |
| | | 46 | | |
| | 3 | 47 | | var date = data.LastPlayedDate ?? DateTime.MinValue; |
| | 3 | 48 | | if (!hasWinner || date > winnerDate) |
| | | 49 | | { |
| | 3 | 50 | | winner = item; |
| | 3 | 51 | | winnerDate = date; |
| | 3 | 52 | | hasWinner = true; |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | 3 | 56 | | return winner; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | } |