Skip to content

Commit

Permalink
Remove ZPHAndroidUtils;
Browse files Browse the repository at this point in the history
  • Loading branch information
zeropercenthappy committed Oct 30, 2024
1 parent e0f9dfc commit 9b03238
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
3 changes: 0 additions & 3 deletions retrofitLib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ android {
minSdkVersion 14
targetSdkVersion 30

testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'

compileOptions {
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
Expand Down Expand Up @@ -46,7 +44,6 @@ dependencies {

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1"

implementation 'com.github.zeropercenthappy:ZPHAndroidUtils:1.4.1'
api "com.squareup.okhttp3:okhttp:$okhttp_version"
api "com.squareup.retrofit2:retrofit:$retrofit_version"
api "com.squareup.retrofit2:converter-gson:$retrofit_version"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.zeropercenthappy.retrofitutil

import com.zeropercenthappy.utilslibrary.utils.FileUtils
import com.zeropercenthappy.retrofitutil.ext.mimeType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
Expand All @@ -18,15 +18,11 @@ object RequestBodyBuilder {
fun createMultipartBodyPartList(fileMap: Map<String, File>): List<MultipartBody.Part> {
val partList = arrayListOf<MultipartBody.Part>()
for (key in fileMap.keys) {
val file = fileMap[key]
file?.apply {
val mimeType = FileUtils.getFileMimeType(file)
mimeType?.apply {
val requestBody = file.asRequestBody(mimeType.toMediaTypeOrNull())
val part = MultipartBody.Part.createFormData(key, file.name, requestBody)
partList.add(part)
}
}
val file = fileMap[key] ?: continue
val mimeType = file.mimeType.takeIf { it.isNotEmpty() } ?: continue
val requestBody = file.asRequestBody(mimeType.toMediaTypeOrNull())
val part = MultipartBody.Part.createFormData(key, file.name, requestBody)
partList.add(part)
}
return partList
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.zeropercenthappy.retrofitutil.ext

import android.webkit.MimeTypeMap
import java.io.File
import java.io.InputStream

val File.mimeType: String
get() = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: ""

fun File.writeFromInputStream(inputStream: InputStream): Boolean {
return try {
this.outputStream().buffered().use { output ->
inputStream.copyTo(output)
}
true
} catch (e: Exception) {
e.printStackTrace()
false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import android.app.IntentService
import android.content.Context
import android.content.Intent
import com.zeropercenthappy.retrofitutil.RetrofitBuilder
import com.zeropercenthappy.utilslibrary.utils.FileUtils
import com.zeropercenthappy.retrofitutil.ext.writeFromInputStream
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import java.io.File
Expand Down Expand Up @@ -54,7 +54,8 @@ class DownloadService : IntentService("DownloadService") {

@Suppress("UNCHECKED_CAST")
override fun onHandleIntent(intent: Intent?) {
downloadMap = intent?.getSerializableExtra(EXTRA_DOWNLOAD_MAP) as Map<String, File>? ?: return
downloadMap =
intent?.getSerializableExtra(EXTRA_DOWNLOAD_MAP) as Map<String, File>? ?: return
download()
}

Expand All @@ -67,8 +68,7 @@ class DownloadService : IntentService("DownloadService") {
continue
}
val responseBody = api.download(url)
val result = FileUtils.writeFileByIS(cacheFile, responseBody.byteStream())

val result = cacheFile.writeFromInputStream(responseBody.byteStream())
// Download success, move cache file to destination file
if (result) {
cacheFile.renameTo(destinationFile)
Expand Down

0 comments on commit 9b03238

Please sign in to comment.