     // MARK: - Search bar
     
     func updateSearchResultsForSearchController(searchController: UISearchController) {
-        query.query = searchController.searchBar.text
-        let curSearchId = searchId
-        
-        movieIndex.search(query, completionHandler: { (data, error) -> Void in
-            if (curSearchId <= self.displayedSearchId) || (error != nil) {
-                return
-            }
-            
-            self.displayedSearchId = curSearchId
-            self.loadedPage = 0 // reset loaded page
-            self.loadingPage = 0 // reset the loading page
-            
-            guard let
-                hits = data?["hits"] as? [[String: AnyObject]],
-                nbPages = data?["nbPages"] as? UInt
-            else {
-                return
-            }
-            self.nbPages = nbPages
-            
-            var tmp = [MovieRecord]()
-            for hit in hits {
-                tmp.append(MovieRecord(json: hit))
-            }
-            
-            self.movies = tmp
-            self.tableView.reloadData()
-        })
-        
-        self.searchId += 1
+        movieSearcher.query.query = searchController.searchBar.text
+        movieSearcher.search()
     }
     
-    // MARK: - Load more
-    
-    func loadMore() {
-        if loadedPage + 1 >= nbPages {
-            return
-        }
-        // check if already loading this page so a redundent query isn't generated
-        if loadedPage + 1 == loadingPage {
-            return
-        }
-        loadingPage = loadedPage + 1
-        
-        let nextQuery = Query(copy: query)
-        nextQuery.page = loadedPage + 1
-        movieIndex.search(nextQuery, completionHandler: { (data , error) -> Void in
-            // Reject if query has changed
-            if (nextQuery.query != self.query.query) || (error != nil) {
-                return
-            }
-            
-            self.loadedPage = nextQuery.page!.unsignedIntegerValue
-            
-            guard let hits = data?["hits"] as? [[String: AnyObject]] else {
-                return
-            }
-            var tmp = [MovieRecord]()
-            for hit in hits {
-                tmp.append(MovieRecord(json: hit))
-            }
-            
-            self.movies.appendContentsOf(tmp)
-            self.tableView.reloadData()
-        })
-    }
-
 }

import UIKit
import AlgoliaSearch
import AFNetworking
import SwiftyJSON

class MoviesTableViewController: UITableViewController, UISearchBarDelegate, UISearchResultsUpdating {

    var searchController: UISearchController!
    
    var movieIndex: Index!
    let query = Query()
    
    var movies = [MovieRecord]()
    
    var searchId = 0
    var displayedSearchId = -1
    var loadedPage: UInt = 0
    var nbPages: UInt = 0
    var loadingPage: UInt = 0
    
    let placeholder = UIImage(named: "white")
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Search controller
        searchController = UISearchController(searchResultsController: nil)
        searchController.searchResultsUpdater = self

        tableView.tableHeaderView = self.searchController!.searchBar
        definesPresentationContext = true
        searchController!.searchBar.sizeToFit()
        
        // First load
        updateSearchResultsForSearchController(searchController)
