dola 1 year ago
parent
commit
35504a790b

+ 25 - 5
app/src/main/java/com/sambath/kunkhmer/adapter/EventAdapter.kt

@@ -7,9 +7,13 @@ import android.widget.ImageView
 import android.widget.TextView
 import androidx.recyclerview.widget.RecyclerView
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.remote.EventInfo
 import com.squareup.picasso.Picasso
+import java.text.SimpleDateFormat
+import java.util.Locale
+import java.util.TimeZone
 
-class EventAdapter(private val cardList: List<EventCardItem>) : RecyclerView.Adapter<EventAdapter.ViewHolder>() {
+class EventAdapter(var cardList: MutableList<EventInfo>) : RecyclerView.Adapter<EventAdapter.ViewHolder>() {
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EventAdapter.ViewHolder {
         val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_event_item_card, parent, false)
@@ -19,10 +23,10 @@ class EventAdapter(private val cardList: List<EventCardItem>) : RecyclerView.Ada
     override fun onBindViewHolder(holder: EventAdapter.ViewHolder, position: Int) {
         val currentItem = cardList[position]
 
-        Picasso.get().load(currentItem.imageResource).into(holder.imageView)
-        holder.subTitleTextView.text = currentItem.subTitle
-        holder.titleTextView.text = currentItem.title
-        holder.dateTitleTextView.text = currentItem.dateTitle
+        Picasso.get().load(R.drawable.default_background).into(holder.imageView)
+        holder.subTitleTextView.text = currentItem.eventInfoId.desc
+        holder.titleTextView.text = currentItem.eventInfoId.title
+        holder.dateTitleTextView.text = parseAndFormatDate(currentItem.eventInfoId.createdAt)
     }
 
     override fun getItemCount() = cardList.size
@@ -33,4 +37,20 @@ class EventAdapter(private val cardList: List<EventCardItem>) : RecyclerView.Ada
         val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
         val dateTitleTextView: TextView = itemView.findViewById(R.id.dateTitleTextView)
     }
+
+    private fun parseAndFormatDate(dateString: String): String {
+        // Create a SimpleDateFormat for parsing the date string
+        val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault())
+        parser.timeZone = TimeZone.getTimeZone("UTC")
+
+        // Parse the date
+        val date = parser.parse(dateString)
+
+        // Create a SimpleDateFormat for formatting the date
+        val formatter = SimpleDateFormat("dd MMM yyyy HH:mm", Locale.getDefault())
+        formatter.timeZone = TimeZone.getDefault() // Use local timezone
+
+        // Format the date
+        return formatter.format(date)
+    }
 }

+ 64 - 0
app/src/main/java/com/sambath/kunkhmer/remote/EventDataModel.kt

@@ -0,0 +1,64 @@
+package com.sambath.kunkhmer.remote
+
+import com.squareup.moshi.Json
+import com.squareup.moshi.JsonClass
+
+@JsonClass(generateAdapter = true)
+data class EventResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: EventData?
+)
+
+@JsonClass(generateAdapter = true)
+data class EventData(
+    @Json(name = "object")
+    val obj: MutableList<EventInfo>,
+)
+
+@JsonClass(generateAdapter = true)
+data class EventInfo(
+    @Json(name = "_id")
+    val eventInfoId: EventInfoId,
+    @Json(name = "fighters")
+    val fighters: MutableList<EventInfoFighters>,
+)
+
+@JsonClass(generateAdapter = true)
+data class EventInfoId(
+    @Json(name = "_id")
+    val id: String,
+    @Json(name = "title")
+    val title: String,
+    @Json(name = "desc")
+    val desc: 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 EventInfoFighters(
+    @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 = "status")
+    val status: Boolean,
+    @Json(name = "filename")
+    val filename: String,
+)

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

