From 453bcaa1f6c4bae636743e216e53fe0af231a3cb Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Thu, 13 Feb 2020 23:04:29 +0100 Subject: [PATCH] [Dialog/Day] Show lesson changes and teacher absences in the day dialog. --- .../data/db/dao/TeacherAbsenceDao.kt | 8 +++ .../edziennik/ui/dialogs/day/DayDialog.kt | 55 ++++++++++++++++--- .../lessonchange/LessonChangeAdapter.kt | 22 ++++---- .../lessonchange/LessonChangeDialog.kt | 17 +++++- app/src/main/res/layout/dialog_day.xml | 50 +++++++++++------ .../res/layout/row_lesson_change_item.xml | 2 +- .../res/layout/row_teacher_absence_item.xml | 2 +- 7 files changed, 116 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/TeacherAbsenceDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/TeacherAbsenceDao.kt index 714e85fa..3da88080 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/TeacherAbsenceDao.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/TeacherAbsenceDao.kt @@ -41,6 +41,14 @@ interface TeacherAbsenceDao { "AND :date BETWEEN teacherAbsenceDateFrom AND teacherAbsenceDateTo") fun getAllByDateFull(profileId: Int, date: Date): LiveData> + @Query("SELECT *, teachers.teacherName || ' ' || teachers.teacherSurname as teacherFullName, " + + "metadata.seen, metadata.notified, metadata.addedDate FROM teacherAbsence " + + "LEFT JOIN teachers USING (profileId, teacherId) " + + "LEFT JOIN metadata ON teacherAbsenceId = thingId AND metadata.thingType = " + Metadata.TYPE_TEACHER_ABSENCE + + " AND metadata.profileId = :profileId WHERE teachers.profileId = :profileId " + + "AND :date BETWEEN teacherAbsenceDateFrom AND teacherAbsenceDateTo") + fun getAllByDateNow(profileId: Int, date: Date): List + @Query("DELETE FROM teacherAbsence WHERE profileId = :profileId") fun clear(profileId: Int) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/day/DayDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/day/DayDialog.kt index 2d8885a5..52a3f53a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/day/DayDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/day/DayDialog.kt @@ -10,17 +10,16 @@ import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.dialog.MaterialAlertDialogBuilder -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job -import pl.szczodrzynski.edziennik.App -import pl.szczodrzynski.edziennik.R +import kotlinx.android.synthetic.main.row_lesson_change_item.view.* +import kotlinx.android.synthetic.main.row_teacher_absence_item.view.* +import kotlinx.coroutines.* +import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.databinding.DialogDayBinding -import pl.szczodrzynski.edziennik.onClick -import pl.szczodrzynski.edziennik.setText import pl.szczodrzynski.edziennik.ui.dialogs.event.EventDetailsDialog import pl.szczodrzynski.edziennik.ui.dialogs.event.EventListAdapter import pl.szczodrzynski.edziennik.ui.dialogs.event.EventManualDialog +import pl.szczodrzynski.edziennik.ui.dialogs.lessonchange.LessonChangeDialog +import pl.szczodrzynski.edziennik.ui.dialogs.teacherabsence.TeacherAbsenceDialog import pl.szczodrzynski.edziennik.utils.SimpleDividerItemDecoration import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Week @@ -77,13 +76,51 @@ class DayDialog( update() }} - private fun update() { + private fun update() { launch { b.dayDate.setText( R.string.dialog_day_date_format, Week.getFullDayName(date.weekDay), date.formattedString ) + val lessonChanges = withContext(Dispatchers.Default) { + app.db.timetableDao().getChangesForDateNow(profileId, date) + } + + lessonChanges.ifNotEmpty { + b.lessonChangeContainer.visibility = View.VISIBLE + b.lessonChangeContainer.lessonChangeCount.text = it.size.toString() + + b.lessonChangeLayout.onClick { + LessonChangeDialog( + activity, + profileId, + date, + onShowListener = onShowListener, + onDismissListener = onDismissListener + ) + } + } + + val teacherAbsences = withContext(Dispatchers.Default) { + app.db.teacherAbsenceDao().getAllByDateNow(profileId, date) + } + + teacherAbsences.ifNotEmpty { + b.teacherAbsenceContainer.visibility = View.VISIBLE + b.teacherAbsenceContainer.teacherAbsenceCount.text = it.size.toString() + + b.teacherAbsenceLayout.onClick { + TeacherAbsenceDialog( + activity, + profileId, + date, + onShowListener = onShowListener, + onDismissListener = onDismissListener + ) + } + } + adapter = EventListAdapter( activity, onItemClick = { @@ -126,5 +163,5 @@ class DayDialog( b.eventsNoData.visibility = View.VISIBLE } }) - } + }} } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/lessonchange/LessonChangeAdapter.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/lessonchange/LessonChangeAdapter.kt index 7f95c5b0..b15803ed 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/lessonchange/LessonChangeAdapter.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/lessonchange/LessonChangeAdapter.kt @@ -4,27 +4,29 @@ package pl.szczodrzynski.edziennik.ui.dialogs.lessonchange +import android.content.Context import android.graphics.PorterDuff import android.graphics.PorterDuffColorFilter import android.view.LayoutInflater import android.view.ViewGroup -import androidx.appcompat.app.AppCompatActivity import androidx.core.view.updateLayoutParams import androidx.recyclerview.widget.RecyclerView import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.data.db.entity.Lesson import pl.szczodrzynski.edziennik.data.db.full.LessonFull import pl.szczodrzynski.edziennik.databinding.TimetableLessonBinding -import pl.szczodrzynski.edziennik.ui.dialogs.timetable.LessonDetailsDialog import pl.szczodrzynski.navlib.getColorFromAttr -class LessonChangeAdapter(val activity: AppCompatActivity) : RecyclerView.Adapter() { +class LessonChangeAdapter( + val context: Context, + private val onItemClick: ((lesson: LessonFull) -> Unit)? = null +) : RecyclerView.Adapter() { var items = listOf() private val arrowRight = " → " private val bullet = " • " - private val colorSecondary = android.R.attr.textColorSecondary.resolveAttr(activity) + private val colorSecondary = android.R.attr.textColorSecondary.resolveAttr(context) override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val inflater = LayoutInflater.from(parent.context) @@ -36,8 +38,8 @@ class LessonChangeAdapter(val activity: AppCompatActivity) : RecyclerView.Adapte val lesson = items[position] val b = holder.b - b.root.setOnClickListener { - LessonDetailsDialog(activity, lesson) + b.root.onClick { + onItemClick?.invoke(lesson) } val startTime = lesson.displayStartTime ?: return @@ -99,7 +101,7 @@ class LessonChangeAdapter(val activity: AppCompatActivity) : RecyclerView.Adapte b.annotationVisible = true b.annotation.setText(R.string.timetable_lesson_cancelled) b.annotation.background.colorFilter = PorterDuffColorFilter( - getColorFromAttr(activity, R.attr.timetable_lesson_cancelled_color), + getColorFromAttr(context, R.attr.timetable_lesson_cancelled_color), PorterDuff.Mode.SRC_ATOP ) //lb.subjectName.typeface = Typeface.DEFAULT @@ -129,7 +131,7 @@ class LessonChangeAdapter(val activity: AppCompatActivity) : RecyclerView.Adapte } b.annotation.background.colorFilter = PorterDuffColorFilter( - getColorFromAttr(activity, R.attr.timetable_lesson_change_color), + getColorFromAttr(context, R.attr.timetable_lesson_change_color), PorterDuff.Mode.SRC_ATOP ) } @@ -152,7 +154,7 @@ class LessonChangeAdapter(val activity: AppCompatActivity) : RecyclerView.Adapte else -> b.annotation.setText(R.string.timetable_lesson_shifted) } - b.annotation.background.setTintColor(R.attr.timetable_lesson_shifted_source_color.resolveAttr(activity)) + b.annotation.background.setTintColor(R.attr.timetable_lesson_shifted_source_color.resolveAttr(context)) } Lesson.TYPE_SHIFTED_TARGET -> { b.annotationVisible = true @@ -174,7 +176,7 @@ class LessonChangeAdapter(val activity: AppCompatActivity) : RecyclerView.Adapte } b.annotation.background.colorFilter = PorterDuffColorFilter( - getColorFromAttr(activity, R.attr.timetable_lesson_shifted_target_color), + getColorFromAttr(context, R.attr.timetable_lesson_shifted_target_color), PorterDuff.Mode.SRC_ATOP ) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/lessonchange/LessonChangeDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/lessonchange/LessonChangeDialog.kt index 435d89c3..78d40e68 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/lessonchange/LessonChangeDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/dialogs/lessonchange/LessonChangeDialog.kt @@ -8,6 +8,7 @@ import kotlinx.coroutines.* import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.databinding.DialogLessonChangeListBinding +import pl.szczodrzynski.edziennik.ui.dialogs.timetable.LessonDetailsDialog import pl.szczodrzynski.edziennik.utils.models.Date import kotlin.coroutines.CoroutineContext @@ -53,7 +54,21 @@ class LessonChangeDialog( app.db.timetableDao().getChangesForDateNow(profileId, defaultDate) } - b.lessonChangeView.adapter = LessonChangeAdapter(activity).apply { items = lessonChanges } + val adapter = LessonChangeAdapter( + activity, + onItemClick = { + LessonDetailsDialog( + activity, + it, + onShowListener = onShowListener, + onDismissListener = onDismissListener + ) + } + ).apply { + items = lessonChanges + } + + b.lessonChangeView.adapter = adapter b.lessonChangeView.layoutManager = LinearLayoutManager(activity) dialog.show() diff --git a/app/src/main/res/layout/dialog_day.xml b/app/src/main/res/layout/dialog_day.xml index 355e1c92..d2823940 100644 --- a/app/src/main/res/layout/dialog_day.xml +++ b/app/src/main/res/layout/dialog_day.xml @@ -30,29 +30,43 @@ android:textIsSelectable="true" tools:text="wtorek, 17 grudnia" /> - + android:background="?selectableItemBackground"> - + + + + android:background="?selectableItemBackground"> + + +