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

352 lines
12 KiB
C#

using Assets.Scripts;
using Assets.Scripts.Apis;
using Assets.Scripts.Apis.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class ResultListController : MonoBehaviour
{
[SerializeField]Transform routeResult;
Transform localRouteItem;
Transform scrollContent;
Transform routeScroll, matchScroll;
Transform routeContent, matchContent;
Transform btnRoute, btnMatch,searchInput;
Color c1, c2;
// Start is called before the first frame update
int pageSize = 20;
string name = "";
Transform[] contents,scrolls;
Dictionary<int, string> filterOptionDict = new Dictionary<int, string>
{
{0,"routes" },{1,"competition" }
};
void Awake()
{
//ApiBase.SetCookie("73385F5F719B610D132C1ECF3E9143272BF15214D57ED91CD7A9DFD832407471535112AAEB8E9271F75D54FBBF2D99F18FA313C1EEA5676F5D722D7FBB07C926BEC5905591BF9AFDDC6336552DF273112C2DA1794E6FA2F465B11FECD2E82E52");
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");
contents = new Transform[] { routeContent, matchContent};
localRouteItem = Resources.Load<Transform>("UI/Prefab/ResultList/LocalRouteItem");
if (btnMatch != null)
{
UIManager.AddEvent(btnMatch.gameObject, EventTriggerType.PointerClick, (b) =>
{
StartScroll(0);
});
//btnMatch.GetComponent<Button>().onClick.AddListener(() => StartScroll(0));
}
if (btnRoute != null)
{
UIManager.AddEvent(btnRoute.gameObject, EventTriggerType.PointerClick, (b) =>
{
StartScroll(1);
});
//btnRoute.GetComponent<Button>().onClick.AddListener(() => StartScroll(1));
}
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);
}
scrolls = new Transform[] { routeScroll, matchScroll };
//GetList(0);
GetList(1);
}
public void Load()
{
if (contents != null && contents.Length == 2)
{
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 }
};
Dictionary<int, int> pageIndex = new Dictionary<int, int>
{
{0,0 },{1,0 }
};
async void GetList(int? sindex = null)
{
var index = sindex ?? scrollIndex;
if (isEnd[index]) return;
Debug.Log(sindex);
var r = await ConfigHelper.mapInterruptRecordApi.GetMapInterruptRecord(name, pageIndex[index], 20,filterOptionDict[index]);
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<RouteResult> list,Transform content,int index)
{
if (routeResult != null)
{
foreach (var item in list)
{
var obj = Instantiate(routeResult);
obj.GetComponent<RouteItem>().Initial(item, index);
//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 (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;
}
scroll.transform.Find("Error").gameObject.SetActive(contents[scrollIndex].childCount == 0);
}
#region
int scrollIndex = 0;
bool startScroll = false;
ScrollRect scroll;
//private float scrollValue = 0.5f;
int pageNums = 2;
void GoScroll()
{
var index = scrollIndex;
var scrollValue = 1f / (pageNums - 1);
var value = index * scrollValue;
if (scroll != null && startScroll)
{
if (scroll.horizontalNormalizedPosition >= value)
{
scroll.horizontalNormalizedPosition -= (scrollValue / 20);
if (scroll.horizontalNormalizedPosition <= value)
{
Debug.Log(index);
scroll.horizontalNormalizedPosition = value;
startScroll = false;
}
}
else
{
scroll.horizontalNormalizedPosition += (scrollValue / 20);
if (scroll.horizontalNormalizedPosition >= value)
{
Debug.Log(index);
scroll.horizontalNormalizedPosition = value;
startScroll = false;
}
}
}
}
#endregion
//float scrollIndex = 0;
//bool startScroll = false;
//ScrollRect scroll;
//void GoScroll()
//{
// float value = scrollIndex;
// if (scroll != null && startScroll)
// {
// if (scrollIndex == 0)
// {
// scroll.horizontalNormalizedPosition -= (1f / 20);
// if (scroll.horizontalNormalizedPosition <= 0)
// {
// scroll.horizontalNormalizedPosition = 0;
// startScroll = false;
// }
// }
// else if (scrollIndex == 1)
// {
// scroll.horizontalNormalizedPosition += (1f / 20);
// if (scroll.horizontalNormalizedPosition >= 1)
// {
// scroll.horizontalNormalizedPosition = 1;
// startScroll = false;
// }
// }
// }
//}
}