Change chapter update notification to show chapter numbers

This commit is contained in:
arkon 2020-02-17 16:20:31 -05:00
parent 79bc1290ae
commit a1fadce7c6
2 changed files with 38 additions and 10 deletions

View File

@ -45,6 +45,8 @@ import rx.schedulers.Schedulers
import timber.log.Timber
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.ArrayList
import java.util.concurrent.atomic.AtomicInteger
@ -123,6 +125,8 @@ class LibraryUpdateService(
*/
const val KEY_TARGET = "target"
private const val NOTIF_MAX_CHAPTERS = 5
private const val NOTIF_TITLE_MAX_LEN = 45
private const val NOTIF_ICON_SIZE = 192
/**
@ -466,11 +470,11 @@ class LibraryUpdateService(
notify(Notifications.ID_NEW_CHAPTERS, notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setContentTitle(getString(R.string.notification_new_chapters))
if (updates.size == 1) {
setContentText(updates.first().first.title)
setContentText(updates.first().first.title.chop(NOTIF_TITLE_MAX_LEN))
} else {
setContentText(resources.getQuantityString(R.plurals.notification_new_chapters_text, updates.size, updates.size))
setStyle(NotificationCompat.BigTextStyle().bigText(updates.joinToString("\n") {
it.first.title
it.first.title.chop(NOTIF_TITLE_MAX_LEN)
}))
}
@ -495,16 +499,12 @@ class LibraryUpdateService(
}
private fun createNewChaptersNotification(manga: Manga, chapters: Array<Chapter>): Notification {
val chapterNames = chapters.map { chapter -> chapter.name }.toSet()
return notification(Notifications.CHANNEL_NEW_CHAPTERS) {
setContentTitle(manga.title)
val chaptersNames = if (chapterNames.size > 5) {
"${chapterNames.take(4).joinToString(", ")}, " +
resources.getString(R.string.notification_and_n_more, (chapterNames.size - 4))
} else chapterNames.joinToString(", ")
setContentText(chaptersNames)
setStyle(NotificationCompat.BigTextStyle().bigText(chaptersNames))
val description = getChaptersDescriptionString(chapters)
setContentText(description)
setStyle(NotificationCompat.BigTextStyle().bigText(description))
setSmallIcon(R.drawable.ic_tachi)
@ -555,6 +555,30 @@ class LibraryUpdateService(
}
}
private fun getChaptersDescriptionString(chapters: Array<Chapter>): String {
val formatter = DecimalFormat("#.###", DecimalFormatSymbols()
.apply { decimalSeparator = '.' })
val chapterNumbers = chapters
.sortedBy { it.chapter_number }
.map { formatter.format(it.chapter_number) }
.toSet()
val shouldTruncate = chapterNumbers.size > NOTIF_MAX_CHAPTERS
val chaptersDescription = if (shouldTruncate) {
chapterNumbers.take(NOTIF_MAX_CHAPTERS - 1).joinToString(", ")
} else {
chapterNumbers.joinToString(", ")
}
var description = resources.getQuantityString(R.plurals.notification_chapters, chapters.size, chaptersDescription)
if (shouldTruncate) {
description += " ${resources.getString(R.string.notification_and_n_more, (chapterNumbers.size - (NOTIF_MAX_CHAPTERS - 1)))}"
}
return description
}
/**
* Returns an intent to open the main activity.
*/

View File

@ -490,6 +490,10 @@
<item quantity="one">For %d title</item>
<item quantity="other">For %d titles</item>
</plurals>
<plurals name="notification_chapters">
<item quantity="one">Chapter %1$s</item>
<item quantity="other">Chapters %1$s</item>
</plurals>
<string name="notification_and_n_more">and %1$d more</string>
<string name="notification_cover_update_failed">Failed to update cover</string>
<string name="notification_first_add_to_library">Please add the manga to your library before doing this</string>