package com.example.attend.presentation.add_edit_class

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.example.attend.model.local.dao.AttendanceDao
import com.example.attend.model.local.dao.AttendanceReportDao
import com.example.attend.model.local.dao.ClassDao
import com.example.attend.model.local.dao.EnrollmentDao
import com.example.attend.model.local.dao.UserDao

class ClassViewModelFactory(
    private val classDao: ClassDao,
    private val userDao: UserDao,
    private val enrollmentDao: EnrollmentDao,
    private val attendanceDao: AttendanceDao,
    private val attendanceReportDao: AttendanceReportDao
): ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return ClassViewModel(classDao, userDao, enrollmentDao, attendanceDao, attendanceReportDao) as T
    }
} package com.example.attend.presentation.add_edit_class

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.attend.model.data.Attendance
import com.example.attend.model.data.AttendanceReport
import com.example.attend.model.data.ClassEntity
import com.example.attend.model.data.Enrollment
import com.example.attend.model.data.StudentWithEnrollmentStatus
import com.example.attend.model.data.User
import com.example.attend.model.local.dao.AttendanceDao
import com.example.attend.model.local.dao.AttendanceReportDao
import com.example.attend.model.local.dao.ClassDao
import com.example.attend.model.local.dao.EnrollmentDao
import com.example.attend.model.local.dao.UserDao
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.util.Calendar

class ClassViewModel(
    private val classDao: ClassDao,
    private val userDao: UserDao,
    private val enrollmentDao: EnrollmentDao,
    private val attendanceDao: AttendanceDao,
    private val attendanceReportDao: AttendanceReportDao
): ViewModel() {

    private var _classEditStatus = MutableLiveData<Boolean>()
    val classEditStatus: LiveData<Boolean> get() = _classEditStatus

    private val _studentsWithEnrollment = MutableLiveData<List<StudentWithEnrollmentStatus>>()
    val studentsWithEnrollment: LiveData<List<StudentWithEnrollmentStatus>> get() = _studentsWithEnrollment

    var errorCallback: ((String) -> Unit)? = null

    fun addEditClass(classEntity: ClassEntity){
        try {
            viewModelScope.launch {
                val classId = classDao.upsertClass(classEntity)
                if (classEntity.class_id.toInt() == 0) {
                    val attendanceReport = AttendanceReport(0, classId, classEntity.class_name, classEntity.teacher, 0, 0, 0)
                    createAttendanceReport(attendanceReport)
                }
            }
            _classEditStatus.value = true
        }catch (e: Exception) {
            _classEditStatus.value = false
        }
    }

    fun getAllClass() = classDao.getAllClasses()

    fun getClassByTeacher(teacher: String) = classDao.getClassByTeacher(teacher)

    fun deleteClass(classEntity: ClassEntity) {
        try {
            viewModelScope.launch {
                classDao.deleteClass(classEntity)
                _classEditStatus.value = true
            }
        }catch (e: Exception) {
            _classEditStatus.value = false
        }
    }

    fun enrollStudentInClass(studentId: Long, classId: Long) {
        viewModelScope.launch {
            val enrollment = Enrollment(student_id = studentId, class_id = classId)
            enrollmentDao.upsertEnrollment(enrollment)
        }
    }

    fun removeStudentFromClass(studentId: Long, classId: Long) {
        viewModelScope.launch {
            enrollmentDao.removeStudentFromClass(studentId, classId)
        }
    }

    fun getEnrolledStudentsInClass(classId: Long): LiveData<List<User>> {
        return userDao.getStudentsInClass(classId)
    }

    fun getUserByUserType(userType: String) = userDao.getUserByType(userType)

    fun getClassAttendance(classId: Long) = attendanceDao.getClassAttendance(classId)

    fun addStudentAttendance(studentId: Long, classId: Long, attendanceStatus: String) {
        try {
            viewModelScope.launch {
                val attendance = Attendance(0, classId, studentId, attendanceStatus, getCurrentDate())
                attendanceDao.insertAttendance(attendance)
            }
        }catch (e: Exception) {
            errorCallback?.invoke(e.message.toString())
        }
    }

    fun removeStudentAttendance(studentId: Long, classId: Long) {
        try {
            viewModelScope.launch {
                attendanceDao.removeAttendanceOfStudentFromClass(studentId, classId)
            }
        }catch (e: Exception) {
            errorCallback?.invoke(e.message.toString())
        }
    }

    private fun createAttendanceReport(attendanceReport: AttendanceReport) {
        try {
            viewModelScope.launch {
                attendanceReportDao.insertAttendanceReport(attendanceReport)
            }
        }catch (e: Exception) {
            errorCallback?.invoke(e.message.toString())
        }
    }

    fun updateAttendanceReport(attendanceReport: AttendanceReport) {
        try {
            viewModelScope.launch {
                attendanceReportDao.upsertAttendanceReport(attendanceReport)
            }
        }catch (e: Exception) {
            errorCallback?.invoke(e.message.toString())
        }
    }

    fun getAllAttendanceReport() = attendanceReportDao.getAllAttendanceReport()

    fun getAttendanceReportForClass(classId: Long) = attendanceReportDao.getAttendanceReportForClass(classId)

    fun getAttendanceReportForTeacher(teacher: String) = attendanceReportDao.getAttendanceReportForTeacher(teacher)

    fun getAttendanceForStudent(studentId: Long) = attendanceDao.getAttendanceForStudent(studentId)

    fun deleteAttendanceReport(classId: Long) {
        try {
            viewModelScope.launch{
                attendanceReportDao.deleteAttendanceReport(classId)
            }
        }catch (e: Exception) {
            errorCallback?.invoke(e.message.toString())
        }
    }

    private fun getCurrentDate(): String {
        val time = Calendar.getInstance().time
        val formatter = SimpleDateFormat("dd-MM-yyyy")
        return formatter.format(time)
    }

}package com.example.attend.presentation.add_edit_class

import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.attend.R
import com.example.attend.databinding.FragmentClassesBinding
import com.example.attend.model.local.AppDatabase
import com.example.attend.presentation.adapter.ClassAdapter
import com.example.attend.presentation.adapter.UserAdapter
import com.example.attend.utils.Constants.Companion.ADMIN
import com.example.attend.utils.Constants.Companion.CLASS_ID
import com.example.attend.utils.Constants.Companion.CLASS_NAME
import com.example.attend.utils.Constants.Companion.DATE
import com.example.attend.utils.Constants.Companion.FROM
import com.example.attend.utils.Constants.Companion.NAME
import com.example.attend.utils.Constants.Companion.TEACHER
import com.example.attend.utils.Constants.Companion.TO
import com.example.attend.utils.Constants.Companion.USER_TYPE

class ClassesFragment : Fragment() {

    private lateinit var binding: FragmentClassesBinding
    private lateinit var classViewModel: ClassViewModel

    private lateinit var userType: String
    private lateinit var name: String

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        binding = FragmentClassesBinding.inflate(layoutInflater, container, false)

        userType = activity?.intent?.getStringExtra(USER_TYPE).toString()
        name = activity?.intent?.getStringExtra(NAME).toString()

        val userDao = AppDatabase.getInstance(requireContext()).userDao()
        val classDao = AppDatabase.getInstance(requireContext()).classDao()
        val enrollmentDao = AppDatabase.getInstance(requireContext()).enrollmentDao()
        val attendanceDao = AppDatabase.getInstance(requireContext()).attendanceDao()
        val attendanceReportDao = AppDatabase.getInstance(requireContext()).attendanceReportDao()

        classViewModel = ViewModelProvider(
            this,
            ClassViewModelFactory(classDao, userDao, enrollmentDao, attendanceDao, attendanceReportDao)
        )[ClassViewModel::class.java]

        classViewModel.errorCallback = {
            Toast.makeText(activity, "Error: $it", Toast.LENGTH_SHORT).show()
        }

        binding.addEditClass.setOnClickListener {
            val intoAddEditClass = Intent(activity, AddEditClassActivity::class.java)
            startActivity(intoAddEditClass)
        }

        prepareRecyclerView()

