If only one student exists, don't show student name in timetable notification (#1711)

Co-authored-by: Rafał Borcz <RafalBO99@outlook.com>
This commit is contained in:
Michael 2021-12-29 15:44:43 +01:00 committed by GitHub
parent 0965d03f1a
commit 68f0ecc45c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 32 deletions

View File

@ -1,12 +1,7 @@
package io.github.wulkanowy.data.db.dao package io.github.wulkanowy.data.db.dao
import androidx.room.Dao import androidx.room.*
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy.ABORT import androidx.room.OnConflictStrategy.ABORT
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.data.db.entities.StudentNickAndAvatar import io.github.wulkanowy.data.db.entities.StudentNickAndAvatar
import io.github.wulkanowy.data.db.entities.StudentWithSemesters import io.github.wulkanowy.data.db.entities.StudentWithSemesters

View File

@ -128,4 +128,7 @@ class StudentRepository @Inject constructor(
suspend fun updateStudentNickAndAvatar(studentNickAndAvatar: StudentNickAndAvatar) = suspend fun updateStudentNickAndAvatar(studentNickAndAvatar: StudentNickAndAvatar) =
studentDb.update(studentNickAndAvatar) studentDb.update(studentNickAndAvatar)
suspend fun isOneUniqueStudent() = getSavedStudents(false)
.distinctBy { it.student.studentName }.size == 1
} }

View File

