forked from omni/OmniCards
118 lines
3.6 KiB
Kotlin
118 lines
3.6 KiB
Kotlin
package ru.omni_devel.cards
|
|
|
|
import android.os.Bundle
|
|
import android.view.WindowManager
|
|
import android.widget.Toast
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.activity.enableEdgeToEdge
|
|
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.foundation.layout.padding
|
|
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.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.graphics.asImageBitmap
|
|
import androidx.compose.ui.layout.ContentScale
|
|
import androidx.compose.ui.res.stringResource
|
|
import androidx.compose.ui.text.style.TextAlign
|
|
import androidx.compose.ui.unit.dp
|
|
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
|
|
|
class ShowCardActivity : ComponentActivity() {
|
|
private lateinit var db: DbHelper
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
enableEdgeToEdge()
|
|
|
|
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(1f)
|
|
}
|
|
|
|
override fun onPause() {
|
|
super.onPause()
|
|
|
|
setBrightness(WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE)
|
|
}
|
|
|
|
private fun setBrightness(level: Float) {
|
|
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,
|
|
horizontalAlignment = Alignment.CenterHorizontally
|
|
) {
|
|
if (bitmap != null) {
|
|
Image(
|
|
bitmap = bitmap,
|
|
contentDescription = cardInfo.codeValue,
|
|
modifier = Modifier
|
|
.padding(bottom = 8.dp)
|
|
.fillMaxWidth()
|
|
.weight(1f, fill = false),
|
|
contentScale = ContentScale.Fit
|
|
)
|
|
Text(
|
|
cardInfo.codeValue,
|
|
modifier = Modifier.fillMaxWidth(),
|
|
textAlign = TextAlign.Center
|
|
)
|
|
} else {
|
|
Text(
|
|
stringResource(R.string.failed_to_generate_card_code),
|
|
modifier = Modifier.fillMaxWidth(),
|
|
textAlign = TextAlign.Center,
|
|
color = MaterialTheme.colorScheme.error
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|