killapp/Assets/Scripts/UI/Pages/HomePage/HomePageSchedule.cs
“虞渠成” 85ee694121 feat(selfpage&schedule): 添加反馈空内容校验与任务跨天逻辑优化
1. 新增多语言文案用于反馈内容为空提示
2. 调整个人页面头像控件尺寸并隐藏默认显示
3. 增加反馈提交前的内容非空校验
4. 优化BLE设备mac字段提交逻辑
5. 重构日程任务判断逻辑,支持跨天任务识别
2026-06-29 16:47:17 +08:00

215 lines
7.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using Kill.Bluetooth.Protocol;
using Kill.Managers;
using UnityEngine;
using UnityEngine.UI;
namespace Kill.UI.Pages
{
public class HomePageSchedule : MonoBehaviour
{
ScheduleTask[] tasks;
public Image scheduleIcon;
/// <summary>
/// 定时任务图标 0未设置 1已设置
/// </summary>
public Sprite[] scheduleIconSprites;
public Text scheduleText;
// 多语言Key
private const string LANG_STANDBY = "100094"; // Standby 待机
private const string LANG_SCAN = "100095"; // Scan 扫描
private const string LANG_ARMED = "100096"; // Armed 消杀
private const string LANG_NO_SCHEDULE = "100101"; // No Schedule 无定时任务
public void Init(ScheduleTask[] tasks)
{
this.tasks = tasks;
UpdateScheduleDisplay();
}
/// <summary>
/// 更新定时任务显示
/// </summary>
private void UpdateScheduleDisplay()
{
// 获取今天的星期几0=周日, 1=周一, ... 6=周六)
DayOfWeek today = DateTime.Now.DayOfWeek;
byte todayFlag = GetDayFlag(today);
// 查找今天启用的任务
ScheduleTask? todayTask = FindTodayTask(todayFlag);
if (todayTask.HasValue)
{
// 有今日任务,显示最近的一个
DisplayTask(todayTask.Value);
SetIconScheduled(true);
}
else
{
// 没有今日任务
DisplayNoSchedule();
SetIconScheduled(false);
}
}
/// <summary>
/// 查找今天启用的任务(返回最近的一个未结束任务)
/// </summary>
private ScheduleTask? FindTodayTask(byte todayFlag)
{
if (tasks == null || tasks.Length == 0)
return null;
// 昨天的标志位(用于跨天任务判断)
byte yesterdayFlag = (byte)((todayFlag == ScheduleTask.REPEAT_MONDAY)
? ScheduleTask.REPEAT_SUNDAY
: (todayFlag >> 1));
ScheduleTask? nearestTask = null;
int nearestTimeDiff = int.MaxValue;
int currentTime = DateTime.Now.Hour * 60 + DateTime.Now.Minute;
foreach (var task in tasks)
{
// 检查任务是否启用
if (!task.Enabled)
continue;
int taskTime = task.StartHour * 60 + task.StartMinute;
int endTime = task.EndHour * 60 + task.EndMinute;
bool isCrossDay = endTime <= taskTime;
// 检查是否是今天的任务:
// 1. Repeat == 0 表示仅一次,认为是当天的任务
// 2. Repeat & todayFlag != 0 表示今天有重复(任务从今天开始)
// 3. 跨天任务:昨天开始、今天仍未结束,也视为今天任务
bool isTodayTask = task.Repeat == 0
|| (task.Repeat & todayFlag) != 0
|| (isCrossDay && currentTime < endTime && (task.Repeat & yesterdayFlag) != 0);
if (!isTodayTask)
continue;
// 跳过已结束的任务(非跨天任务 && 当前时间已过结束时间)
if (!isCrossDay && currentTime >= endTime)
continue;
int timeDiff = taskTime - currentTime;
// 找距离当前时间最近的任务(包括进行中的和未来未开始的)
int absTimeDiff = Math.Abs(timeDiff);
if (absTimeDiff < nearestTimeDiff)
{
nearestTimeDiff = absTimeDiff;
nearestTask = task;
}
}
return nearestTask;
}
/// <summary>
/// 显示任务信息
/// </summary>
private void DisplayTask(ScheduleTask task)
{
string modeText = GetModeText(task.Mode);
string timeText = $"{task.StartHour:D2}:{task.StartMinute:D2}";
// 判断任务是未来还是进行中
int currentTime = DateTime.Now.Hour * 60 + DateTime.Now.Minute;
int taskTime = task.StartHour * 60 + task.StartMinute;
int endTime = task.EndHour * 60 + task.EndMinute;
bool isCrossDay = endTime <= taskTime;
// 跨天任务:如果 startTime > currentTime 但 currentTime < endTime
// 说明任务昨天已开始、今天仍在运行,应视为进行中而非未开始
bool isFutureTask = taskTime > currentTime
&& !(isCrossDay && currentTime < endTime);
if (scheduleText != null)
{
if (isFutureTask)
{
// 即将开始的任务
scheduleText.text = $"{modeText}: {timeText}";
}
else
{
// 任务进行中,显示时间段
scheduleText.text = $"{modeText}: {timeText}~{task.EndHour:D2}:{task.EndMinute:D2}";
}
}
}
/// <summary>
/// 显示无定时任务
/// </summary>
public void DisplayNoSchedule()
{
if (scheduleText != null)
{
string noScheduleText = LanguageManager.Instance?.GetLanguage(LANG_NO_SCHEDULE) ?? "No Schedule";
scheduleText.text = noScheduleText;
}
}
/// <summary>
/// 设置图标状态
/// </summary>
private void SetIconScheduled(bool hasSchedule)
{
if (scheduleIcon != null && scheduleIconSprites != null && scheduleIconSprites.Length >= 2)
{
scheduleIcon.sprite = hasSchedule ? scheduleIconSprites[1] : scheduleIconSprites[0];
}
}
/// <summary>
/// 获取任务模式显示文本(支持多语言)
/// </summary>
private string GetModeText(ScheduleTaskMode mode)
{
string langKey = mode switch
{
ScheduleTaskMode.Standby => LANG_STANDBY,
ScheduleTaskMode.Scan => LANG_SCAN,
ScheduleTaskMode.Sterilize => LANG_ARMED,
_ => LANG_STANDBY
};
string defaultText = mode switch
{
ScheduleTaskMode.Standby => "Standby",
ScheduleTaskMode.Scan => "Scan",
ScheduleTaskMode.Sterilize => "Armed",
_ => "Standby"
};
return LanguageManager.Instance?.GetLanguage(langKey) ?? defaultText;
}
/// <summary>
/// 获取星期对应的标志位
/// </summary>
private byte GetDayFlag(DayOfWeek dayOfWeek)
{
return dayOfWeek switch
{
DayOfWeek.Monday => ScheduleTask.REPEAT_MONDAY,
DayOfWeek.Tuesday => ScheduleTask.REPEAT_TUESDAY,
DayOfWeek.Wednesday => ScheduleTask.REPEAT_WEDNESDAY,
DayOfWeek.Thursday => ScheduleTask.REPEAT_THURSDAY,
DayOfWeek.Friday => ScheduleTask.REPEAT_FRIDAY,
DayOfWeek.Saturday => ScheduleTask.REPEAT_SATURDAY,
DayOfWeek.Sunday => ScheduleTask.REPEAT_SUNDAY,
_ => 0
};
}
}
}