Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send data from custom MessageHolder to MessageActivity. #180

Closed
aalap03 opened this issue Aug 9, 2018 · 10 comments
Closed

Send data from custom MessageHolder to MessageActivity. #180

aalap03 opened this issue Aug 9, 2018 · 10 comments

Comments

@aalap03
Copy link

aalap03 commented Aug 9, 2018

So I have a custom MessageHolder classes for both texts and Images, from which I make MessageHolders variable which goes to MessagesListAdapter.

From an action inside MessageHolderImage class, I need to call messageListAdapter.notifyDataSetChanged(), but as per the structure I dont have any use of MessageHolderImage class's constructor, please let me know how to approach this.

MessageActivity.class

messageHolders = MessageHolders()
            .setIncomingTextConfig(MessageHolderText::class.java, R.layout.other_user_message_item)
            .setOutcomingTextConfig(MessageHolderText::class.java, R.layout.my_user_message_item)
            .setIncomingImageConfig(MessagesHolderImage::class.java, R.layout.other_user_message_item_image)
            .setOutcomingImageConfig(MessagesHolderImage::class.java, R.layout.my_user_message_item_image)

messagesAdapter = MessagesListAdapter(pref.chatIdentity, messageHolders, ImageLoader { _, _ -> })

MessageHolderImage.class and MessageHolderText.class (Same classes except image and text difference ofcourse)

class MessagesHolderImage(itemView: View) : MessageHolders.BaseMessageViewHolder<MessageItem>(itemView), AnkoLogger{

    private var glide = Glide.with(itemView)
        .setDefaultRequestOptions(RequestOptions().placeholder(R.drawable.default))
    private val time: TextView = itemView.findViewById(R.id.user_message_time)
    private val name: TextView = itemView.findViewById(R.id.user_name)

    override fun onBind(messageItem: MessageItem?) {

        time.text = Utils.chatDateFormatterHour(messageItem?.createdAt.toString())
        name.text = messageItem?.user?.name

        if (messageItem?.user?.name?.contains(App.getPreferences().fname)!!)
            glideProfile.asBitmap()
                    .load(byteArray())
                    .into(profileImage)

       //how to go from here to messageActivity, as I dont have constructor which allows an 
       //interface callback which transfers data from here to messageActivity
    }
}

Please tell me if I have a wrong approach or something can be modified here, Thanks in advance.

@aalap03
Copy link
Author

aalap03 commented Aug 13, 2018

Hi, Is there any ideas or help for this issue ????

@bevzaanton
Copy link
Contributor

Hi @aalap03 , thanks for your request. It's very interesting question. Unfortunately you can't add callback to custom ViewHolder. I can't understand how we missed this functionality) In new version we will add this opportunity. I am working now on this task.

@aalap03
Copy link
Author

aalap03 commented Aug 15, 2018

Thanks for the response and library work, will eagerly wait for the update.

@bevzaanton
Copy link
Contributor

ChatKit version 0.3.1 has been released. You can check this documentation. If documentation is not clear, please let me know.

@aalap03
Copy link
Author

aalap03 commented Aug 16, 2018

I tried and if I use the payload parameter in MessageHolderText constructor class, it crashes with error message

Somehow we couldn't create the ViewHolder for message. Please, report this issue on GitHub with full stacktrace in description.

(Posted stack trace at the very end here.)

Payload class with callback var

class Payload {
    lateinit var reactionCallBack: ReactionCallBack
}

Callback interface

interface ReactionCallBack {
     fun passReaction(reaction: Reaction)
}

CustomHolder class If I take payload parameter out, the error will go.

class MessageHolderText(itemView: View, var payload: Payload) : MessageHolders.BaseMessageViewHolder<MessageItem>(itemView, payload), AnkoLogger{
  
  override fun onBind(messageItem: MessageItem) {
      emojiAdapterMessage = EmojiAdapterMessage(itemView.context, reactions, payload)        
  }
}

And in my MessageActivity class I create MessageHolder like this

//creating the payload var.
val payload = Payload()
    payload.reactionCallBack = object : ReactionCallBack{
        override fun passReaction(reaction: Reaction) {
            updateOrSetAttributes(reaction.emoji)
        }
    }

//creating messageHolder and setting configs with Custom MessageHolderText 
messageHolders = MessageHolders()
            .setOutcomingTextConfig(MessageHolderText::class.java, R.layout.my_user_message_item, payload)
            .setIncomingTextConfig(MessageHolderText::class.java, R.layout.other_user_message_item, payload)
            .setOutcomingImageConfig(MessagesHolderImage::class.java, R.layout.my_user_message_item_image, payload)
            .setIncomingImageConfig(MessagesHolderImage::class.java, R.layout.other_user_message_item_image, payload)

This is the stacktrace I get when I enter the MessageActivity class.
Please let me know if my implementation is wrong somewhere ?