@@ -25,6 +25,9 @@ interface ApiService {
 
     @GET("/v1/live/getAll")
     fun getLives(): Single<LivesResponse>
+
+    @GET("/v1/event/getAll")
+    fun getEvents(): Single<EventResponse>
     ///---------------------------------------------------------
 
     @POST("/v1/api/user/changepassword")

+ 25 - 23
app/src/main/java/com/sambath/kunkhmer/screen/event/EventFragment.kt

@@ -9,50 +9,52 @@ import androidx.recyclerview.widget.LinearLayoutManager
 import com.sambath.kunkhmer.R
 import com.sambath.kunkhmer.adapter.EventAdapter
 import com.sambath.kunkhmer.adapter.EventCardItem
-import com.sambath.kunkhmer.adapter.LivesAdapter
-import com.sambath.kunkhmer.adapter.LivesCardItem
+import com.sambath.kunkhmer.app.App
 import kotlinx.android.synthetic.main.fragment_event.view.recyclerViewEvent
 
 class EventFragment : Fragment() {
     private var _root: View? = null
     private val binding get() = _root!!
+    private lateinit var eventViewModel: EventViewModel
+    private lateinit var eventAdapter: EventAdapter
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
     }
 
-    override fun onCreateView(
-        inflater: LayoutInflater,
-        container: ViewGroup?,
-        savedInstanceState: Bundle?
-    ): View? {
+    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         _root = inflater.inflate(R.layout.fragment_event, container, false)
 
+        eventViewModel = EventViewModel(App.injectApiService(), App.injectPrefHelper())
+        eventViewModel.getEvents()
+        eventViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+            render(it)
+        })
+
         setEvent()
 
         return binding
     }
 
+    private fun render(state: EventViewModel.EventViewState) {
+        if (state.isProgress) {
+        }
+        if (state.isLoginSuccess) {
+            if(state.eventsData != null) {
+                eventAdapter.cardList = state.eventsData
+                eventAdapter.notifyDataSetChanged()
+            }
+        }
+        if (state.error != null) {
+        }
+    }
+
     private fun setEvent() {
         val recyclerView = binding.recyclerViewEvent
-        val cardList = createCardList() // Create your card data list here
-        val cardAdapter = EventAdapter(cardList)
+        eventAdapter = EventAdapter(mutableListOf())
 
         recyclerView.layoutManager =
             LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
-        recyclerView.adapter = cardAdapter
-    }
-
-    private fun createCardList(): List<EventCardItem> {
-        // Create and return your list of CardItems
-        return listOf(
-            EventCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
-                "ទស្សនាការប្រកួតសង្វៀនប្រដាល់គុនខ្មែរឯកភ្នំ", "ម៉ន សាម៉េត(ខ្មែរ) vs (ថៃ)ហ្វាម៉ែ សែងចាក់", "កម្មវិធីប្រកួតថ្ងៃអាទិត្យ ទី៣១ ខែមិនា ឆ្នាំ២០២៣"),
-            EventCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
-                "ទស្សនាការប្រកួតសង្វៀនប្រដាល់គុនខ្មែរឯកភ្នំ", "ម៉ន សាម៉េត(ខ្មែរ) vs (ថៃ)ហ្វាម៉ែ សែងចាក់", "កម្មវិធីប្រកួតថ្ងៃអាទិត្យ ទី៣១ ខែមិនា ឆ្នាំ២០២៣"),
-            EventCardItem("https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
-                "ទស្សនាការប្រកួតសង្វៀនប្រដាល់គុនខ្មែរឯកភ្នំ", "ម៉ន សាម៉េត(ខ្មែរ) vs (ថៃ)ហ្វាម៉ែ សែងចាក់", "កម្មវិធីប្រកួតថ្ងៃអាទិត្យ ទី៣១ ខែមិនា ឆ្នាំ២០២៣"),
-            // Add more card items as needed
-        )
+        recyclerView.adapter = eventAdapter
     }
 }

+ 52 - 0
app/src/main/java/com/sambath/kunkhmer/screen/event/EventViewModel.kt

@@ -0,0 +1,52 @@
+package com.sambath.kunkhmer.screen.event
+
+import android.graphics.Bitmap
+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.EventData
+import com.sambath.kunkhmer.remote.EventInfo
+import com.sambath.kunkhmer.remote.MatchesData
+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 EventViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
+    private val _state = MutableLiveData<EventViewState>(EventViewState())
+    val state: LiveData<EventViewState> = _state
+    private fun prev() = _state.value!!
+
+    data class EventViewState(
+        val initial: Boolean = false,
+        val isProgress: Boolean = false,
+        val isLoginSuccess: Boolean = false,
+        val error: String? = null,
+        val eventsData: MutableList<EventInfo>? = null,
+    )
+
+    fun getEvents(){
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+
+        disposables.add(
+            apiService.getEvents()
+                .timeout(10, TimeUnit.SECONDS)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe({
+                    if (it.resultCode == 1) {
+                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true, eventsData = 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)
+                })
+        )
+    }
+}