Getting Started with Scenario Player
This page will help you set a scenario player up with Inarix SDK.
What's the Inarix Scenario Player?
This component of the Inarix SDK enables you to evaluate quality criteria for your crops through customizable scenarios.
Initializing the player
In this section, we assume you've already completed the "How to Authenticate?" page. We'll continue from the previous examples, focusing on the implementation of the ContentView.
We'll begin by implementing a new function called setupPlayer, which initializes the standalone ScenarioPlayer component. This component manages the complexities of running a scenario within your application, ensuring it operates independently from the UI thread.
func setupPlayer() {
self.player = InarixSDK.ScenarioPlayer()
Task { @MainActor in
do {
try await self.player.loadScenario(ids: [784])
} catch let err {
debugPrint("Couldn't load scenario: \(err)")
return
}
do {
try await self.player.start()
self.ready = true
} catch let err {
debugPrint("Couldn't start scenario player: \(err)")
return
}
}
}self.player is a member of the ContentView's ViewModel. The first step is to load a given scenario, and for this example, we've specified an arbitrary value of 784. Finally, we try to start the scenario player and we define the ContentView as "ready".
You should have received a list of scenario ids or a scenario id during your onboarding.
We will be using a boolean to define if we're displaying the scenario player in the ContentView or not. You'll find below the complete code sample of the ViewModel.
import Foundation
import InarixFramework
extension ContentView {
@Observable
class ViewModel {
private(set) var displayPlayer = false
private(set) var ready = false
private(set) var player: InarixSDK.ScenarioPlayer!
func setDisplayPlayer(value: Bool) {
self.displayPlayer = value
}
func setupPlayer() {
self.player = InarixSDK.ScenarioPlayer()
Task { @MainActor in
do {
try await self.player.loadScenario(id: 784)
} catch let err {
debugPrint("Couldn't load scenario: \(err)")
return
}
do {
try await self.player.start()
self.ready = true
} catch let err {
debugPrint("Couldn't start scenario player: \(err)")
return
}
}
}
}
}Displaying the player
Now that we have defined a ViewModel for our ContentView, let's finished this up by displaying the ScenarioPlayerView when displayPlayer is set to true and when we're ready.
import InarixFramework
import SwiftUI
struct ContentView: View {
@State private var viewModel = ViewModel()
var body: some View {
if viewModel.ready && viewModel.displayPlayer {
InarixSDK.ScenarioPlayerView(player: viewModel.player)
} else {
VStack {
Button("Launch player") {
viewModel.setDisplayPlayer(value: true)
}
.disabled(!viewModel.ready)
}
.padding()
.onAppear {
viewModel.setupPlayer()
}
}
}
}
#Preview {
ContentView()
}
You can now run any given scenario and perform quality assessment on your crops! 🧑🌾
Updated about 2 months ago
Now that we can run a scenario and perform some assessments, when do we exit the scenario player? 🚪
