Код: Выделить всё
@Parcelize
data class DataSurvey(
@field:SerializedName("name")
val name: String,
@field:SerializedName("id")
val id: String,
@field:SerializedName("items")
val items: List,
@field:SerializedName("mobile_only")
val isMobile: Boolean,
) : Parcelable
@Parcelize
data class SurveyItem(
@field:SerializedName("survey_id")
val surveyId: String,
@field:SerializedName("name")
val name: String,
@field:SerializedName("survey_type_id")
val surveyTypeId: Int,
@field:SerializedName("id")
val id: String,
@field:SerializedName("value")
var valueItem: @RawValue Any? = null,
@field:SerializedName("options")
var options: ArrayList? = arrayListOf(),
var imageUri: Uri? = null,
var isComplete: @RawValue MutableLiveData = MutableLiveData(false),
) : Parcelable
Код: Выделить всё
class CheckInViewModel : ViewModel() {
// LiveData for etCheckInNotes
val etCheckInNotesLiveData: MutableLiveData = MutableLiveData()
// Combined LiveData for etCheckInNotes and surveyItem
val combinedLiveData: MediatorLiveData = MediatorLiveData()
val chosenSurvey = MutableLiveData()
val listImageUri = MutableLiveData().apply {
value = arrayListOf()
}
init {
combinedLiveData.addSource(chosenSurvey) { surveyItem ->
val checkInData = CheckInData(
notes = etCheckInNotesLiveData.value,
surveyItems = surveyItem,
imageUri = listImageUri.value ?: arrayListOf(),
)
combinedLiveData.value = checkInData
}
combinedLiveData.addSource(etCheckInNotesLiveData) { notes ->
val checkInData = CheckInData(
notes = notes,
surveyItems = chosenSurvey.value ?: arrayListOf(),
imageUri = listImageUri.value ?: arrayListOf(),
)
combinedLiveData.value = checkInData
}
combinedLiveData.addSource(listImageUri) { imageUri ->
val checkInData = CheckInData(
notes = etCheckInNotesLiveData.value,
surveyItems = chosenSurvey.value ?: arrayListOf(),
imageUri = imageUri,
)
combinedLiveData.value = checkInData
}
}
data class CheckInData(
val notes: String?,
val surveyItems: ArrayList,
val imageUri: ArrayList,
)
}
Код: Выделить всё
// Observe combinedLiveData
checkInViewModel.combinedLiveData.observe(this) { (etCheckInNotes, surveyData, uriImage) ->
val allValuesPresent =
surveyData.all { it.items.all { item -> item.valueItem != null } }
Log.e(TAG, "allvalues : $allValuesPresent")
Log.e(TAG, "etcheckinnotes : ${etCheckInNotes.toString()}")
Log.e(TAG, "etcheckinnotes null or empty ${etCheckInNotes.isNullOrEmpty()}")
Log.e(TAG, "surveyItem $surveyData")
Log.e(TAG, "buttonStatus : ${binding.btCheckInSubmit.getButtonEnable()}")
Log.e(TAG, "uriImage $uriImage")
if (allValuesPresent && !etCheckInNotes.isNullOrEmpty()) {
binding.btCheckInSubmit.setButtonEnabled(true)
binding.btCheckInSubmit.showLoading(false)
} else {
binding.btCheckInSubmit.setButtonEnabled(false)
}
}
Код: Выделить всё
binding.tiSurveyItemDesc.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int,
) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// Log.e("test", "$s")
val note = s.toString()
// viewModel.updateSurveyItemValueItem(
// surveyData.surveyId,
// surveyData.id,
// note
// )
// Update the valueItem of the survey item and notify observers
viewModel.viewModelScope.launch {
viewModel.chosenSurvey.value?.get(outerIndex)?.items?.get(position)?.valueItem =
note
// viewModel.chosenSurvey.value = viewModel.chosenSurvey.value
}
}
override fun afterTextChanged(s: Editable?) {
}
})
binding.tiSurveyItemDesc.setOnEditorActionListener { v, actionId, event ->
if (actionId == EditorInfo.IME_ACTION_NEXT) {
// Hilangkan fokus dari TextInputEditText
binding.tiSurveyItemDesc.clearFocus()
viewModel.chosenSurvey.postValue(viewModel.chosenSurvey.value) // Notify observers
// Opsional: Sembunyikan keyboard
// val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
// imm.hideSoftInputFromWindow(v.windowToken, 0)
true // Return true untuk menandakan event sudah ditangani
} else {
false
}
}
Это предварительный просмотр:

Подробнее здесь: https://stackoverflow.com/questions/790 ... d-livedata
Мобильная версия