Initialer Upload neues Unity-Projekt
This commit is contained in:
154
app/src/main/java/com/dano/test1/HandlerDateSpinner.kt
Normal file
154
app/src/main/java/com/dano/test1/HandlerDateSpinner.kt
Normal file
@ -0,0 +1,154 @@
|
||||
package com.dano.test1
|
||||
|
||||
import android.content.Context
|
||||
import android.view.View
|
||||
import android.widget.*
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
class HandlerDateSpinner(
|
||||
private val context: Context,
|
||||
private val answers: MutableMap<String, Any>,
|
||||
private val languageID: String,
|
||||
private val goToNextQuestion: () -> Unit,
|
||||
private val goToPreviousQuestion: () -> Unit,
|
||||
private val showToast: (String) -> Unit
|
||||
) : QuestionHandler {
|
||||
|
||||
private lateinit var question: QuestionItem.DateSpinnerQuestion
|
||||
private lateinit var layout: View
|
||||
|
||||
private lateinit var spinnerDay: Spinner
|
||||
private lateinit var spinnerMonth: Spinner
|
||||
private lateinit var spinnerYear: Spinner
|
||||
|
||||
override fun bind(layout: View, question: QuestionItem) {
|
||||
if (question !is QuestionItem.DateSpinnerQuestion) return
|
||||
|
||||
this.layout = layout
|
||||
this.question = question
|
||||
|
||||
initViews()
|
||||
|
||||
val questionTextView = layout.findViewById<TextView>(R.id.question)
|
||||
val textView = layout.findViewById<TextView>(R.id.textView)
|
||||
|
||||
questionTextView.text = question.question?.let { LanguageManager.getText(languageID, it) } ?: ""
|
||||
textView.text = question.textKey?.let { LanguageManager.getText(languageID, it) } ?: ""
|
||||
|
||||
val (savedYear, savedMonthIndex, savedDay) = question.question?.let {
|
||||
parseSavedDate(answers[it] as? String)
|
||||
} ?: Triple(null, null, null)
|
||||
|
||||
val days = (1..31).toList()
|
||||
val months = Months.getAllMonths(languageID)
|
||||
val years = (1900..MAX_VALUE_YEAR + 1).toList().reversed()
|
||||
|
||||
val today = Calendar.getInstance()
|
||||
val defaultDay = savedDay ?: today.get(Calendar.DAY_OF_MONTH)
|
||||
val defaultMonth = savedMonthIndex?.takeIf { it in months.indices }?.let { months[it] }
|
||||
?: months[today.get(Calendar.MONTH)]
|
||||
val defaultYear = savedYear ?: today.get(Calendar.YEAR)
|
||||
|
||||
setupSpinner(spinnerDay, days, defaultDay)
|
||||
setupSpinner(spinnerMonth, months, defaultMonth)
|
||||
setupSpinner(spinnerYear, years, defaultYear)
|
||||
|
||||
layout.findViewById<Button>(R.id.Qnext).setOnClickListener {
|
||||
if (validate()) {
|
||||
saveAnswer()
|
||||
goToNextQuestion()
|
||||
}
|
||||
}
|
||||
|
||||
layout.findViewById<Button>(R.id.Qprev).setOnClickListener {
|
||||
goToPreviousQuestion()
|
||||
}
|
||||
}
|
||||
|
||||
private fun initViews() {
|
||||
spinnerDay = layout.findViewById(R.id.spinner_value_day)
|
||||
spinnerMonth = layout.findViewById(R.id.spinner_value_month)
|
||||
spinnerYear = layout.findViewById(R.id.spinner_value_year)
|
||||
}
|
||||
|
||||
override fun validate(): Boolean {
|
||||
val day = spinnerDay.selectedItem.toString().padStart(2, '0')
|
||||
val month = spinnerMonth.selectedItem as Month
|
||||
val year = spinnerYear.selectedItem.toString()
|
||||
|
||||
val allMonths = Months.getAllMonths(languageID)
|
||||
val monthNumber = (allMonths.indexOf(month) + 1).toString().padStart(2, '0')
|
||||
val selectedDateString = "$year-$monthNumber-$day"
|
||||
val selectedDate = parseDate(selectedDateString)
|
||||
|
||||
question.constraints?.notBefore?.let { key ->
|
||||
val referenceDateString = answers[key] as? String
|
||||
val referenceDate = referenceDateString?.let { parseDate(it) }
|
||||
if (referenceDate != null && selectedDate.before(referenceDate)) {
|
||||
val message1 = LanguageManager.getText(languageID, "date_after")
|
||||
val message2 = LanguageManager.getText(languageID, "lay")
|
||||
showToast(message1 + " " + referenceDateString + " " + message2)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
question.constraints?.notAfter?.let { key ->
|
||||
val referenceDate = when (key) {
|
||||
"today" -> Date()
|
||||
else -> (answers[key] as? String)?.let { parseDate(it) }
|
||||
}
|
||||
if (referenceDate != null && selectedDate.after(referenceDate)) {
|
||||
val message1 = LanguageManager.getText(languageID, "date_before")
|
||||
val message2 = LanguageManager.getText(languageID, "lay")
|
||||
showToast(message1 + " " + referenceDate + " " + message2)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun saveAnswer() {
|
||||
val day = spinnerDay.selectedItem.toString().padStart(2, '0')
|
||||
val month = spinnerMonth.selectedItem as Month
|
||||
val year = spinnerYear.selectedItem.toString()
|
||||
|
||||
val allMonths = Months.getAllMonths(languageID)
|
||||
val monthNumber = (allMonths.indexOf(month) + 1).toString().padStart(2, '0')
|
||||
|
||||
question.question?.let { key ->
|
||||
answers[key] = "$year-$monthNumber-$day"
|
||||
}
|
||||
}
|
||||
|
||||
private fun parseSavedDate(dateString: String?): Triple<Int?, Int?, Int?> {
|
||||
if (dateString.isNullOrBlank()) return Triple(null, null, null)
|
||||
val parts = dateString.split("-")
|
||||
if (parts.size != 3) return Triple(null, null, null)
|
||||
|
||||
val year = parts[0].toIntOrNull()
|
||||
val monthIndex = parts[1].toIntOrNull()?.minus(1)
|
||||
val day = parts[2].toIntOrNull()
|
||||
|
||||
return Triple(year, monthIndex, day)
|
||||
}
|
||||
|
||||
private fun parseDate(dateString: String): Date {
|
||||
val sdf = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
|
||||
return sdf.parse(dateString)
|
||||
}
|
||||
|
||||
private fun <T> setupSpinner(spinner: Spinner, items: List<T>, defaultSelection: T?) {
|
||||
val adapter = ArrayAdapter(context, android.R.layout.simple_spinner_item, items)
|
||||
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
|
||||
spinner.adapter = adapter
|
||||
|
||||
defaultSelection?.let {
|
||||
val index = items.indexOf(it)
|
||||
if (index >= 0) {
|
||||
spinner.setSelection(index)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user