604 lines
25 KiB
Kotlin
604 lines
25 KiB
Kotlin
package com.dano.test1
|
||
|
||
import android.content.res.ColorStateList
|
||
import android.graphics.Color
|
||
import android.util.TypedValue
|
||
import android.view.Gravity
|
||
import android.view.View
|
||
import android.widget.*
|
||
import com.google.android.material.button.MaterialButton
|
||
import org.json.JSONArray
|
||
import org.json.JSONObject
|
||
import java.io.File
|
||
|
||
var RHS_POINTS: Int? = null
|
||
|
||
class HandlerOpeningScreen(private val activity: MainActivity) {
|
||
|
||
private var languageID: String = "GERMAN"
|
||
private lateinit var editText: EditText
|
||
private lateinit var spinner: Spinner
|
||
private lateinit var textView: TextView
|
||
private lateinit var buttonContainer: LinearLayout
|
||
private lateinit var buttonLoad: Button
|
||
private lateinit var saveButton: Button
|
||
private lateinit var editButton: Button
|
||
private lateinit var uploadButton: Button
|
||
private lateinit var downloadButton: Button
|
||
private lateinit var databaseButton: Button
|
||
|
||
private val dynamicButtons = mutableListOf<Button>()
|
||
private val questionnaireFiles = mutableMapOf<Button, String>()
|
||
|
||
private data class CardParts(
|
||
val title: TextView,
|
||
val subtitle: TextView,
|
||
val chip: TextView
|
||
)
|
||
private val cardParts = mutableMapOf<Button, CardParts>()
|
||
|
||
private val buttonPoints: MutableMap<String, Int> = mutableMapOf()
|
||
private var questionnaireEntries: List<QuestionItem.QuestionnaireEntry> = emptyList()
|
||
|
||
private var uiFreeze: Boolean = false
|
||
|
||
// Feste Standard-Randfarben
|
||
private val STROKE_ENABLED = Color.parseColor("#8C79F2") // wenn anklickbar
|
||
private val STROKE_DISABLED = Color.parseColor("#D8D3F5") // wenn nicht anklickbar
|
||
|
||
private fun t(id: String) = LanguageManager.getText(languageID, id)
|
||
|
||
fun init() {
|
||
activity.setContentView(R.layout.opening_screen)
|
||
bindViews()
|
||
loadQuestionnaireOrder()
|
||
createQuestionnaireButtons()
|
||
restorePreviousClientCode()
|
||
setupLanguageSpinner()
|
||
setupLoadButton()
|
||
setupSaveButton()
|
||
setupEditButtonHandler()
|
||
setupUploadButton()
|
||
setupDownloadButton()
|
||
setupDatabaseButtonHandler()
|
||
|
||
val pathExists = File("/data/data/com.dano.test1/databases/questionnaire_database").exists()
|
||
updateMainButtonsState(pathExists)
|
||
if (pathExists && !editText.text.isNullOrBlank()) buttonLoad.performClick()
|
||
}
|
||
|
||
private fun bindViews() {
|
||
editText = activity.findViewById(R.id.editText)
|
||
spinner = activity.findViewById(R.id.string_spinner1)
|
||
textView = activity.findViewById(R.id.textView)
|
||
buttonContainer = activity.findViewById(R.id.buttonContainer)
|
||
buttonLoad = activity.findViewById(R.id.loadButton)
|
||
saveButton = activity.findViewById(R.id.saveButton)
|
||
editButton = activity.findViewById(R.id.editButton)
|
||
uploadButton = activity.findViewById(R.id.uploadButton)
|
||
downloadButton = activity.findViewById(R.id.downloadButton)
|
||
databaseButton = activity.findViewById(R.id.databaseButton)
|
||
|
||
val tag = editText.tag as? String ?: ""
|
||
editText.hint = t(tag)
|
||
textView.text = t("example_text")
|
||
}
|
||
|
||
private fun loadQuestionnaireOrder() {
|
||
try {
|
||
val inputStream = activity.assets.open("questionnaire_order.json")
|
||
val json = inputStream.bufferedReader().use { it.readText() }
|
||
val jsonArray = JSONArray(json)
|
||
|
||
questionnaireEntries = (0 until jsonArray.length()).map { i ->
|
||
val obj = jsonArray.getJSONObject(i)
|
||
val file = obj.getString("file")
|
||
val conditionObj = obj.optJSONObject("condition")
|
||
val condition = parseCondition(conditionObj)
|
||
val showPoints = obj.optBoolean("showPoints", false)
|
||
QuestionItem.QuestionnaireEntry(file, condition, showPoints)
|
||
}
|
||
} catch (_: Exception) {
|
||
questionnaireEntries = emptyList()
|
||
}
|
||
}
|
||
|
||
private fun parseCondition(conditionObj: JSONObject?): QuestionItem.Condition? {
|
||
if (conditionObj == null) return null
|
||
if (conditionObj.has("anyOf")) {
|
||
val arr = conditionObj.optJSONArray("anyOf") ?: JSONArray()
|
||
val conditions = mutableListOf<QuestionItem.Condition>()
|
||
for (i in 0 until arr.length()) {
|
||
val sub = arr.optJSONObject(i)
|
||
parseCondition(sub)?.let { conditions.add(it) }
|
||
}
|
||
return QuestionItem.Condition.AnyOf(conditions)
|
||
}
|
||
if (conditionObj.has("alwaysAvailable") && conditionObj.optBoolean("alwaysAvailable", false)) {
|
||
return QuestionItem.Condition.AlwaysAvailable
|
||
}
|
||
val requiresList = mutableListOf<String>()
|
||
if (conditionObj.has("requiresCompleted")) {
|
||
val reqArr = conditionObj.optJSONArray("requiresCompleted")
|
||
if (reqArr != null) {
|
||
for (i in 0 until reqArr.length()) requiresList.add(reqArr.optString(i))
|
||
} else {
|
||
conditionObj.optString("requiresCompleted")?.let { if (it.isNotBlank()) requiresList.add(it) }
|
||
}
|
||
}
|
||
val questionnaire = conditionObj.optString("questionnaire", null)
|
||
val questionId = conditionObj.optString("questionId", null)
|
||
val operator = conditionObj.optString("operator", null)
|
||
val value = conditionObj.optString("value", null)
|
||
val hasQuestionCheck =
|
||
!questionnaire.isNullOrBlank() && !questionId.isNullOrBlank() && !operator.isNullOrBlank() && value != null
|
||
return when {
|
||
requiresList.isNotEmpty() && hasQuestionCheck ->
|
||
QuestionItem.Condition.Combined(requiresList, QuestionItem.Condition.QuestionCondition(questionnaire!!, questionId!!, operator!!, value!!))
|
||
hasQuestionCheck ->
|
||
QuestionItem.Condition.QuestionCondition(questionnaire!!, questionId!!, operator!!, value!!)
|
||
requiresList.isNotEmpty() ->
|
||
QuestionItem.Condition.RequiresCompleted(requiresList)
|
||
else -> null
|
||
}
|
||
}
|
||
|
||
private fun createQuestionnaireButtons() {
|
||
buttonContainer.removeAllViews()
|
||
dynamicButtons.clear()
|
||
questionnaireFiles.clear()
|
||
cardParts.clear()
|
||
|
||
val vMargin = dp(8)
|
||
val startEnabled = mutableListOf<Button>()
|
||
|
||
questionnaireEntries.forEachIndexed { index, entry ->
|
||
val row = FrameLayout(activity).apply {
|
||
layoutParams = LinearLayout.LayoutParams(
|
||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||
LinearLayout.LayoutParams.WRAP_CONTENT
|
||
).also { it.setMargins(0, vMargin, 0, vMargin) }
|
||
}
|
||
|
||
val btn = MaterialButton(activity).apply {
|
||
layoutParams = FrameLayout.LayoutParams(
|
||
FrameLayout.LayoutParams.MATCH_PARENT,
|
||
dp(84)
|
||
)
|
||
text = ""
|
||
isAllCaps = false
|
||
setMinimumWidth(0)
|
||
setMinimumHeight(0)
|
||
cornerRadius = dp(22)
|
||
strokeWidth = dp(1)
|
||
strokeColor = ColorStateList.valueOf(STROKE_DISABLED)
|
||
backgroundTintList = ColorStateList.valueOf(Color.WHITE)
|
||
rippleColor = ColorStateList.valueOf(Color.parseColor("#22000000"))
|
||
gravity = Gravity.START or Gravity.CENTER_VERTICAL
|
||
setPadding(dp(20), 0, dp(20), 0)
|
||
id = View.generateViewId()
|
||
setOnClickListener {
|
||
GlobalValues.LAST_CLIENT_CODE = GlobalValues.LOADED_CLIENT_CODE
|
||
val fileName = questionnaireFiles[this] ?: return@setOnClickListener
|
||
val questionnaire = QuestionnaireGeneric(fileName)
|
||
startQuestionnaire(questionnaire)
|
||
applySetButtonsEnabled(dynamicButtons.filter { it == this }, allowCompleted = false, force = false)
|
||
}
|
||
}
|
||
|
||
val textColumn = LinearLayout(activity).apply {
|
||
orientation = LinearLayout.VERTICAL
|
||
layoutParams = FrameLayout.LayoutParams(
|
||
FrameLayout.LayoutParams.WRAP_CONTENT,
|
||
FrameLayout.LayoutParams.WRAP_CONTENT,
|
||
Gravity.START or Gravity.CENTER_VERTICAL
|
||
).also { it.marginStart = dp(24) }
|
||
}
|
||
val tvTitle = TextView(activity).apply {
|
||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
|
||
setTextColor(Color.parseColor("#2F2A49"))
|
||
setPadding(0, dp(6), 0, dp(2))
|
||
}
|
||
val tvSubtitle = TextView(activity).apply {
|
||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 13f)
|
||
setTextColor(Color.parseColor("#7B7794"))
|
||
visibility = View.GONE
|
||
}
|
||
textColumn.addView(tvTitle)
|
||
textColumn.addView(tvSubtitle)
|
||
|
||
val chip = TextView(activity).apply {
|
||
setPadding(dp(14), dp(8), dp(14), dp(8))
|
||
setTextColor(Color.WHITE)
|
||
text = t("start")
|
||
setBackgroundResource(R.drawable.bg_chip_amber)
|
||
setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
|
||
layoutParams = FrameLayout.LayoutParams(
|
||
FrameLayout.LayoutParams.WRAP_CONTENT,
|
||
FrameLayout.LayoutParams.WRAP_CONTENT,
|
||
Gravity.END or Gravity.CENTER_VERTICAL
|
||
).also { it.marginEnd = dp(16) }
|
||
}
|
||
|
||
row.addView(btn)
|
||
row.addView(textColumn)
|
||
row.addView(chip)
|
||
|
||
buttonContainer.addView(row)
|
||
dynamicButtons.add(btn)
|
||
questionnaireFiles[btn] = entry.file
|
||
cardParts[btn] = CardParts(tvTitle, tvSubtitle, chip)
|
||
|
||
tvTitle.text = "Questionnaire ${index + 1}"
|
||
|
||
if (entry.condition is QuestionItem.Condition.AlwaysAvailable) startEnabled.add(btn)
|
||
}
|
||
|
||
applyUpdateButtonTexts(force = false)
|
||
applySetButtonsEnabled(startEnabled, allowCompleted = false, force = false)
|
||
}
|
||
|
||
private fun restorePreviousClientCode() {
|
||
GlobalValues.LAST_CLIENT_CODE?.let { editText.setText(it) }
|
||
}
|
||
|
||
private fun setupLanguageSpinner() {
|
||
val languages = listOf("GERMAN", "ENGLISH", "FRENCH", "ROMANIAN", "ARABIC", "POLISH", "TURKISH", "UKRAINIAN", "RUSSIAN", "SPANISH")
|
||
val adapter = ArrayAdapter(activity, android.R.layout.simple_spinner_item, languages).apply {
|
||
setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||
}
|
||
spinner.adapter = adapter
|
||
spinner.setSelection(languages.indexOf(languageID))
|
||
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
|
||
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
|
||
languageID = languages[position]
|
||
applyUpdateButtonTexts(force = false)
|
||
val hintTag = editText.tag as? String ?: ""
|
||
editText.hint = t(hintTag)
|
||
}
|
||
override fun onNothingSelected(parent: AdapterView<*>) {}
|
||
}
|
||
}
|
||
|
||
private fun setupLoadButton() {
|
||
LoadButtonHandler(
|
||
activity = activity,
|
||
loadButton = buttonLoad,
|
||
editText = editText,
|
||
languageIDProvider = { languageID },
|
||
questionnaireEntriesProvider = { questionnaireEntries },
|
||
dynamicButtonsProvider = { dynamicButtons },
|
||
buttonPoints = buttonPoints,
|
||
updateButtonTexts = { applyUpdateButtonTexts(force = false) },
|
||
setButtonsEnabled = { list -> applySetButtonsEnabled(list, allowCompleted = false, force = false) },
|
||
updateMainButtonsState = { updateMainButtonsState(it) },
|
||
).setup()
|
||
}
|
||
|
||
private fun applyUpdateButtonTexts(force: Boolean) {
|
||
if (uiFreeze && !force) return
|
||
|
||
questionnaireFiles.forEach { (button, fileName) ->
|
||
val entry = questionnaireEntries.firstOrNull { it.file == fileName }
|
||
val key = fileName.substringAfter("questionnaire_").substringAfter("_").removeSuffix(".json")
|
||
val titleText = t(key)
|
||
|
||
val parts = cardParts[button] ?: return@forEach
|
||
parts.title.text = titleText
|
||
|
||
val points = buttonPoints.entries.firstOrNull { fileName.contains(it.key, ignoreCase = true) }?.value
|
||
val completed = isCompleted(button)
|
||
val enabled = button.isEnabled
|
||
val locked = !enabled && !completed
|
||
|
||
// Rahmenbreite & -farbe je nach Klickbarkeit
|
||
setClickableStroke(button, enabled)
|
||
|
||
if (locked) {
|
||
setLockedAppearance(button, true)
|
||
parts.title.setTextColor(Color.WHITE)
|
||
parts.subtitle.setTextColor(Color.parseColor("#E0E0E0"))
|
||
} else {
|
||
setLockedAppearance(button, false)
|
||
parts.title.setTextColor(Color.parseColor("#2F2A49"))
|
||
parts.subtitle.setTextColor(Color.parseColor("#7B7794"))
|
||
// Tönung nur wenn showPoints == true
|
||
applyTintForButton(button, points, emphasize = enabled)
|
||
}
|
||
|
||
if (entry?.showPoints == true && points != null) {
|
||
parts.subtitle.visibility = View.VISIBLE
|
||
parts.subtitle.text = "${t("points")}: $points"
|
||
} else {
|
||
parts.subtitle.visibility = View.GONE
|
||
parts.subtitle.text = ""
|
||
}
|
||
|
||
when {
|
||
completed -> {
|
||
parts.chip.text = t("done")
|
||
parts.chip.setBackgroundResource(R.drawable.bg_chip_green)
|
||
parts.chip.setTextColor(Color.WHITE)
|
||
}
|
||
enabled -> {
|
||
parts.chip.text = t("start")
|
||
parts.chip.setBackgroundResource(R.drawable.bg_chip_amber)
|
||
parts.chip.setTextColor(Color.WHITE)
|
||
}
|
||
else -> {
|
||
parts.chip.text = t("locked")
|
||
parts.chip.setBackgroundResource(R.drawable.bg_chip_grey)
|
||
parts.chip.setTextColor(Color.parseColor("#2F2A49"))
|
||
}
|
||
}
|
||
}
|
||
|
||
buttonLoad.text = t("load")
|
||
saveButton.text = t("save")
|
||
editButton.text = t("edit")
|
||
uploadButton.text = t("upload")
|
||
downloadButton.text = t("download")
|
||
databaseButton.text = t("database")
|
||
|
||
val hintTag = editText.tag as? String ?: ""
|
||
editText.hint = t(hintTag)
|
||
textView.text = t("example_text")
|
||
}
|
||
|
||
private fun applySetButtonsEnabled(enabledButtons: List<Button>, allowCompleted: Boolean, force: Boolean) {
|
||
if (uiFreeze && !force) return
|
||
|
||
questionnaireFiles.keys.forEach { button ->
|
||
val completed = isCompleted(button)
|
||
val isAllowed = enabledButtons.contains(button)
|
||
val shouldEnable = if (allowCompleted) isAllowed else isAllowed && !completed
|
||
val locked = !shouldEnable && !completed
|
||
|
||
button.isEnabled = shouldEnable
|
||
button.alpha = if (completed || shouldEnable) 1.0f else 0.6f
|
||
|
||
// Rahmenbreite & -farbe je nach Klickbarkeit
|
||
setClickableStroke(button, shouldEnable)
|
||
|
||
cardParts[button]?.let { parts ->
|
||
if (locked) {
|
||
setLockedAppearance(button, true)
|
||
parts.title.setTextColor(Color.WHITE)
|
||
parts.subtitle.setTextColor(Color.parseColor("#E0E0E0"))
|
||
} else {
|
||
setLockedAppearance(button, false)
|
||
parts.title.setTextColor(Color.parseColor("#2F2A49"))
|
||
parts.subtitle.setTextColor(Color.parseColor("#7B7794"))
|
||
// Tönung nur wenn showPoints == true
|
||
applyTintForButton(button, getPointsForButton(button), emphasize = shouldEnable)
|
||
}
|
||
|
||
when {
|
||
completed -> {
|
||
parts.chip.text = t("done")
|
||
parts.chip.setBackgroundResource(R.drawable.bg_chip_green)
|
||
parts.chip.setTextColor(Color.WHITE)
|
||
}
|
||
shouldEnable -> {
|
||
parts.chip.text = t("start")
|
||
parts.chip.setBackgroundResource(R.drawable.bg_chip_amber)
|
||
parts.chip.setTextColor(Color.WHITE)
|
||
}
|
||
else -> {
|
||
parts.chip.text = t("locked")
|
||
parts.chip.setBackgroundResource(R.drawable.bg_chip_grey)
|
||
parts.chip.setTextColor(Color.parseColor("#2F2A49"))
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// öffentliche Wrapper
|
||
private fun updateButtonTexts() = applyUpdateButtonTexts(force = false)
|
||
private fun setButtonsEnabled(enabledButtons: List<Button>, allowCompleted: Boolean = false) =
|
||
applySetButtonsEnabled(enabledButtons, allowCompleted, force = false)
|
||
|
||
private fun startQuestionnaire(questionnaire: QuestionnaireBase<*>) {
|
||
activity.startQuestionnaire(questionnaire, languageID)
|
||
}
|
||
|
||
fun onBackPressed(): Boolean = false
|
||
|
||
private fun setupSaveButton() {
|
||
SaveButtonHandler(
|
||
activity = activity,
|
||
saveButton = saveButton,
|
||
editText = editText,
|
||
languageIDProvider = { languageID }
|
||
).setup()
|
||
}
|
||
|
||
private fun setupEditButtonHandler() {
|
||
EditButtonHandler(
|
||
activity = activity,
|
||
editButton = editButton,
|
||
editText = editText,
|
||
languageIDProvider = { languageID },
|
||
questionnaireFiles = questionnaireFiles,
|
||
buttonPoints = buttonPoints,
|
||
updateButtonTexts = { applyUpdateButtonTexts(force = true) },
|
||
setButtonsEnabled = { list, allowCompleted ->
|
||
applySetButtonsEnabled(list, allowCompleted, force = true)
|
||
},
|
||
setUiFreeze = { freeze -> uiFreeze = freeze },
|
||
triggerLoad = { buttonLoad.performClick() }
|
||
).setup()
|
||
}
|
||
|
||
private fun setupUploadButton() {
|
||
uploadButton.text = t("upload")
|
||
uploadButton.setOnClickListener {
|
||
GlobalValues.LAST_CLIENT_CODE = editText.text.toString().trim()
|
||
promptCredentials(
|
||
title = t("login_required") ?: "Login erforderlich",
|
||
onOk = { user, pass ->
|
||
// Login -> Upload
|
||
DatabaseUploader.uploadDatabaseWithLogin(activity, user, pass)
|
||
}
|
||
)
|
||
}
|
||
}
|
||
|
||
private fun setupDownloadButton() {
|
||
downloadButton.text = t("download")
|
||
downloadButton.setOnClickListener {
|
||
GlobalValues.LAST_CLIENT_CODE = editText.text.toString().trim()
|
||
promptCredentials(
|
||
title = t("login_required") ?: "Login erforderlich",
|
||
onOk = { user, pass ->
|
||
// Login -> Token -> Download
|
||
LoginManager.loginUserWithCredentials(
|
||
context = activity,
|
||
username = user,
|
||
password = pass,
|
||
onSuccess = { token ->
|
||
Toast.makeText(activity, t("login_ok") ?: "Login OK", Toast.LENGTH_SHORT).show()
|
||
DatabaseDownloader.downloadAndReplaceDatabase(activity, token)
|
||
updateMainButtonsState(true)
|
||
},
|
||
onError = { error ->
|
||
Toast.makeText(activity, error, Toast.LENGTH_LONG).show()
|
||
}
|
||
)
|
||
}
|
||
)
|
||
}
|
||
}
|
||
|
||
private fun promptCredentials(
|
||
title: String,
|
||
onOk: (username: String, password: String) -> Unit
|
||
) {
|
||
val wrapper = LinearLayout(activity).apply {
|
||
orientation = LinearLayout.VERTICAL
|
||
setPadding(dp(20), dp(8), dp(20), 0)
|
||
}
|
||
val etUser = EditText(activity).apply {
|
||
hint = "Username"
|
||
setSingleLine()
|
||
}
|
||
val etPass = EditText(activity).apply {
|
||
hint = "Passwort"
|
||
setSingleLine()
|
||
inputType = android.text.InputType.TYPE_CLASS_TEXT or
|
||
android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD
|
||
}
|
||
wrapper.addView(etUser)
|
||
wrapper.addView(etPass)
|
||
|
||
android.app.AlertDialog.Builder(activity)
|
||
.setTitle(title)
|
||
.setView(wrapper)
|
||
.setPositiveButton("OK") { _, _ ->
|
||
val u = etUser.text.toString().trim()
|
||
val p = etPass.text.toString()
|
||
if (u.isNotEmpty() && p.isNotEmpty()) {
|
||
onOk(u, p)
|
||
} else {
|
||
Toast.makeText(activity, t("enter_password") ?: "Bitte Username & Passwort eingeben", Toast.LENGTH_SHORT).show()
|
||
}
|
||
}
|
||
.setNegativeButton(t("cancel") ?: "Abbrechen", null)
|
||
.show()
|
||
}
|
||
|
||
private fun setupDatabaseButtonHandler() {
|
||
DatabaseButtonHandler(
|
||
activity = activity,
|
||
databaseButton = databaseButton,
|
||
onClose = { init() },
|
||
languageIDProvider = { languageID }
|
||
).setup()
|
||
}
|
||
|
||
private fun updateMainButtonsState(isDatabaseAvailable: Boolean) {
|
||
listOf(buttonLoad, saveButton, editButton, databaseButton).forEach { b ->
|
||
b.isEnabled = isDatabaseAvailable
|
||
b.alpha = if (isDatabaseAvailable) 1.0f else 0.5f
|
||
}
|
||
}
|
||
|
||
private fun dp(v: Int): Int =
|
||
(v * activity.resources.displayMetrics.density).toInt()
|
||
|
||
private fun isCompleted(button: Button): Boolean {
|
||
val fileName = questionnaireFiles[button] ?: return false
|
||
return buttonPoints.keys.any { k ->
|
||
fileName.contains(k, ignoreCase = true) || k.contains(fileName, ignoreCase = true)
|
||
}
|
||
}
|
||
|
||
private fun getPointsForButton(button: Button): Int? {
|
||
val fileName = questionnaireFiles[button] ?: return null
|
||
return buttonPoints.entries.firstOrNull { (k, _) ->
|
||
fileName.contains(k, ignoreCase = true) || k.contains(fileName, ignoreCase = true)
|
||
}?.value
|
||
}
|
||
|
||
/** Locked-Optik: schwarzer Hintergrund, fester grauer Rand */
|
||
private fun setLockedAppearance(button: Button, locked: Boolean) {
|
||
val mb = button as? MaterialButton ?: return
|
||
if (locked) {
|
||
mb.backgroundTintList = ColorStateList.valueOf(Color.BLACK)
|
||
mb.strokeColor = ColorStateList.valueOf(STROKE_DISABLED)
|
||
} else {
|
||
mb.backgroundTintList = ColorStateList.valueOf(Color.WHITE)
|
||
mb.strokeColor = ColorStateList.valueOf(if (button.isEnabled) STROKE_ENABLED else STROKE_DISABLED)
|
||
}
|
||
}
|
||
|
||
/** Standardfarben wiederherstellen (wenn showPoints == false) – hier nicht mehr für locked zuständig */
|
||
private fun resetTint(button: Button) {
|
||
val mb = button as? MaterialButton ?: return
|
||
mb.backgroundTintList = ColorStateList.valueOf(Color.WHITE)
|
||
mb.strokeColor = ColorStateList.valueOf(STROKE_DISABLED)
|
||
}
|
||
|
||
/** Tönung nur anwenden, wenn showPoints == true (und Button nicht locked) */
|
||
private fun applyTintForButton(button: Button, points: Int?, emphasize: Boolean) {
|
||
val file = questionnaireFiles[button] ?: return
|
||
val entry = questionnaireEntries.firstOrNull { it.file == file }
|
||
|
||
if (entry?.showPoints != true) {
|
||
val mb = button as? MaterialButton ?: return
|
||
mb.backgroundTintList = ColorStateList.valueOf(if (emphasize) Color.parseColor("#F1EEFF") else Color.WHITE)
|
||
mb.strokeColor = ColorStateList.valueOf(if (emphasize) STROKE_ENABLED else STROKE_DISABLED)
|
||
return
|
||
}
|
||
|
||
setScoreTint(button, points, emphasize)
|
||
}
|
||
|
||
/** Dickere Kontur, wenn anklickbar; Farbe bleibt fix */
|
||
private fun setClickableStroke(button: Button, enabled: Boolean) {
|
||
val mb = button as? MaterialButton ?: return
|
||
mb.strokeWidth = if (enabled) dp(2) else dp(1)
|
||
mb.strokeColor = ColorStateList.valueOf(if (enabled) STROKE_ENABLED else STROKE_DISABLED)
|
||
}
|
||
|
||
/** Hintergrund nach Score; Rand bleibt fix */
|
||
private fun setScoreTint(button: Button, points: Int?, emphasize: Boolean) {
|
||
val mb = button as? MaterialButton ?: return
|
||
|
||
val bg = when {
|
||
points == null && emphasize -> Color.parseColor("#F1EEFF")
|
||
points == null -> Color.parseColor("#FFFFFF")
|
||
points in 0..12 && emphasize -> Color.parseColor("#C8E6C9")
|
||
points in 0..12 -> Color.parseColor("#E8F5E9")
|
||
points in 13..36 && emphasize -> Color.parseColor("#FFE0B2")
|
||
points in 13..36 -> Color.parseColor("#FFF8E1")
|
||
points >= 37 && emphasize -> Color.parseColor("#FFCDD2")
|
||
else -> Color.parseColor("#FFEBEE")
|
||
}
|
||
|
||
mb.backgroundTintList = ColorStateList.valueOf(bg)
|
||
mb.strokeColor = ColorStateList.valueOf(if (emphasize) STROKE_ENABLED else STROKE_DISABLED)
|
||
}
|
||
}
|