powerfun-unity/Assets/Scripts/UI/Prefab/ResultList/ResultListController.cs
2021-09-28 11:27:24 +08:00

352 lines
12 KiB
C#

using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
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;
public class ResultListController : PFUIPanel
{
[SerializeField]Transform routeResult;
Transform localRouteItem;
Transform scrollContent;
Transform routeScroll, matchScroll, rowerScroll;
Transform routeContent, matchContent, rowerContent;
Transform btnRoute, btnMatch ,btnRower, searchInput;
Color c1, c2;
// Start is called before the first frame update
int pageSize = 20;
string name = "";
Transform[] contents,scrolls,btns;
Dictionary<int, string> filterOptionDict = new Dictionary<int, string>
{
{0,"routes" },{1,"competition" }
};
//交互用
public Transform currentItem { get; set; }
int scrollIndex = 0;
ScrollRect scroll;
void Awake()
{
//ApiBase.SetCookie("73385F5F719B610D132C1ECF3E9143272BF15214D57ED91CD7A9DFD832407471535112AAEB8E9271F75D54FBBF2D99F18FA313C1EEA5676F5D722D7FBB07C926BEC5905591BF9AFDDC6336552DF273112C2DA1794E6FA2F465B11FECD2E82E52");
localRouteItem = Resources.Load<Transform>("UI/Prefab/ResultList/LocalRouteItem");
#if UNITY_ANDROID || UNITY_IOS
routeResult = Resources.Load<Transform>("UI/Prefab/ResultList/RouteItem-Mobile");
localRouteItem = Resources.Load<Transform>("UI/Prefab/ResultList/LocalRouteItem-Mobile");
#endif
InitialColor();
scroll = transform.Find("ListPanel").Find("Scroll").GetComponent<ScrollRect>();
btnMatch = transform.Find("ListPanel").Find("BtnMatch");
btnRoute = transform.Find("ListPanel").Find("BtnRoute");
btnRower = transform.Find("ListPanel").Find("BtnRower");
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");
rowerScroll = scrollContent.Find("RowerList");
rowerContent = rowerScroll.Find("Viewport").Find("Content");
contents = new Transform[] { routeContent, matchContent, rowerContent};
if (btnRoute != null)
{
UIManager.AddEvent(btnRoute.gameObject, EventTriggerType.PointerClick, (b) =>
{
StartScroll(0);
});
//btnRoute.GetComponent<Button>().onClick.AddListener(() => StartScroll(1));
}
if (btnMatch != null)
{
UIManager.AddEvent(btnMatch.gameObject, EventTriggerType.PointerClick, (b) =>
{
StartScroll(1);
});
//btnMatch.GetComponent<Button>().onClick.AddListener(() => StartScroll(0));
}
if (btnRower != null)
{
UIManager.AddEvent(btnRower.gameObject, EventTriggerType.PointerClick, (b) =>
{
StartScroll(2);
});
}
btns = new Transform[] { btnRoute,btnMatch,btnRower };
if (searchInput != null)
{
UIManager.AddEvent(searchInput.Find("Button").gameObject, EventTriggerType.PointerClick, (b) =>
{
onEndEdit();
});
searchInput.GetComponent<InputField>().onValueChanged.AddListener((v) =>
{
name = v;
});
//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);
}
if (matchScroll != null)
{
UIManager.AddEvent(matchScroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
}
if (rowerScroll != null)
{
UIManager.AddEvent(rowerScroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
}
scrolls = new Transform[] { routeScroll, matchScroll, rowerScroll };
//GetList(0);
GetList(1);
GetList(2);
}
public void Load()
{
Debug.Log("刷新show");
//Refresh(contents[0]);
if (contents != null && contents.Length == 3)
{
Debug.Log("刷新show进入");
Refresh(contents[scrollIndex]);
}
//if (scroll!=null)
//{
// scroll.transform.Find("Error").gameObject.SetActive(false);
//}
}
private void OnEndDrag(BaseEventData e)
{
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);
pageIndex[scrollIndex]++;
GetList();
}
if (scrollrect.verticalNormalizedPosition >= 1)
{
Refresh(contents[scrollIndex]);
}
}
private void Refresh(Transform content)
{
content.transform.DestroyChildren();
isEnd[scrollIndex] = false;
pageIndex[scrollIndex] = 0;
GetList();
DisplayLocalRouteResult(contents[0], 0);//显示本地未上传成功的记录
}
public void Reset()
{
if (searchInput != null)
{
searchInput.GetComponent<InputField>().text = "";
name = "";
Load();
StartScroll(1);
}
}
Dictionary<int, bool> isEnd = new Dictionary<int, bool>
{
{0,false },{1,false },{2,false }
};
Dictionary<int, int> pageIndex = new Dictionary<int, int>
{
{0,0 },{1,0 },{2,0 }
};
async void GetList(int? sindex = null)
{
var index = sindex ?? scrollIndex;
if (isEnd[index]) return;
Debug.Log(sindex);
JsonResult<List<JObject>> r = null;
if (index == 0 || index == 1)
{
r = await ConfigHelper.mapInterruptRecordApi.GetMapInterruptRecord(name, pageIndex[index], 20, filterOptionDict[index]);
}
else
{
r = await ConfigHelper.rowerApi.GetList(pageIndex[index], 20);
}
if (r.result)
{
if (r.data.Count == 0)
{
if (pageIndex[index] != 0)
{
isEnd[index] = true;
scroll.transform.Find("Error").gameObject.SetActive(false);
}
else
{
//contents[index];
if (!sindex.HasValue || (sindex.HasValue && sindex.Value == scrollIndex))
{
scroll.transform.Find("Error").gameObject.SetActive(true);
}
}
}
else
{
isEnd[index] = false;
DisplayRouteResult(r.data, contents[index], index);
scroll.transform.Find("Error").gameObject.SetActive(false);
//DisplayRouteResult(r.data, matchContent);
}
}
else
{
Utils.showToast(gameObject, r.errMsg);
}
}
void DisplayRouteResult(List<JObject> list,Transform content,int index)
{
if (routeResult != null)
{
foreach (var item in list)
{
var obj = Instantiate(routeResult);
if (index == 0 || index == 1)
{
obj.GetComponent<RouteItem>().Initial(item.ToObject<RouteResult>(), index, transform, true);
}
else
{
obj.GetComponent<RouteItem>().Initial(item.ToObject<RowerResultModel>(), index, transform,true);
}
//obj.SendMessage("Initial", );
obj.transform.parent = 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))
{
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
void onEndEdit()
{
name = searchInput.GetComponent<InputField>().text;
Refresh(contents[scrollIndex]);
}
bool startMouse = false;
void Update()
{
//GoScroll();
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))
{
onEndEdit();
}
}
void StartScroll(int index)
{
#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++)
{
if (!btns[i]) continue;
if (i == index)
{
btns[i].GetComponent<Text>().color = c1;
}
else
{
btns[i].GetComponent<Text>().color = c2;
}
}
//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;
//}
}
}