implement regular expression check

This commit is contained in:
True Zhuanjia committed 2024-07-24 18:56:41 +08:00
1 parent 4cff75c531
commit 84b198ed29
2 files changed
+30 -6

No files matched your search

+20 -2
View File
@@ -60,25 +60,43 @@ import UIKit
return keyMappings return keyMappings
} }
// extractKeyStrings from keyboardCMDString
@objc public func extractKeyStrings(from input: String) -> [String]? { @objc public func extractKeyStrings(from input: String) -> [String]? {
let keys = keyMappings.keys.joined(separator: "|") let keys = keyMappings.keys.joined(separator: "|")
let pattern = "^((?:\(keys))(?:\\+\(keys))*)$" let pattern = "^(?:(\(keys))(?:\\+(\(keys))*)*)$"
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
print("Failed to create regex")
return nil return nil
} }
let range = NSRange(location: 0, length: input.utf16.count) let range = NSRange(location: 0, length: input.utf16.count)
guard let match = regex.firstMatch(in: input, options: [], range: range) else { guard let match = regex.firstMatch(in: input, options: [], range: range) else {
print("No match found for input: \(input)")
return nil 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) } 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 return keyStrings
} }
@objc public func addCommand(_ command: RemoteCommand) { @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) commands.append(command)
saveCommands() 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. 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.
@@ -146,12 +146,9 @@ import UIKit
deleteButton.isEnabled = isEditingMode deleteButton.isEnabled = isEditingMode
if(isEditingMode){ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Done"), for: .normal) } if(isEditingMode){ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Done"), for: .normal) }
else{ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Edit"), for: .normal) } else{ editButton.setTitle(SwiftLocalizationHelper.localizedString(forKey: "Edit"), for: .normal) }
} }
@objc private func addButtonTapped() { @objc private func addButtonTapped() {
//CommandManager.shared.promptForNewCommand()
let alert = UIAlertController(title: "New Command", message: "Enter a new command and alias", preferredStyle: .alert) let alert = UIAlertController(title: "New Command", message: "Enter a new command and alias", preferredStyle: .alert)
alert.addTextField { $0.placeholder = "Command" } alert.addTextField { $0.placeholder = "Command" }
alert.addTextField { $0.placeholder = "Alias" } alert.addTextField { $0.placeholder = "Alias" }
@@ -195,7 +192,16 @@ import UIKit
} }
@objc public func reloadTableView() { @objc public func reloadTableView() {
let previouslySelectedIndexPath = tableView.indexPathForSelectedRow
tableView.reloadData() 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 // UITableViewDataSource
@@ -239,7 +245,7 @@ import UIKit
if !isEditingMode { if !isEditingMode {
// Call the dummy method for key-value sending implementation // Call the dummy method for key-value sending implementation
dummyMethodForKeySending() dummyMethodForKeySending()
dismiss(animated: false, completion: nil) dismiss(animated: false, completion: nil) // dimiss the view in sending mode
} }
} }