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 caches; GameObject map; ScrollRect scroll; protected override void Awake() { caches = new Dictionary(); scroll = transform.Find("Container/Right/Scroll View").GetComponent(); UIManager.AddEvent(scroll.gameObject, UnityEngine.EventSystems.EventTriggerType.EndDrag, OnEndDrag); #if UNITY_ANDROID || UNITY_IOS map = Resources.Load("UI/Prefab/MapList/MapItem-Mobile"); #else map = Resources.Load("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().ShowBack(); var rect = transform.GetComponent(); 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().texture = null; scroll.content.DestroyChildren(); transform.Find("Container/Left/RideContainer/Text").GetComponent() .text = string.Empty; transform.Find("Container/Left/Infos/DistanceContainer/Text").GetComponent() .text = string.Empty; transform.Find("Container/Left/Infos/EleContainer/Text").GetComponent() .text = string.Empty; transform.Find("Container/Left/Infos/SlopeContainer/Text").GetComponent() .text = string.Empty; if (area.BannerImage != null) { Utils.DisplayImageTempDict(left.Find("Icon").GetComponent(), area.BannerImage, caches); } left.Find("Name").GetComponent().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 = res.data.RideCount.ToString(); transform.Find("Container/Left/Infos/DistanceContainer/Text").GetComponent() .text = $"{res.data.TotalDistance.ToString("#0")}KM"; transform.Find("Container/Left/Infos/EleContainer/Text").GetComponent() .text = $"{res.data.TotalClimb.ToString("#0")}M"; transform.Find("Container/Left/Infos/SlopeContainer/Text").GetComponent() .text = $"{res.data.AverageGrade.ToString("#0.00")}%"; LayoutRebuilder.ForceRebuildLayoutImmediate(transform.Find("Container/Left/RideContainer").GetComponent()); } } 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 list) { if (map != null) { foreach (var item in list) { var obj = Instantiate(map); obj.GetComponent().Initial(item, caches, area); //obj.SendMessage("Initial", ); obj.transform.SetParent(scroll.content.transform); obj.transform.localScale = new Vector3(1, 1, 1); } } } }