using System; using System.Collections.Generic; using Kill.Bluetooth.Protocol; using Kill.UI.Components; using UnityEngine; using UnityEngine.UI; namespace Kill.UI.Pages { public class OneSchedule : MonoBehaviour { /// /// 状态颜色 0: 关闭(灰色) 1: 开启(白色/正常) /// public Color[] statusColors; /// /// 时间文本 开始时间 - 结束时间(00:00 - 01:00) /// public Text timeText; public Image modeImg; public Sprite[] modeSprites; /// /// 模式和重复时间 模式|重复时间(扫描|周日,周一) /// public Text modeAndRepeatText; public Switch isOnSwitch; // 当前任务索引 private int _taskIndex = -1; // 开关状态变化回调 private Action _onSwitchChanged; // 点击回调 private Action _onClick; // 删除回调 private Action _onDelete; /// /// 初始化任务项 /// /// 任务索引 /// 任务数据 /// 模式文本 /// 重复文本 /// 开关变化回调 /// 点击回调 /// 删除回调 public void Init(int taskIndex, ScheduleTask task, string modeText, string repeatText, Action onSwitchChanged = null, Action onClick = null, Action onDelete = null) { _taskIndex = taskIndex; _onSwitchChanged = onSwitchChanged; _onClick = onClick; _onDelete = onDelete; // 更新时间文本 UpdateTimeText(task.StartHour, task.StartMinute, task.EndHour, task.EndMinute); // 更新模式图标 UpdateModeIcon(task.Mode); // 更新模式和重复文本 UpdateModeAndRepeatText(modeText, repeatText); // 更新开关状态 UpdateSwitchState(task.Enabled); // 根据开关状态更新颜色 UpdateColors(task.Enabled); } /// /// 更新时间文本 /// public void UpdateTimeText(byte startHour, byte startMinute, byte endHour, byte endMinute) { if (timeText != null) { timeText.text = $"{startHour:D2}:{startMinute:D2} - {endHour:D2}:{endMinute:D2}"; } } /// /// 更新模式图标 /// public void UpdateModeIcon(ScheduleTaskMode mode) { if (modeImg != null && modeSprites != null && modeSprites.Length > 0) { int modeIndex = (int)mode; if (modeIndex >= 0 && modeIndex < modeSprites.Length) { modeImg.sprite = modeSprites[modeIndex]; } } } /// /// 更新模式和重复文本 /// public void UpdateModeAndRepeatText(string modeText, string repeatText) { if (modeAndRepeatText != null) { // 如果有重复时间,显示模式和重复时间;否则只显示模式 if (!string.IsNullOrEmpty(repeatText)) { modeAndRepeatText.text = $"{modeText} | {repeatText}"; } else { modeAndRepeatText.text = modeText; } } } /// /// 更新开关状态 /// public void UpdateSwitchState(bool isOn) { if (isOnSwitch != null) { isOnSwitch.Init(isOn, OnSwitchClicked); } } /// /// 根据开关状态更新颜色 /// public void UpdateColors(bool isOn) { if (statusColors == null || statusColors.Length < 2) { return; } Color targetColor = isOn ? statusColors[1] : statusColors[0]; // 更新时间文本颜色 if (timeText != null) { timeText.color = targetColor; } // 更新模式图标颜色 if (modeImg != null) { modeImg.color = targetColor; } // 更新模式和重复文本颜色 if (modeAndRepeatText != null) { modeAndRepeatText.color = targetColor; } } /// /// 开关点击回调 /// private void OnSwitchClicked(bool isOn) { // 更新颜色 UpdateColors(isOn); // 触发回调 if (_taskIndex >= 0) { _onSwitchChanged?.Invoke(_taskIndex, isOn); } } /// /// 获取当前任务索引 /// public int GetTaskIndex() { return _taskIndex; } /// /// 设置任务索引 /// public void SetTaskIndex(int index) { _taskIndex = index; } } }