stream-chat-android icon indicating copy to clipboard operation
stream-chat-android copied to clipboard

Attachment message not localized to the current set user's device

Open savked opened this issue 7 years ago • 12 comments

In our app, we have a system messages that notify users within the chat for some events.

This attachment looks like this:

{
  text: 'Seems you both travel to Australia and could meet each other between August 17, 2015 and August 21, 2015',
  system_event: 'system:match',
  attachments: [
    {
      type: 'event',
      title: 'system:match'
    },
    {
      type: 'i18n',
      text: "{\"en\":\"Seems you both travel to Australia and could meet each other between August 17, 2015 and August 21, 2015\",\"de\":\"Es scheint als ob ihr nach Australien reisen werdet und euch zwischen 17. August 2015 und 21. August 2015 treffen könntet\",\"es\":\"Parece que ambas viajan a Australia y que podrían coincidir entre 17 de agosto de 2015 y 21 de agosto de 2015\"}"
    }
  ]
}

I am displaying the message with the type "i18n" however the messages list does not read this text localized. I have my phone on German for example, but the message is in English.

savked avatar Nov 27 '19 14:11 savked

I have just came to a realisation that the chat is actually showin the message under field text and it's obviously in English. However you can see on the screenshot provided that there is an "empty" attachment above the message that is actually attachment with the message library should display in language. What are my options here, what can we do about this?

testupload

savked avatar Nov 28 '19 12:11 savked

@savked how're you able to send a system message in a conversation? I've not been able to successfully do this

Chikrix avatar Dec 01 '19 22:12 Chikrix

@Chikrix at the moment the APIs only allow you to send system messages when updating a channel (ie. updating the channel data, add/remove members, ban/unban members)

Beside this; you can also add a custom field to the message (ie. system:true) and let the Android SDK render them differently

tbarbugli avatar Dec 02 '19 12:12 tbarbugli

@savked the SDK does not have built-in i18n for message text; I think the easiest approach is to use a custom message class which picks the right version of the message text. @adrian09h perhaps you can provide some advice here?

tbarbugli avatar Dec 02 '19 12:12 tbarbugli

How would I implement that in a ChannelActivity, any documentation that you can refer me to?

savked avatar Dec 02 '19 13:12 savked

@savked ,SDK doesn't provide i18n type , provides attachment types as default such as image, video, file, giphy, link and product. So if you want to use custom type of attachment, it needs some consideration about custom type of attachment additionally.

That is, you should set your MessageViewHolderFactory and register it ChannelActivity Please read A custom attachment type of java-tutorial

image

adrian09h avatar Dec 02 '19 14:12 adrian09h

@savked , the other question for custom type of Attachment?

adrian09h avatar Dec 03 '19 07:12 adrian09h

That's it for now, thank you. If I need any help I'll reopen this issue.

savked avatar Dec 03 '19 08:12 savked

Followed the example you provided and made a CustomMessageViewHolderFactory that looks like this:

public class CustomMessageViewHolderFactory extends MessageViewHolderFactory {

    private final String TAG = "CustomMessageViewHolder";

    private int I18N_TYPE = 0;

    @Override
    public int getAttachmentViewType(Message message, Boolean mine, Position position, List<Attachment> attachments, Attachment attachment) {
    if (attachment.getType().equals("i18n")) {
        return I18N_TYPE;
    }

        return super.getAttachmentViewType(message, mine, position, attachments, attachment);
    }

    @Override
    public BaseAttachmentViewHolder createAttachmentViewHolder(AttachmentListItemAdapter adapter, ViewGroup parent, int viewType) {
        BaseAttachmentViewHolder holder;
        if (viewType == I18N_TYPE)
            holder = new AttachmentViewHolderCustom(R.layout.chat_custom_attachment, parent);
        else
            holder = super.createAttachmentViewHolder(adapter, parent, viewType);

        return holder;
    }
}

The R.layout.chat_custom_attachment is literally a TextView inside the LinearLayout centered.

And the AttachmentViewHolderCustom:

public class AttachmentViewHolderCustom extends BaseAttachmentViewHolder {

    private final String TAG = "AttachmentViewHolder";

    private TextView attachmentText;

    public AttachmentViewHolderCustom(int resId, ViewGroup parent) {
        super(resId, parent);

        attachmentText = itemView.findViewById(R.id.attachmentText);
    }

    @Override
    public void bind(Context context, MessageListItem messageListItem, Attachment attachment, MessageListViewStyle style, MessageListView.AttachmentClickListener clickListener, MessageListView.MessageLongClickListener longClickListener) {
        super.bind(context, messageListItem, attachment, style, clickListener, longClickListener);

        attachmentText.setText(attachment.getText());
    }
}

And of course setting the ViewHolderFactory inside my MessagingActivity:

    binding.setViewModel(viewModel);
    binding.messageList.setViewHolderFactory(new CustomMessageViewHolderFactory());
    binding.messageList.setViewModel(viewModel, this);
    binding.messageInput.setViewModel(viewModel, this);

But the layout is not visible, while debugging it is seen that the text was set, but it's not visible, I also messed around with the layout width and height and it is changing inside the chat, but the TextView is not showing, any clue what it could be?

Also, when I resolve this, since I will be showing the attachment as a system message, how can I hide the ordinary system messages? I tried checking the message sender in CustomMessageViewHolderFactory and returning -1 or something in getAttachmentViewType, but returning an empty view would be hacky, there probably is a better way?

savked avatar Dec 03 '19 10:12 savked

@savked , I just checked custom attachment type feature, but works well. I think your code that is handling the size of custom attachment layout is working wrong. I'd like you to check it again.

And then if you want to hide system message in case that attachment type is i18n, you must handle CustomMessageViewHolder as well as AttachmentViewHolderCustom .

I will introduce the way below.

  • Create list_custom_item_message.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <com.getstream.sdk.chat.view.AttachmentListView
        android:id="@+id/attachmentview"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
  • Create CustomMessageViewHolder as subclassing of BaseMessageListItemViewHolder
public class CustomMessageViewHolder extends BaseMessageListItemViewHolder {

    private AttachmentListView attachmentListView;
    private MessageViewHolderFactory viewHolderFactory;
    private MessageListViewStyle style;

    public CustomMessageViewHolder(int resId, ViewGroup parent) {
        super(resId, parent);
        attachmentListView = itemView.findViewById(com.getstream.sdk.chat.R.id.attachmentview);
    }

    @Override
    public void bind(Context context, ChannelState channelState, @NonNull MessageListItem messageListItem, int position) { 
        attachmentListView.setViewHolderFactory(viewHolderFactory);
        attachmentListView.setStyle(style);
        attachmentListView.setBubbleHelper(getBubbleHelper());
        attachmentListView.setEntity(messageListItem);
    }

    @Override
    public void setStyle(MessageListViewStyle style) {
         this.style = style;
    }

    public void setViewHolderFactory(CustomMessageViewHolderFactory viewHolderFactory) {
        this.viewHolderFactory = viewHolderFactory;
    }
}
  • Update your CustomMessageViewHolderFactory
public class CustomMessageViewHolderFactory extends MessageViewHolderFactory {
    private static int I18N_TYPE = 0;

    @Override
    public int getEntityViewType(MessageListItem messageListItem, Boolean mine, List<Position> positions) {
        if (isI18N_TYPE(messageListItem))
			return I18N_TYPE;

        return super.getEntityViewType(messageListItem, mine, positions);
    }

    @Override
    public int getAttachmentViewType(Message message, Boolean mine, Position position, List<Attachment> attachments, Attachment attachment) {
        if (attachment.getType().equals("i18n"))
            return I18N_TYPE;
        
        return super.getAttachmentViewType(message, mine, position, attachments, attachment);
    }

    public BaseMessageListItemViewHolder createMessageViewHolder(MessageListItemAdapter adapter, ViewGroup parent, int viewType) {
        if (viewType == I18N_TYPE) {
            CustomMessageViewHolder holder = new CustomMessageViewHolder(R.layout.list_custom_item_message, parent);
            holder.setViewHolderFactory(this);
            holder.setStyle(adapter.getStyle());
            return holder;
        }
        return super.createMessageViewHolder(adapter, parent, viewType);
    }

    @Override
    public BaseAttachmentViewHolder createAttachmentViewHolder(AttachmentListItemAdapter adapter, ViewGroup parent, int viewType) {
        BaseAttachmentViewHolder holder;
        if (viewType == I18N_TYPE) {
            holder = new AttachmentViewHolderCustom(R.layout.list_item_attach_custom, parent);
            holder.setStyle(adapter.getStyle());
        } else {
            holder = super.createAttachmentViewHolder(adapter, parent, viewType);
        }
        return holder;
    }	

    private boolean isI18N_TYPE(MessageListItem messageListItem){
        // TODO: check if messageListItem has attachamt of I18N_TYPE type.
        return true;
    }
}

adrian09h avatar Dec 03 '19 20:12 adrian09h

Just tried your solution but having a crash within the SDK, please check it out and get back to me. Issue reported #360

savked avatar Dec 04 '19 08:12 savked

@savked , Sorry. I have missed setStyle() updated resolution code above. Please try again.

adrian09h avatar Dec 04 '19 20:12 adrian09h