Enable SwiftLint rule 'cyclomatic_complexity' and fix violation which is reasonable

This commit is contained in:
Danny Moesch 2020-09-19 17:15:49 +02:00 committed by Mingshen Sun
parent 8345bb89a4
commit 2cdd0e2521
3 changed files with 39 additions and 51 deletions

View file

@ -32,7 +32,7 @@ whitelist_rules:
- control_statement - control_statement
- convenience_type - convenience_type
- custom_rules - custom_rules
# - cyclomatic_complexity - cyclomatic_complexity
- deployment_target - deployment_target
- discarded_notification_center_observer - discarded_notification_center_observer
- discouraged_direct_init - discouraged_direct_init

View file

@ -137,6 +137,7 @@ class PasswordEditorTableViewController: UITableViewController {
additionsCell?.contentTextView.setContentOffset(.zero, animated: false) additionsCell?.contentTextView.setContentOffset(.zero, animated: false)
} }
// swiftlint:disable:next cyclomatic_complexity
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellData = tableData[indexPath.section][indexPath.row] let cellData = tableData[indexPath.section][indexPath.row]

View file

@ -63,63 +63,50 @@ class ExtensionViewController: UIViewController, UITableViewDataSource, UITableV
} }
for extensionItem in extensionItems { for extensionItem in extensionItems {
if let itemProviders = extensionItem.attachments { guard let itemProviders = extensionItem.attachments else {
for provider in itemProviders { continue
// search using the extensionContext inputs }
if provider.hasItemConformingToTypeIdentifier(OnePasswordExtensionActions.findLogin) { for provider in itemProviders {
provider.loadItem(forTypeIdentifier: OnePasswordExtensionActions.findLogin, options: nil) { item, _ -> Void in // search using the extensionContext inputs
let dictionary = item as! NSDictionary if provider.hasItemConformingToTypeIdentifier(OnePasswordExtensionActions.findLogin) {
var url: String? provider.loadItem(forTypeIdentifier: OnePasswordExtensionActions.findLogin, options: nil) { item, _ in
if var urlString = dictionary[OnePasswordExtensionKey.URLStringKey] as? String { self.updateExtension(with: self.getUrl(from: item as! NSDictionary), action: .findLogin)
if !urlString.hasPrefix("http://"), !urlString.hasPrefix("https://") { }
urlString = "http://" + urlString } else if provider.hasItemConformingToTypeIdentifier(kUTTypePropertyList as String) {
} provider.loadItem(forTypeIdentifier: kUTTypePropertyList as String, options: nil) { item, _ in
url = URL(string: urlString)?.host if let dictionary = item as? NSDictionary, let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary {
} self.updateExtension(with: self.getUrl(from: results))
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)!)
}
} }
} }
} 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 // define cell contents, and set long press action
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath) let cell = tableView.dequeueReusableCell(withIdentifier: "passwordTableViewCell", for: indexPath)