        return binding.root
    }

    private fun prepareRecyclerView() {
        val classAdapter = ClassAdapter(
            onItemClick = {
                val intoClassDetails = Intent(activity, ClassDetailsActivity::class.java)
                intoClassDetails.putExtra(CLASS_ID, it.class_id)
                intoClassDetails.putExtra(CLASS_NAME, it.class_name)
                intoClassDetails.putExtra(TEACHER, it.teacher)
                intoClassDetails.putExtra(DATE, it.date)
                intoClassDetails.putExtra(FROM, it.from)
                intoClassDetails.putExtra(TO, it.to)
                intoClassDetails.putExtra(USER_TYPE, userType)
                startActivity(intoClassDetails)
            },
            onEditClick = {
                val intoAddEditClass = Intent(activity, AddEditClassActivity::class.java)
                intoAddEditClass.putExtra(CLASS_ID, it.class_id)
                intoAddEditClass.putExtra(CLASS_NAME, it.class_name)
                intoAddEditClass.putExtra(TEACHER, it.teacher)
                intoAddEditClass.putExtra(DATE, it.date)
                intoAddEditClass.putExtra(FROM, it.from)
                intoAddEditClass.putExtra(TO, it.to)
                startActivity(intoAddEditClass)
            }
        )

        binding.rvClasses.apply {
            layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
            adapter = classAdapter
        }

        if (userType == ADMIN) {
            classViewModel.getAllClass().observe(viewLifecycleOwner) {
                classAdapter.setClassList(it)
            }
        }else if (userType == TEACHER) {
            classViewModel.getClassByTeacher(name).observe(viewLifecycleOwner) {
                classAdapter.setClassList(it)
            }
        }

    }
}package com.example.attend.presentation.add_edit_class

import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.attend.databinding.ActivityClassDetailsBinding
import com.example.attend.model.data.AttendanceReport
import com.example.attend.model.local.AppDatabase
import com.example.attend.presentation.adapter.StudentAttendanceAdapter
import com.example.attend.utils.AttendanceDialog
import com.example.attend.utils.Constants
import com.example.attend.utils.Constants.Companion.ABSENT
import com.example.attend.utils.Constants.Companion.CLASS_ID
import com.example.attend.utils.Constants.Companion.CLASS_NAME
import com.example.attend.utils.Constants.Companion.PRESENT
import com.example.attend.utils.Constants.Companion.TARDY
import com.example.attend.utils.Constants.Companion.TEACHER
import com.example.attend.utils.Constants.Companion.USER_TYPE

class ClassDetailsActivity : AppCompatActivity() {

    private lateinit var binding: ActivityClassDetailsBinding
    private lateinit var classViewModel: ClassViewModel

    private var classId: Long = 0
    private lateinit var className: String
    private lateinit var teacher: String
    private lateinit var date: String
    private lateinit var from: String
    private lateinit var to: String

    private var attendanceReportId: Long = 0
    private var present: Int = 0
    private var absence: Int = 0
    private var tardy: Int = 0

    private lateinit var userType: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityClassDetailsBinding.inflate(layoutInflater)
        setContentView(binding.root)


        val userDao = AppDatabase.getInstance(applicationContext).userDao()
        val classDao = AppDatabase.getInstance(applicationContext).classDao()
        val enrollmentDao = AppDatabase.getInstance(applicationContext).enrollmentDao()
        val attendanceDao = AppDatabase.getInstance(applicationContext).attendanceDao()
        val attendanceReportDao = AppDatabase.getInstance(applicationContext).attendanceReportDao()

        classViewModel =
            ViewModelProvider(
                this,
                ClassViewModelFactory(classDao, userDao, enrollmentDao, attendanceDao, attendanceReportDao)
            )[ClassViewModel::class.java]

        userType = intent.getStringExtra(USER_TYPE).toString()

        classViewModel.errorCallback = {
            Toast.makeText(this, "Error: $it", Toast.LENGTH_SHORT).show()
        }

        setClassDetails()
        prepareRecyclerView()

        classViewModel.getAttendanceReportForClass(classId).observe(this) {
            attendanceReportId = it.attendance_report_id
            present = it.present
            absence = it.absence
            tardy = it.tardy
        }

        binding.back.setOnClickListener {
            finish()
        }

