powerfun-unity/Assets/Scripts/UI/Prefab/ResultList/ResultListController.cs

340 lines
11 KiB
C#
Raw Normal View History

using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
2021-09-23 18:14:53 +08:00
using DG.Tweening;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
2021-08-26 09:29:35 +08:00
public class ResultListController : PFUIPanel
{
[SerializeField]Transform routeResult;
Transform localRouteItem;
Transform scrollContent;
2022-07-15 14:28:37 +08:00
Transform routeScroll, matchScroll;
Transform routeContent, matchContent;
2022-07-15 13:07:23 +08:00
Transform btnRoute, btnMatch , searchInput;
Color c1, c2;
// Start is called before the first frame update
2021-04-19 15:44:11 +08:00
int pageSize = 20;
string name = "";
2021-09-23 18:14:53 +08:00
Transform[] contents,scrolls,btns;
2021-04-19 15:44:11 +08:00
Dictionary<int, string> filterOptionDict = new Dictionary<int, string>
{
{0,"routes" },{1,"competition" }
};
//交互用
public Transform currentItem { get; set; }
2021-09-23 18:14:53 +08:00
int scrollIndex = 0;
ScrollRect scroll;
2021-07-29 15:45:44 +08:00
void Awake()
{
2021-04-15 15:58:37 +08:00
//ApiBase.SetCookie("73385F5F719B610D132C1ECF3E9143272BF15214D57ED91CD7A9DFD832407471535112AAEB8E9271F75D54FBBF2D99F18FA313C1EEA5676F5D722D7FBB07C926BEC5905591BF9AFDDC6336552DF273112C2DA1794E6FA2F465B11FECD2E82E52");
localRouteItem = Resources.Load<Transform>("UI/Prefab/ResultList/LocalRouteItem");
caches = new Dictionary<string, Texture>();
2021-08-26 15:08:02 +08:00
#if UNITY_ANDROID || UNITY_IOS
routeResult = Resources.Load<Transform>("UI/Prefab/ResultList/RouteItem-Mobile");
localRouteItem = Resources.Load<Transform>("UI/Prefab/ResultList/LocalRouteItem-Mobile");
2021-08-26 15:08:02 +08:00
#endif
InitialColor();
scroll = transform.Find("ListPanel").Find("Scroll").GetComponent<ScrollRect>();
btnMatch = transform.Find("ListPanel").Find("BtnMatch");
btnRoute = transform.Find("ListPanel").Find("BtnRoute");
searchInput = transform.Find("ListPanel").Find("SearchInput");
scrollContent = scroll.transform.Find("Viewport").Find("Content");
routeScroll = scrollContent.Find("RouteList");
routeContent = routeScroll.Find("Viewport").Find("Content");
matchScroll = scrollContent.Find("MatchList");
matchContent = matchScroll.Find("Viewport").Find("Content");
2022-07-15 14:28:37 +08:00
contents = new Transform[] { routeContent, matchContent};
2021-09-23 18:14:53 +08:00
if (btnRoute != null)
{
2021-09-23 18:14:53 +08:00
UIManager.AddEvent(btnRoute.gameObject, EventTriggerType.PointerClick, (b) =>
2021-04-19 15:44:11 +08:00
{
StartScroll(0);
});
2021-09-23 18:14:53 +08:00
//btnRoute.GetComponent<Button>().onClick.AddListener(() => StartScroll(1));
}
2021-09-23 18:14:53 +08:00
if (btnMatch != null)
{
2021-09-23 18:14:53 +08:00
UIManager.AddEvent(btnMatch.gameObject, EventTriggerType.PointerClick, (b) =>
2021-04-19 15:44:11 +08:00
{
StartScroll(1);
});
2021-09-23 18:14:53 +08:00
//btnMatch.GetComponent<Button>().onClick.AddListener(() => StartScroll(0));
}
2022-07-15 13:07:23 +08:00
btns = new Transform[] { btnRoute,btnMatch };
if (searchInput != null)
{
2021-04-19 15:44:11 +08:00
UIManager.AddEvent(searchInput.Find("Button").gameObject, EventTriggerType.PointerClick, (b) =>
{
2021-04-23 15:36:18 +08:00
onEndEdit();
});
searchInput.GetComponent<InputField>().onValueChanged.AddListener((v) =>
{
name = v;
});
2021-04-19 15:44:11 +08:00
//searchInput.Find("Button").GetComponent<Button>().onClick.AddListener(() =>
//{
// name = searchInput.GetComponent<InputField>().text;
// Refresh(contents[scrollIndex]);
//});
}
if (routeScroll != null)
{
UIManager.AddEvent(routeScroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
}
2021-04-25 15:33:17 +08:00
if (matchScroll != null)
{
UIManager.AddEvent(matchScroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
}
2022-07-15 14:28:37 +08:00
scrolls = new Transform[] { routeScroll, matchScroll};
2021-07-29 15:45:44 +08:00
//GetList(0);
2021-04-19 15:44:11 +08:00
GetList(1);
#if UNITY_ANDROID || UNITY_IOS
2021-09-23 18:14:53 +08:00
GetList(2);
#endif
}
2021-04-29 23:45:29 +08:00
public void Load()
2021-04-29 22:05:19 +08:00
{
2021-09-02 14:18:26 +08:00
Debug.Log("刷新show");
2021-09-23 18:14:53 +08:00
//Refresh(contents[0]);
if (contents != null && contents.Length == 3)
2021-04-29 22:05:19 +08:00
{
2021-09-02 14:18:26 +08:00
Debug.Log("刷新show进入");
2021-04-29 23:45:29 +08:00
Refresh(contents[scrollIndex]);
2021-04-29 22:05:19 +08:00
}
2021-04-29 23:45:29 +08:00
//if (scroll!=null)
//{
// scroll.transform.Find("Error").gameObject.SetActive(false);
//}
2021-04-29 22:05:19 +08:00
}
private void OnEndDrag(BaseEventData e)
{
2021-04-25 15:33:17 +08:00
ScrollRect scrollrect = null;
if (e != null)
{
var pe = (PointerEventData)e;
scrollrect = pe.pointerDrag.GetComponent<ScrollRect>();
}
else
{
scrollrect = scrolls[scrollIndex].GetComponent<ScrollRect>();
}
if (scrollrect.verticalNormalizedPosition <= 0)
{
Debug.Log(scrollrect.verticalNormalizedPosition);
2021-04-19 15:44:11 +08:00
pageIndex[scrollIndex]++;
GetList();
}
if (scrollrect.verticalNormalizedPosition >= 1)
{
2021-04-15 15:58:37 +08:00
Refresh(contents[scrollIndex]);
}
}
2021-04-15 15:58:37 +08:00
private void Refresh(Transform content)
{
2021-04-15 15:58:37 +08:00
content.transform.DestroyChildren();
2021-04-19 15:44:11 +08:00
isEnd[scrollIndex] = false;
pageIndex[scrollIndex] = 0;
GetList();
2021-07-30 15:38:40 +08:00
DisplayLocalRouteResult(contents[0], 0);//显示本地未上传成功的记录
}
public void Reset()
{
if (searchInput != null)
{
searchInput.GetComponent<InputField>().text = "";
name = "";
Load();
StartScroll(1);
}
2022-05-31 16:51:07 +08:00
}
2021-04-19 15:44:11 +08:00
Dictionary<int, bool> isEnd = new Dictionary<int, bool>
{
2021-09-23 18:14:53 +08:00
{0,false },{1,false },{2,false }
2021-04-19 15:44:11 +08:00
};
Dictionary<int, int> pageIndex = new Dictionary<int, int>
{
2021-09-23 18:14:53 +08:00
{0,0 },{1,0 },{2,0 }
2021-04-19 15:44:11 +08:00
};
async void GetList(int? sindex = null)
{
2021-04-19 15:44:11 +08:00
var index = sindex ?? scrollIndex;
if (isEnd[index]) return;
2021-07-23 17:59:38 +08:00
Debug.Log(sindex);
2021-09-23 18:14:53 +08:00
JsonResult<List<JObject>> r = null;
if (index == 0 || index == 1)
{
r = await ConfigHelper.mapInterruptRecordApi.GetMapInterruptRecord(name, pageIndex[index], 20, filterOptionDict[index]);
}
2022-07-15 13:07:23 +08:00
if (r.result)
{
2021-04-23 15:36:18 +08:00
if (r.data.Count == 0)
{
2021-04-23 15:36:18 +08:00
if (pageIndex[index] != 0)
{
isEnd[index] = true;
scroll.transform.Find("Error").gameObject.SetActive(false);
}
2021-09-23 18:14:53 +08:00
else
2021-04-23 15:36:18 +08:00
{
//contents[index];
2021-09-23 18:14:53 +08:00
if (!sindex.HasValue || (sindex.HasValue && sindex.Value == scrollIndex))
2021-04-29 23:45:29 +08:00
{
scroll.transform.Find("Error").gameObject.SetActive(true);
}
2021-04-23 15:36:18 +08:00
}
}
else
{
2021-04-19 15:44:11 +08:00
isEnd[index] = false;
2021-09-23 18:14:53 +08:00
DisplayRouteResult(r.data, contents[index], index);
2021-04-23 15:36:18 +08:00
scroll.transform.Find("Error").gameObject.SetActive(false);
2021-04-15 15:58:37 +08:00
//DisplayRouteResult(r.data, matchContent);
2021-09-23 18:14:53 +08:00
}
}
2021-09-23 18:14:53 +08:00
else
{
Utils.showToast(gameObject, r.errMsg);
}
}
[HideInInspector]
public Dictionary<string, Texture> caches = null;
2021-09-23 18:14:53 +08:00
void DisplayRouteResult(List<JObject> list,Transform content,int index)
{
if (routeResult != null)
{
foreach (var item in list)
{
var obj = Instantiate(routeResult);
2021-09-23 18:14:53 +08:00
if (index == 0 || index == 1)
{
obj.GetComponent<RouteItem>().Initial(item.ToObject<RouteResult>(), index, transform, true);
}
2022-05-25 18:40:47 +08:00
obj.transform.SetParent(content);
obj.transform.localScale = new Vector3(1, 1, 1);
}
}
}
void DisplayLocalRouteResult(Transform content, int index)
{
//查询本地上传失败的文件
var parentPath = PFConstants.MapWorkoutRecordFolder;
var dirList = Directory.GetDirectories(parentPath);
foreach (var item in dirList)
{
if (Directory.Exists(item))
{
2022-02-21 16:16:35 +08:00
//var d = new DirectoryInfo(item);
//var createTime = d.CreationTime;//create time
//var newTime = createTime.AddSeconds(30D);
//if (newTime < DateTime.Now)
{
var obj = Instantiate(localRouteItem);
obj.GetComponent<LocalRouteItem>().Initial(item, 0);
obj.transform.parent = content;
obj.transform.localScale = new Vector3(1, 1, 1);
obj.SetAsFirstSibling();
}
}
}
}
void InitialColor()
{
ColorUtility.TryParseHtmlString("#FFFFFF", out c1);
ColorUtility.TryParseHtmlString("#5C5C6E", out c2);
}
// Update is called once per frame
2021-04-23 15:36:18 +08:00
void onEndEdit()
{
name = searchInput.GetComponent<InputField>().text;
Refresh(contents[scrollIndex]);
}
2021-04-25 15:33:17 +08:00
bool startMouse = false;
void Update()
{
2021-09-23 18:14:53 +08:00
//GoScroll();
2021-04-25 15:33:17 +08:00
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
if (scrolls[scrollIndex].GetComponent<ScrollRect>().verticalNormalizedPosition <= 0 ||
scrolls[scrollIndex].GetComponent<ScrollRect>().verticalNormalizedPosition >= (pageIndex[scrollIndex] == 0 ? 1.05 : 1))
{
startMouse = true;
}
}
else
{
if (startMouse)
{
startMouse = false;
OnEndDrag(null);
}
}
if (Input.GetKeyDown(KeyCode.KeypadEnter) || Input.GetKeyDown(KeyCode.Return))
2021-04-23 15:36:18 +08:00
{
onEndEdit();
}
}
void StartScroll(int index)
{
2021-09-23 18:14:53 +08:00
#if UNITY_ANDROID || UNITY_IOS
scrollContent.DOLocalMoveX(674*index*-1,0.3f);
#else
scrollContent.DOLocalMoveX(1158*index*-1,0.3f);
#endif
scrollIndex = index;
for (int i = 0; i < btns.Length; i++)
{
2021-09-23 18:14:53 +08:00
if (!btns[i]) continue;
if (i == index)
{
2021-09-23 18:14:53 +08:00
btns[i].GetComponent<Text>().color = c1;
}
2021-09-23 18:14:53 +08:00
else
{
2021-09-23 18:14:53 +08:00
btns[i].GetComponent<Text>().color = c2;
}
}
2021-09-23 18:14:53 +08:00
//if (startScroll) return;
//if (index == 0)
//{
// //翻到比赛页,比赛按钮变白,线路按钮变灰
// scrollIndex = 1;
// startScroll = true;
// btnRoute.GetComponent<Text>().color = c2;
// btnMatch.GetComponent<Text>().color = c1;
//}
//else if (index == 1)
//{
// //反过来
// scrollIndex = 0;
// startScroll = true;
// btnRoute.GetComponent<Text>().color = c1;
// btnMatch.GetComponent<Text>().color = c2;
//}
2021-09-23 18:14:53 +08:00
}
protected override void OnDestroy()
{
caches = null;
Resources.UnloadUnusedAssets();
}
}