ProgressBar not updating in RecyclerView item row

Viewed 95

I'm uploading some photos to my server, and want to view the progress of individual uploads. However, my RecyclerView isn't updating.

enter image description here

I know the progress is being updated, as I can see the progress in the my upload task, and the photo is uploaded to the server. I thought the viewholder would be what I would use to update the view, but my progress listener isn't' called.

Does anyone see what I have done wrong? Or could do better?

The PhotoUploadItem is the object that gets updated with the progress.

class PhotoUploadItem(var photoStore: PhotoStore) {

    interface PhotoUploadProgressListener {
        fun onProgressChanged(progress: Int)
    }

    private var uploadProgress = 0
    private var listener: PhotoUploadProgressListener? = null
  

    fun setUploadProgress(uploadProgress: Int) {
        this.uploadProgress = uploadProgress
        listener?.onProgressChanged(uploadProgress)
    }

    fun subscribe(listener: PhotoUploadProgressListener) {
        this.listener = listener
    }

    fun unsubscribe() {
        this.listener = null
    }

    fun progress() = uploadProgress
    fun identifier(): Int = photoStore.key
}

The PhotoUploadsAdapter takes a list of them.

class PhotoUploadsAdapter : RecyclerView.Adapter<PhotoUploadsAdapter.CustomViewHolder>() {

    inner class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    private val differCallback = object : DiffUtil.ItemCallback<PhotoUploadItem>() {
        override fun areItemsTheSame(oldItem: PhotoUploadItem, newItem: PhotoUploadItem) = oldItem.identifier() == newItem.identifier()

        override fun areContentsTheSame(oldItem: PhotoUploadItem, newItem: PhotoUploadItem) = oldItem.hashCode() == newItem.hashCode()
    }

    val differ = AsyncListDiffer(this, differCallback)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        return CustomViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.row_photo_upload, parent, false))
    }

    override fun getItemCount() = differ.currentList.size

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
        val item = differ.currentList[position]
        holder.itemView.apply {
            ///..... set other fields
            tvDisplayName?.text = item.photoStore.displayName
 
// THIS DOESN'T WORK
            item.subscribe(object : PhotoUploadItem.PhotoUploadProgressListener{
                override fun onProgressChanged(progress: Int) {
                    Log.i("UploadViewHolder", "${item.identifier()} has progress: $progress")
                    progressBar?.progress = progress
                    tvProgressAmount?.text = String.format(Locale.ROOT, "%d%%", progress)
                }
            })

        }
    }
}
0 Answers
Related