From d76221bd46a100990a140983217c9818cafef54f Mon Sep 17 00:00:00 2001 From: Abhishek Date: Sat, 22 Jun 2019 13:13:37 -0700 Subject: [PATCH] Extracting out startup code. --- Clocker/Clocker.xcodeproj/project.pbxproj | 4 +++ .../General/PreferencesViewController.swift | 27 ++------------- Clocker/Preferences/StartupManager.swift | 34 +++++++++++++++++++ 3 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 Clocker/Preferences/StartupManager.swift diff --git a/Clocker/Clocker.xcodeproj/project.pbxproj b/Clocker/Clocker.xcodeproj/project.pbxproj index a5b3d71..86b7580 100755 --- a/Clocker/Clocker.xcodeproj/project.pbxproj +++ b/Clocker/Clocker.xcodeproj/project.pbxproj @@ -103,6 +103,7 @@ 9A7547DC1F184DC3004705EF /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9A7547DB1F184DC3004705EF /* Assets.xcassets */; }; 9A7547DF1F184DC3004705EF /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9A7547DD1F184DC3004705EF /* Main.storyboard */; }; 9A7547E51F184E3F004705EF /* ClockerHelper.app in Login Item Helper */ = {isa = PBXBuildFile; fileRef = 9A7547D01F184DC3004705EF /* ClockerHelper.app */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 9A7CDC1B22BEC2170035902D /* StartupManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A7CDC1A22BEC2170035902D /* StartupManager.swift */; }; 9A8605AE1BEC148400A810A4 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 9A8605AD1BEC148400A810A4 /* main.m */; }; 9A9E87621C1FEDB500A7A2DF /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A9E87611C1FEDB500A7A2DF /* CFNetwork.framework */; }; 9A9E876A1C1FEDDB00A7A2DF /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A9E87691C1FEDDB00A7A2DF /* SystemConfiguration.framework */; }; @@ -313,6 +314,7 @@ 9A7547DB1F184DC3004705EF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 9A7547DE1F184DC3004705EF /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 9A7547E01F184DC3004705EF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + 9A7CDC1A22BEC2170035902D /* StartupManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StartupManager.swift; sourceTree = ""; }; 9A8605AD1BEC148400A810A4 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = main.m; path = Clocker/main.m; sourceTree = ""; }; 9A8605C31BEC155B00A810A4 /* Clocker-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "Clocker-Info.plist"; path = "Clocker/Clocker-Info.plist"; sourceTree = ""; }; 9A8605CC1BEC155B00A810A4 /* Clocker-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "Clocker-Prefix.pch"; path = "Clocker/Clocker-Prefix.pch"; sourceTree = ""; }; @@ -472,6 +474,7 @@ 35C36F1E22596253002FA5C6 /* OneWindowController.swift */, 9AB6F1632259D1B800A44663 /* ParentViewController.swift */, 35C36F902259EAF4002FA5C6 /* Preferences.storyboard */, + 9A7CDC1A22BEC2170035902D /* StartupManager.swift */, ); path = Preferences; sourceTree = ""; @@ -1171,6 +1174,7 @@ 35C36F5A2259DD8A002FA5C6 /* Panelr.swift in Sources */, 35C36F712259E185002FA5C6 /* NoTimezoneView.swift in Sources */, 35C36F2B2259D6FA002FA5C6 /* ParentPanelController.swift in Sources */, + 9A7CDC1B22BEC2170035902D /* StartupManager.swift in Sources */, 35C36F582259DD8A002FA5C6 /* PanelTableView.swift in Sources */, 35C36F0F225961DA002FA5C6 /* TimePeriodCollection.swift in Sources */, 35C36F18225961DA002FA5C6 /* Date+Comparators.swift in Sources */, diff --git a/Clocker/Preferences/General/PreferencesViewController.swift b/Clocker/Preferences/General/PreferencesViewController.swift index 5535cfa..ab9f9dc 100644 --- a/Clocker/Preferences/General/PreferencesViewController.swift +++ b/Clocker/Preferences/General/PreferencesViewController.swift @@ -1,7 +1,6 @@ // Copyright © 2015 Abhishek Banthia import Cocoa -import ServiceManagement struct PreferencesConstants { static let timezoneNameIdentifier = "formattedAddress" @@ -30,6 +29,7 @@ class PreferencesViewController: ParentViewController { return DataStore.shared().timezones() } + private lazy var startupManager = StartupManager() private var filteredArray: [Any] = [] private var timezoneArray: [String] = [] private var timezoneFilteredArray: [String] = [] @@ -1028,30 +1028,7 @@ extension PreferencesViewController { extension PreferencesViewController { @IBAction func loginPreferenceChanged(_ sender: NSButton) { - if !SMLoginItemSetEnabled("com.abhishek.ClockerHelper" as CFString, sender.state == .on) { - Logger.log(object: ["Successful": "NO"], for: "Start Clocker Login") - addClockerToLoginItemsManually() - } else { - Logger.log(object: ["Successful": "YES"], for: "Start Clocker Login") - } - } - - private func addClockerToLoginItemsManually() { - NSApplication.shared.activate(ignoringOtherApps: true) - - let alert = NSAlert() - alert.messageText = "Clocker is unable to set to start at login. 😅" - alert.informativeText = "You can manually set it to start at startup by adding Clocker to your login items." - alert.addButton(withTitle: "Add Manually") - alert.addButton(withTitle: "Cancel") - - let response = alert.runModal() - if response.rawValue == 1000 { - OperationQueue.main.addOperation { - let prefPane = "/System/Library/PreferencePanes/Accounts.prefPane" - NSWorkspace.shared.openFile(prefPane) - } - } + startupManager.toggleLogin(sender) } } diff --git a/Clocker/Preferences/StartupManager.swift b/Clocker/Preferences/StartupManager.swift new file mode 100644 index 0000000..b3a6162 --- /dev/null +++ b/Clocker/Preferences/StartupManager.swift @@ -0,0 +1,34 @@ +// Copyright © 2015 Abhishek Banthia + +import Cocoa +import ServiceManagement + +struct StartupManager { + + func toggleLogin(_ sender: NSButton) { + if !SMLoginItemSetEnabled("com.abhishek.ClockerHelper" as CFString, sender.state == .on) { + Logger.log(object: ["Successful": "NO"], for: "Start Clocker Login") + addClockerToLoginItemsManually() + } else { + Logger.log(object: ["Successful": "YES"], for: "Start Clocker Login") + } + } + + private func addClockerToLoginItemsManually() { + NSApplication.shared.activate(ignoringOtherApps: true) + + let alert = NSAlert() + alert.messageText = "Clocker is unable to set to start at login. 😅" + alert.informativeText = "You can manually set it to start at startup by adding Clocker to your login items." + alert.addButton(withTitle: "Add Manually") + alert.addButton(withTitle: "Cancel") + + let response = alert.runModal() + if response.rawValue == 1000 { + OperationQueue.main.addOperation { + let prefPane = "/System/Library/PreferencePanes/Accounts.prefPane" + NSWorkspace.shared.openFile(prefPane) + } + } + } +}