Loading
LoadingConnector( searcher: SingleIndexSearcher, interactor: LoadingInteractor, controller: LoadingController )
About this widget
Components that show a loading indicator during pending requests.
Examples
Instantiate a LoadingConnector
.
1
2
3
4
5
6
7
8
9
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
|
type: Searcher
Required
The |
interactor
|
type: LoadingInteractor
default: .init()
Optional
The business logic that handles showing a loading indicator. |
controller
|
type: LoadingController
default: nil
Optional
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
: TheSearcher
that handles your searches.LoadingInteractor
: The logic that handles showing a loading indicatorLoadingController
: The controller that interfaces with a concrete loading indicator.
1
2
3
4
5
6
7
8
9
10
11
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, e.g., 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
func setItem(_ item: Bool)
Function called when a new state of the loading indicator is set.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.