From 07ae37167d7b63452ba4642cd0ed414ac999c42d Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 24 Nov 2019 11:09:45 +0100 Subject: [PATCH] [Notifications/Timetable] Make notifications for timetable changes --- .../edziennik/api/v2/DataNotifications.kt | 10 ++--- .../data/db/modules/timetable/LessonFull.kt | 38 ++++++++++++++++++- .../data/db/modules/timetable/TimetableDao.kt | 6 +++ app/src/main/res/values-en/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/DataNotifications.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/DataNotifications.kt index 73f6de4f..2975162e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/DataNotifications.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/DataNotifications.kt @@ -45,8 +45,8 @@ class DataNotifications(val data: Data) { return@run } - for (change in app.db.lessonChangeDao().getNotNotifiedNow(profileId)) { - val text = app.getString(R.string.notification_lesson_change_format, change.changeTypeStr(app), if (change.lessonDate == null) "" else change.lessonDate!!.formattedString, change.subjectLongName) + for (lesson in app.db.timetableDao().getNotNotifiedNow(profileId)) { + val text = app.getString(R.string.notification_lesson_change_format, lesson.getDisplayChangeType(app), if (lesson.displayDate == null) "" else lesson.displayDate!!.formattedString, lesson.changeSubjectName) data.notifications += Notification( title = app.getNotificationTitle(TYPE_TIMETABLE_LESSON_CHANGE), text = text, @@ -54,8 +54,8 @@ class DataNotifications(val data: Data) { profileId = profileId, profileName = profileName, viewId = DRAWER_ITEM_TIMETABLE, - addedDate = change.addedDate - ).addExtra("timetableDate", change.lessonDate?.value?.toLong()) + addedDate = lesson.addedDate + ).addExtra("timetableDate", lesson.displayDate?.value?.toLong()) } for (event in app.db.eventDao().getNotNotifiedNow(profileId)) { @@ -207,4 +207,4 @@ class DataNotifications(val data: Data) { data.db.metadataDao().setAllNotified(profileId, true) }} -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/timetable/LessonFull.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/timetable/LessonFull.kt index 49ea2c82..04d35f8e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/timetable/LessonFull.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/timetable/LessonFull.kt @@ -1,5 +1,7 @@ package pl.szczodrzynski.edziennik.data.db.modules.timetable +import android.content.Context +import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Time @@ -17,18 +19,21 @@ class LessonFull(profileId: Int, id: Long) : Lesson(profileId, id) { return oldDate return date ?: oldDate } + val displayLessonNumber: Int? get() { if (type == TYPE_SHIFTED_SOURCE) return oldLessonNumber return lessonNumber ?: oldLessonNumber } + val displayStartTime: Time? get() { if (type == TYPE_SHIFTED_SOURCE) return oldStartTime return startTime ?: oldStartTime } + val displayEndTime: Time? get() { if (type == TYPE_SHIFTED_SOURCE) @@ -42,12 +47,14 @@ class LessonFull(profileId: Int, id: Long) : Lesson(profileId, id) { return oldSubjectName return subjectName ?: oldSubjectName } + val displayTeacherName: String? get() { if (type == TYPE_SHIFTED_SOURCE) return oldTeacherName return teacherName ?: oldTeacherName } + val displayTeamName: String? get() { if (type == TYPE_SHIFTED_SOURCE) @@ -68,12 +75,14 @@ class LessonFull(profileId: Int, id: Long) : Lesson(profileId, id) { return oldTeamId return teamId ?: oldTeamId } + val displaySubjectId: Long? get() { if (type == TYPE_SHIFTED_SOURCE) return oldSubjectId return subjectId ?: oldSubjectId } + val displayTeacherId: Long? get() { if (type == TYPE_SHIFTED_SOURCE) @@ -81,8 +90,35 @@ class LessonFull(profileId: Int, id: Long) : Lesson(profileId, id) { return teacherId ?: oldTeacherId } + fun getDisplayChangeType(context: Context): String { + return context.getString(when (type) { + TYPE_CHANGE -> R.string.lesson_change + TYPE_CANCELLED -> R.string.lesson_cancelled + TYPE_SHIFTED_TARGET, TYPE_SHIFTED_SOURCE -> R.string.lesson_shifted + else -> R.string.lesson_timetable_change + }) + } + + val changeSubjectName: String + get() { + val first = when (type) { + TYPE_CHANGE, TYPE_CANCELLED, TYPE_SHIFTED_SOURCE -> oldSubjectName + else -> subjectName + } + + val second = when (type) { + TYPE_CHANGE -> subjectName + else -> null + } + + return when (second) { + null -> first ?: "" + else -> "$first -> $second" + } + } + // metadata var seen: Boolean = false var notified: Boolean = false var addedDate: Long = 0 -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/timetable/TimetableDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/timetable/TimetableDao.kt index a3ad45ba..9170a20b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/timetable/TimetableDao.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/timetable/TimetableDao.kt @@ -81,4 +81,10 @@ interface TimetableDao { ORDER BY id, type """) fun getByIdNow(profileId: Int, lessonId: Long) : LessonFull? + + @Query(""" + $QUERY + WHERE timetable.profileId = :profileId AND timetable.type NOT IN (${Lesson.TYPE_NORMAL}, ${Lesson.TYPE_NO_LESSONS}, ${Lesson.TYPE_SHIFTED_SOURCE}) AND metadata.notified = 0 + """) + fun getNotNotifiedNow(profileId: Int): List } diff --git a/app/src/main/res/values-en/strings.xml b/app/src/main/res/values-en/strings.xml index c723ce53..8664dcf8 100644 --- a/app/src/main/res/values-en/strings.xml +++ b/app/src/main/res/values-en/strings.xml @@ -295,6 +295,7 @@ Break Lesson cancelled Lesson change + Shifted lesson Timetable change Loading… Allow registration diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 06fc4f4b..62bc6661 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -318,6 +318,7 @@ Przerwa Lekcja odwołana Zastępstwo + Lekcja przeniesiona Zmiana planu Ładowanie… Zezwól na rejestrację