Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.50 KB | None | 0 0
  1. object Utils {
  2.     fun downloadFile(url:String, file:File) : Observable<Int>{
  3.         return PublishSubject.create<Int> {
  4.             val client = OkHttpClient()
  5.  
  6.             val request = Request.Builder()
  7.                 .url(url)
  8.                 .build()
  9.  
  10.             client.newCall(request).enqueue(object : Callback {
  11.                 override fun onFailure(call: Call, e: IOException) {
  12.                     e.printStackTrace()
  13.                 }
  14.  
  15.                 @Throws(IOException::class)
  16.                 override fun onResponse(call: Call, response: Response) {
  17.                     if (!response.isSuccessful) throw IOException("Unexpected code $response")
  18.  
  19.                     val inStream = response.body()?.byteStream()
  20.                     val bis= BufferedInputStream(inStream)
  21.                     val maxSize=response.body()?.contentLength()
  22.                     val stepSize= maxSize?.div(100)
  23.                     var currentValue=0
  24.                     var procent=0
  25.  
  26.                     val bytes=ByteArray(2048)
  27.                     val fos = FileOutputStream(file)
  28.                     while (true) {
  29.                         val byteCount = bis.read(bytes)
  30.                         if (byteCount==-1) break
  31.                         fos.write(bytes, 0, byteCount)
  32.                         currentValue+=byteCount
  33.                         if (currentValue>= stepSize!!){
  34.                             procent++
  35.                             it.onNext(procent)
  36.                             currentValue=0
  37.                         }
  38.                     }
  39.                     fos.close()
  40.                 }
  41.             })
  42.         }
  43.     }
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  fun downloadAudio(url:String, fileExternal: File){
  55.         viewState.showLoading()
  56.             Observable.fromCallable {
  57.                 val array=url.split("/")
  58.                 var nameFile=array[array.size-1]
  59.                 nameFile=nameFile.substring(0,nameFile.indexOf("?"))
  60.                 val file= File(fileExternal, nameFile)
  61.                 val link=""
  62.                 Thread.sleep(4000)
  63.                 Utils.downloadFile(link, file)
  64.             }
  65.             .doOnNext {
  66.                 Log.e("SearchAudioPresenter", "downloadAudio= ASD")
  67.             }
  68.             .subscribeOn(Schedulers.io())
  69.             .observeOn(AndroidSchedulers.mainThread())
  70.             .doFinally { viewState.hideLoading() }
  71.             .subscribe({
  72.                 viewState.loadingSuccess()
  73.             }, Throwable::printStackTrace).isDisposed
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement