From daa0618f268e2231a92fb7982633a97acd574af2 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Mon, 13 Feb 2017 22:08:28 +0800 Subject: [PATCH] support conversation avatar --- .../library/twitter/model/DMResponse.java | 25 ++++++++++++++++- .../twidere/model/ParcelableMessage.java | 15 ++++++----- .../model/ParcelableMessageConversation.java | 7 ++++- ...ava => ConversationInfoUpdatedExtras.java} | 27 ++++++++++++------- .../twidere/model/message/MessageExtras.java | 3 ++- .../TwitterOfficialConversationExtras.java | 1 + .../twidere/provider/TwidereDataStore.java | 1 + .../java/org/mariotaku/twidere/Constants.java | 2 +- .../adapter/MessagesConversationAdapter.kt | 2 +- .../model/ParcelableMessageExtensions.kt | 14 ++++++++-- .../model/util/ParcelableMessageUtils.kt | 20 +++++++++----- .../mariotaku/twidere/task/GetMessagesTask.kt | 1 + .../twidere/util/MediaLoaderWrapper.kt | 9 +++++++ .../holder/message/MessageEntryViewHolder.kt | 3 +-- twidere/src/main/res/values/strings.xml | 3 +++ 15 files changed, 102 insertions(+), 31 deletions(-) rename twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/{NameUpdatedExtras.java => ConversationInfoUpdatedExtras.java} (68%) diff --git a/twidere.component.common/src/main/java/org/mariotaku/microblog/library/twitter/model/DMResponse.java b/twidere.component.common/src/main/java/org/mariotaku/microblog/library/twitter/model/DMResponse.java index 44636b450..943c98d3c 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/microblog/library/twitter/model/DMResponse.java +++ b/twidere.component.common/src/main/java/org/mariotaku/microblog/library/twitter/model/DMResponse.java @@ -129,6 +129,8 @@ public class DMResponse implements Parcelable { Message participantsJoin; @JsonField(name = "conversation_name_update") Message conversationNameUpdate; + @JsonField(name = "conversation_avatar_update") + Message conversationAvatarUpdate; @JsonField(name = "conversation_read") Message conversationRead; @JsonField(name = "message_delete") @@ -160,6 +162,10 @@ public class DMResponse implements Parcelable { return conversationNameUpdate; } + public Message getConversationAvatarUpdate() { + return conversationAvatarUpdate; + } + public Message getConversationRead() { return conversationRead; } @@ -175,7 +181,8 @@ public class DMResponse implements Parcelable { @Override public String toString() { return "Entry{" + - "conversationCreate=" + conversationCreate + + "conversationAvatarUpdate=" + conversationAvatarUpdate + + ", conversationCreate=" + conversationCreate + ", joinConversation=" + joinConversation + ", message=" + message + ", participantsLeave=" + participantsLeave + @@ -183,6 +190,7 @@ public class DMResponse implements Parcelable { ", conversationNameUpdate=" + conversationNameUpdate + ", conversationRead=" + conversationRead + ", messageDelete=" + messageDelete + + ", removeConversation=" + removeConversation + '}'; } @@ -232,6 +240,9 @@ public class DMResponse implements Parcelable { @JsonField(name = "conversation_name") String conversationName; + @JsonField(name = "conversation_avatar_image_https") + String conversationAvatarImageHttps; + @JsonField(name = "request_id") String requestId; @@ -286,6 +297,10 @@ public class DMResponse implements Parcelable { return conversationName; } + public String getConversationAvatarImageHttps() { + return conversationAvatarImageHttps; + } + public String getByUserId() { return byUserId; } @@ -302,6 +317,7 @@ public class DMResponse implements Parcelable { ", time=" + time + ", conversationId='" + conversationId + '\'' + ", conversationName='" + conversationName + '\'' + + ", conversationAvatarImageHttps='" + conversationAvatarImageHttps + '\'' + ", requestId='" + requestId + '\'' + ", senderId='" + senderId + '\'' + ", byUserId='" + byUserId + '\'' + @@ -562,6 +578,9 @@ public class DMResponse implements Parcelable { @JsonField(name = "name") String name; + @JsonField(name = "avatar_image_https") + String avatarImageHttps; + public String getType() { return type; } @@ -618,6 +637,10 @@ public class DMResponse implements Parcelable { return name; } + public String getAvatarImageHttps() { + return avatarImageHttps; + } + @StringDef({Type.ONE_TO_ONE, Type.GROUP_DM}) @Retention(RetentionPolicy.SOURCE) public @interface Type { diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessage.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessage.java index a8c95437d..50bb07c26 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessage.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessage.java @@ -33,7 +33,7 @@ import org.mariotaku.commons.objectcursor.LoganSquareCursorFieldConverter; import org.mariotaku.library.objectcursor.annotation.CursorField; import org.mariotaku.library.objectcursor.annotation.CursorObject; import org.mariotaku.twidere.model.message.MessageExtras; -import org.mariotaku.twidere.model.message.NameUpdatedExtras; +import org.mariotaku.twidere.model.message.ConversationInfoUpdatedExtras; import org.mariotaku.twidere.model.message.StickerExtras; import org.mariotaku.twidere.model.message.UserArrayExtras; import org.mariotaku.twidere.model.util.MessageExtrasConverter; @@ -161,6 +161,7 @@ public class ParcelableMessage { String PARTICIPANTS_LEAVE = "participants_leave"; String PARTICIPANTS_JOIN = "participants_join"; String CONVERSATION_NAME_UPDATE = "conversation_name_update"; + String CONVERSATION_AVATAR_UPDATE = "conversation_avatar_update"; String TEXT = "text"; String STICKER = "sticker"; } @@ -169,8 +170,8 @@ public class ParcelableMessage { static class InternalExtras { @JsonField(name = "sticker") StickerExtras sticker; - @JsonField(name = "name_updated") - NameUpdatedExtras nameUpdated; + @JsonField(name = "info_updated") + ConversationInfoUpdatedExtras infoUpdated; @JsonField(name = "user_array") UserArrayExtras userArray; @@ -179,8 +180,8 @@ public class ParcelableMessage { InternalExtras result = new InternalExtras(); if (extras instanceof StickerExtras) { result.sticker = (StickerExtras) extras; - } else if (extras instanceof NameUpdatedExtras) { - result.nameUpdated = (NameUpdatedExtras) extras; + } else if (extras instanceof ConversationInfoUpdatedExtras) { + result.infoUpdated = (ConversationInfoUpdatedExtras) extras; } else if (extras instanceof UserArrayExtras) { result.userArray = (UserArrayExtras) extras; } else { @@ -192,8 +193,8 @@ public class ParcelableMessage { public MessageExtras getExtras() { if (sticker != null) { return sticker; - } else if (nameUpdated != null) { - return nameUpdated; + } else if (infoUpdated != null) { + return infoUpdated; } else if (userArray != null) { return userArray; } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessageConversation.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessageConversation.java index 1d3eaf401..256a75c29 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessageConversation.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/ParcelableMessageConversation.java @@ -72,6 +72,10 @@ public class ParcelableMessageConversation { @CursorField(Conversations.CONVERSATION_NAME) public String conversation_name; + @JsonField(name = "conversation_avatar") + @CursorField(Conversations.CONVERSATION_AVATAR) + public String conversation_avatar; + @ParcelableMessage.MessageType @JsonField(name = "message_type") @CursorField(Conversations.MESSAGE_TYPE) @@ -167,6 +171,7 @@ public class ParcelableMessageConversation { ", id='" + id + '\'' + ", conversation_type='" + conversation_type + '\'' + ", conversation_name='" + conversation_name + '\'' + + ", conversation_avatar='" + conversation_avatar + '\'' + ", message_type='" + message_type + '\'' + ", message_timestamp=" + message_timestamp + ", local_timestamp=" + local_timestamp + @@ -175,9 +180,9 @@ public class ParcelableMessageConversation { ", media=" + Arrays.toString(media) + ", spans=" + Arrays.toString(spans) + ", message_extras=" + message_extras + + ", conversation_extras_type='" + conversation_extras_type + '\'' + ", conversation_extras=" + conversation_extras + ", participants=" + Arrays.toString(participants) + - ", extras_type='" + conversation_extras_type + '\'' + ", sender_key=" + sender_key + ", recipient_key=" + recipient_key + ", is_outgoing=" + is_outgoing + diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/NameUpdatedExtras.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/ConversationInfoUpdatedExtras.java similarity index 68% rename from twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/NameUpdatedExtras.java rename to twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/ConversationInfoUpdatedExtras.java index e5ecb3c61..6a6ddc712 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/NameUpdatedExtras.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/ConversationInfoUpdatedExtras.java @@ -36,11 +36,13 @@ import org.mariotaku.twidere.model.ParcelableUser; @ParcelablePlease @JsonObject -public class NameUpdatedExtras extends MessageExtras implements Parcelable { +public class ConversationInfoUpdatedExtras extends MessageExtras implements Parcelable { @JsonField(name = "user") ParcelableUser user; @JsonField(name = "name") String name; + @JsonField(name = "avatar") + String avatar; public String getName() { return name; @@ -50,12 +52,19 @@ public class NameUpdatedExtras extends MessageExtras implements Parcelable { this.name = name; } + public String getAvatar() { + return avatar; + } + + public void setAvatar(final String avatar) { + this.avatar = avatar; + } + public void setUser(final ParcelableUser user) { this.user = user; } public ParcelableUser getUser() { - return user; } @@ -66,18 +75,18 @@ public class NameUpdatedExtras extends MessageExtras implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { - NameUpdatedExtrasParcelablePlease.writeToParcel(this, dest, flags); + ConversationInfoUpdatedExtrasParcelablePlease.writeToParcel(this, dest, flags); } - public static final Creator CREATOR = new Creator() { - public NameUpdatedExtras createFromParcel(Parcel source) { - NameUpdatedExtras target = new NameUpdatedExtras(); - NameUpdatedExtrasParcelablePlease.readFromParcel(target, source); + public static final Creator CREATOR = new Creator() { + public ConversationInfoUpdatedExtras createFromParcel(Parcel source) { + ConversationInfoUpdatedExtras target = new ConversationInfoUpdatedExtras(); + ConversationInfoUpdatedExtrasParcelablePlease.readFromParcel(target, source); return target; } - public NameUpdatedExtras[] newArray(int size) { - return new NameUpdatedExtras[size]; + public ConversationInfoUpdatedExtras[] newArray(int size) { + return new ConversationInfoUpdatedExtras[size]; } }; } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/MessageExtras.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/MessageExtras.java index bb3f02ba2..cffd1203c 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/MessageExtras.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/MessageExtras.java @@ -48,7 +48,8 @@ public abstract class MessageExtras implements Parcelable { case MessageType.PARTICIPANTS_JOIN: return LoganSquare.parse(json, UserArrayExtras.class); case MessageType.CONVERSATION_NAME_UPDATE: - return LoganSquare.parse(json, NameUpdatedExtras.class); + case MessageType.CONVERSATION_AVATAR_UPDATE: + return LoganSquare.parse(json, ConversationInfoUpdatedExtras.class); } return null; } diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/conversation/TwitterOfficialConversationExtras.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/conversation/TwitterOfficialConversationExtras.java index a4af5500c..a95fca90e 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/conversation/TwitterOfficialConversationExtras.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/model/message/conversation/TwitterOfficialConversationExtras.java @@ -37,6 +37,7 @@ import org.mariotaku.microblog.library.twitter.model.DMResponse; @ParcelablePlease @JsonObject public class TwitterOfficialConversationExtras extends ConversationExtras implements Parcelable { + @JsonField(name = "max_entry_id") String maxEntryId; @JsonField(name = "min_entry_id") diff --git a/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java b/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java index 1b6feac19..9a1f12b95 100644 --- a/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java +++ b/twidere.component.common/src/main/java/org/mariotaku/twidere/provider/TwidereDataStore.java @@ -384,6 +384,7 @@ public interface TwidereDataStore { String CONVERSATION_ID = "conversation_id"; String CONVERSATION_TYPE = "conversation_type"; String CONVERSATION_NAME = "conversation_name"; + String CONVERSATION_AVATAR = "conversation_avatar"; String MESSAGE_TYPE = "message_type"; String MESSAGE_TIMESTAMP = "message_timestamp"; String SORT_ID = "sort_id"; diff --git a/twidere/src/main/java/org/mariotaku/twidere/Constants.java b/twidere/src/main/java/org/mariotaku/twidere/Constants.java index 2557a371e..206da0dc8 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/Constants.java +++ b/twidere/src/main/java/org/mariotaku/twidere/Constants.java @@ -34,7 +34,7 @@ import static org.mariotaku.twidere.annotation.PreferenceType.STRING; public interface Constants extends TwidereConstants { String DATABASES_NAME = "twidere.sqlite"; - int DATABASES_VERSION = 172; + int DATABASES_VERSION = 173; int EXTRA_FEATURES_NOTICE_VERSION = 0; diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessagesConversationAdapter.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessagesConversationAdapter.kt index f4298d7c7..3aef3709b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessagesConversationAdapter.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/adapter/MessagesConversationAdapter.kt @@ -116,7 +116,7 @@ class MessagesConversationAdapter(context: Context) : LoadMoreSupportAdapter { + MessageType.CONVERSATION_NAME_UPDATE, MessageType.CONVERSATION_AVATAR_UPDATE -> { return ITEM_TYPE_NOTICE_MESSAGE } else -> return ITEM_TYPE_TEXT_MESSAGE diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageExtensions.kt index 3e2742eb2..e954c023d 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/ParcelableMessageExtensions.kt @@ -6,8 +6,8 @@ import org.mariotaku.twidere.model.ParcelableMessage import org.mariotaku.twidere.model.ParcelableMessage.MessageType import org.mariotaku.twidere.model.ParcelableMessageConversation import org.mariotaku.twidere.model.UserKey +import org.mariotaku.twidere.model.message.ConversationInfoUpdatedExtras import org.mariotaku.twidere.model.message.MessageExtras -import org.mariotaku.twidere.model.message.NameUpdatedExtras import org.mariotaku.twidere.model.message.UserArrayExtras import org.mariotaku.twidere.util.UserColorNameManager @@ -65,7 +65,7 @@ internal fun getSummaryText(context: Context, manager: UserColorNameManager, nam } } MessageType.CONVERSATION_NAME_UPDATE -> { - extras as NameUpdatedExtras + extras as ConversationInfoUpdatedExtras val res = context.resources if (extras.user != null) { return res.getString(R.string.message_format_conversation_name_update_by_user, @@ -74,6 +74,16 @@ internal fun getSummaryText(context: Context, manager: UserColorNameManager, nam return res.getString(R.string.message_format_conversation_name_update, extras.name) } } + 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) + } + } } return text } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableMessageUtils.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableMessageUtils.kt index 73fe47e7e..c132e8e20 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableMessageUtils.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/model/util/ParcelableMessageUtils.kt @@ -11,8 +11,8 @@ import org.mariotaku.twidere.model.ParcelableMedia import org.mariotaku.twidere.model.ParcelableMessage import org.mariotaku.twidere.model.ParcelableMessage.MessageType import org.mariotaku.twidere.model.UserKey +import org.mariotaku.twidere.model.message.ConversationInfoUpdatedExtras import org.mariotaku.twidere.model.message.MessageExtras -import org.mariotaku.twidere.model.message.NameUpdatedExtras import org.mariotaku.twidere.model.message.StickerExtras import org.mariotaku.twidere.model.message.UserArrayExtras import org.mariotaku.twidere.util.InternalTwitterContentUtils @@ -60,7 +60,14 @@ object ParcelableMessageUtils { } entry.conversationNameUpdate != null -> { return ParcelableMessage().apply { - applyNameUpdatedEvent(accountKey, entry.conversationNameUpdate, users) + applyInfoUpdatedEvent(accountKey, entry.conversationNameUpdate, users, + MessageType.CONVERSATION_NAME_UPDATE) + } + } + entry.conversationAvatarUpdate != null -> { + return ParcelableMessage().apply { + applyInfoUpdatedEvent(accountKey, entry.conversationAvatarUpdate, users, + MessageType.CONVERSATION_AVATAR_UPDATE) } } } @@ -107,12 +114,13 @@ object ParcelableMessageUtils { this.is_outgoing = false } - private fun ParcelableMessage.applyNameUpdatedEvent(accountKey: UserKey, message: Message, - users: Map) { + private fun ParcelableMessage.applyInfoUpdatedEvent(accountKey: UserKey, message: Message, + users: Map, @MessageType type: String) { this.commonEntry(accountKey, message) - this.message_type = MessageType.CONVERSATION_NAME_UPDATE - this.extras = NameUpdatedExtras().apply { + this.message_type = type + this.extras = ConversationInfoUpdatedExtras().apply { this.name = message.conversationName + this.avatar = message.conversationAvatarImageHttps this.user = users[message.byUserId]?.convert { ParcelableUserUtils.fromUser(it, accountKey) } } this.is_outgoing = false diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetMessagesTask.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetMessagesTask.kt index 6410cfb75..511552dcd 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetMessagesTask.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/task/GetMessagesTask.kt @@ -217,6 +217,7 @@ class GetMessagesTask( val conversation = conversations.addConversation(k, details, message, participants, conversationType) conversation.conversation_name = v.name + conversation.conversation_avatar = v.avatarImageHttps conversation.request_cursor = response.cursor conversation.conversation_extras_type = ParcelableMessageConversation.ExtrasType.TWITTER_OFFICIAL conversation.conversation_extras = TwitterOfficialConversationExtras().apply { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/MediaLoaderWrapper.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/MediaLoaderWrapper.kt index ce66c87de..8579f539b 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/MediaLoaderWrapper.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/MediaLoaderWrapper.kt @@ -56,6 +56,11 @@ class MediaLoaderWrapper(val imageLoader: ImageLoader) { .bitmapConfig(Bitmap.Config.RGB_565) .build() + private val groupConversationAvatarDisplayOptions = DisplayImageOptions.Builder() + .cloneFrom(profileImageDisplayOptions) + .showImageForEmptyUri(R.drawable.ic_profile_image_default_group) + .build() + private val dashboardProfileImageDisplayOptions = DisplayImageOptions.Builder() .cacheInMemory(true) .cacheOnDisk(true) @@ -176,6 +181,10 @@ class MediaLoaderWrapper(val imageLoader: ImageLoader) { imageLoader.displayImage(url, view, profileImageDisplayOptions) } + fun displayGroupConversationAvatar(view: ImageView, url: String?) { + imageLoader.displayImage(url, view, groupConversationAvatarDisplayOptions) + } + fun loadImageSync(url: String, targetImageSize: ImageSize, options: DisplayImageOptions): Bitmap? { return imageLoader.loadImageSync(url, targetImageSize, options) } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/message/MessageEntryViewHolder.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/message/MessageEntryViewHolder.kt index 9b9f2cda0..dc9cbaa14 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/message/MessageEntryViewHolder.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/view/holder/message/MessageEntryViewHolder.kt @@ -77,8 +77,7 @@ class MessageEntryViewHolder(itemView: View, val adapter: MessagesEntriesAdapter // TODO display default profile image } } else { - adapter.mediaLoader.cancelDisplayTask(profileImage) - profileImage.setImageResource(R.drawable.ic_profile_image_default_group) + adapter.mediaLoader.displayGroupConversationAvatar(profileImage, conversation.conversation_avatar) } } diff --git a/twidere/src/main/res/values/strings.xml b/twidere/src/main/res/values/strings.xml index 0c667d539..4b3618e95 100644 --- a/twidere/src/main/res/values/strings.xml +++ b/twidere/src/main/res/values/strings.xml @@ -669,6 +669,9 @@ Direct message deleted. Direct message sent. Some account data are corrupted, Twidere will remove those accounts to prevent crash. + Conversation avatar changed + + %1$s changed conversation avatar Conversation name changed to