Trying to make a file downloadManager
we have a activity which contains recycler view, each item in recycler view represents a file which is either in downloading, downladed state. Each recycler view item has a callback this callback has 3 information: onSuccess (file), onError(Error), progress(Int). Now I want to extract this information in BindViewHolder to show the progress, but i cannot get value in callback: (removing other code for simplicity)
class DownloadActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
val list = mutableListOf<DItem>()
list.add(DItem("url", callback = object : Listener{
override fun onSuccess(file: File) {
}
override fun onFailure(error: Error) {
}
override fun onProgress(progress: Int) {
}
})
// trigger from here for simplicity otherwise each list item has a download button which will trigger this:
FileDownloadManager().download("utl", list[0].callback)
}
// adapter
class DownloadAdapter : RecyclerView.Adapter<MyViewhOdler>() {
val list = mutableListOf<DItem>()
override fun onBindViewHolder(holder: MyViewhOdler, position: Int) {
// how to get value here:
list.get(0).callback
}
}
class FileDownloadManager {
override fun download(url: String, callback: Listener) {
// on donwloading start, will trigger
// callback.progress(<some value>)
}
interface Listener{
fun onSuccess(file: File)
fun onFailure(error: Error)
fun onProgress(progress: Int)
}
}
// Ditem
data class DItem (
var url: String? = null,
var callback: Listener,
var fcallback: ((File, Error, Int)-> Unit)
)
}
If I understand it correctly, you want to get the "progress" value in "onBindViewHolder()" but you don't know how to do it.
One of the first things that I thought about, not modifying much of the code but may also be a dirty solution, is:
You actually define your object callback later, so the constructor will not accept "callback" anymore, instead it will be a var that you will initialize later.
At this point, in "onBindViewHolder()" you can now define your callback and get the value in that place:
I hope I did understand the issue correctly and I really want to stress the fact that this is not necessarily a clean solution but one of the fastest to implement and with the least amount of code refactoring.