3

I am using Algolia InstantSearch and the HitsTableViewController is displaying the list of data properly, however the results are not changing with each keystroke. Does anyone know how to make the results change with each letter that's typed into the search bar? I've done everything the InstantSearch documentation has written out.

Here's the code

import UIKit
import InstantSearch

struct BestBuyItem: Codable {
  let name: String
}

struct BestBuyTableViewCellConfigurator: TableViewCellConfigurable {
   
  let model: BestBuyItem
  
  init(model: BestBuyItem, indexPath: IndexPath) {
    self.model = model
  }
  
  func configure(_ cell: UITableViewCell) {
    cell.textLabel?.text = model.name
  }

}

typealias BestBuyHitsViewController = HitsTableViewController<BestBuyTableViewCellConfigurator>

class ViewController: UIViewController {
      
  let searcher = SingleIndexSearcher(appID: "latency",
                                     apiKey: "1f6fd3a6fb973cb08419fe7d288fa4db",
                                     indexName: "bestbuy")
  lazy var searchController: UISearchController = .init(searchResultsController: hitsViewController)
  lazy var searchConnector: SingleIndexSearchConnector<BestBuyItem> = .init(searcher: searcher,
                                                                            searchController: searchController,
                                                                            hitsController: hitsViewController,
                                                                            filterState: filterState)
  let hitsViewController: BestBuyHitsViewController = .init()
  let statsInteractor: StatsInteractor = .init()
  let filterState: FilterState = .init()
  lazy var categoryConnector: FacetListConnector = .init(searcher: searcher,
                                                         filterState: filterState,
                                                         attribute: "category",
                                                         operator: .or,
                                                         controller: categoryListController,
                                                         presenter: FacetListPresenter(sortBy: [.isRefined]))
  
  lazy var categoryListController: FacetListTableController = .init(tableView: categoryTableViewController.tableView)
  let categoryTableViewController: UITableViewController = .init()
  
  override func viewDidLoad() {
    super.viewDidLoad()
    searchConnector.connect()
    categoryConnector.connect()
    statsInteractor.connectSearcher(searcher)
    statsInteractor.connectController(self)
    searcher.search()
    
    setupUI()
  }
  
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    searchController.searchBar.becomeFirstResponder()
  }
  
  func setupUI() {
    view.backgroundColor = .white
    navigationItem.searchController = searchController
    navigationItem.rightBarButtonItem = .init(title: "Category", style: .plain, target: self, action: #selector(showFilters))
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.showsSearchResultsController = true
    searchController.automaticallyShowsCancelButton = false
    categoryTableViewController.title = "Category"
  }
  
  @objc func showFilters() {
    let navigationController = UINavigationController(rootViewController: categoryTableViewController)
    categoryTableViewController.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissFilters))
    present(navigationController, animated: true, completion: .none)
  }
  
  @objc func dismissFilters() {
    categoryTableViewController.navigationController?.dismiss(animated: true, completion: .none)
  }
  
}

extension ViewController: StatsTextController {
  
  func setItem(_ item: String?) {
    title = item
  }
  
}

I really appreciate anyone that helps, thanks

Adam P
  • 31
  • 2

0 Answers0