killapp/Assets/Scripts/UI/Pages/HomePage/HomePageSchedule.cs

171 lines
5.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;
ScheduleTask? nearestTask = null;
int nearestTimeDiff = int.MaxValue;
int currentTime = DateTime.Now.Hour * 60 + DateTime.Now.Minute;
foreach (var task in tasks)
{
// 检查任务是否启用且今天有重复
if (task.Enabled && (task.Repeat & todayFlag) != 0)
{
int taskTime = task.StartHour * 60 + task.StartMinute;
int timeDiff = taskTime - currentTime;
// 只找未来的任务timeDiff > 0 表示任务时间在未来)
if (timeDiff > 0 && timeDiff < nearestTimeDiff)
{
nearestTimeDiff = timeDiff;
nearestTask = task;
}
}
}
return nearestTask;
}
/// <summary>
/// 显示任务信息
/// </summary>
private void DisplayTask(ScheduleTask task)
{
string modeText = GetModeText(task.Mode);
string timeText = $"{task.StartHour:D2}:{task.StartMinute:D2}";
if (scheduleText != null)
{
scheduleText.text = $"{modeText}: {timeText}";
}
}
/// <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
};
}
}
}