Browse Source

fixing upload problem (news)

dola 1 year ago
parent
commit
3d745ab4cf

+ 46 - 0
app/src/main/java/com/sambath/kunkhmer/remote/HighlightDataModel.kt

@@ -31,4 +31,50 @@ data class Highlight(
     val url: String,
     @Json(name = "createdAt")
     val createdAt: String,
+)
+
+/////////
+
+@JsonClass(generateAdapter = true)
+class CreateHighlightRequest(
+    @Json(name = "title")
+    val title: String,
+    @Json(name = "date")
+    val date: String,
+    @Json(name = "url")
+    val url: String
+)
+
+@JsonClass(generateAdapter = true)
+data class CreateHighlightResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: HighlightCreatedData?
+)
+
+@JsonClass(generateAdapter = true)
+data class HighlightCreatedData(
+    @Json(name = "highLight")
+    val obj: CreatedHighlight,
+)
+
+@JsonClass(generateAdapter = true)
+data class CreatedHighlight(
+    @Json(name = "_id")
+    val id: String,
+    @Json(name = "title")
+    val title: String,
+    @Json(name = "date")
+    val date: String,
+    @Json(name = "url")
+    val url: String,
+    @Json(name = "createdAt")
+    val createdAt: String,
+    @Json(name = "updatedAt")
+    val updatedAt: String,
+    @Json(name = "__v")
+    val _v: Int,
 )

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

@@ -41,14 +41,6 @@ interface ApiService {
     @POST("/v1/users/get")
     fun getCurrentUser(): Single<CurrentUserResponse>
 
-
-//    @POST("/v1/new/create")
-//    fun createNews(@Body createNewsRequest: CreateNewsRequest): Single<CreateNewsResponse>
-
-    @Multipart
-    @POST("/v1/new/create")
-    fun createNews(@Part request: MultipartBody.Part): Single<CreateNewsResponse>
-
     @Multipart
     @POST("/v1/new/create")
     fun createNews(@Part("title") title: RequestBody,
@@ -56,19 +48,15 @@ interface ApiService {
                    @Part file: MultipartBody.Part
     ): Single<CreateNewsResponse>
 
-    @Multipart
-    @POST("/v1/new/create")
-    fun createNews(@Part("title") title: RequestBody,
-                   @Part("desc") desc: RequestBody,
-                   @Part("upload") upload: RequestBody
-    ): Single<CreateNewsResponse>
-
     @Multipart
     @POST("/v1/new/create")
     fun createNews(@Part("title") title: RequestBody,
                    @Part("desc") desc: RequestBody
     ): Single<CreateNewsResponse>
 
+    @POST("/v1/highLight/create")
+    fun createHighlight(@Body createHighlightRequest: CreateHighlightRequest): Single<CreateHighlightResponse>
+
     @POST("/v1/match/create")
     fun createMatch(@Body createMatchRequest: CreateMatchRequest): Single<CreateMatchResponse>
 

+ 38 - 1
app/src/main/java/com/sambath/kunkhmer/screen/news/HighlightAdminFragment.kt

@@ -6,8 +6,12 @@ import androidx.fragment.app.Fragment
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import android.widget.Toast
 import com.sambath.kunkhmer.R
-import kotlinx.android.synthetic.main.fragment_fighter_admin.view.et_date
+import com.sambath.kunkhmer.app.App
+import kotlinx.android.synthetic.main.fragment_highlight_admin.view.et_title
+import kotlinx.android.synthetic.main.fragment_highlight_admin.view.et_url
+import kotlinx.android.synthetic.main.fragment_highlight_admin.view.submitButton
 import kotlinx.android.synthetic.main.fragment_highlight_admin.view.til_date
 import java.text.SimpleDateFormat
 import java.util.Calendar
@@ -15,10 +19,12 @@ import java.util.Calendar
 class HighlightAdminFragment : Fragment() {
     private var _root: View? = null
     private val binding get() = _root!!
+    private lateinit var highlightViewModel: NewsViewModel
 
     private val calendar = Calendar.getInstance()
     private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS+0000")
 
+
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
     }
@@ -29,13 +35,44 @@ class HighlightAdminFragment : Fragment() {
         // Inflate the layout for this fragment
         _root = inflater.inflate(R.layout.fragment_highlight_admin, container, false)
 
+        highlightViewModel = NewsViewModel(App.injectApiService(), App.injectPrefHelper())
+        highlightViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+            render(it)
+        })
+
         binding.til_date.setOnClickListener {
             showDatePickerDialog()
         }
 
+        binding.submitButton.setOnClickListener{
+            highlightViewModel.createHighlight(
+                binding.et_title.text.toString(),
+                binding.til_date.text.toString(),
+                binding.et_url.text.toString()
+            )
+        }
+
+        setDefault()
+
         return binding
     }
 
