mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-22 22:36:01 +00:00
Refactoring
This commit is contained in:
1 parent
af5ff5e4fe
commit
a311aa01b0
18 files changed
+50
-18
No files matched your search
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// SleepManager.swift
|
||||
// QuickDrop
|
||||
//
|
||||
// Created by Leon Böttger on 03.05.25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import IOKit
|
||||
import IOKit.pwr_mgt
|
||||
|
||||
class SleepManager {
|
||||
public static let shared = SleepManager()
|
||||
private var assertionID: IOPMAssertionID = 0
|
||||
private var timer: Timer?
|
||||
|
||||
private init() {
|
||||
// Start periodic check
|
||||
timer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true) { [weak self] _ in
|
||||
self?.updateSleepState()
|
||||
}
|
||||
|
||||
RunLoop.main.add(timer!, forMode: .common)
|
||||
}
|
||||
|
||||
private func disableSleep() {
|
||||
if assertionID != 0 {
|
||||
return
|
||||
}
|
||||
|
||||
log("[SleepManager] Enabling Wakelock")
|
||||
|
||||
IOPMAssertionCreateWithName(
|
||||
kIOPMAssertionTypePreventUserIdleDisplaySleep as CFString,
|
||||
IOPMAssertionLevel(kIOPMAssertionLevelOn),
|
||||
"QuickDrop Data Transfer" as CFString,
|
||||
&assertionID
|
||||
)
|
||||
}
|
||||
|
||||
private func enableSleep() {
|
||||
if assertionID == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
log("[SleepManager] Disabling Wakelock")
|
||||
IOPMAssertionRelease(assertionID)
|
||||
assertionID = 0
|
||||
}
|
||||
|
||||
private func updateSleepState() {
|
||||
let activeConnections = NearbyConnectionManager.shared.getActiveConnectionsCount()
|
||||
if activeConnections > 0 {
|
||||
disableSleep()
|
||||
} else {
|
||||
enableSleep()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user