From f2452bbc704f931fcb3a79f0cb886deadc58aa4f Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Sun, 30 Jan 2022 19:27:44 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20Keep=20track=20of=20times=20(suc?= =?UTF-8?q?cessfully)=20switched?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some users might not reboot their computer and in that situation they will never see the message in bba961269ccd5f1bcbf0abaea3dbb0b5b8d6f73f. This has been remedied by also checking how many times the version switch has occurred. The thresholds for the alert are now: - Must have launched the app at least 7 times OR - Must have switched PHP versions at least 40 times If the alert has been seen, it'll never be shown again. For more info please consult the linked commit for the rationale behind this change. --- phpmon/Domain/Menu/MainMenu.swift | 4 ++ phpmon/Domain/Preferences/Preferences.swift | 5 ++- phpmon/Domain/Preferences/Stats.swift | 43 +++++++++++++++------ 3 files changed, 39 insertions(+), 13 deletions(-) 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 {