@ -41,6 +41,8 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
const val NOTIFICATION_TYPE_UPCOMING = 2 const val NOTIFICATION_TYPE_UPCOMING = 2
const val NOTIFICATION_TYPE_LAST_LESSON_CANCELLATION = 3 const val NOTIFICATION_TYPE_LAST_LESSON_CANCELLATION = 3
// FIXME only shows one notification even if there are multiple students.
// Probably want to fix after #721 is merged.
const val NOTIFICATION_ID = 2137 const val NOTIFICATION_ID = 2137
const val STUDENT_NAME = "student_name" const val STUDENT_NAME = "student_name"
@ -60,16 +62,21 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
Timber.d("Receiving intent... ${intent.toUri(0)}") Timber.d("Receiving intent... ${intent.toUri(0)}")
flowWithResource { flowWithResource {
val showStudentName = !studentRepository.isOneUniqueStudent()
val student = studentRepository.getCurrentStudent(false) val student = studentRepository.getCurrentStudent(false)
val studentId = intent.getIntExtra(STUDENT_ID, 0) val studentId = intent.getIntExtra(STUDENT_ID, 0)
if (student.studentId == studentId) prepareNotification(context, intent)
else Timber.d("Notification studentId($studentId) differs from current(${student.studentId})") if (student.studentId == studentId) {
prepareNotification(context, intent, showStudentName)
} else {
Timber.d("Notification studentId($studentId) differs from current(${student.studentId})")
}
}.onEach { }.onEach {
if (it.status == Status.ERROR) Timber.e(it.error!!) if (it.status == Status.ERROR) Timber.e(it.error!!)
}.launchIn(GlobalScope) }.launchIn(GlobalScope)
} }
private fun prepareNotification(context: Context, intent: Intent) { private fun prepareNotification(context: Context, intent: Intent, showStudentName: Boolean) {
val type = intent.getIntExtra(LESSON_TYPE, 0) val type = intent.getIntExtra(LESSON_TYPE, 0)
val isPersistent = preferencesRepository.isUpcomingLessonsNotificationsPersistent val isPersistent = preferencesRepository.isUpcomingLessonsNotificationsPersistent
@ -78,7 +85,7 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
} }
val studentId = intent.getIntExtra(STUDENT_ID, 0) val studentId = intent.getIntExtra(STUDENT_ID, 0)
val studentName = intent.getStringExtra(STUDENT_NAME) val studentName = intent.getStringExtra(STUDENT_NAME).takeIf { showStudentName }
val subject = intent.getStringExtra(LESSON_TITLE) val subject = intent.getStringExtra(LESSON_TITLE)
val room = intent.getStringExtra(LESSON_ROOM) val room = intent.getStringExtra(LESSON_ROOM)
@ -91,19 +98,26 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
Timber.d("TimetableNotification receive: type: $type, subject: $subject, start: ${start.toLocalDateTime()}, student: $studentId") Timber.d("TimetableNotification receive: type: $type, subject: $subject, start: ${start.toLocalDateTime()}, student: $studentId")
showNotification( val notificationTitleResId =
context, isPersistent, studentName, if (type == NOTIFICATION_TYPE_CURRENT) R.string.timetable_now else R.string.timetable_next
if (type == NOTIFICATION_TYPE_CURRENT) end else start, end - start, val notificationTitle =
context.getString( context.getString(notificationTitleResId, "($room) $subject".removePrefix("()"))
if (type == NOTIFICATION_TYPE_CURRENT) R.string.timetable_now else R.string.timetable_next,
"($room) $subject".removePrefix("()") val nextLessonText = nextSubject?.let {
),
nextSubject?.let {
context.getString( context.getString(
R.string.timetable_later, R.string.timetable_later,
"($nextRoom) $nextSubject".removePrefix("()") "($nextRoom) $nextSubject".removePrefix("()")
) )
} }
showNotification(
context = context,
isPersistent = isPersistent,
studentName = studentName,
countDown = if (type == NOTIFICATION_TYPE_CURRENT) end else start,
timeout = end - start,
title = notificationTitle,
next = nextLessonText
) )
} }
@ -130,9 +144,10 @@ class TimetableNotificationReceiver : HiltBroadcastReceiver() {
.setTimeoutAfter(timeout) .setTimeoutAfter(timeout)
.setSmallIcon(R.drawable.ic_stat_timetable) .setSmallIcon(R.drawable.ic_stat_timetable)
.setColor(context.getCompatColor(R.color.colorPrimary)) .setColor(context.getCompatColor(R.color.colorPrimary))
.setStyle(NotificationCompat.InboxStyle().also { .setStyle(NotificationCompat.InboxStyle()
it.setSummaryText(studentName) .addLine(next)
it.addLine(next) .also { inboxStyle ->
studentName?.let { inboxStyle.setSummaryText(it) }
}) })
.setContentIntent( .setContentIntent(
PendingIntent.getActivity( PendingIntent.getActivity(

View File

@ -57,7 +57,7 @@ class AppNotificationManager @Inject constructor(
NotificationCompat.BigTextStyle() NotificationCompat.BigTextStyle()
.bigText(notificationData.content) .bigText(notificationData.content)
.also { builder -> .also { builder ->
if (shouldShowStudentName()) { if (!studentRepository.isOneUniqueStudent()) {
builder.setSummaryText(student.nickOrName) builder.setSummaryText(student.nickOrName)
} }
} }
@ -102,7 +102,7 @@ class AppNotificationManager @Inject constructor(
NotificationCompat.BigTextStyle() NotificationCompat.BigTextStyle()
.bigText(notificationData.content) .bigText(notificationData.content)
.also { builder -> .also { builder ->
if (shouldShowStudentName()) { if (!studentRepository.isOneUniqueStudent()) {
builder.setSummaryText(student.nickOrName) builder.setSummaryText(student.nickOrName)
} }
} }
@ -134,7 +134,7 @@ class AppNotificationManager @Inject constructor(
.setStyle( .setStyle(
NotificationCompat.InboxStyle() NotificationCompat.InboxStyle()
.also { builder -> .also { builder ->
if (shouldShowStudentName()) { if (!studentRepository.isOneUniqueStudent()) {
builder.setSummaryText(student.nickOrName) builder.setSummaryText(student.nickOrName)
} }
groupNotificationData.notificationDataList.forEach { groupNotificationData.notificationDataList.forEach {
@ -174,7 +174,4 @@ class AppNotificationManager @Inject constructor(
notificationRepository.saveNotification(notificationEntity) notificationRepository.saveNotification(notificationEntity)
} }
private suspend fun shouldShowStudentName(): Boolean =
studentRepository.getSavedStudents(decryptPass = false).size > 1
} }