Untitled

 avatar
Maurya
kotlin
2 months ago
2.1 kB
3
Indexable
Never
    override fun onItemCheckedChange(position: Int, isChecked: Boolean) {

        Log.d(
            "CheckboxClick",
            "onItemCheckedChange called with position=$position, isChecked=$isChecked"
        )

        val incompleteTasksRef = dataBaseRef.child("incompleteTasks")
        val completeTasksRef = dataBaseRef.child("completeTasks")

        if (isChecked && inCompleteList.size > position) {
            val inCompletedItem = inCompleteList[position]
            Log.d("CheckboxClick", "Moving item to completeTasks")

            val taskId = inCompletedItem.id
            incompleteTasksRef.child(taskId).removeValue()

            val completeTask = DataToDo(
                taskId,
                inCompletedItem.taskName,
                inCompletedItem.taskDetails,
                inCompletedItem.taskCompleteUpToDate,
                inCompletedItem.isImportant,
                true
            )
            completeTasksRef.child(taskId).setValue(completeTask)

            adapterToDoInComplete.removeInCompleteItem(position)
            adapterToDoComplete.addCompleteItem(completeTask)
            Toast.makeText(context, "Task Completed", Toast.LENGTH_SHORT).show()
        } else if (!isChecked && completeList.size > position) {
            val completedItem = completeList[position]
            Log.d("CheckboxClick", "Moving item back to incompleteTasks")

            val taskId = completedItem.id

            completeTasksRef.child(taskId).removeValue()

            val incompleteTask = DataToDo(
                taskId,
                completedItem.taskName,
                completedItem.taskDetails,
                completedItem.taskCompleteUpToDate,
                completedItem.isImportant,
                false
            )
            incompleteTasksRef.child(taskId).setValue(incompleteTask)

            adapterToDoComplete.removeCompleteItem(position)
            adapterToDoInComplete.addInCompleteItem(incompleteTask)
            Toast.makeText(context, "Task Marked as InComplete", Toast.LENGTH_SHORT).show()
        }
    }
Leave a Comment