Skip to content

Commit

Permalink
Handle when API it not available (#2027)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizgrp authored Feb 7, 2024
1 parent 6af6af6 commit 4042bc4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ fun InstallAppPromptDemo2Screen(
) {
val state by viewModel.uiState.collectAsStateWithLifecycle()

if (state == InstallAppPromptDemo2ScreenState.Idle) {
SideEffect {
viewModel.initialize()
}
}

val context = LocalContext.current

InstallAppPromptDemo2Screen(
Expand Down Expand Up @@ -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 */
}

Expand Down Expand Up @@ -142,6 +151,10 @@ fun InstallAppPromptDemo2Screen(
),
)
}

InstallAppPromptDemo2ScreenState.ApiNotAvailable -> {
Text(stringResource(id = R.string.wearable_message_api_unavailable))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,10 +36,28 @@ class InstallAppPromptDemo2ViewModel
val phoneUiDataLayerHelper: PhoneUiDataLayerHelper,
) : ViewModel() {

private var initializeCalled = false

private val _uiState =
MutableStateFlow<InstallAppPromptDemo2ScreenState>(InstallAppPromptDemo2ScreenState.Idle)
public val uiState: StateFlow<InstallAppPromptDemo2ScreenState> = _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

Expand Down Expand Up @@ -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()
}

0 comments on commit 4042bc4

Please sign in to comment.