        binding.addStudents.setOnClickListener {
            val intoAddStudent = Intent(this, AddStudentsActivity::class.java)
            intoAddStudent.putExtra(CLASS_ID, classId)
            startActivity(intoAddStudent)
        }

    }

    @SuppressLint("SetTextI18n")
    private fun setClassDetails() {
        classId = intent.getLongExtra(CLASS_ID, 0)
        className = intent.getStringExtra(CLASS_NAME).toString()
        teacher = intent.getStringExtra(TEACHER).toString()
        date = intent.getStringExtra(Constants.DATE).toString()
        from = intent.getStringExtra(Constants.FROM).toString()
        to = intent.getStringExtra(Constants.TO).toString()

        binding.className.text = className
        binding.teacherName.text = teacher
        binding.date.text = date
        binding.time.text = "$from - $to"
    }

    private fun prepareRecyclerView() {

        val studentAdapter: StudentAttendanceAdapter
        var attendanceDialog: AttendanceDialog

        if (userType == TEACHER) {
            studentAdapter = StudentAttendanceAdapter(
                onItemClick = {
                    attendanceDialog = AttendanceDialog(it.name)
                    attendanceDialog.show(supportFragmentManager, "AttendanceDialog")

                    attendanceDialog.clickCallback = {attendance ->
                        when (attendance) {
                            PRESENT -> {
                                present++
                                updateAttendanceReport()
                                classViewModel.addStudentAttendance(it.user_id, classId, PRESENT)
                            }
                            ABSENT -> {
                                absence++
                                updateAttendanceReport()
                                classViewModel.addStudentAttendance(it.user_id, classId, ABSENT)
                            }
                            TARDY -> {
                                tardy++
                                updateAttendanceReport()
                                classViewModel.addStudentAttendance(it.user_id, classId, TARDY)
                            }
                        }
                    }
                },
                onAttendanceStatusClick = {user, status ->
                    attendanceDialog = AttendanceDialog(user.name)
                    attendanceDialog.show(supportFragmentManager, "AttendanceDialog")
                    attendanceDialog.clickCallback = {attendance ->

                        when(status) {
                            PRESENT -> {
                                present -= 1
                                updateAttendanceReport()
                            }
                            ABSENT -> {
                                absence -= 1
                                updateAttendanceReport()
                            }
                            else -> {
                                tardy -= 1
                                updateAttendanceReport()
                            }
                        }
                        classViewModel.removeStudentAttendance(user.user_id, classId)

                        when (attendance) {
                            PRESENT -> {
                                present++
                                updateAttendanceReport()
                                classViewModel.addStudentAttendance(user.user_id, classId, PRESENT)
                            }
                            ABSENT -> {
                                absence++
                                updateAttendanceReport()
                                classViewModel.addStudentAttendance(user.user_id, classId, ABSENT)
                            }
                            TARDY -> {
                                tardy++
                                updateAttendanceReport()
                                classViewModel.addStudentAttendance(user.user_id, classId, TARDY)
                            }
                        }
                    }

                }
            )
        }else {
            studentAdapter = StudentAttendanceAdapter(
                onItemClick = {

                },
                onAttendanceStatusClick = {user, status ->

                }
            )
        }


        binding.rvStudents.apply {
            layoutManager = LinearLayoutManager(this@ClassDetailsActivity, LinearLayoutManager.VERTICAL, false)
            adapter = studentAdapter
        }

        classViewModel.getEnrolledStudentsInClass(classId).observe(this) {
            studentAdapter.setStudentList(it)
        }

        classViewModel.getClassAttendance(classId).observe(this) {
            studentAdapter.setAttendanceList(it)
        }
    }

    private fun updateAttendanceReport() {
        val attendanceReport = AttendanceReport(attendanceReportId, classId, className, teacher, present, absence, tardy)
        classViewModel.updateAttendanceReport(attendanceReport)
    }
}package com.example.attend.presentation.add_edit_class

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.attend.R
import com.example.attend.databinding.ActivityAddStudentsBinding
import com.example.attend.model.data.User
import com.example.attend.model.local.AppDatabase
import com.example.attend.presentation.adapter.AddStudentsInClassAdapter
import com.example.attend.utils.Constants.Companion.CLASS_ID
import com.example.attend.utils.Constants.Companion.STUDENT

class AddStudentsActivity : AppCompatActivity() {

    private lateinit var binding: ActivityAddStudentsBinding
    private lateinit var classViewModel: ClassViewModel

