From 6a161b3c97416512036b97023d6cf46c73acb535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 2 Dec 2019 21:25:18 +0100 Subject: [PATCH] [UI/Timetable] Add showing event indicators on lessons. --- .../pl/szczodrzynski/edziennik/Extensions.kt | 11 +++ .../timetable/v2/day/TimetableDayFragment.kt | 43 +++++++--- app/src/main/res/layout/timetable_lesson.xml | 84 +++++++++++++------ 3 files changed, 103 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt index 581f372a..3ac87027 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/Extensions.kt @@ -5,6 +5,8 @@ import android.app.Activity import android.content.Context import android.content.pm.PackageManager import android.content.res.Resources +import android.graphics.PorterDuff +import android.graphics.PorterDuffColorFilter import android.graphics.Typeface import android.graphics.drawable.Drawable import android.os.Build @@ -36,6 +38,7 @@ import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher import pl.szczodrzynski.edziennik.data.db.modules.teams.Team import pl.szczodrzynski.edziennik.utils.models.Time +import pl.szczodrzynski.navlib.getColorFromAttr import pl.szczodrzynski.navlib.getColorFromRes import java.text.SimpleDateFormat import java.util.* @@ -595,3 +598,11 @@ inline fun Any?.instanceOfOrNull(): T? { else -> null } } + +fun Drawable.setTintColor(color: Int): Drawable { + colorFilter = PorterDuffColorFilter( + color, + PorterDuff.Mode.SRC_ATOP + ) + return this +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/day/TimetableDayFragment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/day/TimetableDayFragment.kt index 9fbbfa51..240e4e33 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/day/TimetableDayFragment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/timetable/v2/day/TimetableDayFragment.kt @@ -15,13 +15,12 @@ import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import com.linkedin.android.tachyon.DayView import com.linkedin.android.tachyon.DayViewConfig -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.Job +import kotlinx.coroutines.* import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_TIMETABLE import pl.szczodrzynski.edziennik.api.v2.LOGIN_TYPE_LIBRUS import pl.szczodrzynski.edziennik.api.v2.events.task.EdziennikTask +import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull import pl.szczodrzynski.edziennik.data.db.modules.timetable.Lesson import pl.szczodrzynski.edziennik.data.db.modules.timetable.LessonFull import pl.szczodrzynski.edziennik.databinding.TimetableLessonBinding @@ -109,11 +108,16 @@ class TimetableDayFragment : Fragment(), CoroutineScope { // observe lesson database app.db.timetableDao().getForDate(App.profileId, date).observe(this, Observer { lessons -> - processLessonList(lessons) + launch { + val events = withContext(Dispatchers.Default) { + app.db.eventDao().getAllByDateNow(App.profileId, date) + } + processLessonList(lessons, events) + } }) } - private fun processLessonList(lessons: List) { + private fun processLessonList(lessons: List, events: List) { // no lessons - timetable not downloaded yet if (lessons.isEmpty()) { inflater.inflate(R.layout.timetable_no_timetable, view as FrameLayout) { view, _, parent -> @@ -166,10 +170,10 @@ class TimetableDayFragment : Fragment(), CoroutineScope { } dayView.setHourLabelViews(hourLabelViews) - buildLessonViews(lessons) + buildLessonViews(lessons, events) } - private fun buildLessonViews(lessons: List) { + private fun buildLessonViews(lessons: List, events: List) { if (!isAdded) return @@ -206,6 +210,26 @@ class TimetableDayFragment : Fragment(), CoroutineScope { LessonDetailsDialog(activity, it.tag as LessonFull) } + val eventList = events.filter { it.startTime != null && it.startTime == lesson.displayStartTime }.take(3) + eventList.getOrNull(0).let { + lb.event1.visibility = if (it == null) View.GONE else View.VISIBLE + lb.event1.background = it?.let { + R.drawable.bg_circle.resolveDrawable(activity).setTintColor(it.getColor()) + } + } + eventList.getOrNull(1).let { + lb.event2.visibility = if (it == null) View.GONE else View.VISIBLE + lb.event2.background = it?.let { + R.drawable.bg_circle.resolveDrawable(activity).setTintColor(it.getColor()) + } + } + eventList.getOrNull(2).let { + lb.event3.visibility = if (it == null) View.GONE else View.VISIBLE + lb.event3.background = it?.let { + R.drawable.bg_circle.resolveDrawable(activity).setTintColor(it.getColor()) + } + } + val timeRange = "${startTime.stringHM} - ${endTime.stringHM}".asColoredSpannable(colorSecondary) @@ -311,10 +335,7 @@ class TimetableDayFragment : Fragment(), CoroutineScope { else -> lb.annotation.setText(R.string.timetable_lesson_shifted) } - lb.annotation.background.colorFilter = PorterDuffColorFilter( - getColorFromAttr(activity, R.attr.timetable_lesson_shifted_source_color), - PorterDuff.Mode.SRC_ATOP - ) + lb.annotation.background.setTintColor(R.attr.timetable_lesson_shifted_source_color.resolveAttr(activity)) } Lesson.TYPE_SHIFTED_TARGET -> { lb.annotationVisible = true diff --git a/app/src/main/res/layout/timetable_lesson.xml b/app/src/main/res/layout/timetable_lesson.xml index 0e2d4cdc..ad912e8e 100644 --- a/app/src/main/res/layout/timetable_lesson.xml +++ b/app/src/main/res/layout/timetable_lesson.xml @@ -73,16 +73,16 @@ tools:maxLines="2" tools:text="pracownia urządzeń techniki komputerowej" /> - + android:background="@drawable/unread_red_circle" /> - + android:orientation="vertical"> - + + + + + + + + + + + + + -