Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample screen for connectedAndInstalledNodes function #1998

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sealed class Screen(
) {
data object MenuScreen : Screen("menuScreen")
data object AppHelperNodesScreen : Screen("appHelperNodesScreen")
data object AppHelperNodesListenerScreen : Screen("appHelperNodesListenerScreen")
data object InstallAppPromptDemoScreen : Screen("installAppPromptDemoScreen")
data object InstallAppPromptDemo2Screen : Screen("installAppPromptDemo2Screen")
data object CounterScreen : Screen("counterScreen")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -48,7 +49,9 @@ fun CounterScreen(
val state by viewModel.uiState.collectAsStateWithLifecycle()

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

CounterScreen(
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.counter

import androidx.annotation.MainThread
import androidx.datastore.core.DataStore
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
Expand Down Expand Up @@ -61,6 +62,7 @@ class CounterScreenViewModel
private val _uiState = MutableStateFlow<CounterScreenUiState>(CounterScreenUiState.Idle)
public val uiState: StateFlow<CounterScreenUiState> = _uiState

@MainThread
fun initialize() {
if (initializeCalled) return
initializeCalled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.google.android.horologist.datalayer.sample.screens.inappprompts.Insta
import com.google.android.horologist.datalayer.sample.screens.inappprompts.InstallAppPromptDemoScreen
import com.google.android.horologist.datalayer.sample.screens.menu.MenuScreen
import com.google.android.horologist.datalayer.sample.screens.nodes.NodesScreen
import com.google.android.horologist.datalayer.sample.screens.nodeslistener.NodesListenerScreen

@Composable
fun MainScreen(
Expand Down Expand Up @@ -62,6 +63,9 @@ fun MainScreen(
composable(route = Screen.AppHelperNodesScreen.route) {
NodesScreen()
}
composable(route = Screen.AppHelperNodesListenerScreen.route) {
NodesListenerScreen()
}
composable(route = Screen.InstallAppPromptDemoScreen.route) {
InstallAppPromptDemoScreen(onShowInstallAppPrompt = onShowInstallAppPrompt)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ fun MenuScreen(
Text(text = stringResource(id = R.string.menu_screen_nodes_item))
}

Button(onClick = { navController.navigate(Screen.AppHelperNodesListenerScreen.route) }) {
Text(text = stringResource(id = R.string.menu_screen_nodes_listener_item))
}

Text(
text = stringResource(id = R.string.menu_screen_inapp_prompts_header),
modifier = Modifier.padding(top = 10.dp),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
Expand All @@ -53,12 +54,14 @@ import com.google.android.horologist.datalayer.sample.util.toProtoTimestamp
@Composable
fun NodesScreen(
modifier: Modifier = Modifier,
viewModel: NodesActionViewModel = hiltViewModel(),
viewModel: NodesViewModel = hiltViewModel(),
) {
val state by viewModel.uiState.collectAsStateWithLifecycle()

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

NodesScreen(
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.nodes

import androidx.annotation.MainThread
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.horologist.data.AppHelperResultCode
Expand All @@ -32,7 +33,7 @@ private const val REMOTE_ACTIVITY_SAMPLE_CLASS_FULL_NAME =
"com.google.android.horologist.datalayer.sample.screens.startremote.StartRemoteSampleActivity"

@HiltViewModel
class NodesActionViewModel
class NodesViewModel
@Inject
constructor(
private val phoneDataLayerAppHelper: PhoneDataLayerAppHelper,
Expand All @@ -45,6 +46,7 @@ class NodesActionViewModel
MutableStateFlow<NodesScreenState>(NodesScreenState.Idle)
public val uiState: StateFlow<NodesScreenState> = _uiState

@MainThread
fun initialize() {
if (initializeCalled) return
initializeCalled = true
Expand All @@ -61,8 +63,6 @@ class NodesActionViewModel
}

fun onRefreshClick() {
_uiState.value = NodesScreenState.Loading

viewModelScope.launch {
loadNodes()
}
Expand Down Expand Up @@ -108,6 +108,8 @@ class NodesActionViewModel
}

private suspend fun loadNodes() {
_uiState.value = NodesScreenState.Loading

cachedNodeList = phoneDataLayerAppHelper.connectedNodes()
_uiState.value = NodesScreenState.Loaded(nodeList = cachedNodeList)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* 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.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.android.gms.wearable.Node
import com.google.android.horologist.datalayer.sample.R

@Composable
fun NodesListenerScreen(
modifier: Modifier = Modifier,
viewModel: NodesListenerViewModel = hiltViewModel(),
) {
val state by viewModel.uiState.collectAsStateWithLifecycle()

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

NodesListenerScreen(
state = state,
modifier = modifier,
)
}

@Composable
fun NodesListenerScreen(
state: NodesListenerScreenState,
modifier: Modifier = Modifier,
) {
LazyColumn(
modifier = modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
) {
item {
Text(
text = stringResource(id = R.string.nodes_listener_screen_header),
modifier = Modifier.padding(vertical = 10.dp),
style = MaterialTheme.typography.titleLarge,
)
}

if (state != NodesListenerScreenState.ApiNotAvailable) {
item {
Text(
text = stringResource(id = R.string.nodes_listener_screen_message),
modifier = Modifier.padding(vertical = 10.dp),
style = MaterialTheme.typography.bodyLarge,
)
}
}

when (state) {
NodesListenerScreenState.Idle,
NodesListenerScreenState.Loading,
-> {
item {
CircularProgressIndicator()
}
}

is NodesListenerScreenState.Loaded -> {
if (state.nodeList.isNotEmpty()) {
items(items = state.nodeList.toList()) { node ->
Box(
modifier = modifier
.padding(16.dp)
.fillMaxWidth(),
contentAlignment = Alignment.Center,
) {
Card {
Column(
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),

) {
Text(
stringResource(
R.string.nodes_listener_screen_node_name_label,
node.displayName,
),
)
Text(
style = MaterialTheme.typography.labelMedium,
text = stringResource(
R.string.nodes_listener_screen_node_id_label,
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))
}
}
}
}
}

@Preview(showBackground = true)
@Composable
fun NodesListenerScreenPreview() {
NodesListenerScreen(
state = NodesListenerScreenState.Loaded(
nodeList = setOf(
NodePreviewImpl(
displayName = "Google Pixel Watch",
id = "903b8371",
isNearby = true,
),
NodePreviewImpl(
displayName = "Galaxy Watch4 Classic",
id = "813d1812",
isNearby = false,
),
),
),
)
}

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
}
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.phone.PhoneDataLayerAppHelper
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 phoneDataLayerAppHelper: PhoneDataLayerAppHelper,
) : 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 (!phoneDataLayerAppHelper.isAvailable()) {
_uiState.value = NodesListenerScreenState.ApiNotAvailable
} else {
loadNodes()
}
}
}

private suspend fun loadNodes() {
_uiState.value = NodesListenerScreenState.Loading

phoneDataLayerAppHelper.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()
}
Loading