Skip to content

Commit

Permalink
move parts of needToActivateNudge to ObservableObject
Browse files Browse the repository at this point in the history
  • Loading branch information
erikng committed Aug 22, 2021
1 parent 6a195f0 commit 62b51d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
6 changes: 4 additions & 2 deletions Nudge/UI/ContentView.swift
Expand Up @@ -12,11 +12,13 @@ import SwiftUI

class ViewState: ObservableObject {
@Published var allowButtons = true
@Published var afterFirstRun = false
@Published var daysRemaining = Utils().getNumberOfDaysBetween()
@Published var deferralCountPastThreshhold = false
@Published var deferRunUntil = nudgeDefaults.object(forKey: "deferRunUntil") as? Date
@Published var hasLoggedDeferralCountPastThreshhold = false
@Published var hasLoggedDeferralCountPastThresholdDualQuitButtons = false
@Published var lastRefreshTime = Utils().getInitialDate()
@Published var requireDualQuitButtons = false
@Published var shouldExit = false
@Published var userDeferrals = nudgeDefaults.object(forKey: "userDeferrals") as? Int ?? 0
Expand Down Expand Up @@ -50,7 +52,7 @@ struct ContentView: View {
window?.standardWindowButton(.zoomButton)?.isHidden = true //this removes the green zoom button
window?.center() // center
window?.isMovable = false // not movable
_ = needToActivateNudge(lastRefreshTimeVar: lastRefreshTime)
_ = needToActivateNudge()
}
)
.edgesIgnoringSafeArea(.all)
Expand All @@ -59,7 +61,7 @@ struct ContentView: View {
updateUI()
}
.onReceive(nudgeRefreshCycleTimer) { _ in
if needToActivateNudge(lastRefreshTimeVar: lastRefreshTime) {
if needToActivateNudge() {
viewState.userSessionDeferrals += 1
viewState.userDeferrals = viewState.userSessionDeferrals + viewState.userQuitDeferrals
}
Expand Down
23 changes: 9 additions & 14 deletions Nudge/Utilities/UILogic.swift
Expand Up @@ -30,8 +30,8 @@ func nudgeStartLogic() {
if Utils().requireDualQuitButtons() || nudgePrimaryState.userDeferrals > allowedDeferralsUntilForcedSecondaryQuitButton {
nudgePrimaryState.requireDualQuitButtons = true
}
if nudgePrimaryState.deferRunUntil ?? lastRefreshTime > Utils().getCurrentDate() && !Utils().pastRequiredInstallationDate() {
let msg = "User has selected a deferral date (\(nudgePrimaryState.deferRunUntil ?? lastRefreshTime)) that is greater than the launch date (\(Utils().getCurrentDate()))"
if nudgePrimaryState.deferRunUntil ?? nudgePrimaryState.lastRefreshTime > Utils().getCurrentDate() && !Utils().pastRequiredInstallationDate() {
let msg = "User has selected a deferral date (\(nudgePrimaryState.deferRunUntil ?? nudgePrimaryState.lastRefreshTime)) that is greater than the launch date (\(Utils().getCurrentDate()))"
uiLog.notice("\(msg, privacy: .public)")
Utils().exitNudge()
}
Expand Down Expand Up @@ -61,11 +61,6 @@ func nudgeStartLogic() {
}
}

// These are initial variables that needToActivateNudge() will update within the timer controller
// This type of logic is not indeal and should be redesigned.
var lastRefreshTime = Utils().getInitialDate()
var afterFirstRun = false

func userHasClickedSecondaryQuitButton() {
let msg = "User clicked secondaryQuitButton"
uiLog.notice("\(msg, privacy: .public)")
Expand All @@ -76,7 +71,7 @@ func userHasClickedDeferralQuitButton(deferralTime: Date) {
uiLog.notice("\(msg, privacy: .public)")
}

func needToActivateNudge(lastRefreshTimeVar: Date) -> Bool {
func needToActivateNudge() -> Bool {
// Center Nudge
Utils().centerNudge()
Utils().logUserSessionDeferrals()
Expand All @@ -88,14 +83,14 @@ func needToActivateNudge(lastRefreshTimeVar: Date) -> Bool {
}

let currentTime = Date().timeIntervalSince1970
let timeDiff = Int((currentTime - lastRefreshTimeVar.timeIntervalSince1970))
let timeDiff = Int((currentTime - nudgePrimaryState.lastRefreshTime.timeIntervalSince1970))

// The first time the main timer contoller hits we don't care
if !afterFirstRun {
let msg = "Initializing nudgeRefreshCycle"
if !nudgePrimaryState.afterFirstRun {
let msg = "Initializing nudgeRefreshCycle: \(nudgeRefreshCycle)"
uiLog.info("\(msg, privacy: .public)")
_ = afterFirstRun = true
_ = lastRefreshTime = Date()
nudgePrimaryState.afterFirstRun = true
nudgePrimaryState.lastRefreshTime = Date()
}

if Utils().getTimerController() > timeDiff {
Expand Down Expand Up @@ -141,7 +136,7 @@ func needToActivateNudge(lastRefreshTimeVar: Date) -> Bool {

// If we get here, Nudge if not frontmostApplication
if !NSApplication.shared.isActive {
_ = lastRefreshTime = Date()
nudgePrimaryState.lastRefreshTime = Date()
if nudgePrimaryState.deferralCountPastThreshhold || Utils().pastRequiredInstallationDate() {
// Loop through all the running applications and hide them
for runningApplication in runningApplications {
Expand Down

0 comments on commit 62b51d7

Please sign in to comment.