0

I am trying to call a function from a ViewModel of an Activity and unfortunately I don't know how.

Why am I trying to call a function from a ViewModel of an Activity?: I have specific items in the model which I want to click and they should do different things.

Currently I am using Intent with putExtra and startActivity like this:

val context = viewHolder.itemView.context
val intent = Intent( context, MainActivity::class.java)
            intent.putExtra("type","VideoItem")
            context.startActivity(intent)

and using val type = intent.getStringExtra("type") to use it to call my function but this is quite hacky way.

So my question is how can I do this better?


Here is my ViewModel I am using:

class VideoItem(val video: Video) : Item<GroupieViewHolder>() {
    @SuppressLint("SetTextI18n")
    override fun bind(viewHolder: GroupieViewHolder, position: Int) {
        val context = viewHolder.itemView.context

        val ref = database.getReference("/channels")
        ref.addListenerForSingleValueEvent(object: ValueEventListener {
            override fun onDataChange(p0: DataSnapshot) {
                p0.children.forEach {
                    val channel = it.getValue(Channel::class.java)
                    if (channel != null && channel.uid == video.publisher_uid) {
                        val profileImageUrl =  channel.channel_logo

                        viewHolder.itemView.channel_name.text = channel.channel_name

                        val targetImageView = viewHolder.itemView.user_profile_image
                        Picasso.get().load(profileImageUrl)
                            .placeholder(R.drawable.ic_baseline_account_circle_24)
                            .into(targetImageView)
                    }
                }
            }
            override fun onCancelled(e: DatabaseError) {
                Log.d("VideoItem","Cancelled")
            }
        })

        // Here is the "hacky" way I am using to do something else in a class
        viewHolder.itemView.user_profile_image.setOnClickListener {
            selectedChannel = video.publisher_uid
            val intent = Intent( context, MainActivity::class.java)
            intent.putExtra("type","VideoItem")
            context.startActivity(intent)
        }

        // Here is one example how I tried it
        /*viewHolder.itemView.videoItem_dot_menu.setOnClickListener {
            selectedVideo = video.video_uid
            Toast.makeText(context, selectedVideo,Toast.LENGTH_SHORT).show()
            val mainActivity = MainActivity()
            mainActivity.videoItemOptions()
        }*/

        try { Picasso.get().load(video.thumbnail).into(viewHolder.itemView.thumbnail) }
        catch (e:Exception){ println(e.message) }

        viewHolder.itemView.video_title.text = video.video_title
        viewHolder.itemView.views_count.text = "${video.views} Views"
        viewHolder.itemView.publish_date.text = video.date?.substring(0,10)?.replace("-",".").toString()
    }
    override fun getLayout(): Int {
        return R.layout.video_item
    }

    companion object{
        val TAG = "VideoItem"
        var selectedVideo: String? = null
        var selectedChannel: String? = null
    }
Andrey
  • 55
  • 7

0 Answers0