Skip to content

Commit

Permalink
Overcome bug identified by Alan F that seems to affect people upgradi…
Browse files Browse the repository at this point in the history
…ng from 1.07 to 1.09
  • Loading branch information
DarraghRogan committed Oct 1, 2020
1 parent 2392cab commit 39396e4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 45 deletions.
8 changes: 4 additions & 4 deletions Miasma.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 14;
CURRENT_PROJECT_VERSION = 15;
DEVELOPMENT_TEAM = VDLCQ62KZH;
ENABLE_HARDENED_RUNTIME = YES;
EXCLUDED_ARCHS = "";
Expand All @@ -711,7 +711,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = 1.09;
MARKETING_VERSION = 1.10;
PRODUCT_BUNDLE_IDENTIFIER = "Darragh-Rogan.Miasma";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -727,7 +727,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 14;
CURRENT_PROJECT_VERSION = 15;
DEVELOPMENT_TEAM = VDLCQ62KZH;
ENABLE_HARDENED_RUNTIME = YES;
EXCLUDED_ARCHS = "";
Expand All @@ -737,7 +737,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.12;
MARKETING_VERSION = 1.09;
MARKETING_VERSION = 1.10;
PRODUCT_BUNDLE_IDENTIFIER = "Darragh-Rogan.Miasma";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down
91 changes: 50 additions & 41 deletions Miasma/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
// setup ability to have notifications -code from https://nabtron.com/show-notification-cocoa-xcode-swift/

func showNotification(title: String, subtitle: String, informativeText: String) -> Void {

var notification = NSUserNotification()

notification.title = title
notification.subtitle = subtitle
notification.informativeText = informativeText
// notification.contentImage = contentImage
// notification.contentImage = contentImage
notification.soundName = NSUserNotificationDefaultSoundName

NSUserNotificationCenter.default.deliver(notification)

}

func userNotificationCenter(_ center: NSUserNotificationCenter, shouldPresent notification: NSUserNotification) -> Bool {
Expand All @@ -53,7 +53,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
// setup ability to have notifications -code from https://nabtron.com/show-notification-cocoa-xcode-swift/

NSUserNotificationCenter.default.delegate = self


func isKeyPresentInUserDefaults(key: String) -> Bool {
return UserDefaults.standard.object(forKey: key) != nil
}

// setting first launch to default to using WAQI with "here" as the city, to give users a nice first impression. Following technique from: https://medium.com/better-programming/checking-for-the-users-first-launch-in-swift-df02a1feb472

Expand All @@ -65,47 +68,53 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
} else if (defaults.bool(forKey: "First Launch") == true || defaults.integer(forKey: "PurpleAirInUse") == 1) {

defaults.set(true, forKey: "First Launch")

} else {

defaults.set(true, forKey: "First Launch")
defaults.set("here", forKey: "WAQICity")
defaults.set(1, forKey: "WAQIInUse")
defaults.set(1, forKey:"CO2SignalInUse")
defaults.set(1, forKey:"OpenSkyInUse")
defaults.set(1, forKey:"ClimbingAQINotificationsWanted")
defaults.set(50, forKey:"ClimbingAQINotificationsTrigger")

} else if (defaults.bool(forKey: "First Launch") == true && defaults.integer(forKey: "ClimbingAQINotificationsWanted") == 1 && isKeyPresentInUserDefaults(key: "ClimbingAQINotificationsTrigger") == false ) {

defaults.set(50, forKey:"ClimbingAQINotificationsTrigger")

} else {

defaults.set(true, forKey: "First Launch")
defaults.set("here", forKey: "WAQICity")
defaults.set(1, forKey: "WAQIInUse")
defaults.set(1, forKey:"CO2SignalInUse")
defaults.set(1, forKey:"OpenSkyInUse")
defaults.set(1, forKey:"ClimbingAQINotificationsWanted")
defaults.set(50, forKey:"ClimbingAQINotificationsTrigger")
}

// Launching automatically at startup from tutorial: https://theswiftdev.com/how-to-launch-a-macos-app-at-login/

let launcherAppId = "Darragh-Rogan.MiasmaLauncher"
let runningApps = NSWorkspace.shared.runningApplications
let isRunning = !runningApps.filter { $0.bundleIdentifier == launcherAppId }.isEmpty

// SMLoginItemSetEnabled(launcherAppId as CFString, true)

if isRunning {
DistributedNotificationCenter.default().post(name: .killLauncher, object: Bundle.main.bundleIdentifier!)
}


// Technique from https://stackoverflow.com/questions/55995415/nsmenuitem-with-action-added-to-nsstatusbar-is-grayed-out-when-the-selected-func to get status bar actions working
statusBarItemController = menuFunctions()

// Run the dataloaders at app startup
// menuFunctions().menuRefresh(NSMenuItem)

// periodically update the menu
_ = Timer.scheduledTimer(withTimeInterval: 1200.0, repeats: true) { timer in
self.statusBarItemController = menuFunctions() }

}

// Launching automatically at startup from tutorial: https://theswiftdev.com/how-to-launch-a-macos-app-at-login/

let launcherAppId = "Darragh-Rogan.MiasmaLauncher"
let runningApps = NSWorkspace.shared.runningApplications
let isRunning = !runningApps.filter { $0.bundleIdentifier == launcherAppId }.isEmpty

// SMLoginItemSetEnabled(launcherAppId as CFString, true)

if isRunning {
DistributedNotificationCenter.default().post(name: .killLauncher, object: Bundle.main.bundleIdentifier!)
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}


// Technique from https://stackoverflow.com/questions/55995415/nsmenuitem-with-action-added-to-nsstatusbar-is-grayed-out-when-the-selected-func to get status bar actions working
statusBarItemController = menuFunctions()

// Run the dataloaders at app startup
// menuFunctions().menuRefresh(NSMenuItem)

// periodically update the menu
_ = Timer.scheduledTimer(withTimeInterval: 1200.0, repeats: true) { timer in
self.statusBarItemController = menuFunctions() }

}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}


}

0 comments on commit 39396e4

Please sign in to comment.