Skip to content

Commit

Permalink
search bar and filtered results
Browse files Browse the repository at this point in the history
  • Loading branch information
grenos committed Mar 29, 2020
1 parent 0b2582a commit c895c85
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
Binary file not shown.
60 changes: 56 additions & 4 deletions GHFollowers/Screens/FollowerListVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class FollowerListVC: UIViewController {
var collectionView: UICollectionView!

var followers: [Follower] = []
var filteredFollowers: [Follower] = []

var page = 1
var hasMoreFollowers = true

Expand All @@ -32,6 +34,7 @@ class FollowerListVC: UIViewController {
super.viewDidLoad()

configureViewController()
configureSearchController()
configureCollectionView()
getFollowers(username: username, page: page)
configureDataSource()
Expand Down Expand Up @@ -70,7 +73,6 @@ class FollowerListVC: UIViewController {
collectionView.register(FollowerCell.self, forCellWithReuseIdentifier: FollowerCell.reuseID)
}


// called in init
func configureDataSource() {
dataSource = UICollectionViewDiffableDataSource<Section, Follower>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, follower) -> UICollectionViewCell? in
Expand All @@ -83,7 +85,7 @@ class FollowerListVC: UIViewController {
}

// called every time a new response arrives from the server
func updateData() {
func updateData(on followers: [Follower]) {
// declare snapshot
var snapshot = NSDiffableDataSourceSnapshot<Section, Follower>()
// tell what section it needs to wathc
Expand All @@ -96,6 +98,25 @@ class FollowerListVC: UIViewController {




func configureSearchController() {
let searchController = UISearchController()

// set the delegate for searchResultsUpdater
searchController.searchResultsUpdater = self
// set the delegate to listen to the search bar events (needed for the cancel button)
searchController.searchBar.delegate = self
searchController.searchBar.placeholder = "Search followers"
// set overlay to false so we can tap on a user
searchController.obscuresBackgroundDuringPresentation = false

// set the searchController we created as a nav item (so we cam see it in the bav)
navigationItem.searchController = searchController

}



// MARK: API call
func getFollowers(username: String, page: Int) {
// api call for followers
Expand Down Expand Up @@ -131,8 +152,10 @@ class FollowerListVC: UIViewController {
return
}

// call to update snapshot
self.updateData()
// call to update snapshot with the followers array
self.updateData(on: self.followers)



case .failure(let error):
self.presentGFAlertOnMainThread(title: "Bad Stuff Happend", message: error.rawValue, buttonTitle: "Ok")
Expand Down Expand Up @@ -171,3 +194,32 @@ extension FollowerListVC: UICollectionViewDelegate {

}
}


// MARK: Filter arrays
extension FollowerListVC: UISearchResultsUpdating, UISearchBarDelegate {

// this informs the VC every time the search results are changed
func updateSearchResults(for searchController: UISearchController) {


guard let filter = searchController.searchBar.text, !filter.isEmpty else {return}

// get each item of the followers array
// check if contains the text from the searchBar
// append the return to the filteredFollowers array
filteredFollowers = followers.filter {
$0.login.lowercased().contains(filter.lowercased())
}

// Update collection view with new results passing the new array
updateData(on: filteredFollowers)
}

// when cancel button is tapped update the collection View using the original follwers array
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
updateData(on: followers)
}


}

0 comments on commit c895c85

Please sign in to comment.