Initialer Upload neues Unity-Projekt
This commit is contained in:
93
app/src/main/java/com/dano/test1/HandlerLastPage.kt
Normal file
93
app/src/main/java/com/dano/test1/HandlerLastPage.kt
Normal file
@ -0,0 +1,93 @@
|
||||
package com.dano.test1
|
||||
|
||||
import android.view.View
|
||||
import android.widget.*
|
||||
import android.text.Html
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
class HandlerLastPage(
|
||||
private val answers: Map<String, Any>,
|
||||
private val languageID: String,
|
||||
private val goToNextQuestion: () -> Unit,
|
||||
private val goToPreviousQuestion: () -> Unit,
|
||||
private val saveAnswersToDatabase: suspend (Map<String, Any>) -> Unit
|
||||
) : QuestionHandler {
|
||||
|
||||
private lateinit var currentQuestion: QuestionItem.LastPage
|
||||
private lateinit var layout: View
|
||||
private val minLoadingTimeMs = 2000L // Minimum loading time in milliseconds (2 seconds)
|
||||
|
||||
override fun bind(layout: View, question: QuestionItem) {
|
||||
this.layout = layout
|
||||
currentQuestion = question as QuestionItem.LastPage
|
||||
|
||||
// Set localized text for the last page
|
||||
layout.findViewById<TextView>(R.id.textView).text =
|
||||
LanguageManager.getText(languageID, currentQuestion.textKey)
|
||||
|
||||
// Set question text with HTML formatting
|
||||
layout.findViewById<TextView>(R.id.question).text =
|
||||
Html.fromHtml(
|
||||
LanguageManager.getText(languageID, currentQuestion.question),
|
||||
Html.FROM_HTML_MODE_LEGACY
|
||||
)
|
||||
|
||||
// Setup previous button
|
||||
layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
|
||||
goToPreviousQuestion()
|
||||
}
|
||||
|
||||
// Setup finish button
|
||||
layout.findViewById<Button>(R.id.Qfinish).setOnClickListener {
|
||||
showLoading(true) // Show loading indicator
|
||||
|
||||
// Save answers on a background thread
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val startTime = System.currentTimeMillis()
|
||||
|
||||
// Save answers to database (suspend function)
|
||||
saveAnswersToDatabase(answers)
|
||||
|
||||
// Calculate total points and update global value
|
||||
GlobalValues.INTEGRATION_INDEX = sumPoints()
|
||||
|
||||
// Save last client code globally if available
|
||||
val clientCode = answers["client_code"] as? String
|
||||
if (clientCode != null) GlobalValues.LAST_CLIENT_CODE = clientCode
|
||||
|
||||
// Ensure loading animation runs at least 2 seconds
|
||||
val elapsedTime = System.currentTimeMillis() - startTime
|
||||
if (elapsedTime < minLoadingTimeMs) {
|
||||
delay(minLoadingTimeMs - elapsedTime)
|
||||
}
|
||||
|
||||
// Switch back to main thread to update UI
|
||||
withContext(Dispatchers.Main) {
|
||||
showLoading(false) // Hide loading indicator
|
||||
val activity = layout.context as? MainActivity
|
||||
activity?.finishQuestionnaire() ?: goToNextQuestion()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun validate(): Boolean = true // No validation needed on last page
|
||||
override fun saveAnswer() {} // No answers to save here
|
||||
|
||||
// Calculate the sum of all keys ending with "_points"
|
||||
private fun sumPoints(): Int =
|
||||
answers.filterKeys { it.endsWith("_points") }
|
||||
.values.mapNotNull { it as? Int }
|
||||
.sum()
|
||||
|
||||
// Show or hide a ProgressBar (loading spinner)
|
||||
private fun showLoading(show: Boolean) {
|
||||
val progressBar = layout.findViewById<ProgressBar>(R.id.progressBar)
|
||||
val finishButton = layout.findViewById<Button>(R.id.Qfinish)
|
||||
val prevButton = layout.findViewById<Button>(R.id.Qprev)
|
||||
|
||||
progressBar?.visibility = if (show) View.VISIBLE else View.GONE
|
||||
finishButton?.isEnabled = !show
|
||||
prevButton?.isEnabled = !show
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user