diff --git a/datalayer/sample/phone/src/main/java/com/google/android/horologist/datalayer/sample/screens/inappprompts/InstallAppPromptDemo2Screen.kt b/datalayer/sample/phone/src/main/java/com/google/android/horologist/datalayer/sample/screens/inappprompts/InstallAppPromptDemo2Screen.kt index 93330ca81f..74b1f2b07b 100644 --- a/datalayer/sample/phone/src/main/java/com/google/android/horologist/datalayer/sample/screens/inappprompts/InstallAppPromptDemo2Screen.kt +++ b/datalayer/sample/phone/src/main/java/com/google/android/horologist/datalayer/sample/screens/inappprompts/InstallAppPromptDemo2Screen.kt @@ -45,6 +45,12 @@ fun InstallAppPromptDemo2Screen( ) { val state by viewModel.uiState.collectAsStateWithLifecycle() + if (state == InstallAppPromptDemo2ScreenState.Idle) { + SideEffect { + viewModel.initialize() + } + } + val context = LocalContext.current InstallAppPromptDemo2Screen( @@ -97,12 +103,15 @@ fun InstallAppPromptDemo2Screen( modifier = Modifier .padding(top = 10.dp) .align(Alignment.CenterHorizontally), + enabled = state != InstallAppPromptDemo2ScreenState.ApiNotAvailable, ) { Text(text = stringResource(id = R.string.install_app_prompt_run_demo2_button_label)) } when (state) { - InstallAppPromptDemo2ScreenState.Idle -> { + InstallAppPromptDemo2ScreenState.Idle, + InstallAppPromptDemo2ScreenState.Loaded, + -> { /* do nothing */ } @@ -142,6 +151,10 @@ fun InstallAppPromptDemo2Screen( ), ) } + + InstallAppPromptDemo2ScreenState.ApiNotAvailable -> { + Text(stringResource(id = R.string.wearable_message_api_unavailable)) + } } } } diff --git a/datalayer/sample/phone/src/main/java/com/google/android/horologist/datalayer/sample/screens/inappprompts/InstallAppPromptDemo2ViewModel.kt b/datalayer/sample/phone/src/main/java/com/google/android/horologist/datalayer/sample/screens/inappprompts/InstallAppPromptDemo2ViewModel.kt index e8eb7092d2..59500cc643 100644 --- a/datalayer/sample/phone/src/main/java/com/google/android/horologist/datalayer/sample/screens/inappprompts/InstallAppPromptDemo2ViewModel.kt +++ b/datalayer/sample/phone/src/main/java/com/google/android/horologist/datalayer/sample/screens/inappprompts/InstallAppPromptDemo2ViewModel.kt @@ -16,6 +16,7 @@ package com.google.android.horologist.datalayer.sample.screens.inappprompts +import androidx.annotation.MainThread import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.android.horologist.data.apphelper.appInstalled @@ -35,10 +36,28 @@ class InstallAppPromptDemo2ViewModel val phoneUiDataLayerHelper: PhoneUiDataLayerHelper, ) : ViewModel() { + private var initializeCalled = false + private val _uiState = MutableStateFlow(InstallAppPromptDemo2ScreenState.Idle) public val uiState: StateFlow = _uiState + @MainThread + fun initialize() { + if (initializeCalled) return + initializeCalled = true + + _uiState.value = InstallAppPromptDemo2ScreenState.Loading + + viewModelScope.launch { + if (!phoneDataLayerAppHelper.isAvailable()) { + _uiState.value = InstallAppPromptDemo2ScreenState.ApiNotAvailable + } else { + _uiState.value = InstallAppPromptDemo2ScreenState.Loaded + } + } + } + fun onRunDemoClick() { _uiState.value = InstallAppPromptDemo2ScreenState.Loading @@ -69,8 +88,10 @@ class InstallAppPromptDemo2ViewModel sealed class InstallAppPromptDemo2ScreenState { data object Idle : InstallAppPromptDemo2ScreenState() data object Loading : InstallAppPromptDemo2ScreenState() + data object Loaded : InstallAppPromptDemo2ScreenState() data class WatchFound(val watchName: String) : InstallAppPromptDemo2ScreenState() data object WatchNotFound : InstallAppPromptDemo2ScreenState() data object InstallPromptInstallClicked : InstallAppPromptDemo2ScreenState() data object InstallPromptInstallCancelled : InstallAppPromptDemo2ScreenState() + data object ApiNotAvailable : InstallAppPromptDemo2ScreenState() }