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 arrivesearcher.search()
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 arrivesearcher.search()
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.
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.