refactor: 简化空闲状态检测逻辑,直接显示保护状态
移除空闲状态检测及相关UI显示,直接显示"保护中"状态
This commit is contained in:
@@ -11,20 +11,12 @@ class AppState: ObservableObject {
|
||||
// 功能的总开关
|
||||
@Published var isFunctionEnabled: Bool = false
|
||||
|
||||
// 用户当前是否空闲
|
||||
@Published var isUserIdle: Bool = false
|
||||
|
||||
// 自动启动设置
|
||||
@Published var isLaunchAtLogin: Bool = false
|
||||
|
||||
// 内部属性
|
||||
private var monitorTimer: Timer?
|
||||
private var caffeinateProcess: Process? // 直接使用Process对象,但简化实现
|
||||
|
||||
// 配置参数
|
||||
private let idleThreshold: TimeInterval = 300 // 5分钟无操作视为空闲
|
||||
private let checkInterval: TimeInterval = 10 // 每10秒检查一次
|
||||
|
||||
// 初始化方法
|
||||
init() {
|
||||
// 检查并设置初始的自动启动状态
|
||||
@@ -37,52 +29,11 @@ class AppState: ObservableObject {
|
||||
func toggleFunction() {
|
||||
isFunctionEnabled.toggle()
|
||||
if isFunctionEnabled {
|
||||
// 开启功能,立即开始一轮检查,并设置定时器
|
||||
performCheck()
|
||||
startMonitoringTimer()
|
||||
} else {
|
||||
// 关闭功能,停止一切
|
||||
stopMonitoringTimer()
|
||||
stopCaffeinate()
|
||||
updateUserIdleStatus(false) // 重置状态
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Monitoring Logic
|
||||
|
||||
private func startMonitoringTimer() {
|
||||
guard monitorTimer == nil else { return }
|
||||
print("开始监控...")
|
||||
// 使用 weak self 避免循环引用
|
||||
monitorTimer = Timer.scheduledTimer(withTimeInterval: checkInterval, repeats: true) { [weak self] _ in
|
||||
self?.performCheck()
|
||||
}
|
||||
}
|
||||
|
||||
private func stopMonitoringTimer() {
|
||||
monitorTimer?.invalidate()
|
||||
monitorTimer = nil
|
||||
print("监控已停止。")
|
||||
}
|
||||
|
||||
private func performCheck() {
|
||||
// 1. 获取系统空闲时间
|
||||
let idleTime = getSystemIdleTime()
|
||||
|
||||
// 2. 判断是否空闲
|
||||
let isIdleNow = (idleTime >= idleThreshold)
|
||||
|
||||
// 3. 如果用户状态发生了变化,则采取行动
|
||||
if isIdleNow != isUserIdle {
|
||||
updateUserIdleStatus(isIdleNow)
|
||||
|
||||
if isIdleNow {
|
||||
// 从“活动中”变为“空闲”
|
||||
stopCaffeinate()
|
||||
} else {
|
||||
// 从“空闲”变为“活动中”
|
||||
// 开启功能,直接启动caffeinate,不检查用户活动
|
||||
startCaffeinate()
|
||||
}
|
||||
} else {
|
||||
// 关闭功能,停止caffeinate
|
||||
stopCaffeinate()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,14 +106,7 @@ class AppState: ObservableObject {
|
||||
print("已重置 caffeinate 进程引用")
|
||||
}
|
||||
|
||||
// MARK: - UI State Update
|
||||
|
||||
/// 在主线程更新用户空闲状态
|
||||
private func updateUserIdleStatus(_ isIdle: Bool) {
|
||||
DispatchQueue.main.async {
|
||||
self.isUserIdle = isIdle
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Launch at Login Management
|
||||
|
||||
@@ -252,7 +196,6 @@ class AppState: ObservableObject {
|
||||
|
||||
deinit {
|
||||
// 确保 App 退出时清理资源
|
||||
stopMonitoringTimer()
|
||||
stopCaffeinate()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,18 +93,10 @@ struct MenuContent: View {
|
||||
if appState.isFunctionEnabled {
|
||||
HStack(spacing: 6) {
|
||||
Circle()
|
||||
.fill(appState.isUserIdle ? Color.orange : Color.green)
|
||||
.fill(Color.green)
|
||||
.frame(width: 8, height: 8)
|
||||
.transition(.scale(scale: 0.5, anchor: .center))
|
||||
Text(appState.isUserIdle ? "空闲" : "活动中")
|
||||
Text("保护中")
|
||||
.font(.body)
|
||||
|
||||
// 附加显示 caffeinate 状态
|
||||
if !appState.isUserIdle {
|
||||
Text("(保护中)")
|
||||
.font(.caption2)
|
||||
.foregroundColor(.secondary)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
HStack(spacing: 6) {
|
||||
|
||||
Reference in New Issue
Block a user