forked from omni/OmniCards
61 lines
1.7 KiB
Kotlin
61 lines
1.7 KiB
Kotlin
package ru.omni_devel.cards
|
|
|
|
import android.content.Context
|
|
import android.graphics.Bitmap
|
|
import com.google.android.gms.wearable.Wearable
|
|
import com.google.zxing.BarcodeFormat
|
|
import com.google.zxing.MultiFormatWriter
|
|
import com.journeyapps.barcodescanner.BarcodeEncoder
|
|
|
|
fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
|
val writer = MultiFormatWriter()
|
|
val matrix = writer.encode(value, format, 800, 400)
|
|
|
|
return BarcodeEncoder().createBitmap(matrix)
|
|
}
|
|
|
|
fun sendToWatch(context: Context, path: String, message: String, onResult: (Boolean) -> Unit) {
|
|
val nodeClient = Wearable.getNodeClient(context)
|
|
|
|
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
|
if (nodes.isEmpty()) {
|
|
onResult(false)
|
|
|
|
return@addOnSuccessListener
|
|
}
|
|
|
|
var completedCount = 0
|
|
var isAnySuccess = false
|
|
|
|
nodes.forEach { node ->
|
|
Wearable.getMessageClient(context).sendMessage(
|
|
node.id,
|
|
path,
|
|
message.toByteArray(Charsets.UTF_8)
|
|
).addOnCompleteListener(context.mainExecutor) { task ->
|
|
completedCount++
|
|
|
|
if (task.isSuccessful) {
|
|
isAnySuccess = true
|
|
}
|
|
|
|
if (completedCount == nodes.size) {
|
|
onResult(isAnySuccess)
|
|
}
|
|
}.addOnFailureListener(context.mainExecutor) {
|
|
onResult(false)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fun checkCardData(name: String, value: String): String? {
|
|
if (name.trim().isEmpty()) {
|
|
return "Name should not be empty"
|
|
} else if (value.trim().isEmpty()) {
|
|
return "Code value should not be empty"
|
|
}
|
|
|
|
return null
|
|
}
|