In this example, we'll use a Fragment to set up the SDK configuration. Add a new Fragment class named ConfigFragment.kt
to handle the configuration process.
Copy package com.passioai.quickstart.ui.config
import ai.passio.passiosdk.core.config.PassioConfiguration
import ai.passio.passiosdk.core.config.PassioMode
import ai.passio.passiosdk.passiofood.PassioSDK
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.passioai.quickstart.BuildConfig
import com.passioai.quickstart.databinding.FragmentConfigBinding
class ConfigFragment : Fragment () {
private lateinit var _binding: FragmentConfigBinding
private val binding get () = _binding
override fun onCreateView (
inflater: LayoutInflater , container: ViewGroup ?,
savedInstanceState: Bundle ?
): View {
_binding = FragmentConfigBinding. inflate (inflater, container, false )
return _binding.root
}
override fun onViewCreated (view: View , savedInstanceState: Bundle ?) {
super . onViewCreated (view, savedInstanceState)
configureSDK ()
}
private fun configureSDK () {
val passioConfiguration = PassioConfiguration (
requireActivity ().applicationContext,
BuildConfig.SDK_KEY
). apply {
sdkDownloadsModels = true
debugMode = 0
remoteOnly = true
}
PassioSDK.instance. configure (passioConfiguration) { passioStatus ->
when (passioStatus.mode) {
PassioMode.NOT_READY -> onSDKError ( "Not ready" )
PassioMode.FAILED_TO_CONFIGURE -> onSDKError ( getString (passioStatus.error !! .errorRes))
PassioMode.IS_READY_FOR_DETECTION -> onSDKReadyToUse ()
PassioMode.IS_BEING_CONFIGURED -> {
binding.tvSDKStatus.text = "Configuring..."
}
PassioMode.IS_DOWNLOADING_MODELS -> {
binding.tvSDKStatus.text = "Downloading models..."
}
}
}
}
private fun onSDKError (message: String ) {
binding.tvSDKStatus.text = message
}
private fun onSDKReadyToUse () {
binding.tvSDKStatus.text = "Configured and ready to use"
findNavController (). navigate (ConfigFragmentDirections. configToImageRecognitions ())
}
}
Copy <? xml version = "1.0" encoding = "utf-8" ?>
< androidx.constraintlayout.widget.ConstraintLayout xmlns : android = "http://schemas.android.com/apk/res/android"
xmlns : tools = "http://schemas.android.com/tools"
android : layout_width = "match_parent"
android : layout_height = "match_parent"
xmlns : app = "http://schemas.android.com/apk/res-auto"
tools : context = ".ui.config.ConfigFragment" >
< androidx.appcompat.widget.AppCompatTextView
android : id = "@+id/tvSDKStatus"
android : layout_width = "wrap_content"
android : layout_height = "wrap_content"
app : layout_constraintBottom_toBottomOf = "parent"
app : layout_constraintEnd_toEndOf = "parent"
app : layout_constraintStart_toStartOf = "parent"
app : layout_constraintTop_toTopOf = "parent"
tools : text = "Hello World!" />
</ androidx.constraintlayout.widget.ConstraintLayout >
The SDK setup can fail due to configuration or network issues. The onSDKError
function in the ConfigFragment
updates the UI with the relevant error message to help troubleshoot.