BugFix: Multiple object creation

This commit is contained in:
Hariharan Mudaliar 2025-09-06 19:43:40 +05:30
commit f4bcb96906
8 changed files with 202 additions and 33 deletions

View file

@ -7,31 +7,38 @@
import Foundation
import Defaults
import Combine
class SystemHUDManager: ObservableObject {
class SystemHUDManager {
static let shared = SystemHUDManager()
private var changesObserver: SystemChangesObserver?
private weak var coordinator: DynamicIslandViewCoordinator?
@Default(.enableSystemHUD) var enableSystemHUD {
didSet {
Task { @MainActor in
if enableSystemHUD {
await startSystemHUD()
} else {
await stopSystemHUD()
}
}
}
private init() {
// Set up observer for settings changes
setupSettingsObserver()
}
private init() {}
private func setupSettingsObserver() {
// Use Defaults publisher instead of @Default property wrapper
Defaults.publisher(.enableSystemHUD, options: []).sink { [weak self] change in
Task { @MainActor in
if change.newValue {
await self?.startSystemHUD()
} else {
await self?.stopSystemHUD()
}
}
}.store(in: &cancellables)
}
private var cancellables = Set<AnyCancellable>()
func setup(coordinator: DynamicIslandViewCoordinator) {
self.coordinator = coordinator
if enableSystemHUD {
if Defaults[.enableSystemHUD] {
Task { @MainActor in
await startSystemHUD()
}
@ -56,6 +63,7 @@ class SystemHUDManager: ObservableObject {
@MainActor
private func stopSystemHUD() async {
changesObserver?.stopObserving()
changesObserver = nil
// Re-enable system HUD
@ -65,18 +73,36 @@ class SystemHUDManager: ObservableObject {
}
private func disableSystemHUD() {
print("🔇 Disabling system HUD (OSDUIHelper running: \(SystemOSDManager.isOSDUIHelperRunning()))")
// Disable the system HUD using SystemOSDManager
SystemOSDManager.disableSystemHUD()
}
private func enableOriginalSystemHUD() {
print("🔊 Re-enabling system HUD (OSDUIHelper running: \(SystemOSDManager.isOSDUIHelperRunning()))")
// Re-enable the system HUD using SystemOSDManager
SystemOSDManager.enableSystemHUD()
// Verify it's working after a short delay
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
let isRunning = SystemOSDManager.isOSDUIHelperRunning()
print("🔍 OSDUIHelper status after re-enable: \(isRunning ? "✅ Running" : "❌ Not running")")
}
}
deinit {
print("🧹 SystemHUDManager deinitializing - ensuring system HUD is restored")
cancellables.removeAll()
Task { @MainActor in
await stopSystemHUD()
}
// Also call the emergency restore as backup
SystemHUDDebugger.forceRestartOSDUIHelper()
}
/// Emergency restore function - call this if system HUD is not working
public static func emergencyRestoreSystemHUD() {
print("🚨 Emergency system HUD restore initiated")
SystemHUDDebugger.forceRestartOSDUIHelper()
}
}