diff --git a/phpmon/Domain/Menu/MainMenu.swift b/phpmon/Domain/Menu/MainMenu.swift index cac7b28..1112c3c 100644 --- a/phpmon/Domain/Menu/MainMenu.swift +++ b/phpmon/Domain/Menu/MainMenu.swift @@ -279,6 +279,7 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate { self.switchToPhpVersion(sender.version) } + // TODO (5.1): Investigate if `waitAndExecute` cannot be used here @objc func switchToPhpVersion(_ version: String) { setBusyImage() PhpEnv.shared.isBusy = true @@ -315,6 +316,9 @@ class MainMenu: NSObject, NSWindowDelegate, NSMenuDelegate { } else { sendLocalNotification() } + + Stats.incrementSuccessfulSwitchCount() + Stats.evaluateSponsorMessageShouldBeDisplayed() } } diff --git a/phpmon/Domain/Preferences/Preferences.swift b/phpmon/Domain/Preferences/Preferences.swift index b670699..ea2b1a3 100644 --- a/phpmon/Domain/Preferences/Preferences.swift +++ b/phpmon/Domain/Preferences/Preferences.swift @@ -27,6 +27,7 @@ enum PreferenceName: String { */ enum InternalStats: String { case launchCount = "times_launched" + case switchCount = "times_switched_versions" case didSeeSponsorEncouragement = "did_see_sponsor_encouragement" } @@ -61,13 +62,15 @@ class Preferences { */ static func handleFirstTimeLaunch() { UserDefaults.standard.register(defaults: [ + /// Preferences PreferenceName.shouldDisplayDynamicIcon.rawValue: true, PreferenceName.shouldDisplayPhpHintInIcon.rawValue: true, PreferenceName.fullPhpVersionDynamicIcon.rawValue: false, PreferenceName.autoServiceRestartAfterExtensionToggle.rawValue: true, PreferenceName.autoComposerGlobalUpdateAfterSwitch.rawValue: false, PreferenceName.allowProtocolForIntegrations.rawValue: true, - /// + /// Stats + InternalStats.switchCount.rawValue: 0, InternalStats.launchCount.rawValue: 0, InternalStats.didSeeSponsorEncouragement.rawValue: false ]) diff --git a/phpmon/Domain/Preferences/Stats.swift b/phpmon/Domain/Preferences/Stats.swift index a3cbc63..77ff36e 100644 --- a/phpmon/Domain/Preferences/Stats.swift +++ b/phpmon/Domain/Preferences/Stats.swift @@ -13,11 +13,10 @@ class Stats { /** Keep track of how many times the app has been successfully launched. + This is used to determine whether it is time to show the sponsor - encouragement alert, but I'd like to include this stat in the - preferences window as well. "PHP Monitor has been started X - times." If the count is over 99, it should say "Think about - all of the time the app has saved you!" + encouragement alert, but I'd like to include this stat somewhere + else as well. */ public static var successfulLaunchCount: Int { UserDefaults.standard.integer( @@ -25,6 +24,20 @@ class Stats { ) } + /** + Keep track of how many times the app has successfully switched + between different PHP versions. + + This is used to determine whether it is time to show the sponsor + encouragement alert, but I'd like to include this stat somewhere + else as well. + */ + public static var successfulSwitchCount: Int { + UserDefaults.standard.integer( + forKey: InternalStats.switchCount.rawValue + ) + } + /** Did the user see the sponsor encouragement / thank you message? Annoying the user is the worst, so let's not show the message twice. @@ -41,23 +54,29 @@ class Stats { up the application. */ public static func incrementSuccessfulLaunchCount() { - let count = Stats.successfulLaunchCount UserDefaults.standard.set( - count + 1, + Stats.successfulLaunchCount + 1, forKey: InternalStats.launchCount.rawValue ) } + /** + Increment the successful switch count. + */ + public static func incrementSuccessfulSwitchCount() { + UserDefaults.standard.set( + Stats.successfulSwitchCount + 1, + forKey: InternalStats.switchCount.rawValue + ) + } + public static func evaluateSponsorMessageShouldBeDisplayed() { if Stats.didSeeSponsorEncouragement { - Log.info("Awesome, the user has already seen the sponsor message.") - return + return Log.info("Awesome, the user has already seen the sponsor message.") } - if Stats.successfulLaunchCount < 7 { - Log.info("It is too soon to see the sponsor message.") - Log.info("The application has been launched successfully \(Stats.successfulLaunchCount) times.") - return + if Stats.successfulLaunchCount < 7 && Stats.successfulSwitchCount < 40 { + return Log.info("It is too soon to see the sponsor message (launched \(Stats.successfulLaunchCount) times, switched \(Stats.successfulSwitchCount) times).") } DispatchQueue.main.async {