Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Created February 25, 2020 20:21
Show Gist options
  • Save lordcodes/aa95d7944ac0fca64910daf037c8e73b to your computer and use it in GitHub Desktop.
Save lordcodes/aa95d7944ac0fca64910daf037c8e73b to your computer and use it in GitHub Desktop.
Code for the article: "Uploading a file with progress in Kotlin"
fun uploadAttachment(
filename: String, file: File, mimeType: String
): Observable<AttachmentUploadRemoteResult> {
val progressEmitter = PublishSubject.create<Double>()
val uploadRequest = createUploadRequest(
filename, file, mimeType, progressEmitter
)
val uploadResult = uploadRequest
.map<AttachmentUploadRemoteResult> {
CountingRequestResult.Completed(it.result)
}
.toObservable()
val progressResult = progressEmitter
.map<AttachmentUploadRemoteResult> {
CountingRequestResult.Progress(it)
}
return progressResult.mergeWith(uploadResult)
}
typealias AttachmentUploadRemoteResult =
CountingRequestResult<AttachmentUploadedRemoteDto>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment