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)
|
|
|
|
}
|
|
|
|
if (sender != null) {
|
|
|
|
return res.getString(R.string.message_format_participants_join_added,
|
|
|
|
manager.getDisplayName(sender, nameFirst), joinName)
|
|
|
|
} else {
|
|
|
|
return res.getString(R.string.message_format_participants_join, joinName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MessageType.PARTICIPANTS_LEAVE -> {
|
|
|
|
val users = (extras as UserArrayExtras).users
|
|
|
|
val res = context.resources
|
|
|
|
if (users.size == 1) {
|
|
|
|
val displayName = manager.getDisplayName(users[0], nameFirst)
|
|
|
|
return res.getString(R.string.message_format_participants_leave, displayName)
|
|
|
|
} else {
|
|
|
|
val usersName = res.getQuantityString(R.plurals.N_users, users.size, users.size)
|
|
|
|
return res.getString(R.string.message_format_participants_leave, usersName)
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 06:46:30 +01:00
|
|
|
MessageType.CONVERSATION_NAME_UPDATE -> {
|
2017-02-13 15:08:28 +01:00
|
|
|
extras as ConversationInfoUpdatedExtras
|
2017-02-13 06:46:30 +01:00
|
|
|
val res = context.resources
|
|
|
|
if (extras.user != null) {
|
|
|
|
return res.getString(R.string.message_format_conversation_name_update_by_user,
|
|
|
|
manager.getDisplayName(extras.user, nameFirst), extras.name)
|
|
|
|
} else {
|
|
|
|
return 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
|
|
|
|
if (extras.user != null) {
|
|
|
|
return res.getString(R.string.message_format_conversation_avatar_update_by_user,
|
|
|
|
manager.getDisplayName(extras.user, nameFirst))
|
|
|
|
} else {
|
|
|
|
return res.getString(R.string.message_format_conversation_avatar_update)
|
|
|
|
}
|
|
|
|
}
|
2017-02-12 15:26:09 +01:00
|
|
|
}
|
|
|
|
return text
|
|
|
|
}
|