Skip to content

Commit

Permalink
feat: change icon for 1 second to show success state
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Jun 15, 2023
1 parent a40015e commit fae27bf
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 8 deletions.
21 changes: 21 additions & 0 deletions MiniSim/Assets.xcassets/success_action.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "checkmark.rectangle.portrait@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions MiniSim/Extensions/NSNotificationName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import Foundation
extension NSNotification.Name {
static let menuWillOpen = NSNotification.Name("menuWillOpen")
static let menuDidClose = NSNotification.Name("menuDidClose")
static let commandDidSucceed = NSNotification.Name("commandDidSucceed")
}
19 changes: 12 additions & 7 deletions MiniSim/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ class Menu: NSMenu {
case .copyAdbId:
if let deviceId = device.ID {
NSPasteboard.general.copyToPasteboard(text: deviceId)
UNUserNotificationCenter.showNotification(title: "Device ID copied to clipboard!", body: deviceId)
handleSuccess(title: "Device ID copied to clipboard!", body: deviceId)
}

case .copyName:
NSPasteboard.general.copyToPasteboard(text: device.name)
UNUserNotificationCenter.showNotification(title: "Device name copied to clipboard!", body: device.name)
handleSuccess(title: "Device name copied to clipboard!", body: device.name)

case .pasteToEmulator:
let pasteboard = NSPasteboard.general
guard let clipboard = pasteboard.pasteboardItems?.first?.string(forType: .string) else { break }
try deviceService.sendText(device: device, text: clipboard)

case .customCommand:
let androidCommands = getAdditionalCommands(platform: .android)
guard let command = androidCommands.first(where: {$0.name == sender.title}) else { return }
Expand Down Expand Up @@ -141,11 +141,11 @@ class Menu: NSMenu {
switch tag {
case .copyName:
NSPasteboard.general.copyToPasteboard(text: device.name)
UNUserNotificationCenter.showNotification(title: "Device name copied to clipboard!", body: device.name)
handleSuccess(title: "Device name copied to clipboard!", body: device.name)
case .copyUDID:
if let deviceID = device.ID {
NSPasteboard.general.copyToPasteboard(text: deviceID)
UNUserNotificationCenter.showNotification(title: "Device ID copied to clipboard!", body: deviceID)
handleSuccess(title: "Device ID copied to clipboard!", body: deviceID)
}
case .deleteSim:
guard let deviceID = device.ID else { return }
Expand All @@ -155,7 +155,7 @@ class Menu: NSMenu {
DispatchQueue.global().async { [self] in
do {
try deviceService.deleteSimulator(uuid: deviceID)
UNUserNotificationCenter.showNotification(title: "Simulator deleted!", body: deviceID)
handleSuccess(title: "Simulator deleted!", body: deviceID)
getDevices()
} catch {
NSAlert.showError(message: error.localizedDescription)
Expand Down Expand Up @@ -241,6 +241,11 @@ class Menu: NSMenu {
}
}

private func handleSuccess(title: String, body: String) {
UNUserNotificationCenter.showNotification(title: title, body: body)
NotificationCenter.default.post(name: .commandDidSucceed, object: nil)
}

private func populateDevices(isFirst: Bool) {
let sortedDevices = devices.sorted(by: { $0.platform == .android && $1.platform == .ios })
for (index, device) in sortedDevices.enumerated() {
Expand Down Expand Up @@ -292,7 +297,7 @@ class Menu: NSMenu {
}
subMenu.addItem(menuItem)
}

for item in self.getAdditionalCommands(platform: .android) {
let menuItem = AndroidSubMenuItem.customCommand.menuItem
menuItem.target = self
Expand Down
24 changes: 23 additions & 1 deletion MiniSim/MiniSim.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MiniSim: NSObject {

deinit {
isOnboardingFinishedObserver?.invalidate()
NotificationCenter.default.removeObserver(self, name: .commandDidSucceed, object: nil)
}

private lazy var settingsController = SettingsWindowController(
Expand Down Expand Up @@ -82,21 +83,41 @@ class MiniSim: NSObject {
self.onboarding.showPopOver(button: self.statusItem.button)
}
}
NotificationCenter.default.addObserver(self, selector: #selector(toggleSuccessCheckmark), name: .commandDidSucceed, object: nil)
}

private func appendMenu() {
if !(UserDefaults.standard.androidHome != nil && UserDefaults.standard.isOnboardingFinished) {
onboarding.show()
return
}
setMenuImage()
populateSections()
}

private func setMenuImage() {
if let button = statusItem.button {
button.toolTip = "MiniSim"
let itemImage = NSImage(named: "menu_icon")
itemImage?.size = NSSize(width: 9, height: 16)
itemImage?.isTemplate = true
button.image = itemImage
}
populateSections()
}

@objc private func toggleSuccessCheckmark() {
DispatchQueue.main.async {
if let button = self.statusItem.button {
let itemImage = NSImage(named: "success_action")
itemImage?.size = NSSize(width: 9, height: 15)
itemImage?.isTemplate = true
button.image = itemImage
}
}

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
self.setMenuImage()
}
}

@objc func menuItemAction(_ sender: NSMenuItem) {
Expand All @@ -115,6 +136,7 @@ class MiniSim: NSObject {
do {
let amountCleared = try DeviceService.clearDerivedData()
UNUserNotificationCenter.showNotification(title: "Derived data has been cleared!", body: "Removed \(amountCleared) of data")
NotificationCenter.default.post(name: .commandDidSucceed, object: nil)
} catch {
NSAlert.showError(message: error.localizedDescription)
}
Expand Down
1 change: 1 addition & 0 deletions MiniSim/Service/DeviceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class DeviceService: DeviceServiceProtocol {

do {
try shellOut(to: commandToExecute)
NotificationCenter.default.post(name: .commandDidSucceed, object: nil)
} catch {
throw CustomCommandError.commandError(errorMessage: error.localizedDescription)
}
Expand Down

0 comments on commit fae27bf

Please sign in to comment.