EnumerableExtensions.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Model.Providers;
  5. namespace MediaBrowser.Model.Extensions
  6. {
  7. /// <summary>
  8. /// Extension methods for <see cref="IEnumerable{T}"/>.
  9. /// </summary>
  10. public static class EnumerableExtensions
  11. {
  12. /// <summary>
  13. /// Orders <see cref="RemoteImageInfo"/> by requested language in descending order, prioritizing "en" over other non-matches.
  14. /// </summary>
  15. /// <param name="remoteImageInfos">The remote image infos.</param>
  16. /// <param name="requestedLanguage">The requested language for the images.</param>
  17. /// <returns>The ordered remote image infos.</returns>
  18. public static IEnumerable<RemoteImageInfo> OrderByLanguageDescending(this IEnumerable<RemoteImageInfo> remoteImageInfos, string requestedLanguage)
  19. {
  20. if (string.IsNullOrWhiteSpace(requestedLanguage))
  21. {
  22. // Default to English if no requested language is specified.
  23. requestedLanguage = "en";
  24. }
  25. return remoteImageInfos.OrderByDescending(i =>
  26. {
  27. // Image priority ordering:
  28. // - Images that match the requested language
  29. // - Images with no language
  30. // - TODO: Images that match the original language
  31. // - Images in English
  32. // - Images that don't match the requested language
  33. if (string.Equals(requestedLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
  34. {
  35. return 4;
  36. }
  37. if (string.IsNullOrEmpty(i.Language))
  38. {
  39. return 3;
  40. }
  41. if (string.Equals(i.Language, "en", StringComparison.OrdinalIgnoreCase))
  42. {
  43. return 2;
  44. }
  45. return 0;
  46. })
  47. .ThenByDescending(i => Math.Round(i.CommunityRating ?? 0, 1) )
  48. .ThenByDescending(i => i.VoteCount ?? 0);
  49. }
  50. }
  51. }