Skip to content

Commit 65c7a76

Browse files
authored
Refactor DataLayer method to use coroutines rather than callback. (#777)
* Add missing snippet and add more code for formatting * Adds coroutines for Data Layer sample * fix format
1 parent 8238d07 commit 65c7a76

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "
236236
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
237237
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }
238238
kotlinx-coroutines-guava = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-guava", version.ref = "kotlinxCoroutinesGuava" }
239+
kotlinx-coroutines-play-services = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-play-services", version.ref = "coroutines" }
239240
kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" }
240241
kotlinx-metadata-jvm = { module = "org.jetbrains.kotlin:kotlin-metadata-jvm", version.ref = "kotlin" }
241242
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinxSerializationJson" }

wear/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ dependencies {
7575

7676
implementation(libs.compose.ui.tooling)
7777
implementation(libs.play.services.wearable)
78+
implementation(libs.kotlinx.coroutines.play.services)
7879
implementation(libs.androidx.tiles)
7980
implementation(libs.androidx.wear)
8081
implementation(libs.androidx.wear.ongoing)

wear/src/main/java/com/example/wear/snippets/datalayer/DataLayerActivity.kt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import com.google.android.gms.wearable.Wearable
3535
import java.io.ByteArrayOutputStream
3636
import java.io.InputStream
3737
import java.util.concurrent.ExecutionException
38+
import kotlinx.coroutines.tasks.await
3839

3940
class DataLayerActivity : ComponentActivity(), DataClient.OnDataChangedListener {
4041
private val dataClient by lazy { Wearable.getDataClient(this) }
@@ -170,24 +171,24 @@ class DataLayerActivity2 : ComponentActivity(), DataClient.OnDataChangedListener
170171
}
171172

172173
// [START android_wear_datalayer_async_call]
173-
private fun Context.sendDataAsync(count: Int) {
174-
// Create a data item with the path and data to be sent
175-
val putDataReq: PutDataRequest = PutDataMapRequest.create("/count").run {
176-
dataMap.putInt("count_key", count)
177-
asPutDataRequest()
174+
private suspend fun Context.sendDataAsync(count: Int) {
175+
try {
176+
val putDataReq: PutDataRequest = PutDataMapRequest.create("/count").run {
177+
dataMap.putInt("count_key", count)
178+
asPutDataRequest()
179+
}
180+
val dataItem = Wearable.getDataClient(this).putDataItem(putDataReq).await()
181+
handleDataItem(dataItem)
182+
} catch (e: Exception) {
183+
handleDataItemError(e)
184+
} finally {
185+
handleTaskComplete()
178186
}
179-
// Create a task to send the data to the data layer
180-
val task: Task<DataItem> = Wearable.getDataClient(this).putDataItem(putDataReq)
181-
182-
// Using Kotlin function references
183-
task.addOnSuccessListener(::handleDataItem)
184-
task.addOnFailureListener(::handleDataItemError)
185-
task.addOnCompleteListener(::handleTaskComplete)
186187
}
187188

188189
private fun handleDataItem(dataItem: DataItem) { }
189190
private fun handleDataItemError(exception: Exception) { }
190-
private fun handleTaskComplete(task: Task<DataItem>) { }
191+
private fun handleTaskComplete() { }
191192
// [END android_wear_datalayer_async_call]
192193

193194
// [START android_wear_datalayer_sync_call]

0 commit comments

Comments
 (0)