Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions Sources/Juice/BatteryViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ final class BatteryViewModel: ObservableObject {
lowPowerModeProvider: @escaping () -> Bool = {
ProcessInfo.processInfo.isLowPowerModeEnabled
},
notificationCenter: NotificationCenter = .default
notificationCenter: NotificationCenter = .default,
batteryPollingPublisher: @escaping () -> AnyPublisher<Date, Never> = {
Timer.publish(every: 60, on: .main, in: .common)
.autoconnect()
.eraseToAnyPublisher()
}
) {
self.onReading = onReading
self.isMacMini = isMacMini
self.batteryReader = batteryReader
self.lowPowerModeProvider = lowPowerModeProvider
isLowPowerModeEnabled = lowPowerModeProvider()
refresh()
// Mac mini current-power UI is driven by LivePowerCoordinator. There is
// no battery state or Low Power Mode status icon to keep synchronized,
// so avoid creating background resources that can only trigger no-ops.
guard !isMacMini else { return }

// Background cadence; the popover triggers an immediate refresh on open.
timer = Timer.publish(every: 60, on: .main, in: .common)
.autoconnect()
timer = batteryPollingPublisher()
.sink { [weak self] _ in self?.refresh() }
// Power mode can change independently of a battery reading. Observe the
// system notification so the menu bar icon updates immediately.
Expand Down
Loading