Skip to content

Instantly share code, notes, and snippets.

@francescogatto
Last active July 5, 2022 16:11
Show Gist options
  • Save francescogatto/218bb70d01b392ba7f8e935c4d4da3d6 to your computer and use it in GitHub Desktop.
Save francescogatto/218bb70d01b392ba7f8e935c4d4da3d6 to your computer and use it in GitHub Desktop.
#kkd
private fun downloadWithFlow(dummy: DummyData) {
CoroutineScope(Dispatchers.IO).launch {
ktor.downloadFile(dummy.file, dummy.url).collect {
withContext(Dispatchers.Main) {
when (it) {
is DownloadResult.Success -> {
myAdapter.setDownloading(dummy, false)
}
is DownloadResult.Error -> {
myAdapter.setDownloading(dummy, false)
Toast.makeText(this@MainActivity, "Error while downloading ${dummy.title}", Toast.LENGTH_LONG).show()
}
is DownloadResult.Progress -> {
myAdapter.setProgress(dummy, it.progress)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment