Guest User

Untitled

a guest
Mar 3rd, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. init{
  2. fileUpload()
  3. }
  4.  
  5. private fun fileUpload() {
  6. compositeDisposable.add(
  7. getFileUploadObservable()
  8. .doOnComplete {
  9. Logger.d("File uploading doOnComplete.")
  10. }
  11. .subscribeOn(Schedulers.io())
  12. .observeOn(AndroidSchedulers.mainThread())
  13. .subscribe({
  14. Logger.d("FingerFile Upload done for : " + it.filename)
  15. }, {
  16. it.printStackTrace()
  17. Logger.d("Error while file uploading")
  18. notificationHelper?.updateNotification("Error while file uploading")
  19. android.os.Handler().postDelayed({
  20. notificationHelper?.cancelAllNotification()
  21. }, 2000L)
  22. }, {
  23. Logger.d("File uploading completed.")
  24. notificationHelper?.updateNotification("File uploading completed.")
  25. android.os.Handler().postDelayed({
  26. notificationHelper?.cancelAllNotification()
  27. }, 2000L)
  28. }, {
  29. Logger.d("File uploading onDisposable disposed called.")
  30. })
  31. )
  32. }
  33.  
  34. private fun getFileUploadObservable(): Observable<AWSFile> {
  35. val list: MutableList<Observable<AWSFile>> = ArrayList()
  36. photos?.forEach {
  37. list.add(upload(it.photoUri.toString()))
  38. Logger.d("FingerFile file: " + it.photoUri)
  39. }
  40. return Observable.merge(list).doOnComplete {
  41. Logger.d("File uploading merge(list).doOnComplete.")
  42. }
  43. }
  44.  
  45. private fun upload(filePath: String): Observable<AWSFile> {
  46. if (filePath == null) {
  47. return Observable.never<AWSFile>()
  48. }
  49. return Observable.create { emitter: ObservableEmitter<AWSFile> ->
  50. val file = File(filePath)
  51. val observer: TransferObserver = transferUtility.upload(
  52. AppConstant.FingerFileBucket,
  53. "user-parth/" + AppConstant.FingerFileTypePhoto + "/" + file.name,
  54. file,
  55. CannedAccessControlList.PublicReadWrite
  56. )
  57.  
  58. val notificationTitle = context.resources.getString(R.string.app_name)
  59. val notificationDesc = "File uploading : " + file.name
  60. notificationHelper?.showNotification(notificationTitle, notificationDesc, null, observer.id)
  61.  
  62. observer.setTransferListener(object : TransferListener {
  63. override fun onStateChanged(id: Int, state: TransferState?) {
  64. emitter.onNext(AWSFile(id, state, file.name, file.absolutePath))
  65. Logger.d("FingerFileA onStateChanged : " + state.toString())
  66. }
  67. override fun onProgressChanged(id: Int, bytesCurrent: Long, bytesTotal: Long) {
  68. val progress = getProgressPercentage(bytesCurrent, bytesTotal)
  69. notificationHelper?.updateNotification(progress, observer.id)
  70. Logger.d("FingerFileA onProgressChanged : " + progress)
  71. }
  72. override fun onError(id: Int, ex: Exception) {
  73. ex.printStackTrace()
  74. emitter.onError(ex)
  75. }
  76. })
  77. emitter.setCancellable { observer.cleanTransferListener() }
  78. }
  79. }
  80.  
  81. private fun getProgressPercentage(bytesCurrent: Long, bytesTotal: Long): Int {
  82. var percentage = 0
  83. percentage = ((bytesCurrent * 100L) / bytesTotal).toInt()
  84. return percentage
  85. }
Add Comment
Please, Sign In to add comment