+    private fun setDefault() {
+        binding.et_title.setText("Thiago (Kun Khmer) Vs Joe (Muay Thai)")
+        binding.et_url.setText("https://youtu.be/K9sHyZE5BmI")
+    }
+
+    private fun render(state: NewsViewState) {
+        if (state.isLoginSuccess) {
+            Toast.makeText(context, "Create Success...", Toast.LENGTH_LONG).show()
+
+            binding.til_date.text = ""
+            binding.et_title.setText("")
+            binding.et_url.setText("")
+        }
+        // Other state handling...
+    }
+
     private fun showDatePickerDialog() {
         val year = calendar.get(Calendar.YEAR)
         val month = calendar.get(Calendar.MONTH)

+ 44 - 20
app/src/main/java/com/sambath/kunkhmer/screen/news/NewsViewModel.kt

@@ -5,6 +5,7 @@ import androidx.lifecycle.LiveData
 import androidx.lifecycle.MutableLiveData
 import com.sambath.kunkhmer.app.getErrorCode
 import com.sambath.kunkhmer.base.BaseViewModel
+import com.sambath.kunkhmer.remote.CreateHighlightRequest
 import com.sambath.kunkhmer.remote.CreateNewsRequest
 import com.sambath.kunkhmer.remote.CreatedNews
 import com.sambath.kunkhmer.remote.HighlightData
@@ -38,15 +39,15 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
 
         disposables.add(
             apiService.getNews().timeout(10, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({
-                    if (it.resultCode == 1) {
-                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true, newsData = it.data!!)
-                    } else {
-                        _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
-                    }
-                }, {
-                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
-                    _state.value = prev().copy(isProgress = false, error = message)
-                })
+                if (it.resultCode == 1) {
+                    _state.value = prev().copy(isProgress = false, isLoginSuccess = true, newsData = it.data!!)
+                } else {
+                    _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
+                }
+            }, {
+                val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
+                _state.value = prev().copy(isProgress = false, error = message)
+            })
         )
     }
 
@@ -56,8 +57,31 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
 
         disposables.add(
             apiService.getHighlights().timeout(10, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe({
+                if (it.resultCode == 1) {
+                    _state.value = prev().copy(isProgress = false, isLoginSuccess = true, highlightData = it.data!!)
+                } else {
+                    _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
+                }
+            }, {
+                val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
+                _state.value = prev().copy(isProgress = false, error = message)
+            })
+        )
+    }
+
+    fun createHighlight(title: String, date: String, url: String) {
+        if (_state.value!!.isProgress) return
+        val request = CreateHighlightRequest(title, date, url)
+        _state.value = prev().copy(isProgress = true)
+
+        disposables.add(
+            apiService.createHighlight(request).timeout(60, 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, highlightData = it.data!!)
+                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
                     } else {
                         _state.value = prev().copy(isProgress = false, error = "[${it.message.description}]")
                     }
@@ -83,16 +107,16 @@ class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : Ba
 
             disposables.add(
                 apiService.createNews(titlePart, descPart, filePart).timeout(60, 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()
-                        _state.value = prev().copy(isProgress = false, error = message)
-                    })
+                    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()
+                    _state.value = prev().copy(isProgress = false, error = message)
+                })
             )
         } else {
             _state.value = prev().copy(isProgress = true)