| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using UnityEngine;
- namespace GFGGame
- {
- public static class ListUtil
- {
- /// <summary>
- /// 前后端共享排序,相册列表的排序方法
- /// </summary>
- /// <param name="photoInfos"></param>
- /// <typeparam name="T">T务必含有ToppingStatus,CreationTime</typeparam>
- /// <returns></returns>
- public static List<T> SortInfos<T>(this List<T> photoInfos)
- {
- photoInfos.Sort((a, b) =>
- {
- PropertyInfo toppingStatusProperty = typeof(T).GetProperty("ToppingStatus");
- if (toppingStatusProperty != null && toppingStatusProperty.PropertyType == typeof(bool))
- {
- bool aValue = (bool)toppingStatusProperty.GetValue(a);
- bool bValue = (bool)toppingStatusProperty.GetValue(b);
- if (aValue && !bValue) return -1;
- if (bValue && !aValue) return 1;
- }
- // 如果无法获取或不满足条件,则按照 CreationTime 降序排序
- PropertyInfo creationTimeProperty = typeof(T).GetProperty("CreationTime");
- if (creationTimeProperty != null && creationTimeProperty.PropertyType == typeof(long))
- {
- long aValue = (long)creationTimeProperty.GetValue(a);
- long bValue = (long)creationTimeProperty.GetValue(b);
- return bValue.CompareTo(aValue);
- }
- return 0;
- });
- return photoInfos;
- }
- /// <summary>
- /// 用来做泛型数据切换,上一条,下一条...
- /// </summary>
- /// <param name="list"></param>
- /// <param name="type"></param>
- /// <param name="currentIndex">当前处于的索引</param>
- /// <param name="newIndex">操作数据之后返回的数据的索引,也就是最新的索引</param>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- public static T Navigate<T>(List<T> list, NavigateType type, int currentIndex, out int newIndex) where T : new()
- {
- if (type == NavigateType.Previous)
- {
- //如果已经是最前面的一条数据,则切换到最后一条数据,形成一个切换循环
- if (currentIndex <= 0)
- {
- currentIndex = list.Count - 1;
- newIndex = currentIndex;
- return list[currentIndex];
- }
- }
- else if (type == NavigateType.Next)
- {
- //如果已经是最后一条数据,则切换到第一条数据,形成一个切换循环
- if (currentIndex >= list.Count - 1)
- {
- currentIndex = 0;
- newIndex = currentIndex;
- return list[currentIndex];
- }
- }
- else
- {
- newIndex = currentIndex;
- return list[currentIndex];
- }
- int previousIndex = currentIndex - 1;
- int nextIndex = currentIndex + 1;
- if (previousIndex < 0 && nextIndex >= list.Count)
- {
- newIndex = currentIndex;
- return list[currentIndex];
- }
- if (type == NavigateType.Previous && previousIndex >= 0)
- {
- newIndex = previousIndex;
- return list[previousIndex];
- }
- if (type == NavigateType.Next && nextIndex < list.Count)
- {
- newIndex = nextIndex;
- return list[nextIndex];
- }
- newIndex = currentIndex;
- return list[currentIndex];
- }
- /// <summary>
- /// 动作枚举
- /// </summary>
- public enum NavigateType
- {
- /// <summary>
- /// 不作切换动作,返回当前索引的数据
- /// </summary>
- None = 0,
- /// <summary>
- /// 上一条
- /// </summary>
- Previous = 1,
- /// <summary>
- /// 下一条
- /// </summary>
- Next = 2
- }
- }
- }
|