ChatKit icon indicating copy to clipboard operation
ChatKit copied to clipboard

Unable to correctly implement setLastMessage()

Open arogyakoirala opened this issue 6 years ago • 2 comments

HI, thank you so much for this amazing library! I ran into a bit of trouble so wanted your help..

Here's my Message Model, which implements IMessage:

@Entity(tableName = "messages")
public class Message implements IMessage {

    @SerializedName("id")
    @PrimaryKey
    private int messageId;

    @SerializedName("text")
    private String text;

    @SerializedName("date")
    private String createdDate;

    @SerializedName("thread")
    private String thread;


    @SerializedName("user")
    private User user;

    public Message(int messageId, String text, String createdDate, String thread, User user) {
        this.messageId = messageId;
        this.text = text;
        this.createdDate = createdDate;
        this.thread = thread;
        this.user = user;
    }

    public int getMessageId() {
        return messageId;
    }

    public void setMessageId(int messageId) {
        this.messageId = messageId;
    }

    @Override
    public String getId() {
        return String.valueOf(messageId);
    }

    @Override
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public String getThread() {
        return thread;
    }

    public void setThread(String thread) {
        this.thread = thread;
    }

    @Override
    public User getUser() {
        return user;
    }

    @Override
    public Date getCreatedAt() {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        try {
            return f.parse(createdDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    public void setUser(User user) {
        this.user = user;
    }


    @Override
    public String toString() {
        return "Message{" +
                "messageId=" + messageId +
                ", text='" + text + '\'' +
                ", createdDate='" + createdDate + '\'' +
                ", thread='" + thread + '\'' +
                ", user=" + user +
                '}';
    }
}


And here's my Conversation (IDialog) model:


@Entity(tableName = "conversations")
public class Conversation implements IDialog {

    @SerializedName("id")
    @PrimaryKey
    private int conversationId;

    @SerializedName("message")
    private Message lastMessage;


    @SerializedName("participants")
    private List<User> participants;


    public Conversation(int conversationId, Message lastMessage, List<User> participants) {
        this.conversationId = conversationId;
        this.lastMessage = lastMessage;
        this.participants = participants;
    }

    public int getConversationId() {
        return conversationId;
    }

    public void setConversationId(int conversationId) {
        this.conversationId = conversationId;
    }

    @Override
    public String getId() {
        return lastMessage.getThread();
    }

    @Override
    public String getDialogPhoto() {
        return participants.get(0).getAvatar();
    }

    @Override
    public String getDialogName() {
        return participants.size() > 2 ? participants.get(0).getFirstName() + ", +" + String.valueOf(participants.size()-1) + " more": participants.get(0).getUserFullName();
    }

    @Override
    public List<? extends IUser> getUsers() {
        return participants;
    }

    public Message getLastMessage() {
        return lastMessage;
    }

    @Override
    public void setLastMessage(IMessage message) {
//        this.lastMessage = message;

    }

    @Override
    public int getUnreadCount() {
        return 0;
    }



    public List<User> getParticipants() {
        return participants;
    }

    public void setParticipants(List<User> participants) {
        this.participants = participants;
    }


}

As you can see, setLastMessage() throws an error because lastMessage is of type Message, as opposed to IMessage

How do I get around this?

arogyakoirala avatar Jul 31 '19 13:07 arogyakoirala

Hi @arogyakoirala Check this example: https://github.com/stfalcon-studio/ChatKit/blob/master/sample/src/main/java/com/stfalcon/chatkit/sample/common/data/model/Dialog.java

bevzaanton avatar Sep 18 '19 07:09 bevzaanton

You need to implement IDialog<Message>

bevzaanton avatar Sep 18 '19 07:09 bevzaanton