feat: added color support for buttons on WearOS

This commit is contained in:
2026-06-14 16:49:35 +03:00
parent 26fbbcf19f
commit 245c8540b7
5 changed files with 93 additions and 39 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ activityKtx = "1.13.0"
constraintlayout = "2.2.1" constraintlayout = "2.2.1"
kotlin = "2.4.0" kotlin = "2.4.0"
composeBom = "2024.09.00" composeBom = "2024.09.00"
composeMaterial3 = "1.5.6" composeMaterial3 = "1.6.2"
composeFoundation = "1.5.6" composeFoundation = "1.5.6"
composeUiTooling = "1.5.6" composeUiTooling = "1.5.6"
wearToolingPreview = "1.0.0" wearToolingPreview = "1.0.0"
@@ -9,4 +9,5 @@ class CardInfo (
val name: String, val name: String,
val codeType: BarcodeFormat, val codeType: BarcodeFormat,
val codeValue: String, val codeValue: String,
val color: String?
) )
@@ -6,17 +6,19 @@ import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
import com.google.zxing.BarcodeFormat import com.google.zxing.BarcodeFormat
class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 1) { class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?) : SQLiteOpenHelper(context, "omni_cards", factory, 2) {
override fun onCreate(db: SQLiteDatabase?) { override fun onCreate(db: SQLiteDatabase?) {
db!!.execSQL("CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT)") db!!.execSQL("CREATE TABLE IF NOT EXISTS cards (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, codeType TEXT, codeValue TEXT, color TEXT)")
} }
override fun onUpgrade( override fun onUpgrade(
db: SQLiteDatabase?, db: SQLiteDatabase?,
p1: Int, oldVersion: Int,
p2: Int p2: Int
) { ) {
db!!.execSQL("DROP TABLE IF EXISTS cards") if (oldVersion < 2) {
db!!.execSQL("ALTER TABLE cards ADD COLUMN color TEXT")
}
} }
fun clearDatabase() { fun clearDatabase() {
@@ -37,6 +39,7 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
values.put("name", card.name) values.put("name", card.name)
values.put("codeValue", card.codeValue) values.put("codeValue", card.codeValue)
values.put("codeType", card.codeType.name) values.put("codeType", card.codeType.name)
values.put("color", card.color)
db.insert("cards", null, values) db.insert("cards", null, values)
} }
@@ -55,7 +58,8 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")), id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
name = cursor.getString(cursor.getColumnIndexOrThrow("name")), name = cursor.getString(cursor.getColumnIndexOrThrow("name")),
codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))), codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))),
codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")) codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")),
color = cursor.getString(cursor.getColumnIndexOrThrow("color"))
)) ))
} }
@@ -81,7 +85,8 @@ class DbHelper(val context: Context, val factory: SQLiteDatabase.CursorFactory?)
id = cursor.getInt(cursor.getColumnIndexOrThrow("id")), id = cursor.getInt(cursor.getColumnIndexOrThrow("id")),
name = cursor.getString(cursor.getColumnIndexOrThrow("name")), name = cursor.getString(cursor.getColumnIndexOrThrow("name")),
codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))), codeType = BarcodeFormat.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("codeType"))),
codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")) codeValue = cursor.getString(cursor.getColumnIndexOrThrow("codeValue")),
color = cursor.getString(cursor.getColumnIndexOrThrow("color"))
) )
cursor.close() cursor.close()
@@ -0,0 +1,78 @@
package ru.omni_devel.cards.presentation
import android.content.Intent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.graphics.toColorInt
import androidx.wear.compose.material3.Button
import androidx.wear.compose.material3.ButtonDefaults
import androidx.wear.compose.material3.MaterialTheme
import androidx.wear.compose.material3.Text
@Composable
fun CardButton(cardInfo: CardInfo) {
val context = LocalContext.current
Box(modifier = Modifier.fillMaxWidth()) {
Button(
modifier = Modifier.fillMaxWidth(),
colors = ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.primaryContainer
),
contentPadding = PaddingValues(0.dp),
onClick = {
val intent = Intent(context, ShowCardActivity::class.java)
intent.putExtra("cardId", cardInfo.id)
context.startActivity(intent)
}
) {
Row(
modifier = Modifier
.fillMaxWidth()
.height(IntrinsicSize.Min)
.clip(ButtonDefaults.shape)
) {
Box(
modifier = Modifier
.fillMaxHeight()
.width(32.dp)
.background(
if (cardInfo.color == null)
MaterialTheme.colorScheme.secondaryContainer.copy(alpha = 0.625f)
else
Color(cardInfo.color.toColorInt()).copy(alpha = 0.625f)
)
)
Text(
cardInfo.name,
fontSize = 16.sp,
color = MaterialTheme.colorScheme.onPrimaryContainer,
modifier = Modifier
.padding(16.dp)
.fillMaxWidth()
.weight(1f),
textAlign = TextAlign.Start
)
}
}
}
}
@@ -1,7 +1,6 @@
package ru.omni_devel.cards.presentation package ru.omni_devel.cards.presentation
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
@@ -10,15 +9,11 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.wear.compose.foundation.lazy.TransformingLazyColumn import androidx.wear.compose.foundation.lazy.TransformingLazyColumn
import androidx.wear.compose.material3.Button
import androidx.wear.compose.material3.Text import androidx.wear.compose.material3.Text
import com.google.zxing.BarcodeFormat import com.google.zxing.BarcodeFormat
@@ -31,15 +26,7 @@ class MainActivity : ComponentActivity() {
db = DbHelper(this, null) db = DbHelper(this, null)
setContent { setContent {
// App(db.getCards()) App(db.getCards())
App(listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10).map {
CardInfo(
id = it,
name = "Card #$it",
codeType = BarcodeFormat.QR_CODE,
codeValue = "000$it"
)
})
} }
} }
} }
@@ -47,8 +34,6 @@ class MainActivity : ComponentActivity() {
@SuppressLint("UnusedBoxWithConstraintsScope") @SuppressLint("UnusedBoxWithConstraintsScope")
@Composable @Composable
fun App(cards: List<CardInfo>) { fun App(cards: List<CardInfo>) {
val context = LocalContext.current
BoxWithConstraints(modifier = Modifier.fillMaxSize()) { BoxWithConstraints(modifier = Modifier.fillMaxSize()) {
val topBottomPadding = maxHeight * 0.4f val topBottomPadding = maxHeight * 0.4f
@@ -70,22 +55,7 @@ fun App(cards: List<CardInfo>) {
) { ) {
for (card in cards) { for (card in cards) {
item { item {
Button( CardButton(card)
modifier = Modifier.fillMaxWidth(),
onClick = {
val intent = Intent(context, ShowCardActivity::class.java)
intent.putExtra("cardId", card.id)
context.startActivity(intent)
}
) {
Text(
text = card.name,
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center
)
}
} }
} }
} }