    private var classId: Long = 0
    private var enrolledStudents: List<User> = emptyList()
    private var allStudents: List<User> = emptyList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAddStudentsBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val userDao = AppDatabase.getInstance(applicationContext).userDao()
        val classDao = AppDatabase.getInstance(applicationContext).classDao()
        val enrollmentDao = AppDatabase.getInstance(applicationContext).enrollmentDao()
        val attendanceDao = AppDatabase.getInstance(applicationContext).attendanceDao()
        val attendanceReportDao = AppDatabase.getInstance(applicationContext).attendanceReportDao()

        classViewModel =
            ViewModelProvider(
                this,
                ClassViewModelFactory(classDao, userDao, enrollmentDao, attendanceDao, attendanceReportDao)
            )[ClassViewModel::class.java]

        classViewModel.errorCallback = {
            Toast.makeText(this, "Error: $it", Toast.LENGTH_SHORT).show()
        }

        getExtrasData()

        binding.back.setOnClickListener {
            finish()
        }

        prepareRecyclerView()

    }

    private fun prepareRecyclerView() {
        val studentAdapter = AddStudentsInClassAdapter(
            onAddClick = {user, isCheck ->
                if (isCheck) {
                    classViewModel.enrollStudentInClass(user.user_id, classId)
                }else {
                    classViewModel.removeStudentFromClass(user.user_id, classId)
                }

            }
        )

        binding.rvStudents.apply {
            layoutManager = LinearLayoutManager(this@AddStudentsActivity, LinearLayoutManager.VERTICAL, false)
            adapter = studentAdapter
        }

        classViewModel.getUserByUserType(STUDENT).observe(this, Observer {
            allStudents = it
            studentAdapter.setAllStudentList(it)

        })

        classViewModel.getEnrolledStudentsInClass(classId).observe(this, Observer {
            enrolledStudents = it
            studentAdapter.setEnrolledStudentList(it)
        })
    }

    private fun getExtrasData() {
        classId = intent.getLongExtra(CLASS_ID, 0)
    }
}package com.example.attend.presentation.add_edit_class

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.InputType
import android.view.View
import android.widget.ArrayAdapter
import android.widget.DatePicker
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import com.example.attend.R
import com.example.attend.databinding.ActivityAddEditClassBinding
import com.example.attend.model.data.AttendanceReport
import com.example.attend.model.data.ClassEntity
import com.example.attend.model.data.User
import com.example.attend.model.local.AppDatabase
import com.example.attend.presentation.add_user.UserViewModel
import com.example.attend.presentation.add_user.UserViewModelFactory
import com.example.attend.utils.Constants.Companion.CLASS_ID
import com.example.attend.utils.Constants.Companion.CLASS_NAME
import com.example.attend.utils.Constants.Companion.DATE
import com.example.attend.utils.Constants.Companion.FROM
import com.example.attend.utils.Constants.Companion.TEACHER
import com.example.attend.utils.Constants.Companion.TO
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import javax.xml.datatype.DatatypeConstants.MONTHS

class AddEditClassActivity : AppCompatActivity() {

    private lateinit var binding: ActivityAddEditClassBinding
    private lateinit var classViewModel: ClassViewModel

    private var classId: Long = 0
    private lateinit var className: String
    private lateinit var teacher: String
    private lateinit var date: String
    private lateinit var from: String
    private lateinit var to: String

    private var calendar = Calendar.getInstance()

    private var teacherList = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityAddEditClassBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val userDao = AppDatabase.getInstance(applicationContext).userDao()
        val classDao = AppDatabase.getInstance(applicationContext).classDao()
        val enrollmentDao = AppDatabase.getInstance(applicationContext).enrollmentDao()
        val attendanceDao = AppDatabase.getInstance(applicationContext).attendanceDao()
        val attendanceReportDao = AppDatabase.getInstance(applicationContext).attendanceReportDao()

        classViewModel = ViewModelProvider(
            this,
            ClassViewModelFactory(
                classDao,
                userDao,
                enrollmentDao,
                attendanceDao,
                attendanceReportDao
            )
        )[ClassViewModel::class.java]

        if (intent.getStringExtra(CLASS_NAME) != null) {
            addDataFields()
            binding.delete.visibility = View.VISIBLE
        }

