From 84b198ed29efaad80930914cc323a09a78288dac Mon Sep 17 00:00:00 2001 From: True Zhuanjia Date: Wed, 24 Jul 2024 18:56:41 +0800 Subject: [PATCH] implement regular expression check --- Limelight/Input/CommandManager.swift | 22 +++++++++++++++++-- .../CommandManagerViewController.swift | 14 ++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Limelight/Input/CommandManager.swift b/Limelight/Input/CommandManager.swift index 1e60ad4b..2f6a0396 100644 --- a/Limelight/Input/CommandManager.swift +++ b/Limelight/Input/CommandManager.swift @@ -60,25 +60,43 @@ import UIKit return keyMappings } + // extractKeyStrings from keyboardCMDString @objc public func extractKeyStrings(from input: String) -> [String]? { let keys = keyMappings.keys.joined(separator: "|") - let pattern = "^((?:\(keys))(?:\\+\(keys))*)$" + let pattern = "^(?:(\(keys))(?:\\+(\(keys))*)*)$" guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { + print("Failed to create regex") return nil } let range = NSRange(location: 0, length: input.utf16.count) guard let match = regex.firstMatch(in: input, options: [], range: range) else { + print("No match found for input: \(input)") return nil } - let matchedString = (input as NSString).substring(with: match.range(at: 1)) + print("Regex matched for input: \(input)") + + let matchedString = (input as NSString).substring(with: match.range(at: 0)) let keyStrings = matchedString.split(separator: "+").map { String($0) } + + guard !keyStrings.isEmpty else { + print("No key strings found in the matched string") + return nil + } + + for (index, key) in keyStrings.enumerated() { + print("Key \(index): \(key)") + } + return keyStrings } @objc public func addCommand(_ command: RemoteCommand) { + command.keyboardCmdString = command.keyboardCmdString.uppercased() // convert all letters to upper case + let keyStrings = extractKeyStrings(from: command.keyboardCmdString) + if (keyStrings == nil) {return} // in case of non-keyboard command strings, return 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. diff --git a/Limelight/ViewControllers/CommandManagerViewController.swift b/Limelight/ViewControllers/CommandManagerViewController.swift index e12eba11..1b367e5a 100644 --- a/Limelight/ViewControllers/CommandManagerViewController.swift +++ b/Limelight/ViewControllers/CommandManagerViewController.swift @@ -146,12 +146,9 @@ import UIKit deleteButton.isEnabled = isEditingMode if(isEditingMode){ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Done"), for: .normal) } else{ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Edit"), for: .normal) } - } @objc private func addButtonTapped() { - //CommandManager.shared.promptForNewCommand() - let alert = UIAlertController(title: "New Command", message: "Enter a new command and alias", preferredStyle: .alert) alert.addTextField { $0.placeholder = "Command" } alert.addTextField { $0.placeholder = "Alias" } @@ -195,7 +192,16 @@ import UIKit } @objc public func reloadTableView() { + let previouslySelectedIndexPath = tableView.indexPathForSelectedRow + tableView.reloadData() + + if let indexPath = previouslySelectedIndexPath { + // Make sure the indexPath is still valid and scroll to the selected indexPath + if indexPath.row < tableView.numberOfRows(inSection: indexPath.section) { + tableView.scrollToRow(at: indexPath, at: .middle, animated: true) + } + } } // UITableViewDataSource @@ -239,7 +245,7 @@ import UIKit if !isEditingMode { // Call the dummy method for key-value sending implementation dummyMethodForKeySending() - dismiss(animated: false, completion: nil) + dismiss(animated: false, completion: nil) // dimiss the view in sending mode } }