ListUtil.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public static class ListUtil
  8. {
  9. /// <summary>
  10. /// 前后端共享排序,相册列表的排序方法
  11. /// </summary>
  12. /// <param name="photoInfos"></param>
  13. /// <typeparam name="T">T务必含有ToppingStatus,CreationTime</typeparam>
  14. /// <returns></returns>
  15. public static List<T> SortInfos<T>(this List<T> photoInfos)
  16. {
  17. photoInfos.Sort((a, b) =>
  18. {
  19. PropertyInfo toppingStatusProperty = typeof(T).GetProperty("ToppingStatus");
  20. if (toppingStatusProperty != null && toppingStatusProperty.PropertyType == typeof(bool))
  21. {
  22. bool aValue = (bool)toppingStatusProperty.GetValue(a);
  23. bool bValue = (bool)toppingStatusProperty.GetValue(b);
  24. if (aValue && !bValue) return -1;
  25. if (bValue && !aValue) return 1;
  26. }
  27. // 如果无法获取或不满足条件,则按照 CreationTime 降序排序
  28. PropertyInfo creationTimeProperty = typeof(T).GetProperty("CreationTime");
  29. if (creationTimeProperty != null && creationTimeProperty.PropertyType == typeof(long))
  30. {
  31. long aValue = (long)creationTimeProperty.GetValue(a);
  32. long bValue = (long)creationTimeProperty.GetValue(b);
  33. return bValue.CompareTo(aValue);
  34. }
  35. return 0;
  36. });
  37. return photoInfos;
  38. }
  39. /// <summary>
  40. /// 用来做泛型数据切换,上一条,下一条...
  41. /// </summary>
  42. /// <param name="list"></param>
  43. /// <param name="type"></param>
  44. /// <param name="currentIndex">当前处于的索引</param>
  45. /// <param name="newIndex">操作数据之后返回的数据的索引,也就是最新的索引</param>
  46. /// <typeparam name="T"></typeparam>
  47. /// <returns></returns>
  48. public static T Navigate<T>(List<T> list, NavigateType type, int currentIndex, out int newIndex) where T : new()
  49. {
  50. if (type == NavigateType.Previous)
  51. {
  52. //如果已经是最前面的一条数据,则切换到最后一条数据,形成一个切换循环
  53. if (currentIndex <= 0)
  54. {
  55. currentIndex = list.Count - 1;
  56. newIndex = currentIndex;
  57. return list[currentIndex];
  58. }
  59. }
  60. else if (type == NavigateType.Next)
  61. {
  62. //如果已经是最后一条数据,则切换到第一条数据,形成一个切换循环
  63. if (currentIndex >= list.Count - 1)
  64. {
  65. currentIndex = 0;
  66. newIndex = currentIndex;
  67. return list[currentIndex];
  68. }
  69. }
  70. else
  71. {
  72. newIndex = currentIndex;
  73. return list[currentIndex];
  74. }
  75. int previousIndex = currentIndex - 1;
  76. int nextIndex = currentIndex + 1;
  77. if (previousIndex < 0 && nextIndex >= list.Count)
  78. {
  79. newIndex = currentIndex;
  80. return list[currentIndex];
  81. }
  82. if (type == NavigateType.Previous && previousIndex >= 0)
  83. {
  84. newIndex = previousIndex;
  85. return list[previousIndex];
  86. }
  87. if (type == NavigateType.Next && nextIndex < list.Count)
  88. {
  89. newIndex = nextIndex;
  90. return list[nextIndex];
  91. }
  92. newIndex = currentIndex;
  93. return list[currentIndex];
  94. }
  95. /// <summary>
  96. /// 动作枚举
  97. /// </summary>
  98. public enum NavigateType
  99. {
  100. /// <summary>
  101. /// 不作切换动作,返回当前索引的数据
  102. /// </summary>
  103. None = 0,
  104. /// <summary>
  105. /// 上一条
  106. /// </summary>
  107. Previous = 1,
  108. /// <summary>
  109. /// 下一条
  110. /// </summary>
  111. Next = 2
  112. }
  113. }
  114. }