Skip to content

Commit

Permalink
Fix/app store crash (#4)
Browse files Browse the repository at this point in the history
* Properly unwrap nils to avoid Swift Runtime Error

* Updated versioning

* Add sensible default values

* Update build version
  • Loading branch information
billycastelli authored Nov 30, 2020
1 parent c2d6c6c commit 7cb7c9a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Silicon Info.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1.0.3;
CURRENT_PROJECT_VERSION = 5;
DEVELOPMENT_ASSET_PATHS = "\"silicon-info/Preview Content\"";
DEVELOPMENT_TEAM = 2J6P9NJJH8;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down Expand Up @@ -301,7 +301,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1.0.3;
CURRENT_PROJECT_VERSION = 5;
DEVELOPMENT_ASSET_PATHS = "\"silicon-info/Preview Content\"";
DEVELOPMENT_TEAM = 2J6P9NJJH8;
ENABLE_HARDENED_RUNTIME = YES;
Expand Down
2 changes: 1 addition & 1 deletion silicon-info/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ struct ContentView: View {

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(appName: "", architecture: "", appIcon: NSImage(named: "processor-icon")!)
ContentView(appName: "", architecture: "", appIcon: NSImage(named: "processor-icon") ?? NSImage())
}
}
45 changes: 28 additions & 17 deletions silicon-info/silicon-infoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
menu.delegate = self;

// Grab application information from frontmost application
let app = getApplicationInfo(application: NSWorkspace.shared.frontmostApplication!)
let app = getApplicationInfo(application: NSWorkspace.shared.frontmostApplication)

// Set view
let contentView = ContentView(appName: app.appName, architecture: app.architecture, appIcon: app.appImage)
Expand All @@ -66,7 +66,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
// Run function when menu bar icon is clicked
func menuWillOpen(_ menu: NSMenu) {
// Grab application information from frontmost application
let app = getApplicationInfo(application: NSWorkspace.shared.frontmostApplication!)
let app = getApplicationInfo(application: NSWorkspace.shared.frontmostApplication)

// Set view
let contentView = ContentView(appName: app.appName, architecture: app.architecture, appIcon: app.appImage)
Expand All @@ -86,40 +86,51 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {

// Run function when a new application is sent to front
@objc func iconSwitcher(notification: NSNotification) {
let runningApplication = notification.userInfo!["NSWorkspaceApplicationKey"] as! NSRunningApplication
let app = getApplicationInfo(application: runningApplication)
guard let notification = notification.userInfo else {
return;
}
guard let runningApplication = notification["NSWorkspaceApplicationKey"] else {
return
}
let app = getApplicationInfo(application: runningApplication as? NSRunningApplication)
let itemImage = app.processorIcon;
itemImage.isTemplate = true
statusBarItem?.button?.image = itemImage
}

func getApplicationInfo(application: NSRunningApplication) ->RunningApplication{
let frontAppName = application.localizedName
let frontAppImage = application.icon
let architectureInt = application.executableArchitecture

func getApplicationInfo(application: NSRunningApplication?) ->RunningApplication{
// Check if application is nil, passed in item is not guaranteed to be an object
guard let runningApp = application else {
return RunningApplication(appName: "Unknown", architecture: "Cannot identify frontmost app", appImage: NSImage(named: "processor-icon-empty") ?? NSImage(), processorIcon: NSImage(named: "processor-icon-empty") ?? NSImage())
}
// After checking for nil, we can refer to runningApp, guarenteed to be NSRunningApplication
let frontAppName = runningApp.localizedName ?? String()
let frontAppImage = runningApp.icon ?? NSImage()
let architectureInt = runningApp.executableArchitecture


var architecture = ""
var processorIcon = NSImage()
switch architectureInt {
case NSBundleExecutableArchitectureARM64:
architecture = "arm64 • Apple Silicon"
processorIcon = NSImage(named: "processor-icon")!
processorIcon = NSImage(named: "processor-icon") ?? NSImage()
case NSBundleExecutableArchitectureI386:
architecture = "x86 • Intel 32-bit"
processorIcon = NSImage(named: "processor-icon-empty")!
processorIcon = NSImage(named: "processor-icon-empty") ?? NSImage()
case NSBundleExecutableArchitectureX86_64:
architecture = "x86-64 • Intel 64-bit"
processorIcon = NSImage(named: "processor-icon-empty")!
processorIcon = NSImage(named: "processor-icon-empty") ?? NSImage()
case NSBundleExecutableArchitecturePPC:
architecture = "ppc32 • PowerPC 32-bit"
processorIcon = NSImage(named: "processor-icon-empty")!
processorIcon = NSImage(named: "processor-icon-empty") ?? NSImage()
case NSBundleExecutableArchitecturePPC64:
architecture = "ppc64 • PowerPC 64-bit"
processorIcon = NSImage(named: "processor-icon-empty")!
processorIcon = NSImage(named: "processor-icon-empty") ?? NSImage()
default:
architecture = "Unknown"
processorIcon = NSImage(named: "processor-icon-empty")!
architecture = "Unknown • Unknown"
processorIcon = NSImage(named: "processor-icon-empty") ?? NSImage()
}
return RunningApplication(appName: frontAppName!, architecture: architecture, appImage: frontAppImage!, processorIcon: processorIcon)
return RunningApplication(appName: frontAppName, architecture: architecture, appImage: frontAppImage, processorIcon: processorIcon)
}
}

0 comments on commit 7cb7c9a

Please sign in to comment.