313 lines
10 KiB
C#
Raw Permalink Normal View History

2026-06-08 08:55:10 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Kill.UI.Components;
using Kill.UI;
using System.Threading.Tasks;
using System;
using Kill.Network;
using Kill.Managers;
using UnityEngine.Video;
using System.IO;
namespace Kill.UI.Pages
{
public class VideoPageCtrl : MonoBehaviour
{
public class Videodata
{
public string videoUrl;
public string coverUrl;
public DateTime time;
}
public class KillData
{
public DateTime time;
public float angle;
public float dis;
}
public Button backBtn;
public VerticalScrollLoader scrollLoader;
public VideoUIItem videoUIItemPrefab;
public Transform content;
int totalCount=0;
List<GameObject> videoList = new List<GameObject>();
private int currentPage = 1;
private bool isLoading = false;
private bool hasMoreData = true;
public GameObject noTip;
void Start()
{
backBtn.onClick.RemoveAllListeners();
backBtn.onClick.AddListener(Back);
UIManager.Instance.RegisterBackAction(Back);
ChangeType(0);
}
public Button[] changeTypeButtons;
public void ChangeType(int type)
{
if(videoList!=null)
{
foreach(var g in videoList)
{
Destroy(g);
}
videoList.Clear();
}
videoList=new List<GameObject>();
changeTypeButtons[type].interactable = false;
changeTypeButtons[type].transform.Find("选中").gameObject.SetActive(true);
changeTypeButtons[1-type].interactable = true;
changeTypeButtons[1-type].transform.Find("选中").gameObject.SetActive(false);
if (type == 0)
{
// 设置滚动加载阈值
if (scrollLoader != null)
{
scrollLoader.onLoadTriggered.AddListener(OnScrollToLoadKill);
scrollLoader.ResetLoader();
}
currentPage = 1;
hasMoreData = true;
totalCount=0;
GetKillDataList();
}
else
{
// 设置滚动加载阈值
if (scrollLoader != null)
{
scrollLoader.onLoadTriggered.AddListener(OnScrollToLoadVideo);
scrollLoader.ResetLoader();
}
currentPage = 1;
hasMoreData = true;
totalCount=0;
GetVideoList();
}
}
public void Back()
{
UIManager.Instance.OpenMainPage(UIManager.PageName.homePage);
}
public async Task GetVideoList()
{
isLoading = true;
LoadingUI.Show();
string url = $"/api/v1/stats/device/videos?device_sn={DataManager.Instance.selectedDevice.ble_mac}&page={currentPage}&page_size=30";
var response = await NetworkCtrl.Instance.Get<DeviceVideoDataListResponse>(url);
LoadingUI.Hide();
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
if(data.data.total>0)
{
noTip.SetActive(false);
}
else
{
noTip.SetActive(true);
}
isLoading = false;
if (data.data.list != null && data.data.list.Count > 0)
{
foreach (var item in data.data.list)
{
Videodata vd = new Videodata()
{
time = item.record_time,
videoUrl = item.video_url,
coverUrl=item.cover_url
};
CreateVideoItem(vd);
totalCount++;
}
if(totalCount>=data.data.total)
{
hasMoreData = false;
scrollLoader?.SetAllDataLoaded();
}
else
{
hasMoreData = true;
scrollLoader?.OnLoadComplete();
}
}
},
onError: null
);
}
public async Task GetKillDataList()
{
isLoading = true;
LoadingUI.Show();
string url = $"/api/v1/stats/device/records?device_sn={DataManager.Instance.selectedDevice.ble_mac}&type=1&page={currentPage}&page_size=30";
var response = await NetworkCtrl.Instance.Get<RecordDataListResponse>(url);
LoadingUI.Hide();
ResponseCodeHandler.HandleResponse(response,
onSuccess: (data) =>
{
if(data.data.total>0)
{
noTip.SetActive(false);
}
else
{
noTip.SetActive(true);
}
isLoading = false;
if (data.data.records != null && data.data.records.Count > 0)
{
foreach (var item in data.data.records)
{
KillData kd = new KillData()
{
time = item.time,
angle = item.angle,
dis=item.distance
};
CreateKillItem(kd);
totalCount++;
}
Debug.Log(totalCount);
if(totalCount>=data.data.total)
{
hasMoreData = false;
scrollLoader?.SetAllDataLoaded();
}
else
{
hasMoreData = true;
scrollLoader?.OnLoadComplete();
}
}
},
onError: null
);
}
/// <summary>
/// 滚动到指定位置加载新消息
/// </summary>
private void OnScrollToLoadKill()
{
Debug.Log("加载新记录");
if (isLoading || !hasMoreData) return;
currentPage++;
GetKillDataList();
}
private void OnScrollToLoadVideo()
{
if (isLoading || !hasMoreData) return;
currentPage++;
GetVideoList();
}
/// <summary>
/// 创建视频UI
/// </summary>
private void CreateVideoItem(Videodata data)
{
if (videoUIItemPrefab == null || content == null) return;
var item = Instantiate(videoUIItemPrefab, content);
item.GetComponent<VideoUIItem>().Init(data.time, data.coverUrl);
item.GetComponent<Button>().onClick.AddListener(() => ClickOneVideo(data.videoUrl));
videoList.Add(item.gameObject);
}
/// <summary>
/// 创建灭蚊记录UI
/// </summary>
/// <param name="data"></param>
private void CreateKillItem(KillData data)
{
if (videoUIItemPrefab == null || content == null) return;
var item = Instantiate(videoUIItemPrefab, content);
item.GetComponent<VideoUIItem>().Init(data.time, "",data.angle,data.dis);
videoList.Add(item.gameObject);
}
public VideoCtrl videoCtrl;
public void ClickOneVideo(string url)
{
videoCtrl.transform.parent.gameObject.SetActive(true);
2026-06-12 09:42:44 +08:00
videoCtrl.Init(url,null);
2026-06-08 08:55:10 +08:00
downloadBtn.onClick.RemoveAllListeners();
downloadBtn.onClick.AddListener(() => ClickDownload(url));
}
public void CloseVideo()
{
videoCtrl.transform.parent.gameObject.SetActive(false);
videoCtrl.Stop();
}
public async Task DownLoadVideo(string url)
{
2026-06-12 16:07:27 +08:00
UpdateLoadingUI.Instance.Show();
2026-06-08 08:55:10 +08:00
string name = url.Substring(url.LastIndexOf('/') + 1);
string savePath = Application.persistentDataPath + "/"+name;
2026-06-12 16:07:27 +08:00
var success = await NetworkCtrl.Instance.DownloadFile(url, savePath, UpdateLoadingUI.Instance.GetDownloadProcess);
UpdateLoadingUI.Instance.Hide();
2026-06-08 08:55:10 +08:00
if (success.IsSuccess && File.Exists(savePath))
{
// 下载成功,保存到系统相册
SaveVideoToGallery(savePath, name);
}
else
{
ToastUI.Show("100258");
}
}
/// <summary>
/// 保存视频到系统相册
/// </summary>
private void SaveVideoToGallery(string filePath, string fileName)
{
#if UNITY_ANDROID || UNITY_IOS
NativeGallery.SaveVideoToGallery(filePath, "photonmatrix", fileName, (success, path) =>
{
if (success)
{
ToastUI.Show("100257");
Debug.Log($"[VideoPageCtrl] 视频已保存到相册: {path}");
}
else
{
ToastUI.Show("100258");
Debug.LogError($"[VideoPageCtrl] 保存视频到相册失败: {filePath}");
}
});
#endif
}
public Button downloadBtn;
public void ClickDownload(string url)
{
DownLoadVideo(url);
}
public void GetDownloadProcess( DownloadProgress progress)
{
Debug.Log(progress.ProgressPercentage + "%");
}
}
}