forked from omni/OmniCards
refactor: rewrite mobile app in Compose and add error handling for wear
This commit is contained in:
@@ -2,35 +2,61 @@ package ru.omni_devel.cards
|
||||
|
||||
import android.os.Bundle
|
||||
import android.view.WindowManager
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.ImageBitmap
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
||||
|
||||
class ShowCardActivity : AppCompatActivity() {
|
||||
class ShowCardActivity : ComponentActivity() {
|
||||
private lateinit var db: DbHelper
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
setContentView(R.layout.activity_show_card)
|
||||
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
|
||||
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
|
||||
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
|
||||
insets
|
||||
}
|
||||
|
||||
db = DbHelper(this, null)
|
||||
|
||||
val cardId = intent.getIntExtra("cardId", -1)
|
||||
|
||||
val cardInfo = db.getCard(cardId)
|
||||
|
||||
if (cardInfo == null) {
|
||||
Toast.makeText(this, "Card is not exists", Toast.LENGTH_LONG).show()
|
||||
finish()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
setContent {
|
||||
OmniCardsTheme {
|
||||
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||||
ShowCardPage(innerPadding, cardInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
setBrightness(1.0f)
|
||||
renderCard()
|
||||
setBrightness(1f)
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
@@ -39,31 +65,46 @@ class ShowCardActivity : AppCompatActivity() {
|
||||
setBrightness(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE)
|
||||
}
|
||||
|
||||
private fun renderCard() {
|
||||
val cardId = intent.getIntExtra("cardId", 0)
|
||||
|
||||
val cardInfo = db.getCard(cardId)
|
||||
|
||||
if (cardInfo == null) {
|
||||
Toast.makeText(this, "cardInfo is null", Toast.LENGTH_LONG).show()
|
||||
return
|
||||
}
|
||||
|
||||
val cardCodeView: ImageView = findViewById(R.id.cardCodeView)
|
||||
val cardNameElement: TextView = findViewById(R.id.editCardActionLabel)
|
||||
|
||||
cardNameElement.text = cardInfo.name
|
||||
|
||||
val code = generateCode(cardInfo.codeValue, cardInfo.codeType)
|
||||
|
||||
cardCodeView.setImageBitmap(code)
|
||||
}
|
||||
|
||||
private fun setBrightness(level: Float) {
|
||||
val params = window.attributes
|
||||
|
||||
params.screenBrightness = level
|
||||
|
||||
window.attributes = params
|
||||
window.attributes.screenBrightness = level
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ShowCardPage(innerPadding: PaddingValues, cardInfo: CardInfo) {
|
||||
val bitmap = remember(cardInfo) {
|
||||
try {
|
||||
generateCode(cardInfo.codeValue, cardInfo.codeType).asImageBitmap()
|
||||
} catch (_: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
Page(cardInfo.name, innerPadding) {
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
if (bitmap != null) {
|
||||
Image(
|
||||
bitmap = bitmap,
|
||||
contentDescription = cardInfo.codeValue,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
contentScale = ContentScale.FillWidth
|
||||
)
|
||||
Text(
|
||||
cardInfo.codeValue,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textAlign = TextAlign.Center
|
||||
)
|
||||
} else {
|
||||
Text(
|
||||
"Failed to generate code. Check card data",
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
textAlign = TextAlign.Center,
|
||||
color = MaterialTheme.colorScheme.error
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user