Skip to content

Commit

Permalink
Added remaining time restoring
Browse files Browse the repository at this point in the history
Rstoring of the remaining minutes after detecting the user inactivity
  • Loading branch information
masich committed Mar 12, 2020
1 parent fe30730 commit caaa2c1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 3 additions & 2 deletions SaveMyEyes/Source/Common/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct Constants {

public static let minute: TimeInterval = 60 // seconds

// If user is inactive for more than `allowedUserInactivityInterval` seconds -> internal timer will be paused
public static let allowedUserInactivityInterval: TimeInterval = 5 * minute
// If user is inactive for more than `allowedUserInactivityMinutes` minutes -> internal timer will be paused
public static let allowedUserInactivityMinutes: Int = 5
public static let allowedUserInactivityInterval: TimeInterval = TimeInterval(allowedUserInactivityMinutes) * minute
}
4 changes: 4 additions & 0 deletions SaveMyEyes/Source/Common/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ class System {
public static func isUserInactive(forTimeInterval: TimeInterval) -> Bool {
return (getUserInactiveSeconds() ?? 0) >= forTimeInterval
}

public static func isUserInactive(forMinutes: Int) -> Bool {
return isUserInactive(forTimeInterval: TimeInterval(forMinutes) * Constants.minute)
}
}
9 changes: 8 additions & 1 deletion SaveMyEyes/Source/Main/MainViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class MainViewModel: ObservableObject {

private var timerWorker: TimerWorker!
private var cancellables = [AnyCancellable]()
private var isUserInactive = false

private let allowedUserInactivityInterval: TimeInterval
private let timerInterval: TimeInterval
Expand Down Expand Up @@ -113,7 +114,13 @@ class MainViewModel: ObservableObject {
Performs time management only if user was active for at least last `allowedUserInactivityInterval` seconds
*/
public func timerHandler(timer: Timer) {
if isBreakTimeNow || !System.isUserInactive(forTimeInterval: allowedUserInactivityInterval) {
let isUserIncativeNew = System.isUserInactive(forMinutes: Constants.allowedUserInactivityMinutes)
if !isUserInactive && isUserIncativeNew {
remainingMins += Constants.allowedUserInactivityMinutes
}
isUserInactive = isUserIncativeNew

if isBreakTimeNow || !isUserInactive {
remainingMins -= 1
if remainingMins <= 0 {
if isBreakTimeNow {
Expand Down

0 comments on commit caaa2c1

Please sign in to comment.