Skip to content

Commit

Permalink
CR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrentka committed Nov 21, 2024
1 parent 902785f commit ec21879
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions Zotero/Controllers/IdleTimerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import CocoaLumberjackSwift
import RxSwift

final class IdleTimerController {
private static let customIdleTimerTimemout = 1200
private static let customIdleTimerTimeout: DispatchTimeInterval = .seconds(1200)
private let disposeBag: DisposeBag
/// Processes which require idle timer disabled
private var activeProcesses: Int = 0
Expand All @@ -40,7 +40,7 @@ final class IdleTimerController {
guard let activeTimer = self?.activeTimer else { return }
DDLogInfo("IdleTimerController: reset idle timer")
activeTimer.suspend()
activeTimer.schedule(deadline: .now() + DispatchTimeInterval.seconds(Self.customIdleTimerTimemout))
activeTimer.schedule(deadline: .now() + Self.customIdleTimerTimeout)
activeTimer.resume()
}
}
Expand All @@ -58,7 +58,7 @@ final class IdleTimerController {

func startTimer(controller: IdleTimerController) {
let timer = DispatchSource.makeTimerSource(flags: [], queue: .main)
timer.schedule(deadline: .now() + DispatchTimeInterval.seconds(Self.customIdleTimerTimemout))
timer.schedule(deadline: .now() + Self.customIdleTimerTimeout)
timer.setEventHandler(handler: { [weak controller] in
controller?.forceStopIdleTimer()
})
Expand All @@ -69,15 +69,18 @@ final class IdleTimerController {

func stopCustomIdleTimer() {
inMainThread { [weak self] in
guard let self, activeProcesses > 0 else {
guard let self else { return }

if activeProcesses > 0 {
activeProcesses -= 1
DDLogInfo("IdleTimerController: enable idle timer \(activeProcesses)")
} else {
DDLogWarn("IdleTimerController: tried to enable idle timer with no active processes")
return
activeProcesses = 0
}
activeProcesses -= 1

DDLogInfo("IdleTimerController: enable idle timer \(activeProcesses)")

guard activeProcesses == 0 else { return }

set(disabled: false)
activeTimer?.suspend()
activeTimer?.setEventHandler(handler: nil)
Expand All @@ -97,7 +100,7 @@ final class IdleTimerController {

private func forceStopIdleTimer() {
DDLogInfo("IdleTimerController: force stop timer")
activeProcesses = 0
activeProcesses = 1
stopCustomIdleTimer()
}
}

0 comments on commit ec21879

Please sign in to comment.