Mi Band notification improvements (#1579)

This commit is contained in:
Mikołaj Pich 2021-10-18 00:11:46 +02:00 committed by GitHub
parent 54e9ea6478
commit 84e4167dbd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
import android.os.Build
import androidx.annotation.PluralsRes
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import dagger.hilt.android.qualifiers.ApplicationContext
@ -68,7 +69,8 @@ class AppNotificationManager @Inject constructor(
) {
val groupType = notificationData.type.group ?: return
val group = "${groupType}_${student.id}"
val groupId = student.id * 100 + notificationData.type.ordinal
notificationData.sendSummaryNotification(group, student)
notificationData.lines.forEach { item ->
val title = context.resources.getQuantityString(notificationData.titleStringRes, 1)
@ -88,16 +90,26 @@ class AppNotificationManager @Inject constructor(
saveNotification(title, item, notificationData, student)
}
}
private fun MultipleNotificationsData.sendSummaryNotification(group: String, student: Student) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return
val summaryNotification = getDefaultNotificationBuilder(notificationData)
.setSmallIcon(notificationData.icon)
val summaryNotification = getDefaultNotificationBuilder(this)
.setSmallIcon(icon)
.setContentTitle(getQuantityString(titleStringRes, lines.size))
.setContentText(getQuantityString(contentStringRes, lines.size))
.setStyle(
NotificationCompat.InboxStyle()
.setSummaryText(student.nickOrName)
.also { builder -> lines.forEach { builder.addLine(it) } }
)
.setLocalOnly(true)
.setGroup(group)
.setStyle(NotificationCompat.InboxStyle().setSummaryText(student.nickOrName))
.setGroupSummary(true)
.build()
val groupId = student.id * 100 + type.ordinal
notificationManager.notify(groupId.toInt(), summaryNotification)
}
@ -116,6 +128,7 @@ class AppNotificationManager @Inject constructor(
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setColor(context.getCompatColor(R.color.colorPrimary))
.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY)
.setContentIntent(
PendingIntent.getActivity(
context,
@ -142,4 +155,8 @@ class AppNotificationManager @Inject constructor(
notificationRepository.saveNotification(notificationEntity)
}
}
private fun getQuantityString(@PluralsRes res: Int, arg: Int): String {
return context.resources.getQuantityString(res, arg, arg)
}
}