Skip to content

Commit

Permalink
Add app helper nodes actions screen to datalayer wear sample
Browse files Browse the repository at this point in the history
  • Loading branch information
luizgrp committed Dec 20, 2023
1 parent 47abc88 commit fd942b9
Show file tree
Hide file tree
Showing 11 changed files with 546 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.horologist.datalayer.sample

import com.google.android.horologist.datalayer.sample.screens.info.infoScreenRoute
import com.google.android.horologist.datalayer.sample.screens.nodesactions.nodeDetailsScreenRoute

sealed class Screen(
val route: String,
Expand All @@ -28,5 +29,8 @@ sealed class Screen(

data object AppHelperTrackingScreen : Screen("appHelperTrackingScreen")
data object AppHelperNodesActionsScreen : Screen("appHelperNodesActionsScreen")

data object AppHelperNodeDetailsScreen : Screen(nodeDetailsScreenRoute)

data object InfoScreen : Screen(infoScreenRoute)
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import com.google.android.horologist.datalayer.sample.screens.info.infoScreen
import com.google.android.horologist.datalayer.sample.screens.info.navigateToInfoScreen
import com.google.android.horologist.datalayer.sample.screens.nodes.DataLayerNodesScreen
import com.google.android.horologist.datalayer.sample.screens.nodesactions.NodesActionsScreen
import com.google.android.horologist.datalayer.sample.screens.nodesactions.navigateToNodeDetailsScreen
import com.google.android.horologist.datalayer.sample.screens.nodesactions.nodeDetailsScreen
import com.google.android.horologist.datalayer.sample.screens.tracking.TrackingScreen

@Composable
Expand Down Expand Up @@ -62,8 +64,12 @@ fun WearApp(
)
}
scrollable(route = Screen.AppHelperNodesActionsScreen.route) {
NodesActionsScreen(columnState = it.columnState)
NodesActionsScreen(
onNodeClick = navController::navigateToNodeDetailsScreen,
columnState = it.columnState,
)
}
nodeDetailsScreen()
infoScreen(
onDismissClick = navController::popBackStack,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fun InfoScreen(
item {
Button(
imageVector = Icons.Default.Done,
contentDescription = stringResource(id = R.string.info_done_button_content_description),
contentDescription = stringResource(id = R.string.close_button_content_description),
onClick = onDismissClick,
modifier = Modifier.padding(top = 10.dp),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ package com.google.android.horologist.datalayer.sample.screens.info

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

class InfoScreenViewModel(
savedStateHandle: SavedStateHandle,
) : ViewModel() {
@HiltViewModel
class InfoScreenViewModel
@Inject
constructor(
savedStateHandle: SavedStateHandle,
) : ViewModel() {

private val infoScreenArgs: InfoScreenArgs = InfoScreenArgs(savedStateHandle)
private val infoScreenArgs: InfoScreenArgs = InfoScreenArgs(savedStateHandle)

val message: String
get() = infoScreenArgs.message
}
val message: String
get() = infoScreenArgs.message
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2023 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.nodesactions

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
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.wear.compose.material.Text
import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
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.datalayer.sample.R

@Composable
fun NodeDetailsScreen(
columnState: ScalingLazyColumnState,
modifier: Modifier = Modifier,
viewModel: NodeDetailsViewModel = hiltViewModel(),
) {
NodeDetailsScreen(
nodeId = viewModel.nodeId,
onStartCompanionClick = viewModel::onStartCompanionClick,
onInstallOnNodeClick = viewModel::onInstallOnNodeClick,
onStartRemoteOwnAppClick = viewModel::onStartRemoteOwnAppClick,
columnState = columnState,
modifier = modifier,
)
}

@Composable
fun NodeDetailsScreen(
nodeId: String,
onStartCompanionClick: () -> Unit,
onInstallOnNodeClick: () -> Unit,
onStartRemoteOwnAppClick: () -> Unit,
columnState: ScalingLazyColumnState,
modifier: Modifier = Modifier,
) {
ScalingLazyColumn(
columnState = columnState,
modifier = modifier.fillMaxSize(),
) {
item {
Text(
text = stringResource(id = R.string.node_details_header),
modifier = Modifier.padding(bottom = 10.dp),
)
}

item {
Text(
text = stringResource(id = R.string.node_details_id, nodeId),
)
}
item {
Chip(
label = stringResource(id = R.string.node_details_start_companion_chip_label),
onClick = onStartCompanionClick,
)
}
item {
Chip(
label = stringResource(id = R.string.node_details_install_on_node_chip_label),
onClick = onInstallOnNodeClick,
)
}
item {
Chip(
label = stringResource(id = R.string.node_details_start_remote_own_app_chip_label),
onClick = onStartRemoteOwnAppClick,
)
}
}
}

@WearPreviewDevices
@Composable
fun NodeDetailsScreenPreview() {
NodeDetailsScreen(
nodeId = "12345",
onStartCompanionClick = { },
onInstallOnNodeClick = { },
onStartRemoteOwnAppClick = { },
columnState = belowTimeTextPreview(),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 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.nodesactions

import androidx.lifecycle.SavedStateHandle
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavType
import androidx.navigation.navArgument
import com.google.android.horologist.compose.navscaffold.scrollable
import com.google.android.horologist.datalayer.sample.Screen
import java.net.URLDecoder
import java.net.URLEncoder

private const val nodeIdArg = "nodeId"
private const val routePrefix = "appHelperNodeDetailsScreen"

private val URL_CHARACTER_ENCODING = Charsets.UTF_8.name()

const val nodeDetailsScreenRoute = "$routePrefix/{$nodeIdArg}"

internal class NodeDetailsScreenArgs(val nodeId: String) {
constructor(savedStateHandle: SavedStateHandle) :
this(URLDecoder.decode(checkNotNull(savedStateHandle[nodeIdArg]), URL_CHARACTER_ENCODING))
}

fun NavController.navigateToNodeDetailsScreen(message: String) {
val encodedMessage = URLEncoder.encode(message, URL_CHARACTER_ENCODING)
this.navigate("$routePrefix/$encodedMessage")
}

fun NavGraphBuilder.nodeDetailsScreen() {
scrollable(
route = Screen.AppHelperNodeDetailsScreen.route,
arguments = listOf(
navArgument(nodeIdArg) { type = NavType.StringType },
),
) {
NodeDetailsScreen(
columnState = it.columnState,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 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.nodesactions

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.horologist.datalayer.watch.WearDataLayerAppHelper
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class NodeDetailsViewModel
@Inject
constructor(
savedStateHandle: SavedStateHandle,
private val wearDataLayerAppHelper: WearDataLayerAppHelper,
) : ViewModel() {
private val nodeDetailsScreenArgs: NodeDetailsScreenArgs =
NodeDetailsScreenArgs(savedStateHandle)

val nodeId: String
get() = nodeDetailsScreenArgs.nodeId

fun onStartCompanionClick() {
viewModelScope.launch {
wearDataLayerAppHelper.startCompanion(node = nodeId)
}
}

fun onInstallOnNodeClick() {
viewModelScope.launch {
wearDataLayerAppHelper.installOnNode(node = nodeId)
}
}

fun onStartRemoteOwnAppClick() {
viewModelScope.launch {
wearDataLayerAppHelper.startRemoteOwnApp(node = nodeId)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2023 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.nodesactions

data class NodeUiModel(
val id: String,
val name: String,
val appInstalled: Boolean,
val type: NodeTypeUiModel,
)

enum class NodeTypeUiModel {
WATCH,
PHONE,
UNKNOWN,
}
Loading

0 comments on commit fd942b9

Please sign in to comment.