-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sample screen for connectedAndInstalledNodes function (#1999)
- Loading branch information
Showing
11 changed files
with
283 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
...m/google/android/horologist/datalayer/sample/screens/nodeslistener/NodesListenerScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.horologist.datalayer.sample.screens.nodeslistener | ||
|
||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.SideEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import androidx.wear.compose.foundation.lazy.items | ||
import androidx.wear.compose.material.CircularProgressIndicator | ||
import androidx.wear.compose.material.Text | ||
import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices | ||
import com.google.android.gms.wearable.Node | ||
import com.google.android.horologist.compose.layout.ScalingLazyColumn | ||
import com.google.android.horologist.compose.layout.ScalingLazyColumnState | ||
import com.google.android.horologist.compose.layout.belowTimeTextPreview | ||
import com.google.android.horologist.compose.material.Chip | ||
import com.google.android.horologist.compose.material.Title | ||
import com.google.android.horologist.datalayer.sample.R | ||
|
||
@Composable | ||
fun NodesListenerScreen( | ||
columnState: ScalingLazyColumnState, | ||
modifier: Modifier = Modifier, | ||
viewModel: NodesListenerViewModel = hiltViewModel(), | ||
) { | ||
val state by viewModel.uiState.collectAsStateWithLifecycle() | ||
|
||
if (state == NodesListenerScreenState.Idle) { | ||
SideEffect { | ||
viewModel.initialize() | ||
} | ||
} | ||
|
||
NodesListenerScreen( | ||
state = state, | ||
columnState = columnState, | ||
modifier = modifier, | ||
) | ||
} | ||
|
||
@Composable | ||
fun NodesListenerScreen( | ||
state: NodesListenerScreenState, | ||
columnState: ScalingLazyColumnState, | ||
modifier: Modifier = Modifier, | ||
) { | ||
ScalingLazyColumn( | ||
columnState = columnState, | ||
modifier = modifier.fillMaxSize(), | ||
) { | ||
item { | ||
Title( | ||
textId = R.string.nodes_listener_screen_header, | ||
modifier = Modifier.padding(bottom = 10.dp), | ||
) | ||
} | ||
when (state) { | ||
NodesListenerScreenState.Idle, | ||
NodesListenerScreenState.Loading, | ||
-> { | ||
item { | ||
CircularProgressIndicator() | ||
} | ||
} | ||
|
||
is NodesListenerScreenState.Loaded -> { | ||
if (state.nodeList.isNotEmpty()) { | ||
item { | ||
Text(stringResource(id = R.string.nodes_listener_screen_message)) | ||
} | ||
items(state.nodeList.toList()) { node -> | ||
Chip( | ||
label = node.displayName, | ||
onClick = { /* do nothing */ }, | ||
secondaryLabel = node.id, | ||
) | ||
} | ||
} else { | ||
item { | ||
Text(stringResource(id = R.string.nodes_listener_screen_no_nodes)) | ||
} | ||
} | ||
} | ||
|
||
NodesListenerScreenState.ApiNotAvailable -> { | ||
item { | ||
Text(stringResource(id = R.string.wearable_message_api_unavailable)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@WearPreviewDevices | ||
@Composable | ||
fun NodesListenerScreenPreviewLoaded() { | ||
NodesListenerScreen( | ||
state = NodesListenerScreenState.Loaded( | ||
nodeList = setOf( | ||
NodePreviewImpl( | ||
displayName = "Google Pixel Watch", | ||
id = "903b8371", | ||
isNearby = true, | ||
), | ||
NodePreviewImpl( | ||
displayName = "Galaxy Watch4 Classic", | ||
id = "813d1812", | ||
isNearby = false, | ||
), | ||
), | ||
), | ||
columnState = belowTimeTextPreview(), | ||
) | ||
} | ||
|
||
@WearPreviewDevices | ||
@Composable | ||
fun NodesListenerScreenPreviewEmptyNodes() { | ||
NodesListenerScreen( | ||
state = NodesListenerScreenState.Loaded(emptySet()), | ||
columnState = belowTimeTextPreview(), | ||
) | ||
} | ||
|
||
@WearPreviewDevices | ||
@Composable | ||
fun NodesListenerScreenPreviewApiNotAvailable() { | ||
NodesListenerScreen( | ||
state = NodesListenerScreenState.ApiNotAvailable, | ||
columnState = belowTimeTextPreview(), | ||
) | ||
} | ||
|
||
private class NodePreviewImpl( | ||
private val displayName: String, | ||
private val id: String, | ||
private val isNearby: Boolean, | ||
) : Node { | ||
override fun getDisplayName(): String = displayName | ||
|
||
override fun getId(): String = id | ||
|
||
override fun isNearby(): Boolean = isNearby | ||
} |
76 changes: 76 additions & 0 deletions
76
...oogle/android/horologist/datalayer/sample/screens/nodeslistener/NodesListenerViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.horologist.datalayer.sample.screens.nodeslistener | ||
|
||
import androidx.annotation.MainThread | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.google.android.gms.wearable.Node | ||
import com.google.android.horologist.datalayer.watch.WearDataLayerAppHelper | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class NodesListenerViewModel | ||
@Inject | ||
constructor( | ||
private val wearDataLayerAppHelper: WearDataLayerAppHelper, | ||
) : ViewModel() { | ||
|
||
private var initializeCalled = false | ||
|
||
private val _uiState = | ||
MutableStateFlow<NodesListenerScreenState>(NodesListenerScreenState.Idle) | ||
public val uiState: StateFlow<NodesListenerScreenState> = _uiState | ||
|
||
@MainThread | ||
fun initialize() { | ||
if (initializeCalled) return | ||
initializeCalled = true | ||
|
||
_uiState.value = NodesListenerScreenState.Loading | ||
|
||
viewModelScope.launch { | ||
if (!wearDataLayerAppHelper.isAvailable()) { | ||
_uiState.value = NodesListenerScreenState.ApiNotAvailable | ||
} else { | ||
loadNodes() | ||
} | ||
} | ||
} | ||
|
||
private suspend fun loadNodes() { | ||
_uiState.value = NodesListenerScreenState.Loading | ||
|
||
wearDataLayerAppHelper.connectedAndInstalledNodes.collect { | ||
_uiState.value = NodesListenerScreenState.Loaded(nodeList = it) | ||
} | ||
} | ||
} | ||
|
||
sealed class NodesListenerScreenState { | ||
data object Idle : NodesListenerScreenState() | ||
|
||
data object Loading : NodesListenerScreenState() | ||
|
||
data class Loaded(val nodeList: Set<Node>) : NodesListenerScreenState() | ||
|
||
data object ApiNotAvailable : NodesListenerScreenState() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters