dola 1 gadu atpakaļ
vecāks
revīzija
65edfc580b

+ 58 - 0
app/src/main/java/com/sambath/kunkhmer/remote/LivesDataModel.kt

@@ -0,0 +1,58 @@
+package com.sambath.kunkhmer.remote
+
+import com.squareup.moshi.Json
+import com.squareup.moshi.JsonClass
+
+@JsonClass(generateAdapter = true)
+data class LivesResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: LivesData?
+)
+
+@JsonClass(generateAdapter = true)
+data class LivesData(
+    @Json(name = "object")
+    val obj: MutableList<LivesInfo>,
+)
+
+@JsonClass(generateAdapter = true)
+data class LivesInfo(
+    @Json(name = "_id")
+    val liveInfoId: LivesInfoId,
+    @Json(name = "fighters")
+    val fighters: MutableList<LiveInfoFighters>,
+)
+
+@JsonClass(generateAdapter = true)
+data class LivesInfoId(
+    @Json(name = "_id")
+    val id: String,
+    @Json(name = "status")
+    val status: Boolean,
+    @Json(name = "fighter_id")
+    val fighterId: String,
+    @Json(name = "createdAt")
+    val createdAt: String,
+)
+
+@JsonClass(generateAdapter = true)
+data class LiveInfoFighters(
+    @Json(name = "_id")
+    val id: String,
+    @Json(name = "name")
+    val name: String,
+    @Json(name = "desc")
+    val desc: String,
+    @Json(name = "weight")
+    val weight: String,
+    @Json(name = "high")
+    val height: String,
+    @Json(name = "nationality")
+    val nationality: String,
+    @Json(name = "filename")
+    val filename: String,
+)

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

@@ -22,6 +22,9 @@ interface ApiService {
 
     @GET("/v1/highLight/getAll")
     fun getHighlights(): Single<HighlightResponse>
+
+    @GET("/v1/live/getAll")
+    fun getLives(): Single<LivesResponse>
     ///---------------------------------------------------------
 
     @POST("/v1/api/user/changepassword")

+ 49 - 0
app/src/main/java/com/sambath/kunkhmer/screen/lives/LivesViewModel.kt

@@ -0,0 +1,49 @@
+package com.sambath.kunkhmer.screen.lives
+
+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.LivesInfo
+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 java.util.concurrent.TimeUnit
+
+class LivesViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
+    private val _state = MutableLiveData(LivesViewState())
+    val state: LiveData<LivesViewState> = _state
+    private fun prev() = _state.value!!
+
+    data class LivesViewState(
+        val initial: Boolean = false,
+        val isProgress: Boolean = false,
+        val isLoginSuccess: Boolean = false,
+        val error: String? = null,
+        val livesData: MutableList<LivesInfo>? = null,
+    )
+
+    fun getLives() {
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+
+        disposables.add(
+            apiService.getLives()
+                .timeout(10, TimeUnit.SECONDS)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe({
+                    if (it.resultCode == 1) {
+                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true, livesData = it.data!!.obj)
+                    } else {
+                        _state.value =
+                            prev().copy(isProgress = false, error = "[${it.message.description}]")
+                    }
+                }, {
+                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
+                    _state.value = prev().copy(isProgress = false, error = message)
+                })
+        )
+    }
+}