Skip to main content
Signature
LoadingConnector(
  searcher: SingleIndexSearcher,
  interactor: LoadingInteractor,
  controller: LoadingController
)

About this widget

Components that show a loading indicator during pending requests.

Examples

Instantiate a LoadingConnector.
Swift
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourSearchOnlyAPIKey",
                            indexName: "YourIndexName")
let activityIndicatorController: ActivityIndicatorController = .init(activityIndicator: UIActivityIndicatorView())
let loadingConnector: LoadingConnector = .init(searcher: searcher,
                                               controller: activityIndicatorController)

// Execute a search which will spin the loading indicator until the results arrive
searcher.search()

Parameters

searcher
Searcher
required
The Searcher that handles your searches.
interactor
LoadingInteractor
default: .init()
The business logic that handles showing a loading indicator.
controller
LoadingController
default: nil
Controller that interfaces with a concrete loading view.

Low-level API

If you want to fully control the loading indicator components and connect them manually, you can use these components. The loading indicator consists of the following components:
  • Searcher. The Searcher that handles your searches.
  • LoadingInteractor. The logic that handles showing a loading indicator
  • LoadingController. The controller that interfaces with a concrete loading indicator.
Swift
let searcher = HitsSearcher(appID: "YourApplicationID",
                            apiKey: "YourSearchOnlyAPIKey",
                            indexName: "YourIndexName")
let loadingInteractor: LoadingInteractor = .init()
let activityIndicatorController: ActivityIndicatorController = .init(activityIndicator: UIActivityIndicatorView())

loadingInteractor.connectSearcher(searcher)
loadingInteractor.connectController(activityIndicatorController)

// Execute a search which will spin the loading indicator until the results arrive
searcher.search()

Customizing your view

The default controllers, such as ActivityIndicatorController, work well when you want to use native UIKit with their default behavior like UIActivityIndicatorView. If you want to use another component such as a third-party view, or want to introduce some custom behavior to the already provided UIKit components, you can create your own controller conforming to the LoadingController protocol.

Protocol

Swift
func setItem(_ item: Bool)`
Function called when a new state of the loading indicator is set.

Example

Swift
public class ActivityIndicatorController: LoadingController {

  let activityIndicator: UIActivityIndicatorView

  public init (activityIndicator: UIActivityIndicatorView) {
    self.activityIndicator = activityIndicator
  }

  public func setItem(_ item: Bool) {
    item ? activityIndicator.startAnimating() : activityIndicator.stopAnimating()
  }

}

SwiftUI

InstantSearch provides the LoadingObservableController data model, which is an implementation of the LoadingController protocol adapted for usage with SwiftUI. LoadingObservableController must be connected to the LoadingConnector or LoadingConnector like any other LoadingController implementation. The example of the loading view using the ProgressView component provided by SwiftUI.
Swift
struct ContentView: View {

  @ObservedObject var loadingController: LoadingObservableController

  var body: some View {
    VStack {
      Text("Loading")
      if loadingController.isLoading {
        ProgressView()
      }
    }
  }

}
If you prefer to create a custom SwiftUI view that presents the loading indicator, you can directly use the LoadingObservableController as a data model. It provides the isLoading property to streamline the design process of your custom SwiftUI view.
I