108 lines
3.5 KiB
Kotlin
108 lines
3.5 KiB
Kotlin
package ru.omni_devel.cards
|
|
|
|
import android.app.Activity
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.widget.Toast
|
|
import androidx.activity.ComponentActivity
|
|
import androidx.activity.compose.LocalActivity
|
|
import androidx.activity.compose.setContent
|
|
import androidx.activity.enableEdgeToEdge
|
|
import androidx.compose.foundation.layout.PaddingValues
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
import androidx.compose.material3.OutlinedTextField
|
|
import androidx.compose.material3.Scaffold
|
|
import androidx.compose.material3.Text
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.runtime.getValue
|
|
import androidx.compose.runtime.setValue
|
|
import androidx.compose.runtime.mutableStateOf
|
|
import androidx.compose.runtime.saveable.rememberSaveable
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.compose.ui.platform.LocalContext
|
|
import androidx.compose.ui.res.stringResource
|
|
import ru.omni_devel.cards.ui.theme.OmniCardsTheme
|
|
|
|
class FillTemplateActivity : ComponentActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
enableEdgeToEdge()
|
|
|
|
val templateJson = intent.getStringExtra("templateJson") ?: ""
|
|
val template = Template.parseTemplateFromJson(templateJson)
|
|
|
|
setContent {
|
|
OmniCardsTheme {
|
|
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
|
FillTemplatePage(template, innerPadding)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun FillTemplatePage(template: Template, innerPadding: PaddingValues) {
|
|
val activityContext = LocalActivity.current
|
|
val context = LocalContext.current
|
|
|
|
val language = getLanguage(context)
|
|
|
|
var filledFields by rememberSaveable { mutableStateOf(template.fields.associate { field ->
|
|
field.id to (field.default ?: "")
|
|
}) }
|
|
|
|
Page(
|
|
selectItemNameViaLanguage(template.names, language),
|
|
innerPadding
|
|
) {
|
|
for (field in template.fields) {
|
|
OutlinedTextField(
|
|
value = filledFields[field.id]!!,
|
|
onValueChange = { newValue ->
|
|
filledFields = filledFields.toMutableMap().apply {
|
|
this[field.id] = newValue
|
|
}
|
|
},
|
|
modifier = Modifier.fillMaxWidth(),
|
|
label = { Text((if (field.maybeEmpty) "" else "*") + selectItemNameViaLanguage(field.names, language)) }
|
|
)
|
|
}
|
|
|
|
AdaptiveButton(
|
|
modifier = Modifier.fillMaxWidth(),
|
|
onClick = {
|
|
var isSuccess = true
|
|
|
|
for (field in template.fields) {
|
|
if (filledFields[field.id]!!.isEmpty() && !field.maybeEmpty) {
|
|
isSuccess = false
|
|
}
|
|
}
|
|
|
|
if (!isSuccess) {
|
|
Toast.makeText(context, context.getString(R.string.error_in_data), Toast.LENGTH_LONG).show()
|
|
|
|
return@AdaptiveButton
|
|
}
|
|
|
|
var result = template.template
|
|
|
|
for ((id, value) in filledFields) {
|
|
result = result.replace("$$id$", value)
|
|
}
|
|
|
|
val resultIntent = Intent()
|
|
|
|
resultIntent.putExtra("codeValue", result)
|
|
|
|
activityContext!!.setResult(Activity.RESULT_OK, resultIntent)
|
|
activityContext.finish()
|
|
}
|
|
) {
|
|
Text(stringResource(R.string.do_save))
|
|
}
|
|
}
|
|
}
|