From 78c9d182fcc44666bc0a8b4c4d3c7f5ada92e0f1 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Tue, 5 Oct 2021 22:29:18 +0200 Subject: [PATCH 1/8] Improvements to SearchTableViewCell.swift --- .../UI/SearchTableViewCell.swift | 56 ++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/WordPressAuthenticator/UI/SearchTableViewCell.swift b/WordPressAuthenticator/UI/SearchTableViewCell.swift index e63347123..b362bb9de 100644 --- a/WordPressAuthenticator/UI/SearchTableViewCell.swift +++ b/WordPressAuthenticator/UI/SearchTableViewCell.swift @@ -23,6 +23,14 @@ open class SearchTableViewCell: UITableViewCell { /// open weak var delegate: SearchTableViewCellDelegate? + /// If `true` the search delegate callback is called as the text field is edited. + /// This class does not implement any Debouncer or assume a minimum character count because + /// each search is different. + /// + open var liveSearch: Bool = false + + open var allowSpaces: Bool = true + /// Search UITextField's placeholder /// open var placeholder: String? { @@ -62,16 +70,60 @@ private extension SearchTableViewCell { // extension SearchTableViewCell: UITextFieldDelegate { open func textFieldShouldClear(_ textField: UITextField) -> Bool { - delegate?.startSearch(for: "") + if !liveSearch { + delegate?.startSearch(for: "") + } + return true } open func textFieldShouldReturn(_ textField: UITextField) -> Bool { - if let searchText = textField.text { + if !liveSearch, + let searchText = textField.text { delegate?.startSearch(for: searchText) } + return false } + + open func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { + let sanitizedString: String + + if allowSpaces { + sanitizedString = string + } else { + sanitizedString = string.trimmingCharacters(in: .whitespacesAndNewlines) + } + + if sanitizedString.count > 0 || range.length > 0 { + guard let currentText = (textField.text as? NSString) else { + // This shouldn't really happen but if it does, let's at least let the edit go through + return true + } + + textField.text = currentText.replacingCharacters(in: range, with: sanitizedString) + } + + startLiveSearchAfterEdit() + return false + } + + /// Convenience method to abstract the logic that tells the delegate to start a live search after the search text field + /// has been edited. + /// + private func startLiveSearchAfterEdit() { + guard liveSearch, + let delegate = delegate, + let text = textField.text else { + return + } + + if text.count == 0 { + delegate.startSearch(for: "") + } else { + delegate.startSearch(for: text) + } + } } // MARK: - Loader From e70b71519b1274f1d52b4a6a90c6920f04cf59fd Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Tue, 5 Oct 2021 22:35:42 +0200 Subject: [PATCH 2/8] The live search doesn't trigger if there aren't valid edits. --- WordPressAuthenticator/UI/SearchTableViewCell.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/WordPressAuthenticator/UI/SearchTableViewCell.swift b/WordPressAuthenticator/UI/SearchTableViewCell.swift index b362bb9de..b6d91ff30 100644 --- a/WordPressAuthenticator/UI/SearchTableViewCell.swift +++ b/WordPressAuthenticator/UI/SearchTableViewCell.swift @@ -95,16 +95,18 @@ extension SearchTableViewCell: UITextFieldDelegate { sanitizedString = string.trimmingCharacters(in: .whitespacesAndNewlines) } - if sanitizedString.count > 0 || range.length > 0 { + let hasValidEdits = sanitizedString.count > 0 || range.length > 0 + + if hasValidEdits { guard let currentText = (textField.text as? NSString) else { // This shouldn't really happen but if it does, let's at least let the edit go through return true } textField.text = currentText.replacingCharacters(in: range, with: sanitizedString) + startLiveSearchAfterEdit() } - startLiveSearchAfterEdit() return false } From ff18f5fe3c9766168a31e5c6117c47880dff0de6 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Tue, 5 Oct 2021 22:47:59 +0200 Subject: [PATCH 3/8] Changes in SearchTableViewCell so that the selection isn't lost while typing. --- WordPressAuthenticator/UI/SearchTableViewCell.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/WordPressAuthenticator/UI/SearchTableViewCell.swift b/WordPressAuthenticator/UI/SearchTableViewCell.swift index b6d91ff30..c3e536b17 100644 --- a/WordPressAuthenticator/UI/SearchTableViewCell.swift +++ b/WordPressAuthenticator/UI/SearchTableViewCell.swift @@ -98,12 +98,16 @@ extension SearchTableViewCell: UITextFieldDelegate { let hasValidEdits = sanitizedString.count > 0 || range.length > 0 if hasValidEdits { - guard let currentText = (textField.text as? NSString) else { + guard let currentText = (textField.text as? NSString), + let start = textField.position(from: textField.beginningOfDocument, offset: range.location), + let end = textField.position(from: start, offset: range.length), + let textRange = textField.textRange(from: start, to: end) else { + // This shouldn't really happen but if it does, let's at least let the edit go through return true } - textField.text = currentText.replacingCharacters(in: range, with: sanitizedString) + textField.replace(textRange, withText: sanitizedString) startLiveSearchAfterEdit() } From 161682387aea50251424cb5721aca3d01220600d Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Tue, 5 Oct 2021 23:48:39 +0200 Subject: [PATCH 4/8] Adds documentation to allowSpaces. --- WordPressAuthenticator/UI/SearchTableViewCell.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/WordPressAuthenticator/UI/SearchTableViewCell.swift b/WordPressAuthenticator/UI/SearchTableViewCell.swift index c3e536b17..38c945ee3 100644 --- a/WordPressAuthenticator/UI/SearchTableViewCell.swift +++ b/WordPressAuthenticator/UI/SearchTableViewCell.swift @@ -29,6 +29,9 @@ open class SearchTableViewCell: UITableViewCell { /// open var liveSearch: Bool = false + /// If `true` then the user can type in spaces regularly. If `false` the whitespaces will be + /// stripped before they're entered into the field. + /// open var allowSpaces: Bool = true /// Search UITextField's placeholder From c7bd3c9e289e7364485484577790b1bbdc2efa74 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Wed, 6 Oct 2021 09:28:30 +0200 Subject: [PATCH 5/8] Removes an unused variable. --- WordPressAuthenticator/UI/SearchTableViewCell.swift | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/WordPressAuthenticator/UI/SearchTableViewCell.swift b/WordPressAuthenticator/UI/SearchTableViewCell.swift index 38c945ee3..313475073 100644 --- a/WordPressAuthenticator/UI/SearchTableViewCell.swift +++ b/WordPressAuthenticator/UI/SearchTableViewCell.swift @@ -101,8 +101,7 @@ extension SearchTableViewCell: UITextFieldDelegate { let hasValidEdits = sanitizedString.count > 0 || range.length > 0 if hasValidEdits { - guard let currentText = (textField.text as? NSString), - let start = textField.position(from: textField.beginningOfDocument, offset: range.location), + guard let start = textField.position(from: textField.beginningOfDocument, offset: range.location), let end = textField.position(from: start, offset: range.length), let textRange = textField.textRange(from: start, to: end) else { From 4ab58c9b289f26d0b8dee389acfd62e1d472e350 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Wed, 6 Oct 2021 10:54:41 +0200 Subject: [PATCH 6/8] Generalizes startLiveSearchAfterEdit() and renames it to startLiveSearch(). --- .../UI/SearchTableViewCell.swift | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/WordPressAuthenticator/UI/SearchTableViewCell.swift b/WordPressAuthenticator/UI/SearchTableViewCell.swift index 313475073..a02d53f4b 100644 --- a/WordPressAuthenticator/UI/SearchTableViewCell.swift +++ b/WordPressAuthenticator/UI/SearchTableViewCell.swift @@ -110,18 +110,21 @@ extension SearchTableViewCell: UITextFieldDelegate { } textField.replace(textRange, withText: sanitizedString) - startLiveSearchAfterEdit() + + if liveSearch { + startLiveSearchAfterEdit() + } } return false } - /// Convenience method to abstract the logic that tells the delegate to start a live search after the search text field - /// has been edited. + /// Convenience method to abstract the logic that tells the delegate to start a live search. + /// + /// - Precondition: make sure you check if `liveSearch` is enabled before calling this method. /// - private func startLiveSearchAfterEdit() { - guard liveSearch, - let delegate = delegate, + private func startLiveSearch() { + guard let delegate = delegate, let text = textField.text else { return } From a8c34c323cfb9eae5e2e56b67e8539edbeddc29f Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Wed, 6 Oct 2021 10:57:04 +0200 Subject: [PATCH 7/8] Fixes a method call with a typo. --- WordPressAuthenticator/UI/SearchTableViewCell.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPressAuthenticator/UI/SearchTableViewCell.swift b/WordPressAuthenticator/UI/SearchTableViewCell.swift index a02d53f4b..bfb1c557a 100644 --- a/WordPressAuthenticator/UI/SearchTableViewCell.swift +++ b/WordPressAuthenticator/UI/SearchTableViewCell.swift @@ -112,7 +112,7 @@ extension SearchTableViewCell: UITextFieldDelegate { textField.replace(textRange, withText: sanitizedString) if liveSearch { - startLiveSearchAfterEdit() + startLiveSearch() } } From f3a241034072f7f100c74768de857e0e9b8b22d8 Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Wed, 6 Oct 2021 15:44:47 +0200 Subject: [PATCH 8/8] 1.42.1-beta.2 --- WordPressAuthenticator.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPressAuthenticator.podspec b/WordPressAuthenticator.podspec index 7bd14f806..28e420c0c 100644 --- a/WordPressAuthenticator.podspec +++ b/WordPressAuthenticator.podspec @@ -2,7 +2,7 @@ Pod::Spec.new do |s| s.name = 'WordPressAuthenticator' - s.version = '1.42.1-beta.1' + s.version = '1.42.1-beta.2' s.summary = 'WordPressAuthenticator implements an easy and elegant way to authenticate your WordPress Apps.' s.description = <<-DESC