jasu
07/20/2022, 4:11 AMFilip Wiesner
07/20/2022, 7:39 AMcallbackFlow { ... }
jasu
07/20/2022, 7:42 AMTin Tran
07/20/2022, 7:48 AMWorkManager
allow you to query its progress as a LiveData
which you could then use to collect as state and visualize it on composejasu
07/20/2022, 7:49 AMFilip Wiesner
07/20/2022, 7:49 AMi’m porting that progress through lambda to the repositorySo a callback? If you have some data structure that holds progress for several files that can update independently, then
StateFlow
might be the best option.
If you could make the upload function like fun uploadFile(path: String): Flow<Progress>
, then I would use callbackFlow { ... }
flow { ... }
or callbackFlow { ... }
.jasu
07/20/2022, 7:52 AMFilip Wiesner
07/20/2022, 7:57 AMTin Tran
07/20/2022, 8:03 AMfun ResponseBody?.writeToFileWithProgress(file: File?): Flow<Int> {
if (this == null || file == null) {
this?.close()
return emptyFlow()
}
val body = this
return flow {
body.use {
body.byteStream().use { inStream ->
val outStream = file.outputStream()
outStream.use {
var read: Int
val buffer = ByteArray(Constants.BUFFER_SIZE)
while (inStream.read(buffer).also { read = it } != -1) {
emit(read)
outStream.write(buffer, 0, read)
}
}
}
}
}
}
I’m using this for download but for upload it’s pretty much the same. It’s woking well in my production appjasu
07/20/2022, 10:13 AM