Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageExtensions.kt

90 lines
4.0 KiB
Kotlin
Raw Normal View History

2017-02-09 06:14:54 +01:00
package org.mariotaku.twidere.extension.model
2017-02-12 15:26:09 +01:00
import android.content.Context
import org.mariotaku.twidere.R
2017-02-09 06:14:54 +01:00
import org.mariotaku.twidere.model.ParcelableMessage
2017-02-12 15:26:09 +01:00
import org.mariotaku.twidere.model.ParcelableMessage.MessageType
import org.mariotaku.twidere.model.ParcelableMessageConversation
import org.mariotaku.twidere.model.UserKey
2017-02-13 15:08:28 +01:00
import org.mariotaku.twidere.model.message.ConversationInfoUpdatedExtras
2017-02-12 15:26:09 +01:00
import org.mariotaku.twidere.model.message.MessageExtras
import org.mariotaku.twidere.model.message.UserArrayExtras
import org.mariotaku.twidere.util.UserColorNameManager
2017-02-09 06:14:54 +01:00
/**
* Created by mariotaku on 2017/2/9.
*/
val ParcelableMessage.timestamp: Long
get() = if (message_timestamp > 0) message_timestamp else local_timestamp
2017-02-12 15:26:09 +01:00
fun ParcelableMessage.getSummaryText(context: Context, manager: UserColorNameManager,
conversation: ParcelableMessageConversation?, nameFirst: Boolean): CharSequence? {
return getSummaryText(context, manager, nameFirst, message_type, extras, sender_key,
text_unescaped, conversation)
}
internal fun getSummaryText(context: Context, manager: UserColorNameManager, nameFirst: Boolean,
messageType: String?, extras: MessageExtras?, senderKey: UserKey?, text: String?,
conversation: ParcelableMessageConversation?): CharSequence? {
when (messageType) {
MessageType.STICKER -> {
return context.getString(R.string.message_summary_type_sticker)
}
MessageType.JOIN_CONVERSATION -> {
return context.getString(R.string.message_join_conversation)
}
MessageType.CONVERSATION_CREATE -> {
return context.getString(R.string.message_conversation_created)
}
MessageType.PARTICIPANTS_JOIN -> {
val users = (extras as UserArrayExtras).users
val sender = conversation?.participants?.firstOrNull { senderKey == it.key }
val res = context.resources
val joinName = if (users.size == 1) {
manager.getDisplayName(users[0], nameFirst)
} else {
res.getQuantityString(R.plurals.N_users, users.size, users.size)
}
2020-06-08 23:19:10 +02:00
return if (sender != null) {
res.getString(R.string.message_format_participants_join_added,
manager.getDisplayName(sender, nameFirst), joinName)
2017-02-12 15:26:09 +01:00
} else {
2020-06-08 23:19:10 +02:00
res.getString(R.string.message_format_participants_join, joinName)
2017-02-12 15:26:09 +01:00
}
}
MessageType.PARTICIPANTS_LEAVE -> {
val users = (extras as UserArrayExtras).users
val res = context.resources
2020-06-08 23:19:10 +02:00
return if (users.size == 1) {
2017-02-12 15:26:09 +01:00
val displayName = manager.getDisplayName(users[0], nameFirst)
2020-06-08 23:19:10 +02:00
res.getString(R.string.message_format_participants_leave, displayName)
2017-02-12 15:26:09 +01:00
} else {
val usersName = res.getQuantityString(R.plurals.N_users, users.size, users.size)
2020-06-08 23:19:10 +02:00
res.getString(R.string.message_format_participants_leave, usersName)
2017-02-12 15:26:09 +01:00
}
}
MessageType.CONVERSATION_NAME_UPDATE -> {
2017-02-13 15:08:28 +01:00
extras as ConversationInfoUpdatedExtras
val res = context.resources
2020-06-08 23:19:10 +02:00
return if (extras.user != null) {
res.getString(R.string.message_format_conversation_name_update_by_user,
manager.getDisplayName(extras.user, nameFirst), extras.name)
} else {
2020-06-08 23:19:10 +02:00
res.getString(R.string.message_format_conversation_name_update, extras.name)
}
}
2017-02-13 15:08:28 +01:00
MessageType.CONVERSATION_AVATAR_UPDATE -> {
extras as ConversationInfoUpdatedExtras
val res = context.resources
2020-06-08 23:19:10 +02:00
return if (extras.user != null) {
res.getString(R.string.message_format_conversation_avatar_update_by_user,
manager.getDisplayName(extras.user, nameFirst))
2017-02-13 15:08:28 +01:00
} else {
2020-06-08 23:19:10 +02:00
res.getString(R.string.message_format_conversation_avatar_update)
2017-02-13 15:08:28 +01:00
}
}
2017-02-12 15:26:09 +01:00
}
return text
}