forked from omni/OmniCards
feat: added companion app for WearOS, and setted up icons
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
class CardInfo (
|
||||
val id: Int,
|
||||
val name: String,
|
||||
|
||||
@@ -7,11 +7,13 @@ import android.widget.Button
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.PopupMenu
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import com.google.android.material.button.MaterialButton
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.w3c.dom.Text
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
@@ -32,9 +34,23 @@ class MainActivity : AppCompatActivity() {
|
||||
cardsList = findViewById(R.id.cardsList)
|
||||
|
||||
val addCardButton: Button = findViewById(R.id.addCardButton)
|
||||
val syncWithWearOSButton: Button = findViewById(R.id.syncWithWearOSButton)
|
||||
|
||||
addCardButton.setOnClickListener {
|
||||
startActivity(Intent(this, AddCardActivity::class.java))
|
||||
}
|
||||
|
||||
syncWithWearOSButton.setOnClickListener {
|
||||
Toast.makeText(this, "Sync in progress...", Toast.LENGTH_SHORT).show()
|
||||
|
||||
val isSuccess = sendToWatch(this, "/updateCards", Json.encodeToString(db.getCards()))
|
||||
|
||||
if (isSuccess) {
|
||||
Toast.makeText(this, "Synced!", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Toast.makeText(this, "Sync failed! Please, check your connection", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package ru.omni_devel.cards
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.widget.Toast
|
||||
import com.google.android.gms.wearable.Wearable
|
||||
import com.google.zxing.BarcodeFormat
|
||||
import com.google.zxing.MultiFormatWriter
|
||||
import com.journeyapps.barcodescanner.BarcodeEncoder
|
||||
@@ -11,3 +14,29 @@ fun generateCode(value: String, format: BarcodeFormat): Bitmap {
|
||||
|
||||
return BarcodeEncoder().createBitmap(matrix)
|
||||
}
|
||||
|
||||
fun sendToWatch(context: Context, path: String, message: String): Boolean {
|
||||
val nodeClient = Wearable.getNodeClient(context)
|
||||
|
||||
var isSuccess = false
|
||||
|
||||
nodeClient.connectedNodes.addOnSuccessListener { nodes ->
|
||||
if (nodes.isEmpty()) {
|
||||
Toast.makeText(context, "Watch is not connected", Toast.LENGTH_SHORT).show()
|
||||
|
||||
return@addOnSuccessListener
|
||||
}
|
||||
|
||||
nodes.forEach { node ->
|
||||
Wearable.getMessageClient(context).sendMessage(
|
||||
node.id,
|
||||
path,
|
||||
message.toByteArray(Charsets.UTF_8)
|
||||
).addOnSuccessListener {
|
||||
isSuccess = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return isSuccess
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user