Skip to content

Commit

Permalink
Fix small bug where searching for users would pull old results
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrorseth committed Aug 6, 2017
1 parent 02442df commit 972f2be
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 57 deletions.
7 changes: 5 additions & 2 deletions AtMe/AuthController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,9 @@ class AuthController {
*/
public func searchForUsers(term: String, completion: @escaping ([String : String]) -> ()) {

registeredUsernamesRef.queryOrderedByKey().queryStarting(atValue: term).queryEnding(atValue: "\(term)\u{f8ff}")
.queryLimited(toFirst: Constants.Limits.resultsCount).observeSingleEvent(of: DataEventType.value, with: { snapshot in
var handle: UInt = 0
handle = registeredUsernamesRef.queryOrderedByKey().queryStarting(atValue: term).queryEnding(atValue: "\(term)\u{f8ff}")
.queryLimited(toFirst: Constants.Limits.resultsCount).observe(DataEventType.value, with: { snapshot in

// Parse results as dictionary of (username, uid) pairs
if var results = snapshot.value as? [String : String] {
Expand All @@ -243,6 +244,8 @@ class AuthController {
// If and when found, pass results back to caller
completion(results)
}

self.registeredUsernamesRef.removeObserver(withHandle: handle)
})
}

Expand Down
113 changes: 58 additions & 55 deletions AtMe/NewConvoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import UIKit
import Firebase

class NewConvoViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, AlertController {
class NewConvoViewController: UIViewController, UISearchBarDelegate, AlertController {

lazy var authManager = AuthController()
lazy var databaseManager = DatabaseController()
Expand All @@ -26,7 +26,7 @@ class NewConvoViewController: UIViewController, UITableViewDataSource, UITableVi
}


// MARK: View
// MARK: - View
/** Overridden method called after view controller's view is loaded into memory. */
override func viewDidLoad() {

Expand All @@ -48,9 +48,63 @@ class NewConvoViewController: UIViewController, UITableViewDataSource, UITableVi
dismissKeyboardTap.cancelsTouchesInView = false
self.view.addGestureRecognizer(dismissKeyboardTap)
}


// MARK: - Search Bar + Results
/** Delegate method which fires when the Search button on the specified UISearchBar was tapped. */
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

clearResults()
guard let text = searchBar.text else { return }

// Find all usernames containing search text
// Order record by key (which are the usernames), then query for string bounded in range [text, text]


authManager.searchForUsers(term: text, completion: { results in

if !results.isEmpty {

// One by one, obtain details of each user, and insert the result into table with more info
self.authManager.findDetailsForUsers(results: results, completion: { user in
self.insertResult(user: user)
})
}
})
}


/** Clears the table view (results) and associated data source. */
private func clearResults() {

searchResults.removeAll()
usersTableView.reloadData()
}


/** Inserts a given UserProfile into the table view, done efficiently by only inserting what it needs to.
- parameters:
user: The UserProfile object of the user requested for insertion
*/
private func insertResult(user: UserProfile) {

// Update data source, then insert row
searchResults.append(user)
usersTableView.insertRows(at: [IndexPath(row: searchResults.count - 1, section: 0)], with: .none)
}


// MARK: Additional Functions
/** Dismiss the keyboard from screen if currently displayed. */
func dismissKeyboard() {
self.view.endEditing(true)
}
}


// MARK: - Table View
extension NewConvoViewController: UITableViewDataSource, UITableViewDelegate {

// MARK: Table View
/** Sets the number of sections to display in the table view. */
func numberOfSections(in tableView: UITableView) -> Int {
return 1
Expand Down Expand Up @@ -116,63 +170,12 @@ class NewConvoViewController: UIViewController, UITableViewDataSource, UITableVi
// If successful, exit. If unsuccessful, present alert so user is informed of preexisting conversation
if (success) {
self.navigationController?.popViewController(animated: true)

} else {
self.presentSimpleAlert(title: "Conversation already exists", message: Constants.Errors.conversationAlreadyExists, completion: nil)
}
})

} else { presentSimpleAlert(title: "Error creating conversation", message: Constants.Errors.createConversationError, completion: nil) }
}


// MARK: Search Bar + Results
/** Delegate method which fires when the Search button on the specified UISearchBar was tapped. */
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

clearResults()
guard let text = searchBar.text else { return }

// Find all usernames containing search text
// Order record by key (which are the usernames), then query for string bounded in range [text, text]


authManager.searchForUsers(term: text, completion: { results in

if !results.isEmpty {

// One by one, obtain details of each user, and insert the result into table with more info
self.authManager.findDetailsForUsers(results: results, completion: { user in
self.insertResult(user: user)
})
}
})
}


/** Clears the table view (results) and associated data source. */
private func clearResults() {

searchResults.removeAll()
usersTableView.reloadData()
}


/** Inserts a given UserProfile into the table view, done efficiently by only inserting what it needs to.
- parameters:
user: The UserProfile object of the user requested for insertion
*/
private func insertResult(user: UserProfile) {

// Update data source, then insert row
searchResults.append(user)
usersTableView.insertRows(at: [IndexPath(row: searchResults.count - 1, section: 0)], with: .none)
}


// MARK: Additional Functions
/** Dismiss the keyboard from screen if currently displayed. */
func dismissKeyboard() {
self.view.endEditing(true)
}
}

0 comments on commit 972f2be

Please sign in to comment.