diff --git a/.swiftlint.yml b/.swiftlint.yml index d8ee084..faff282 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -32,7 +32,7 @@ whitelist_rules: - control_statement - convenience_type - custom_rules -# - cyclomatic_complexity + - cyclomatic_complexity - deployment_target - discarded_notification_center_observer - discouraged_direct_init diff --git a/pass/Controllers/PasswordEditorTableViewController.swift b/pass/Controllers/PasswordEditorTableViewController.swift index 842575b..33a21e2 100644 --- a/pass/Controllers/PasswordEditorTableViewController.swift +++ b/pass/Controllers/PasswordEditorTableViewController.swift @@ -137,6 +137,7 @@ class PasswordEditorTableViewController: UITableViewController { additionsCell?.contentTextView.setContentOffset(.zero, animated: false) } + // swiftlint:disable:next cyclomatic_complexity override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cellData = tableData[indexPath.section][indexPath.row] diff --git a/passExtension/Controllers/ExtensionViewController.swift b/passExtension/Controllers/ExtensionViewController.swift index 2114030..eb1bff9 100644 --- a/passExtension/Controllers/ExtensionViewController.swift +++ b/passExtension/Controllers/ExtensionViewController.swift @@ -63,63 +63,50 @@ class ExtensionViewController: UIViewController, UITableViewDataSource, UITableV } for extensionItem in extensionItems { - if let itemProviders = extensionItem.attachments { - for provider in itemProviders { - // search using the extensionContext inputs - if provider.hasItemConformingToTypeIdentifier(OnePasswordExtensionActions.findLogin) { - provider.loadItem(forTypeIdentifier: OnePasswordExtensionActions.findLogin, options: nil) { item, _ -> Void in - let dictionary = item as! NSDictionary - var url: String? - if var urlString = dictionary[OnePasswordExtensionKey.URLStringKey] as? String { - if !urlString.hasPrefix("http://"), !urlString.hasPrefix("https://") { - urlString = "http://" + urlString - } - url = URL(string: urlString)?.host - } - DispatchQueue.main.async { [weak self] in - self?.extensionAction = .findLogin - // force search (set text, set active, force search) - self?.searchBar.text = url - self?.searchBar.becomeFirstResponder() - self?.searchBarSearchButtonClicked((self?.searchBar)!) - } - } - } else if provider.hasItemConformingToTypeIdentifier(kUTTypePropertyList as String) { - provider.loadItem(forTypeIdentifier: kUTTypePropertyList as String, options: nil) { item, _ -> Void in - var url: String? - if let dictionary = item as? NSDictionary, - let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary, - var urlString = results[OnePasswordExtensionKey.URLStringKey] as? String { - if !urlString.hasPrefix("http://"), !urlString.hasPrefix("https://") { - urlString = "http://" + urlString - } - url = URL(string: urlString)?.host - } - DispatchQueue.main.async { [weak self] in - self?.extensionAction = .fillBrowser - // force search (set text, set active, force search) - self?.searchBar.text = url - self?.searchBar.becomeFirstResponder() - self?.searchBarSearchButtonClicked((self?.searchBar)!) - } - } - } else if provider.hasItemConformingToTypeIdentifier(kUTTypeURL as String) { - provider.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil) { item, _ -> Void in - let url = (item as? NSURL)!.host - DispatchQueue.main.async { [weak self] in - self?.extensionAction = .fillBrowser - // force search (set text, set active, force search) - self?.searchBar.text = url - self?.searchBar.becomeFirstResponder() - self?.searchBarSearchButtonClicked((self?.searchBar)!) - } + guard let itemProviders = extensionItem.attachments else { + continue + } + for provider in itemProviders { + // search using the extensionContext inputs + if provider.hasItemConformingToTypeIdentifier(OnePasswordExtensionActions.findLogin) { + provider.loadItem(forTypeIdentifier: OnePasswordExtensionActions.findLogin, options: nil) { item, _ in + self.updateExtension(with: self.getUrl(from: item as! NSDictionary), action: .findLogin) + } + } else if provider.hasItemConformingToTypeIdentifier(kUTTypePropertyList as String) { + provider.loadItem(forTypeIdentifier: kUTTypePropertyList as String, options: nil) { item, _ in + if let dictionary = item as? NSDictionary, let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary { + self.updateExtension(with: self.getUrl(from: results)) } } + } else if provider.hasItemConformingToTypeIdentifier(kUTTypeURL as String) { + provider.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil) { item, _ in + self.updateExtension(with: (item as? NSURL)!.host) + } } } } } + private func getUrl(from dictionary: NSDictionary) -> String? { + if var urlString = dictionary[OnePasswordExtensionKey.URLStringKey] as? String { + if !urlString.hasPrefix("http://"), !urlString.hasPrefix("https://") { + urlString = "http://" + urlString + } + return URL(string: urlString)?.host + } + return nil + } + + private func updateExtension(with url: String?, action: Action = .fillBrowser) { + // Set text, set active, and force search. + DispatchQueue.main.async { [weak self] in + self?.extensionAction = action + self?.searchBar.text = url + self?.searchBar.becomeFirstResponder() + self?.searchBarSearchButtonClicked((self?.searchBar)!) + } + } + // define cell contents, and set long press action func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath)