From 29d74e14bd8516a21c29fe571043aedb15c475ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 11 Nov 2019 18:13:37 +0100 Subject: [PATCH] [Timetable] Fix scrolling to first lesson. Update lesson ID generation. --- .../api/v2/librus/data/api/LibrusApiTimetables.kt | 7 +++++-- .../pl/szczodrzynski/edziennik/api/v2/models/Data.kt | 1 + .../ui/modules/timetable/v2/day/TimetableDayFragment.kt | 9 ++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTimetables.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTimetables.kt index 898e5ea7..88151096 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTimetables.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiTimetables.kt @@ -14,6 +14,7 @@ import pl.szczodrzynski.edziennik.api.v2.models.DataRemoveModel import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata import pl.szczodrzynski.edziennik.data.db.modules.timetable.Lesson +import pl.szczodrzynski.edziennik.utils.Utils.d import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Time @@ -58,6 +59,7 @@ class LibrusApiTimetables(override val data: DataLibrus, val weekStart = Date.fromY_m_d(getDate) val weekEnd = weekStart.clone().stepForward(0, 0, 6) + d(TAG, "Clearing lessons between ${weekStart.stringY_m_d} and ${weekEnd.stringY_m_d} - timetable downloaded for $getDate") data.toRemove.add(DataRemoveModel.Timetable.between(weekStart, weekEnd)) data.setSyncNext(ENDPOINT_LIBRUS_API_TIMETABLES, SYNC_ALWAYS) @@ -66,8 +68,6 @@ class LibrusApiTimetables(override val data: DataLibrus, } private fun parseLesson(lessonDate: Date, lesson: JsonObject) { - val lessonObject = Lesson(profileId, lesson.hashCode().toLong()) - val isSubstitution = lesson.getBoolean("IsSubstitutionClass") ?: false val isCancelled = lesson.getBoolean("IsCanceled") ?: false @@ -80,6 +80,9 @@ class LibrusApiTimetables(override val data: DataLibrus, val virtualClassId = lesson.getJsonObject("VirtualClass")?.getLong("Id") val teamId = lesson.getJsonObject("Class")?.getLong("Id") ?: virtualClassId + val id = lessonDate.combineWith(startTime) / 6L * 10L + (lesson.hashCode() and 0xFFFF) + val lessonObject = Lesson(profileId, id) + if (isSubstitution && isCancelled) { // shifted lesson - source val newDate = lesson.getString("NewDate")?.let { Date.fromY_m_d(it) } ?: return diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt index 6ac629ad..a89d5ff2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt @@ -179,6 +179,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) fun clear() { loginMethods.clear() + toRemove.clear() endpointTimers.clear() teacherList.clear() subjectList.clear() 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 dc4f4d76..07220a2e 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 @@ -22,6 +22,7 @@ import pl.szczodrzynski.edziennik.databinding.TimetableLessonBinding import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.navlib.getColorFromAttr import java.util.* +import kotlin.math.min class TimetableDayFragment(val date: Date) : Fragment() { companion object { @@ -99,6 +100,8 @@ class TimetableDayFragment(val date: Date) : Fragment() { b.noTimetableLayout.visibility = View.GONE b.noLessonsLayout.visibility = View.GONE + var firstEventMinute = 24*60 + val eventViews = mutableListOf() val eventTimeRanges = mutableListOf() @@ -115,6 +118,8 @@ class TimetableDayFragment(val date: Date) : Fragment() { val startTime = lesson.displayStartTime ?: continue val endTime = lesson.displayEndTime ?: continue + firstEventMinute = min(firstEventMinute, startTime.hour*60 + startTime.minute) + // Try to recycle an existing event view if there are enough left, otherwise inflate // a new one val eventView = (if (remaining > 0) recycled?.get(--remaining) else layoutInflater.inflate(R.layout.timetable_lesson, b.day, false)) @@ -270,7 +275,9 @@ class TimetableDayFragment(val date: Date) : Fragment() { eventTimeRanges.add(DayView.EventTimeRange(startMinute, endMinute)) } + val minuteHeight = (b.day.getHourTop(1) - b.day.getHourTop(0)).toFloat() / 60f + val firstEventTop = (firstEventMinute - b.day.startHour * 60) * minuteHeight b.day.setEventViews(eventViews, eventTimeRanges) - b.dayScroll.scrollTo(0, b.day.firstEventTop) + b.dayScroll.scrollTo(0, firstEventTop.toInt()) } }