diff --git a/Limelight/Input/CommandManager.swift b/Limelight/Input/CommandManager.swift index 152d3339..8b9de3a9 100644 --- a/Limelight/Input/CommandManager.swift +++ b/Limelight/Input/CommandManager.swift @@ -308,14 +308,15 @@ import UIKit } - @objc public func addCommand(_ command: RemoteCommand) { + @objc public func addCommand(_ command: RemoteCommand) -> Bool { command.keyboardCmdString = command.keyboardCmdString.uppercased() // convert all letters to upper case if(command.alias.trimmingCharacters(in: .whitespacesAndNewlines).count == 0) {command.alias = command.keyboardCmdString} // copy cmd string as alias when alias is empty let keyStrings = extractKeyStrings(from: command.keyboardCmdString) - if (keyStrings == nil) {return} // in case of non-keyboard command strings, return + if (keyStrings == nil) {return false} // in case of non-keyboard command strings, return false commands.append(command) saveCommands() viewController?.reloadTableView() // don't know why but this reload has to be called from the CommandManager, doesn't work by calling it in the viewcontroller, probably related with the dialog box. + return true } @objc public func deleteCommand(at index: Int) { diff --git a/Limelight/ViewControllers/CommandManagerViewController.swift b/Limelight/ViewControllers/CommandManagerViewController.swift index 14a054ff..d854c929 100644 --- a/Limelight/ViewControllers/CommandManagerViewController.swift +++ b/Limelight/ViewControllers/CommandManagerViewController.swift @@ -173,6 +173,7 @@ import UIKit } @objc private func addButtonTapped() { + let previouslySelectedIndexPath = tableView.indexPathForSelectedRow //memorize selected indexpath let alert = UIAlertController(title: SwiftLocalizationHelper.localizedString(forKey: "New Command"), message: SwiftLocalizationHelper.localizedString(forKey: "Enter a new command and alias"), preferredStyle: .alert) alert.addTextField { $0.placeholder = SwiftLocalizationHelper.localizedString(forKey:"Command") } alert.addTextField { $0.placeholder = SwiftLocalizationHelper.localizedString(forKey: "Alias (optional)") } @@ -187,11 +188,18 @@ import UIKit let keyboardCmdString = alert.textFields?[0].text ?? "" let alias = alert.textFields?[1].text ?? keyboardCmdString let newCommand = RemoteCommand(keyboardCmdString: keyboardCmdString, alias: alias) - CommandManager.shared.addCommand(newCommand) + let addCommandSuceeded = CommandManager.shared.addCommand(newCommand) //self.reloadTableView() // don't know why but this reload has to be called from the CommandManager, it doesn't work here. - let lastRow = self.tableView.numberOfRows(inSection: 0) - 1 //for now there's only 1 section for the tableview, just use setion 0 - let newEntryIndexPath = IndexPath(row: lastRow, section: 0) - self.tableView.selectRow(at: newEntryIndexPath, animated: true, scrollPosition: .middle) // shift the highlight to the newly added entry + + //if previouslySelectedIndexPath == nil { return } + if addCommandSuceeded { + let lastRow = self.tableView.numberOfRows(inSection: 0) - 1 //for now there's only 1 section for the tableview, just use setion 0 + let newEntryIndexPath = IndexPath(row: lastRow, section: 0) + self.tableView.selectRow(at: newEntryIndexPath, animated: true, scrollPosition: .middle) // shift the highlight to the newly added entry + } + else { + self.tableView.selectRow(at: previouslySelectedIndexPath, animated: true, scrollPosition: .middle) // keep the highlight on the previous entry if failed to add command + } } let cancelAction = UIAlertAction(title: SwiftLocalizationHelper.localizedString(forKey:"Cancel"), style: .cancel)