algolia / instantsearch-ios

⚡️ A library of widgets and helpers to build instant-search applications on iOS.
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/ios/
Apache License 2.0
598 stars 59 forks source link

SwiftUI: Setting automaticallyShowsCancelButton #277

Open ACPK opened 1 year ago

ACPK commented 1 year ago

I followed Algolia's SwiftUI instructions in creating the ContentView.swift file below. What is the recommended way to set automaticallyShowsCancelButton to False?

import SwiftUI
import InstantSearchSwiftUI
import InstantSearchCore

struct StockItem: Codable {
  let business: String
}

class AlgoliaController {
  let searcher: HitsSearcher

  let searchBoxInteractor: SearchBoxInteractor
  let searchBoxController: SearchBoxObservableController

  let hitsInteractor: HitsInteractor<StockItem>
  let hitsController: HitsObservableController<StockItem>

  init() {
    self.searcher = HitsSearcher(appID: "adfadfadfadf",
                                apiKey: "23232323",
                                indexName: "adfadfadf")
    self.searchBoxInteractor = .init()
    self.searchBoxController = .init()
    self.hitsInteractor = .init()
    self.hitsController = .init()
    self.searchBoxInteractor.automaticallyShowsCancelButton = false
    setupConnections()
  }

  func setupConnections() {
    searchBoxInteractor.connectSearcher(searcher)
    searchBoxInteractor.connectController(searchBoxController)
    hitsInteractor.connectSearcher(searcher)
    hitsInteractor.connectController(hitsController)
  }

}

struct ContentView: View {
    @ObservedObject var searchBoxController: SearchBoxObservableController
    @ObservedObject var hitsController: HitsObservableController<StockItem>

    @State private var isEditing = false

    var body: some View {
      VStack(spacing: 7) {
        SearchBar(text: $searchBoxController.query,
                  isEditing: $isEditing,
                  onSubmit: searchBoxController.submit)
        HitsList(hitsController) { (hit, _) in
          VStack(alignment: .leading, spacing: 10) {
            Text(hit?.business ?? "")
              .padding(.all, 10)
            Divider()
          }
        } noResults: {
          Text("No Results")
            .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
      }
      .navigationBarTitle("Algolia & SwiftUI")
    }

}

struct ContentView_Previews: PreviewProvider {

  static let algoliaController = AlgoliaController()

  static var previews: some View {
    NavigationView {
      ContentView(searchBoxController: algoliaController.searchBoxController,
                  hitsController: algoliaController.hitsController)
    }.onAppear {
      algoliaController.searcher.search()
    }
  }

}
winstoncex7 commented 1 year ago

After going through your code seems like there is no way with searchbar . You can use the textfield object and set searchboxcontroller to it. I do have an issue - how can i fetch all the filters for a particular searched word ?