        classViewModel.errorCallback = {
            Toast.makeText(this, "Error: $it", Toast.LENGTH_SHORT).show()
        }

        classViewModel.getUserByUserType(TEACHER).observe(this) { teachers ->
            for (teacher in teachers)
                teacherList.add(teacher.name)
        }

        val adapter = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, teacherList)

        binding.teacherSp.setAdapter(adapter)

        binding.save.setOnClickListener {
            className = binding.className.editText?.text.toString().trim()
            teacher = binding.teacher.editText?.text.toString().trim()
            date = binding.date.editText?.text.toString().trim()
            from = binding.from.editText?.text.toString().trim()
            to = binding.to.editText?.text.toString().trim()

            if (className.isNotBlank() && teacher.isNotBlank() && date.isNotBlank() && from.isNotBlank() && to.isNotBlank()) {
                val classEntry = ClassEntity(classId, className, teacher, date, from, to)
                classViewModel.addEditClass(classEntry)
            } else {
                Toast.makeText(this, "Add all fields", Toast.LENGTH_SHORT).show()
            }
        }

        classViewModel.classEditStatus.observe(this) { isSuccess ->
            if (isSuccess) {
                Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show()
                clearFields()
            } else {
                Toast.makeText(this, "Error Occurred", Toast.LENGTH_SHORT).show()
            }
        }

        binding.datePicker.setOnClickListener {
            showDatePickerDialog()
        }

        binding.fromTimePicker.setOnClickListener {
            showTimePickerDialog(FROM)
        }

        binding.toTimePicker.setOnClickListener {
            showTimePickerDialog(TO)
        }

        binding.delete.setOnClickListener {
            val classEntry = ClassEntity(classId, className, teacher, date, from, to)
            classViewModel.deleteClass(classEntry)
            finish()
        }

        binding.back.setOnClickListener {
            finish()
        }

    }

    private fun showDatePickerDialog() {
        val datePickerDialog = DatePickerDialog(
            this, { DatePicker, year: Int, monthOfYear: Int, dayOfMonth: Int ->
                val selectedDate = Calendar.getInstance()
                selectedDate.set(year, monthOfYear, dayOfMonth)
                val dateFormat = SimpleDateFormat("dd/MM/yyyy", Locale.getDefault())
                val formattedDate = dateFormat.format(selectedDate.time)

                binding.date.editText?.setText(formattedDate)
            },
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH)
        )
        datePickerDialog.show()
    }

    private fun showTimePickerDialog(setText: String) {
        val currentTime = Calendar.getInstance()
        val hour = currentTime.get(Calendar.HOUR_OF_DAY)
        val minute = currentTime.get(Calendar.MINUTE)

        val timePickerDialog = TimePickerDialog(
            this,
            TimePickerDialog.OnTimeSetListener { _, selectedHour, selectedMinute ->
                val selectedTime = String.format("%02d:%02d", selectedHour, selectedMinute)

                if (setText == FROM) {
                    binding.from.editText?.setText(selectedTime)
                } else {
                    binding.to.editText?.setText(selectedTime)
                }
            },
            hour,
            minute,
            true
        )

        timePickerDialog.show()
    }

    private fun addDataFields() {
        classId = intent.getLongExtra(CLASS_ID, 0)
        className = intent.getStringExtra(CLASS_NAME).toString()
        teacher = intent.getStringExtra(TEACHER).toString()
        date = intent.getStringExtra(DATE).toString()
        from = intent.getStringExtra(FROM).toString()
        to = intent.getStringExtra(TO).toString()

        binding.className.editText?.setText(className)
        binding.teacher.editText?.setText(teacher)
        binding.date.editText?.setText(date)
        binding.from.editText?.setText(from)
        binding.to.editText?.setText(to)

        binding.className.editText?.inputType = InputType.TYPE_NULL
        binding.teacher.editText?.inputType = InputType.TYPE_NULL
    }

    private fun clearFields() {
        binding.className.editText?.setText("")
        binding.teacher.editText?.setText("")
        binding.date.editText?.setText("")
        binding.from.editText?.setText("")
        binding.to.editText?.setText("")
    }
} how it add and edit class, explain how it work