0

I have rather simple data model TimeSlot -> Subject -> Teacher. My goal is to get a school weekly schedule. So, in SQL it would be SELECT * FROM TimeSlot, Subject, Teacher WHERE TimeSlot.subject_id = Subject.subject_id AND Subject.teacher_id = Teacher.teacher_id. But I need a RecyclerView with cards where are all timeslots with subjects and appropriate teachers. So, the first part is rather simple = the following data class:

data class TimeSlotWithSubjects (

    @Embedded val timeSlot: TimeSlot,
    @Relation(
        parentColumn = "subjectId",
        entityColumn = "subject_id"
    )
    val subjects: List<Subject>
)

But we have no info about teacher here. At the same time I have another data class:

data class SubjectWithTeacher (

    @Embedded val subject: Subject,
    @Relation(
        parentColumn = "teacher_id",
        entityColumn = "teacherId"
    )
    val teachers: List<Teacher>
)

But here I have no info about timeslots. Please suggest an approach how can I get a LiveData list with all needed info?

Mayokun
  • 1,084
  • 1
  • 8
  • 20

1 Answers1

0

Since there were no good proposals I solved the task by the following way:

  1. In ViewModel:
val teachers_list = db.getAllTeachers()
  1. In Adapter:
class ViewHolder private constructor (val binding: ListItemTimeSlotWithSubjectAndWeekdayBinding) : RecyclerView.ViewHolder(binding.root){

        companion object {

            lateinit var t_list: List<Teacher>

            fun getTeacherFromList(teacher_id:Long):Teacher?{

                for (teacher in t_list){
                    if (teacher.teacherId == teacher_id)
                        return teacher
                }
                return null
            }

            fun setTeachersList(list: List<Teacher>){
                t_list = list
            }
        }

       fun bind(item: TimeSlotWithSubjects, clickListener: LessonsScheduleListener) {

            //

            if (!item.subjects.isEmpty()){
                binding.subjectName.text = item.subjects[0].name
                //

                if (item.subjects[0].teacherId > 0){

                    val l_teacher = getTeacherFromList(item.subjects[0].teacherId)
                   //

                }

        }
  1. In Fragment:
override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//
        binding.lessonsScheduleViewModel = viewModel
//
        viewModel.teachers_list.observe(viewLifecycleOwner, Observer {
            it?.let {
                LessonsScheduleAdapter.ViewHolder.setTeachersList(it)
            }
        })
//
    }

I guess there are better approaches - please suggest. At least this one works + not breaks base architectural principles )