Skip to content

Thanks for series ! πŸ‘¨πŸ»β€πŸ’» here is quick suggestionΒ #1

@ch8n

Description

@ch8n

Hi, @codinginflow Nice tutorial series,
Just a suggestion you can reduce some nesting using this with than apply

class TasksViewHolder(private val binding: ItemTaskBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bind(task: Task) {
            binding.apply {
                checkBoxCompleted.isChecked = task.completed
                textViewName.text = task.name
                textViewName.paint.isStrikeThruText = task.completed
                labelPriority.isVisible = task.important
            }
        }
    }
class TasksViewHolder(private val binding: ItemTaskBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(task: Task) = with(binding) {
                checkBoxCompleted.isChecked = task.completed
                textViewName.text = task.name
                textViewName.paint.isStrikeThruText = task.completed
                labelPriority.isVisible = task.important
        }
}

[Edit1]
Nested scoped extension chaining is are considered not a good practice, checkout some awesome kotlin tips be from Huyen talk KotlinConf 2019 youtube.com/watch?v=YeqGfKmJM_g

 //TaskFragment
 binding.apply {
            recyclerViewTasks.apply {
                adapter = taskAdapter
                layoutManager = LinearLayoutManager(requireContext())
                setHasFixedSize(true)
            }
        }

to

 //TaskFragment

binding.recyclerViewTasks.apply {
       adapter = taskAdapter
       layoutManager = LinearLayoutManager(requireContext())
       setHasFixedSize(true)
 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions