This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
val dummy = dummyes[position] | |
with(holder.itemView) { | |
title.text = dummy.title | |
downloadIcon.isVisible = !dummy.file.exists() | |
documentTypeIcon.isVisible = dummy.file.exists() | |
progressBarDocument.isVisible = dummy.isDownloading | |
textProgress.isVisible = dummy.isDownloading | |
setOnClickListener { | |
listener(dummy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun downloadWithFlow(dummy: DummyData) { | |
CoroutineScope(Dispatchers.IO).launch { | |
ktor.downloadFile(dummy.file, dummy.url).collect { | |
when (it) { | |
is DownloadResult.Success -> { | |
} | |
is DownloadResult.Error -> { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun HttpClient.downloadFile(file: File, url: String): Flow<DownloadResult> { | |
return flow { | |
val response = call { | |
url(url) | |
method = HttpMethod.Get | |
}.response | |
val data = ByteArray(response.contentLength()!!.toInt()) | |
var offset = 0 | |
do { | |
val currentRead = response.content.readAvailable(data, offset, data.size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class DownloadResult { | |
object Success : DownloadResult() | |
data class Error(val message: String, val cause: Exception? = null) : DownloadResult() | |
data class Progress(val progress: Int): DownloadResult() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun HttpClient.downloadFile(file: File, url: String, callback: suspend (boolean: Boolean) -> Unit) { | |
val response = call { | |
url(url) | |
method = HttpMethod.Get | |
}.response | |
val data = ByteArray(response.contentLength()!!.toInt()) | |
var offset = 0 | |
do { | |
val currentRead = response.content.readAvailable(data, offset, data.size) | |
offset += currentRead |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend fun HttpClient.downloadFile(file: File, url: String, callback: suspend (boolean: Boolean) -> Unit) { | |
val call = call { | |
url(url) | |
method = HttpMethod.Get | |
} | |
if (!call.response.status.isSuccess()) { | |
callback(false) | |
} | |
call.response.content.copyAndClose(file.writeChannel()) | |
return callback(true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
startKoin { | |
androidContext(this@MyApplication) | |
modules(module { | |
single { initKtorClient() } | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun initKtorClient() = HttpClient(Android) { | |
install(Logging) { | |
logger = Logger.ANDROID | |
level = LogLevel.ALL | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
packagingOptions { | |
exclude 'META-INF/*.kotlin_module' | |
} | |
kotlinOptions { | |
jvmTarget = "1.8" | |
} |
NewerOlder