Skip to content

Commit

Permalink
Move datalayer sample to datalayer sample app (#1852)
Browse files Browse the repository at this point in the history
  • Loading branch information
luizgrp authored and oas004 committed Dec 5, 2023
1 parent b0a20b1 commit df9d382
Show file tree
Hide file tree
Showing 18 changed files with 120 additions and 147 deletions.
45 changes: 45 additions & 0 deletions datalayer/sample/wear/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
* limitations under the License.
*/

import com.google.protobuf.gradle.id

plugins {
id("com.android.application")
id("com.google.protobuf")
kotlin("android")
}

Expand Down Expand Up @@ -87,6 +90,42 @@ android {
namespace = "com.google.android.horologist.datalayer.sample"
}

sourceSets {
create("main") {
java {
srcDirs(
"build/generated/source/proto/debug/java",
"build/generated/source/proto/debug/grpc",
"build/generated/source/proto/debug/kotlin",
"build/generated/source/proto/debug/grpckt",
)
}
}
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.25.1"
}
plugins {
id("javalite") {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
}
}
generateProtoTasks {
all().forEach { task ->
task.builtins {
create("java") {
option("lite")
}
create("kotlin") {
option("lite")
}
}
}
}
}

dependencies {
api(projects.annotations)

Expand All @@ -112,6 +151,9 @@ dependencies {
implementation(libs.wearcompose.foundation)
implementation(libs.wearcompose.navigation)

implementation(libs.kotlinx.coroutines.playservices)
implementation(libs.protobuf.kotlin.lite)

debugImplementation(libs.compose.ui.tooling)
implementation(libs.androidx.wear.tooling.preview)
debugImplementation(projects.composeTools)
Expand All @@ -125,3 +167,6 @@ dependencies {
testImplementation(libs.truth)
testImplementation(libs.robolectric)
}

tasks.maybeCreate("prepareKotlinIdeaImport")
.dependsOn("generateDebugProto")
12 changes: 12 additions & 0 deletions datalayer/sample/wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@

</activity>

<service
android:name="com.google.android.horologist.datalayer.sample.screens.nodes.SampleDataService"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
<data
android:host="*"
android:pathPrefix="/proto/"
android:scheme="wear" />
</intent-filter>
</service>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ sealed class Screen(
object MainScreen : Screen("mainScreen")

object DataLayerScreen : Screen("dataLayerScreen")

object DataLayerNodes : Screen("nodes")
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ package com.google.android.horologist.datalayer.sample

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.wear.compose.navigation.rememberSwipeDismissableNavController
import androidx.wear.compose.ui.tooling.preview.WearPreviewSmallRound
import com.google.android.horologist.compose.navscaffold.WearNavScaffold
import com.google.android.horologist.compose.navscaffold.scrollable
import com.google.android.horologist.datalayer.sample.screens.MainScreen
import com.google.android.horologist.datalayer.sample.screens.datalayer.DataLayerScreen
import com.google.android.horologist.datalayer.sample.screens.nodes.DataLayerNodesScreen
import com.google.android.horologist.datalayer.sample.screens.nodes.DataLayerNodesViewModel

@Composable
fun WearApp(
Expand All @@ -44,6 +47,14 @@ fun WearApp(
scrollable(route = Screen.DataLayerScreen.route) {
DataLayerScreen(columnState = it.columnState, modifier = modifier)
}
scrollable(
Screen.DataLayerNodes.route,
) {
DataLayerNodesScreen(
viewModel = viewModel(factory = DataLayerNodesViewModel.Factory),
columnState = it.columnState,
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package com.google.android.horologist.datalayer.sample.di

import android.util.Log
import com.google.android.horologist.data.ProtoDataStoreHelper.protoFlow
import com.google.android.horologist.data.ProtoDataStoreHelper.registerProtoDataListener
import com.google.android.horologist.data.TargetNodeId
import com.google.android.horologist.data.WearDataLayerRegistry
import com.google.android.horologist.data.proto.SampleProto
import com.google.android.horologist.data.store.ProtoDataListener
import com.google.android.horologist.datalayer.grpc.GrpcExtensions.grpcClient
import com.google.android.horologist.datalayer.sample.SampleApplication
import com.google.android.horologist.datalayer.sample.shared.CounterValueSerializer
Expand Down Expand Up @@ -55,6 +58,18 @@ object SampleAppDI {
coroutineScope = coroutineScope,
).apply {
registerSerializer(CounterValueSerializer)

registerSerializer(com.google.android.horologist.datalayer.sample.screens.nodes.SampleDataSerializer)

registerProtoDataListener(object : ProtoDataListener<SampleProto.Data> {
override fun dataAdded(nodeId: String, path: String, value: SampleProto.Data) {
println("Data Added: $nodeId $path $value")
}

override fun dataDeleted(nodeId: String, path: String) {
println("Data Deleted: $nodeId $path")
}
})
}

private fun servicesCoroutineScope(): CoroutineScope {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.wear.compose.ui.tooling.preview.WearPreviewDevices
import com.google.android.horologist.composables.SectionedList
import com.google.android.horologist.composables.SectionedListScope
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
Expand All @@ -37,31 +36,27 @@ fun MainScreen(
modifier: Modifier = Modifier,
columnState: ScalingLazyColumnState,
) {
SectionedList(
ScalingLazyColumn(
columnState = columnState,
modifier = modifier.fillMaxSize(),
) {
dataLayerSection(navigateToRoute)
}
}

private fun SectionedListScope.dataLayerSection(navigateToRoute: (String) -> Unit) {
section(
listOf(
Pair(
R.string.main_menu_datalayer_item,
Screen.DataLayerScreen.route,
),
),
) {
header {
item {
Title(stringResource(id = R.string.main_menu_datalayer_header))
}
loaded { (textId, route) ->

item {
Chip(
label = stringResource(id = R.string.main_menu_datalayer_item),
modifier = Modifier.fillMaxWidth(),
onClick = { navigateToRoute(Screen.DataLayerScreen.route) },
)
}

item {
Chip(
label = stringResource(id = textId),
label = stringResource(id = R.string.main_menu_datalayer_nodes),
modifier = Modifier.fillMaxWidth(),
onClick = { navigateToRoute(route) },
onClick = { navigateToRoute(Screen.DataLayerNodes.route) },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* 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.
Expand All @@ -14,20 +14,19 @@
* limitations under the License.
*/

package com.google.android.horologist.datalayer
package com.google.android.horologist.datalayer.sample.screens.nodes

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.wear.compose.foundation.lazy.items
import androidx.wear.compose.material.Button
import androidx.wear.compose.material.Chip
import androidx.wear.compose.material.ListHeader
import androidx.wear.compose.material.Text
import com.google.android.horologist.compose.layout.ScalingLazyColumn
import com.google.android.horologist.compose.layout.ScalingLazyColumnState
import com.google.android.horologist.compose.material.Chip

@Composable
fun DataLayerNodesScreen(
Expand All @@ -48,10 +47,8 @@ fun DataLayerNodesScreen(
}
items(state.nodes) {
Chip(
label = "${it.displayName}(${it.id}) ${if (it.isNearby) "NEAR" else ""}",
onClick = { },
label = {
Text("${it.displayName}(${it.id}) ${if (it.isNearby) "NEAR" else ""}")
},
)
}
item {
Expand All @@ -62,11 +59,12 @@ fun DataLayerNodesScreen(
item {
val thisData = state.thisData
if (thisData != null) {
Button(onClick = {
viewModel.increment()
}) {
Text("This Value: ${thisData.value} (${thisData.name})")
}
Chip(
label = "This Value: ${thisData.value} (${thisData.name})",
onClick = {
viewModel.increment()
},
)
} else {
Text("This Value: None")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* 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.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.android.horologist.datalayer
package com.google.android.horologist.datalayer.sample.screens.nodes

import android.os.Build
import androidx.datastore.core.DataStore
Expand All @@ -23,13 +23,13 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.CreationExtras
import com.google.android.gms.wearable.Node
import com.google.android.horologist.components.SampleApplication
import com.google.android.horologist.data.ProtoDataStoreHelper.protoDataStore
import com.google.android.horologist.data.ProtoDataStoreHelper.protoFlow
import com.google.android.horologist.data.TargetNodeId
import com.google.android.horologist.data.WearDataLayerRegistry
import com.google.android.horologist.data.proto.SampleProto.Data
import com.google.android.horologist.data.proto.copy
import com.google.android.horologist.datalayer.sample.SampleApplication
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
Expand Down Expand Up @@ -85,7 +85,7 @@ public class DataLayerNodesViewModel(
viewModelScope.launch {
thisDataStore.updateData { current ->
current.copy {
value += +1
value += 1
name = Build.DEVICE
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* 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.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.android.horologist.datalayer
package com.google.android.horologist.datalayer.sample.screens.nodes

import androidx.datastore.core.CorruptionException
import androidx.datastore.core.Serializer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Android Open Source Project
* 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.
Expand All @@ -14,11 +14,11 @@
* limitations under the License.
*/

package com.google.android.horologist.datalayer
package com.google.android.horologist.datalayer.sample.screens.nodes

import com.google.android.horologist.components.SampleApplication
import com.google.android.horologist.data.WearDataLayerRegistry
import com.google.android.horologist.data.WearDataService
import com.google.android.horologist.datalayer.sample.SampleApplication

class SampleDataService : WearDataService() {
override lateinit var registry: WearDataLayerRegistry
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions datalayer/sample/wear/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
<string name="data_layer_missing_message">Missing</string>
<string name="data_layer_error_message">Error: %1$s</string>
<string name="data_layer_value_message">Value: %1$s</string>
<string name="main_menu_datalayer_nodes">Data Layer Nodes</string>
</resources>
Loading

0 comments on commit df9d382

Please sign in to comment.