Skip to content

Commit

Permalink
fix: fixed multiple simulator with same names and different versions
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Jun 9, 2023
1 parent 5c89e2c commit 3562e95
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
30 changes: 16 additions & 14 deletions MiniSim/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import AppKit
import KeyboardShortcuts
import UserNotifications
import RegexBuilder

class Menu: NSMenu {
public let maxKeyEquivalent = 9
Expand All @@ -19,8 +20,8 @@ class Menu: NSMenu {
assignKeyEquivalents()
}
willSet {
let deviceNames = Set(devices.map({ $0.name }))
let updatedDeviceNames = Set(newValue.map({ $0.name }))
let deviceNames = Set(devices.map({ $0.displayName }))
let updatedDeviceNames = Set(newValue.map({ $0.displayName }))
removeMenuItems(removedDevices: deviceNames.subtracting(updatedDeviceNames))
}
}
Expand Down Expand Up @@ -52,7 +53,7 @@ class Menu: NSMenu {
}

private func getDeviceByName(name: String) -> Device? {
return devices.first { $0.name == name }
return devices.first { $0.displayName == name }
}

private func removeMenuItems(removedDevices: Set<String>) {
Expand Down Expand Up @@ -208,8 +209,8 @@ class Menu: NSMenu {
private func assignKeyEquivalents() {
let sections = MenuSections.allCases.map {$0.title}
let deviceItems = items.filter { !sections.contains($0.title) }
let iosDeviceNames = devices.filter({ !$0.isAndroid }).map { $0.name }
let androidDeviceNames = devices.filter({ $0.isAndroid }).map { $0.name }
let iosDeviceNames = devices.filter({ $0.platform == Platform.ios }).map { $0.displayName }
let androidDeviceNames = devices.filter({ $0.platform == Platform.android }).map { $0.displayName }

let iosDevices = deviceItems.filter { iosDeviceNames.contains($0.title) }
let androidDevices = deviceItems.filter { androidDeviceNames.contains($0.title) }
Expand Down Expand Up @@ -242,32 +243,33 @@ class Menu: NSMenu {
}

private func populateDevices(isFirst: Bool) {
let sortedDevices = devices.sorted(by: { $0.isAndroid && !$1.isAndroid })
let sortedDevices = devices.sorted(by: { $0.platform == .android && $1.platform == .ios })
for (index, device) in sortedDevices.enumerated() {
if let itemIndex = items.firstIndex(where: { $0.title == device.name }) {
let isAndroid = device.platform == .android
if let itemIndex = items.firstIndex(where: { $0.title == device.displayName }) {
DispatchQueue.main.async { [self] in
let item = self.items.get(at: itemIndex)
item?.state = device.booted ? .on : .off
item?.submenu = device.isAndroid ? populateAndroidSubMenu(booted: device.booted) : populateIOSSubMenu(booted: device.booted)
item?.submenu = isAndroid ? populateAndroidSubMenu(booted: device.booted) : populateIOSSubMenu(booted: device.booted)
}
continue
}

let menuItem = NSMenuItem(
title: device.name,
title: device.displayName,
action: #selector(deviceItemClick),
keyEquivalent: "",
type: device.isAndroid ? .launchAndroid : .launchIOS
type: isAndroid ? .launchAndroid : .launchIOS
)

menuItem.target = self
menuItem.keyEquivalentModifierMask = device.isAndroid ? [.option] : [.command]
menuItem.submenu = device.isAndroid ? populateAndroidSubMenu(booted: device.booted) : populateIOSSubMenu(booted: device.booted)
menuItem.keyEquivalentModifierMask = isAndroid ? [.option] : [.command]
menuItem.submenu = isAndroid ? populateAndroidSubMenu(booted: device.booted) : populateIOSSubMenu(booted: device.booted)
menuItem.state = device.booted ? .on : .off

DispatchQueue.main.async {
let iosDevicesCount = self.devices.filter({ !$0.isAndroid }).count
self.safeInsertItem(menuItem, at: device.isAndroid ? (isFirst ? index : iosDevicesCount) + 3 : 1)
let iosDevicesCount = self.devices.filter({ $0.platform == .ios }).count
self.safeInsertItem(menuItem, at: isAndroid ? (isFirst ? index : iosDevicesCount) + 3 : 1)
}

}
Expand Down
14 changes: 13 additions & 1 deletion MiniSim/Model/Device.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ struct Device: Hashable {
var version: String?
var ID: String?
var booted: Bool = false
var platform: Platform

var isAndroid: Bool = false
var displayName: String {
switch platform {
case .ios:
if let version {
return "\(name) - (\(version))"
}
return name

case .android:
return name
}
}
}
23 changes: 13 additions & 10 deletions MiniSim/Service/DeviceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class DeviceService: DeviceServiceProtocol {
func focusDevice(_ device: Device) {
let runningApps = NSWorkspace.shared.runningApplications.filter({$0.activationPolicy == .regular})

if let uuid = device.ID, !device.isAndroid {
if let uuid = device.ID, device.platform == .ios {
try? launchSimulatorApp(uuid: uuid)
}

Expand Down Expand Up @@ -101,7 +101,7 @@ class DeviceService: DeviceServiceProtocol {
}

private func matchDeviceTitle(windowTitle: String, device: Device) -> Bool {
if device.isAndroid {
if device.platform == .android {
let deviceName = windowTitle.match(#"(?<=- ).*?(?=:)"#).first?.first
return deviceName == device.name
}
Expand All @@ -127,16 +127,19 @@ extension DeviceService {

private func parseIOSDevices(result: [String]) -> [Device] {
var devices: [Device] = []
var osVersion = ""
result.forEach { line in
let device = line.match("(.*?) (\\(([0-9.]+)\\) )?\\(([0-9A-F-]+)\\) (\\(.*?)\\)")
if (!device.isEmpty) {
let firstDevice = device[0]
if let currentOs = line.match("-- (.*?) --").first, currentOs.count > 0 {
osVersion = currentOs[1]
}
if let device = line.match("(.*?) (\\(([0-9.]+)\\) )?\\(([0-9A-F-]+)\\) (\\(.*?)\\)").first {
devices.append(
Device(
name: firstDevice[1].trimmingCharacters(in: .whitespacesAndNewlines),
version: firstDevice[3],
ID: firstDevice[4],
booted: firstDevice[5].contains("Booted")
name: device[1].trimmingCharacters(in: .whitespacesAndNewlines),
version: osVersion,
ID: device[4],
booted: device[5].contains("Booted"),
platform: .ios
)
)
}
Expand Down Expand Up @@ -217,7 +220,7 @@ extension DeviceService {

return splitted.filter({ !$0.isEmpty }).map {
let adbId = try? ADB.getAdbId(for: $0, adbPath: adbPath)
return Device(name: $0, ID: adbId, booted: adbId != nil, isAndroid: true)
return Device(name: $0, ID: adbId, booted: adbId != nil, platform: .android)
}
}

Expand Down

0 comments on commit 3562e95

Please sign in to comment.