Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- init{
- fileUpload()
- }
- private fun fileUpload() {
- compositeDisposable.add(
- getFileUploadObservable()
- .doOnComplete {
- Logger.d("File uploading doOnComplete.")
- }
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe({
- Logger.d("FingerFile Upload done for : " + it.filename)
- }, {
- it.printStackTrace()
- Logger.d("Error while file uploading")
- notificationHelper?.updateNotification("Error while file uploading")
- android.os.Handler().postDelayed({
- notificationHelper?.cancelAllNotification()
- }, 2000L)
- }, {
- Logger.d("File uploading completed.")
- notificationHelper?.updateNotification("File uploading completed.")
- android.os.Handler().postDelayed({
- notificationHelper?.cancelAllNotification()
- }, 2000L)
- }, {
- Logger.d("File uploading onDisposable disposed called.")
- })
- )
- }
- private fun getFileUploadObservable(): Observable<AWSFile> {
- val list: MutableList<Observable<AWSFile>> = ArrayList()
- photos?.forEach {
- list.add(upload(it.photoUri.toString()))
- Logger.d("FingerFile file: " + it.photoUri)
- }
- return Observable.merge(list).doOnComplete {
- Logger.d("File uploading merge(list).doOnComplete.")
- }
- }
- private fun upload(filePath: String): Observable<AWSFile> {
- if (filePath == null) {
- return Observable.never<AWSFile>()
- }
- return Observable.create { emitter: ObservableEmitter<AWSFile> ->
- val file = File(filePath)
- val observer: TransferObserver = transferUtility.upload(
- AppConstant.FingerFileBucket,
- "user-parth/" + AppConstant.FingerFileTypePhoto + "/" + file.name,
- file,
- CannedAccessControlList.PublicReadWrite
- )
- val notificationTitle = context.resources.getString(R.string.app_name)
- val notificationDesc = "File uploading : " + file.name
- notificationHelper?.showNotification(notificationTitle, notificationDesc, null, observer.id)
- observer.setTransferListener(object : TransferListener {
- override fun onStateChanged(id: Int, state: TransferState?) {
- emitter.onNext(AWSFile(id, state, file.name, file.absolutePath))
- Logger.d("FingerFileA onStateChanged : " + state.toString())
- }
- override fun onProgressChanged(id: Int, bytesCurrent: Long, bytesTotal: Long) {
- val progress = getProgressPercentage(bytesCurrent, bytesTotal)
- notificationHelper?.updateNotification(progress, observer.id)
- Logger.d("FingerFileA onProgressChanged : " + progress)
- }
- override fun onError(id: Int, ex: Exception) {
- ex.printStackTrace()
- emitter.onError(ex)
- }
- })
- emitter.setCancellable { observer.cleanTransferListener() }
- }
- }
- private fun getProgressPercentage(bytesCurrent: Long, bytesTotal: Long): Int {
- var percentage = 0
- percentage = ((bytesCurrent * 100L) / bytesTotal).toInt()
- return percentage
- }
Add Comment
Please, Sign In to add comment