forked from omni/OmniCards
fix: fixed sync status showing
This commit is contained in:
@@ -46,15 +46,15 @@ class MainActivity : AppCompatActivity() {
|
|||||||
syncWithWearOSButton.setOnClickListener {
|
syncWithWearOSButton.setOnClickListener {
|
||||||
Toast.makeText(this, "Sync in progress...", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, "Sync in progress...", Toast.LENGTH_SHORT).show()
|
||||||
|
|
||||||
val isSuccess = sendToWatch(this, "/updateCards", Json.encodeToString(db.getCards()))
|
sendToWatch(this, "/updateCards", Json.encodeToString(db.getCards())) { isSuccess ->
|
||||||
|
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
Toast.makeText(this, "Synced!", Toast.LENGTH_SHORT).show()
|
Toast.makeText(this, "Successfully synced!", Toast.LENGTH_SHORT).show()
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, "Sync failed! Please, check your connection", Toast.LENGTH_LONG).show()
|
Toast.makeText(this, "Sync failed! Please, check your connection", Toast.LENGTH_LONG).show()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
|
|||||||
@@ -15,25 +15,38 @@ fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
|||||||
return BarcodeEncoder().createBitmap(matrix)
|
return BarcodeEncoder().createBitmap(matrix)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendToWatch(context: Context, path: String, message: String): Boolean {
|
fun sendToWatch(context: Context, path: String, message: String, onResult: (Boolean) -> Unit): Boolean {
|
||||||
val nodeClient = Wearable.getNodeClient(context)
|
val nodeClient = Wearable.getNodeClient(context)
|
||||||
|
|
||||||
var isSuccess = false
|
var isSuccess = false
|
||||||
|
|
||||||
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
||||||
if (nodes.isEmpty()) {
|
if (nodes.isEmpty()) {
|
||||||
Toast.makeText(context, "Watch is not connected", Toast.LENGTH_SHORT).show()
|
onResult(false)
|
||||||
|
|
||||||
return@addOnSuccessListener
|
return@addOnSuccessListener
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var completedCount = 0
|
||||||
|
var isAnySuccess = false
|
||||||
|
|
||||||
nodes.forEach { node ->
|
nodes.forEach { node ->
|
||||||
Wearable.getMessageClient(context).sendMessage(
|
Wearable.getMessageClient(context).sendMessage(
|
||||||
node.id,
|
node.id,
|
||||||
path,
|
path,
|
||||||
message.toByteArray(Charsets.UTF_8)
|
message.toByteArray(Charsets.UTF_8)
|
||||||
).addOnSuccessListener {
|
).addOnCompleteListener { task ->
|
||||||
isSuccess = true
|
completedCount++
|
||||||
|
|
||||||
|
if (task.isSuccessful) {
|
||||||
|
isAnySuccess = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (completedCount == nodes.size) {
|
||||||
|
onResult(isAnySuccess)
|
||||||
|
}
|
||||||
|
}.addOnFailureListener {
|
||||||
|
onResult(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user