Skip to content

Commit

Permalink
fix: prevent crash on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Mar 13, 2023
1 parent 19bf964 commit d444969
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
24 changes: 9 additions & 15 deletions MiniSim/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ class Menu: NSMenu {
itemsToRemove.forEach(safeRemoveItem)
}

private func getAdditionalLaunchParams() -> [Parameter]? {
guard let paramData = UserDefaults.standard.parameters else { return nil }
if let decodedData = try? JSONDecoder().decode([Parameter].self, from: paramData) {
return decodedData
private func getAdditionalLaunchParams() -> [String] {
guard let paramData = UserDefaults.standard.parameters else { return [] }
guard let parameters = try? JSONDecoder().decode([Parameter].self, from: paramData) else {
return []
}
return nil

return parameters.filter({ $0.enabled }).map({ $0.command })
}

@objc private func androidSubMenuClick(_ sender: NSMenuItem) {
Expand All @@ -74,16 +75,12 @@ class Menu: NSMenu {
switch tag {
case .coldBootAndroid:
var params = ["-no-snapshot"]
if let additionalParams = getAdditionalLaunchParams() {
params.append(contentsOf: additionalParams.filter({$0.enabled}).map({$0.command}))
}
params.append(contentsOf: getAdditionalLaunchParams())
try deviceService.launchDevice(name: device.name, additionalArguments:params)

case .androidNoAudio:
var params = ["-no-audio"]
if let additionalParams = getAdditionalLaunchParams() {
params.append(contentsOf: additionalParams.filter({$0.enabled}).map({$0.command}))
}
params.append(contentsOf: getAdditionalLaunchParams())
try deviceService.launchDevice(name: device.name, additionalArguments:params)

case .toggleA11yAndroid:
Expand Down Expand Up @@ -147,10 +144,7 @@ class Menu: NSMenu {
do {
switch tag {
case .launchAndroid:
var params: [String] = []
if let additionalParams = getAdditionalLaunchParams() {
params.append(contentsOf: additionalParams.filter({$0.enabled}).map({$0.command}))
}
let params = getAdditionalLaunchParams()
try deviceService.launchDevice(name: device.name, additionalArguments: params)
case .launchIOS:
try deviceService.launchDevice(uuid: device.ID ?? "")
Expand Down
16 changes: 9 additions & 7 deletions MiniSim/Views/ParametersTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import SwiftUI

struct ParametersTable: View {
@State private var parameters: [Parameter] = [.init(title: "", command: "", enabled: false)]
@State private var selection: Parameter?

func saveData() {
let data = try? JSONEncoder().encode(parameters)
Expand All @@ -23,6 +22,13 @@ struct ParametersTable: View {
}
}

func deleteParameter(_ parameter: Parameter) {
NSApp.keyWindow?.makeFirstResponder(nil)
if let index = parameters.firstIndex(of: parameter) {
parameters.remove(at: index)
}
}

var body: some View {
VStack {
HStack {
Expand Down Expand Up @@ -67,9 +73,7 @@ struct ParametersTable: View {
}
.contextMenu {
Button("Delete") {
if let index = parameters.firstIndex(of: $param.wrappedValue) {
parameters.remove(at: index)
}
deleteParameter($param.wrappedValue)
}
}
Divider()
Expand All @@ -82,10 +86,8 @@ struct ParametersTable: View {
Image(systemName: "plus")
}
Spacer()
Button {
Button("Save") {
saveData()
} label: {
Text("Save")
}
}
}
Expand Down

0 comments on commit d444969

Please sign in to comment.