powerfun-unity/Assets/Scripts/UI/Prefab/Panel/NewRouteDetailController.cs

149 lines
4.8 KiB
C#

using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class NewRouteDetailController : PFUIPanel
{
// Start is called before the first frame update
Dictionary<string, Texture> caches;
GameObject map;
ScrollRect scroll;
protected override void Awake()
{
caches = new Dictionary<string, Texture>();
scroll = transform.Find("Container/Right/Scroll View").GetComponent<ScrollRect>();
UIManager.AddEvent(scroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag);
#if UNITY_ANDROID || UNITY_IOS
map = Resources.Load<GameObject>("UI/Prefab/MapList/MapItem-Mobile");
#else
map = Resources.Load<GameObject>("UI/Prefab/MapList/MapItem");
#endif
}
protected override void OnDestroy()
{
caches = null;
Resources.UnloadUnusedAssets();
GC.Collect();
Debug.Log("list empty");
}
void Start()
{
transform.Find("MainNav").GetComponent<MainNav>().ShowBack();
var rect = transform.GetComponent<RectTransform>();
rect.offsetMax = new Vector2(rect.offsetMax.x, 0);
rect.offsetMin = new Vector2(rect.offsetMin.x, 0);
}
// Update is called once per frame
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
if (scroll.verticalNormalizedPosition <= 0)
{
startMouse = true;
}
}
else
{
if (startMouse)
{
startMouse = false;
OnEndDrag(null);
}
}
}
MapRouteAreaItem area;
int pageIndex = 0, pageSize = 10;
bool isEnd = false, startMouse = false;
public async void Initial(MapRouteAreaItem area)
{
isEnd = false;
pageIndex = 0;
this.area = area;
var left = transform.Find("Container/Left");
left.Find("Icon").GetComponent<RawImage>().texture = null;
scroll.content.DestroyChildren();
transform.Find("Container/Left/RideContainer/Text").GetComponent<Text>()
.text = string.Empty;
transform.Find("Container/Left/Infos/DistanceContainer/Text").GetComponent<Text>()
.text = string.Empty;
transform.Find("Container/Left/Infos/EleContainer/Text").GetComponent<Text>()
.text = string.Empty;
transform.Find("Container/Left/Infos/SlopeContainer/Text").GetComponent<Text>()
.text = string.Empty;
if (area.BannerImage != null)
{
Utils.DisplayImageTempDict(left.Find("Icon").GetComponent<RawImage>(), area.BannerImage, caches);
}
left.Find("Name").GetComponent<Text>().text = area.Name;
GetData();
GetList();
}
private void OnEndDrag(BaseEventData arg0)
{
Debug.Log(scroll.verticalNormalizedPosition);
if (scroll.verticalNormalizedPosition <= 0 && !isEnd)
{
pageIndex++;
GetList();
}
}
private async void GetData()
{
var res = await ConfigHelper.mapApi.GetMapRouteAreaDetail(area.Id);
if (res.result)
{
transform.Find("Container/Left/RideContainer/Text").GetComponent<Text>()
.text = res.data.RideCount.ToString();
transform.Find("Container/Left/Infos/DistanceContainer/Text").GetComponent<Text>()
.text = $"{res.data.TotalDistance.ToString("#0")}KM";
transform.Find("Container/Left/Infos/EleContainer/Text").GetComponent<Text>()
.text = $"{res.data.TotalClimb.ToString("#0")}M";
transform.Find("Container/Left/Infos/SlopeContainer/Text").GetComponent<Text>()
.text = $"{res.data.AverageGrade.ToString("#0.00")}%";
LayoutRebuilder.ForceRebuildLayoutImmediate(transform.Find("Container/Left/RideContainer").GetComponent<RectTransform>());
}
}
private async void GetList()
{
var res = await ConfigHelper.mapApi.GetMapRouteAreaDetailList(area.Id, pageIndex, pageSize);
if (res.result)
{
if (res.data.Count > 0)
{
DisplayMaps(res.data);
}
else
{
isEnd = true;
}
}
}
void DisplayMaps(List<MapRoute> list)
{
if (map != null)
{
foreach (var item in list)
{
var obj = Instantiate(map);
obj.GetComponent<MapItem>().Initial(item, caches, area);
//obj.SendMessage("Initial", );
obj.transform.SetParent(scroll.content.transform);
obj.transform.localScale = new Vector3(1, 1, 1);
}
}
}
}