Pārlūkot izejas kodu

fixing upload problem (news)

dola 1 gadu atpakaļ
vecāks
revīzija
c935e7f84a

+ 13 - 3
app/src/main/java/com/sambath/kunkhmer/remote/service/ApiService.kt

@@ -54,6 +54,19 @@ interface ApiService {
                    @Part("desc") desc: RequestBody
     ): Single<CreateNewsResponse>
 
+    @Multipart
+    @POST("/v1/product/create")
+    fun createProduct(@Part("name") name: RequestBody,
+                      @Part("desc") desc: RequestBody,
+                      @Part("price") price: RequestBody,
+                      @Part file: MultipartBody.Part): Single<CreateProductResponse>
+    @Multipart
+    @POST("/v1/product/create")
+    fun createProduct(@Part("name") name: RequestBody,
+                      @Part("desc") desc: RequestBody,
+                      @Part("price") price: RequestBody
+                      ): Single<CreateProductResponse>
+
     @POST("/v1/highLight/create")
     fun createHighlight(@Body createHighlightRequest: CreateHighlightRequest): Single<CreateHighlightResponse>
 
@@ -63,9 +76,6 @@ interface ApiService {
     @POST("/v1/event/create")
     fun createEvent(@Body createEventRequest: CreateEventRequest): Single<CreateEventResponse>
 
-    @POST("/v1/product/create")
-    fun createProduct(@Body createProductRequest: CreateProductRequest): Single<CreateProductResponse>
-
     @POST("/v1/live/create")
     fun createLives(@Body createLivesRequest: CreateLivesRequest): Single<CreateLivesResponse>
     ///---------------------------------------------------------

+ 32 - 8
app/src/main/java/com/sambath/kunkhmer/screen/shop/ShopAdminFragment.kt

@@ -4,6 +4,7 @@ import android.app.Activity
 import android.content.Intent
 import android.net.Uri
 import android.os.Bundle
+import android.provider.MediaStore
 import androidx.fragment.app.Fragment
 import android.view.LayoutInflater
 import android.view.View
@@ -45,14 +46,26 @@ class ShopAdminFragment : Fragment() {
         }
 
         binding.submitButton.setOnClickListener {
-            val createProductRequest = CreateProductRequest(
-                binding.et_name.text.toString(),
-                binding.et_desc.text.toString(),
-                binding.et_price.text.toString(),
-                imageUri.toString()
-            )
-
-            shopViewModel.createProduct(createProductRequest.name, createProductRequest.desc, createProductRequest.price, createProductRequest.upload)
+            val imagePath = imageUri?.let { it1 -> getImagePathFromUri(it1) }
+            if (imagePath != null) {
+                val createProductRequest = CreateProductRequest(
+                    binding.et_name.text.toString(),
+                    binding.et_desc.text.toString(),
+                    binding.et_price.text.toString(),
+                    imagePath
+                )
+
+                shopViewModel.createProduct(createProductRequest.name, createProductRequest.desc, createProductRequest.price, createProductRequest.upload)
+            } else {
+                val createProductRequest = CreateProductRequest(
+                    binding.et_name.text.toString(),
+                    binding.et_desc.text.toString(),
+                    binding.et_price.text.toString(),
+                    ""
+                )
+
+                shopViewModel.createProduct(createProductRequest.name, createProductRequest.desc, createProductRequest.price, createProductRequest.upload)
+            }
         }
 
         // Inflate the layout for this fragment
@@ -96,4 +109,15 @@ class ShopAdminFragment : Fragment() {
         intent.type = "image/*"
         imagePicker.launch(intent)
     }
+
+    private fun getImagePathFromUri(uri: Uri): String? {
+        val contentResolver = requireContext().contentResolver
+        val cursor = contentResolver.query(uri, null, null, null, null)
+        cursor?.use {
+            it.moveToFirst()
+            val index = it.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
+            return it.getString(index)
+        }
+        return null
+    }
 }

+ 51 - 18
app/src/main/java/com/sambath/kunkhmer/screen/shop/ShopViewModel.kt

@@ -13,6 +13,11 @@ import com.sambath.kunkhmer.remote.service.ApiService
 import com.sambath.kunkhmer.util.PrefHelper
 import io.reactivex.android.schedulers.AndroidSchedulers
 import io.reactivex.schedulers.Schedulers
+import okhttp3.MediaType.Companion.toMediaTypeOrNull
+import okhttp3.MultipartBody
+import okhttp3.RequestBody.Companion.asRequestBody
+import okhttp3.RequestBody.Companion.toRequestBody
+import java.io.File
 import java.util.concurrent.TimeUnit
 
 class ShopViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
@@ -53,25 +58,53 @@ class ShopViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
 
     fun createProduct(name: String, desc: String, price: String, upload: String) {
         if (_state.value!!.isProgress) return
-        val request = CreateProductRequest(name, desc, price, upload)
+        //val request = CreateProductRequest(name, desc, price, upload)
         _state.value = prev().copy(isProgress = true)
 
-        disposables.add(
-            apiService.createProduct(request)
-                .timeout(10, TimeUnit.SECONDS)
-                .subscribeOn(Schedulers.io())
-                .observeOn(AndroidSchedulers.mainThread())
-                .subscribe({
-                    Log.d("login", it.toString())
-                    if (it.resultCode == 1) {
-                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
-                    } else {
-                        _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
-                    }
-                }, {
-                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode() + it.message
-                    _state.value = prev().copy(isProgress = false, error = message)
-                })
-        )
+        val namePart = name.toRequestBody("text/plain".toMediaTypeOrNull())
+        val descPart = desc.toRequestBody("text/plain".toMediaTypeOrNull())
+        val pricePart = price.toRequestBody("text/plain".toMediaTypeOrNull())
+
+        if (!upload.isNullOrEmpty()) {
+            val file = File(upload)
+            val filePart = MultipartBody.Part.createFormData("upload", file.name, file.asRequestBody("image/jpg".toMediaTypeOrNull()))
+            _state.value = prev().copy(isProgress = true)
+
+            disposables.add(
+                apiService.createProduct(namePart, descPart, pricePart, filePart)
+                    .timeout(10, TimeUnit.SECONDS)
+                    .subscribeOn(Schedulers.io())
+                    .observeOn(AndroidSchedulers.mainThread())
+                    .subscribe({
+                        Log.d("login", it.toString())
+                        if (it.resultCode == 1) {
+                            _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
+                        } else {
+                            _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
+                        }
+                    }, {
+                        val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode() + it.message
+                        _state.value = prev().copy(isProgress = false, error = message)
+                    })
+            )
+        } else {
+            disposables.add(
+                apiService.createProduct(namePart, descPart, pricePart)
+                    .timeout(10, TimeUnit.SECONDS)
+                    .subscribeOn(Schedulers.io())
+                    .observeOn(AndroidSchedulers.mainThread())
+                    .subscribe({
+                        Log.d("login", it.toString())
+                        if (it.resultCode == 1) {
+                            _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
+                        } else {
+                            _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
+                        }
+                    }, {
+                        val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode() + it.message
+                        _state.value = prev().copy(isProgress = false, error = message)
+                    })
+            )
+        }
     }
 }