08-16 13:35:53.501 16740-16740/plante.com.cobalt.mobileaccess E/AndroidRuntime: FATAL EXCEPTION: main
Process: plante.com.cobalt.mobileaccess, PID: 16740
java.lang.UnsupportedOperationException: Somehow we couldn't create the ViewHolder for message. Please, report this issue on GitHub with full stacktrace in description.
    at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:609)
    at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:584)
    at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:520)
    at com.stfalcon.chatkit.messages.MessagesListAdapter.onCreateViewHolder(MessagesListAdapter.java:99)
    at com.stfalcon.chatkit.messages.MessagesListAdapter.onCreateViewHolder(MessagesListAdapter.java:47)
    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
    at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:589)
    at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
    at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
    at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
    at android.view.View.layout(View.java:18878)
    at android.view.ViewGroup.layout(ViewGroup.java:5953)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
    at android.view.View.layout(View.java:18878)
    at android.view.ViewGroup.layout(ViewGroup.java:5953)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:18878)
    at android.view.ViewGroup.layout(ViewGroup.java:5953)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:18878)
    at android.view.ViewGroup.layout(ViewGroup.java:5953)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:18878)
    at android.view.ViewGroup.layout(ViewGroup.java:5953)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:18878)
    at android.view.ViewGroup.layout(ViewGroup.java:5953)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:934)
    at android.view.View.layout(View.java:18878)
    at android.view.ViewGroup.layout(ViewGroup.java:5953)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2684)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2400)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1537)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:959)
    at android.view.Choreographer.doCallbacks(Choreographer.java:734)
    at android.view.Choreographer.doFrame(Choreographer.java:670)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:945)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1510)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1400)
 Caused by: java.lang.NoSuchMethodException: <init> [class android.view.View]
    at java.lang.Class.getConstructor0(Class.java:2204)
    at java.lang.Class.getDeclaredConstructor(Class.java:2050)
    at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:600)
    	... 58 more

@bevzaanton
Copy link
Contributor

bevzaanton commented Aug 17, 2018

try to change class MessageHolderText(itemView: View, var payload: Payload)->class MessageHolderText(itemView: View, var payload: Object) and cast object to Payload class in constructor.

@aalap03
Copy link
Author

aalap03 commented Aug 17, 2018

It works.. !!!!!!! Bravo @bevzaanton

Thanks a ton keep up the good work..!!

@aalap03 aalap03 closed this as completed Aug 17, 2018
@yudikarma
Copy link

try to change class MessageHolderText(itemView: View, var payload: Payload)->class MessageHolderText(itemView: View, var payload: Object) and cast object to Payload class in constructor.

please expalin "how cast object to Payload class in constructor."
@bevzaanton @aalap03

@bevzaanton
Copy link
Contributor

e.g. payload as Payload

@yudikarma
Copy link

thanks @bevzaanton

i got this error
Somehow we couldn't create the ViewHolder for message. Please, report this issue on GitHub with full stacktrace in description.
at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:609)
at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolders.java:584)
at com.stfalcon.chatkit.messages.MessageHolders.getHolder(MessageHolde

but iam already do "payload as Payload"
and after try...
this error cause
MessageHolders holdersConfig = new MessageHolders() .setIncomingTextConfig( CustomIncomingTextMessageViewHolder.class, R.layout.item_custom_incoming_text_message, payload) .setOutcomingTextConfig( CustomOutcomingTextMessageViewHolder.class, R.layout.item_custom_outcoming_text_message) .setIncomingImageConfig( CustomIncomingImageMessageViewHolder.class, R.layout.item_custom_incoming_image_message) .setOutcomingImageConfig( CustomOutcomingImageMessageViewHolder.class, R.layout.item_custom_outcoming_image_message);
but after change to this.
`val payload = CustomIncomingTextMessageViewHolder.Payload()
payload.avatarClickListener = object : CustomIncomingTextMessageViewHolder.OnAvatarClickListener {
override fun onAvatarClick() {
// updateOrSetAttributes(reaction.emoji)
toast("clicked")
}
}

    val holdersConfig = MessageHolders()
        .setIncomingTextConfig(CustomIncomingTextMessageViewHolder::class.java,R.layout.item_custom_incoming_text_message,payload)
        .setOutcomingTextConfig(CustomOutcomingTextMessageViewHolder::class.java,R.layout.item_custom_outcoming_text_message,payload)
        .setIncomingImageConfig(CustomIncomingImageMessageViewHolder::class.java,R.layout.item_custom_incoming_image_message,payload)
        .setOutcomingImageConfig(CustomOutcomingImageMessageViewHolder::class.java,R.layout.item_custom_outcoming_image_message,payload)


    super.messagesListAdapter = MessagesListAdapter(SENDERID,holdersConfig,imageLoader)
    super.messagesListAdapter.enableSelectionMode(this)
    super.messagesListAdapter.setLoadMoreListener(this)
    super.messagesListAdapter.setDateHeadersFormatter(this)
    layout_messagelist.setAdapter(super.messagesListAdapter)`

no more error. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants