Provide method to ensure non-empty message text preview for notifications and conversation list

This commit is contained in:
Moez Bhatti
2018-01-11 14:33:15 -05:00
parent 7728b62729
commit 647f1f6e09
3 changed files with 31 additions and 5 deletions
+29 -3
View File
@@ -27,7 +27,6 @@ import io.realm.annotations.PrimaryKey
open class Message : RealmObject() {
enum class DeliveryStatus { NONE, INFO, FAILED, PENDING, RECEIVED }
enum class AttachmentType { TEXT, IMAGE, VIDEO, AUDIO, SLIDESHOW, NOT_LOADED }
@PrimaryKey var id: Long = 0
@@ -95,10 +94,37 @@ open class Message : RealmObject() {
return isOutgoingMms || isOutgoingSms
}
fun getText() : String {
/**
* Returns the text that should be displayed in the messagelist view
* The returned text may be an empty string
*/
fun getText(): String {
return when {
isSms() -> body
else -> parts.filter { it.isText() }.joinToString("") { it.text ?: "" }
else -> parts
.mapNotNull { it.text }
.joinToString("\n") { text -> text }
}
}
/**
* Returns the text that should be displayed when a preview of the message
* needs to be displayed, such as in the conversation view or in a notification
*
* TODO: Don't return a hardcoded string
*/
fun getSummary(): String {
return when {
isSms() -> body
else -> parts
.mapNotNull { it.text }
.joinToString("\n") { text -> text }
.takeIf { it.isNotEmpty() }
?: subject
.takeIf { it.isNotEmpty() }
?: "An MMS message"
}
}