Giter Club home page Giter Club logo

Comments (4)

rusmonster avatar rusmonster commented on July 17, 2024 2

In term of your approach I'd suggest smth like:

 private fun addImageMessageToList(it: Message): com.frameshift.chat.model.Message {
      val imageMessage = ImageMessage(imageUrl = STUB_IMAGE_URL).apply {
          setSender(it.author)
          setMessageId(it.media.sid)
          setTimestamp(it.dateCreated.getTimeInMillis())
      }
      fetchMediaUrlAsync(it, imageMessage)
      return imageMessage;
  }

 private fun fetchMediaUrlAsync(message: Message, model: com.frameshift.chat.model.Message) = viewModelScope.launch {    
    val imageUrl = io { fetchMediaUrlUseCase.perform(message.media) }

    when (imageUrl) {
      is Success -> model.imageUrl.postValue(imageUrl.body) // model.imageUrl is MutableLiveData<Uri>

      else -> model.imageUrl.postValue(LOAD_ERROR_IMAGE_URL)
    }
 }

class ChannelListAdapter(
    private val owner: LifecycleOwner // pass activity here
): RecyclerView.Adapter<...>() {

    var messages: List<com.frameshift.chat.model.Message> by Delegates.observable(emptyList()) { _, old, new ->
        new.forEachIndexed { index, message ->
            message.imageUrl.observe(owner, Observer<Uri> {
                notifyItemChanged(index)
            })
        }
    }

    override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
        val message = messages[position]
        GlideApp.with(this)
            .load(message.imageUrl.value)
            .into(holder.imageView). 
    }
}

In general if it's not too late I'd suggest to switch to the Recommended app architecture where most part of such questions solved out from the box. BTW we are going to publish new twilio demo apps later this year which will demonstrate how to build an app with twilio chat sdk using that architecture.

from twilio-chat-demo-android.

rusmonster avatar rusmonster commented on July 17, 2024

I'd suggest put a stub image into view and show it immediately, at the same time make async request which fetch imageUrl using sdk then download the image then update the view.

from twilio-chat-demo-android.

rooparsh avatar rooparsh commented on July 17, 2024

@rusmonster Even I thought the same. But I wanted the UI to be unaware of the Twilio. So how do you suggest I make an API call? Are you suggesting interface method from onBindViewHolder?

from twilio-chat-demo-android.

rooparsh avatar rooparsh commented on July 17, 2024

@rusmonster Is there any way I can fetch media Url from just message sid? Right now, my viewHolder has just message sid in model. I am thinking of maybe fetch urls in recyclerView oBindViewHolder using sid?

from twilio-chat-demo-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.