StateFlow for SDK-driven UI updatesusers/ directory in Android Studio. Before running, replace the APPLICATION_ID in MainActivity.kt with your app ID from the Meta Developer Portal. Connect your Quest device via USB, build the project, and deploy to the device. For complete setup instructions including SDK installation and developer mode configuration, see the Platform SDK setup guide.| File | What it demonstrates | Key concepts |
|---|---|---|
MainActivity.kt | SDK initialization and Compose UI | HorizonServiceConnection.connect(), state observation with collectAsStateWithLifecycle(), user input handling |
UsersViewModel.kt | Business logic and state management | Users API instantiation, coroutine-based API calls, centralized error handling with executeAction(), immutable state updates with StateFlow; includes UsersUiState data class for UI state modeling |
HorizonServiceConnection.connect() in onCreate() with three parameters: your app ID, the application context, and the activity’s lifecycle scope:HorizonServiceConnection.connect(
APPLICATION_ID, applicationContext, lifecycleScope
)
MainActivity.kt for the complete implementation.APPLICATION_ID rather than a placeholder string. The app crashes at launch until you replace it with your actual Meta app ID:private val APPLICATION_ID: String
get() = throw IllegalStateException(
"Replace with your application ID"
)
MainActivity.kt for the full pattern.ViewModel defines a reusable executeAction() helper that wraps all SDK calls with consistent loading state, error handling, and IO dispatching:private fun executeAction(
actionName: String, action: suspend () -> String
) { /* sets loading, runs on IO, catches exceptions */ }
getUser() and getAccessToken() delegate to this helper, eliminating duplicate coroutine and error-handling code. See UsersViewModel.kt for the complete implementation.Users class provides two methods demonstrated in this sample. To retrieve user profile data, call users.get(userId) within a coroutine context — the method returns a user object with a .json property containing the raw profile data. To fetch an access token, call users.getAccessToken(), which returns a string. Both methods are called on Dispatchers.IO in the sample. See UsersViewModel.kt for usage patterns.MutableStateFlow with a data class for UI state. Updates use the update { it.copy(...) } pattern to ensure atomic, immutable transitions:_uiState.update {
it.copy(isLoading = true, errorMessage = null)
}
UsersViewModel.kt for complete state management.kotlinx.serialization.