dola 1 rok pred
rodič
commit
b0fd5f13c1

+ 7 - 3
app/src/main/java/com/sambath/kunkhmer/adapter/HighlightNewsAdapter.kt

@@ -7,9 +7,11 @@ import android.widget.ImageView
 import android.widget.TextView
 import androidx.recyclerview.widget.RecyclerView
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.config.Config
+import com.sambath.kunkhmer.remote.Highlight
 import com.squareup.picasso.Picasso
 
-class HighlightNewsAdapter (private val cardList: List<TopNewsCardItem>) : RecyclerView.Adapter<HighlightNewsAdapter.ViewHolder>() {
+class HighlightNewsAdapter (var cardList: MutableList<Highlight>) : RecyclerView.Adapter<HighlightNewsAdapter.ViewHolder>() {
     class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@@ -21,9 +23,11 @@ class HighlightNewsAdapter (private val cardList: List<TopNewsCardItem>) : Recyc
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         val currentItem = cardList[position]
 
-        Picasso.get().load(currentItem.imageResource).into(holder.imageView)
+//        Picasso.get()
+//            .load(Config.BASE_URL + "/v1/fighter/image/" + currentItem.)
+//            .into(holder.imageView);
         holder.dateTextView.text = currentItem.date
-        holder.titleTextView.text = currentItem.subTitle
+        holder.titleTextView.text = currentItem.title
     }
 
     override fun getItemCount() = cardList.size

+ 10 - 4
app/src/main/java/com/sambath/kunkhmer/adapter/TopNewsAdapter.kt

@@ -7,9 +7,12 @@ import android.widget.ImageView
 import android.widget.TextView
 import androidx.recyclerview.widget.RecyclerView
 import com.sambath.kunkhmer.R
+import com.sambath.kunkhmer.config.Config
+import com.sambath.kunkhmer.remote.News
+import com.sambath.kunkhmer.remote.NewsData
 import com.squareup.picasso.Picasso
 
-class TopNewsAdapter(private val cardList: List<TopNewsCardItem>) : RecyclerView.Adapter<TopNewsAdapter.ViewHolder>() {
+class TopNewsAdapter(var cardList: MutableList<News>) : RecyclerView.Adapter<TopNewsAdapter.ViewHolder>() {
     class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@@ -20,14 +23,17 @@ class TopNewsAdapter(private val cardList: List<TopNewsCardItem>) : RecyclerView
     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
         val currentItem = cardList[position]
 
-        Picasso.get().load(currentItem.imageResource).into(holder.imageView)
-        holder.dateTextView.text = currentItem.date
+        Picasso.get()
+            .load(Config.BASE_URL + "/v1/new/image/" + currentItem.filename)
+            .into(holder.imageView);
+        holder.dateTextView.text = currentItem.createdAt
         holder.titleTextView.text = currentItem.title
-        holder.subTextView.text = currentItem.subTitle
+        holder.subTextView.text = currentItem.desc
     }
 
     override fun getItemCount() = cardList.size
 
+
     // Holds the views for adding it to image and text
     class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
         val imageView: ImageView = itemView.findViewById(R.id.imageView)

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

@@ -0,0 +1,34 @@
+package com.sambath.kunkhmer.remote
+
+import com.squareup.moshi.Json
+import com.squareup.moshi.JsonClass
+
+@JsonClass(generateAdapter = true)
+data class HighlightResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: HighlightData?
+)
+
+@JsonClass(generateAdapter = true)
+data class  HighlightData(
+    @Json(name = "object")
+    val obj: MutableList<Highlight>,
+)
+
+@JsonClass(generateAdapter = true)
+data class Highlight(
+    @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,
+)

+ 36 - 0
app/src/main/java/com/sambath/kunkhmer/remote/NewsDataModel.kt

@@ -0,0 +1,36 @@
+package com.sambath.kunkhmer.remote
+
+import com.squareup.moshi.Json
+import com.squareup.moshi.JsonClass
+
+@JsonClass(generateAdapter = true)
+data class NewsResponse(
+    @Json(name = "code")
+    val resultCode: Int,
+    @Json(name = "message")
+    val message: ResponseMessage,
+    @Json(name = "data")
+    val data: NewsData?
+)
+
+@JsonClass(generateAdapter = true)
+data class NewsData(
+    @Json(name = "object")
+    val obj: MutableList<News>,
+)
+
+@JsonClass(generateAdapter = true)
+data class News(
+    @Json(name = "_id")
+    val id: String,
+    @Json(name = "viewer")
+    val viewer: Int,
+    @Json(name = "title")
+    val title: String,
+    @Json(name = "desc")
+    val desc: String,
+    @Json(name = "createdAt")
+    val createdAt: String,
+    @Json(name = "filename")
+    val filename: String,
+)

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

@@ -17,7 +17,11 @@ interface ApiService {
     @GET("fighter/image/{imageId}.jpeg")
     fun getFighterImage(@Path("imageId") imageId: String): Single<ResponseBody>
 
+    @GET("/v1/new/getAll")
+    fun getNews(): Single<NewsResponse>
 
+    @GET("/v1/highLight/getAll")
+    fun getHighlights(): Single<HighlightResponse>
     ///---------------------------------------------------------
 
     @POST("/v1/api/user/changepassword")

+ 0 - 12
app/src/main/java/com/sambath/kunkhmer/screen/fighter/FighterFragment.kt

@@ -99,18 +99,6 @@ class FighterFragment : Fragment() {
         recyclerView.adapter = cardAdapter
     }
 
-    private fun createCardList(): List<FighterCardItem> {
-        // Create and return your list of CardItems
-        return listOf(
-            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
-            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
-            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
-            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
-            FighterCardItem(R.drawable.ic_user_white, "Fighter A", R.drawable.ic_user_white, "Fighter B", "Title 1", "Khun Khmer Champion", true),
-            // Add more card items as needed
-        )
-    }
-
     interface TabSelectionListener {
         fun onTabSelected(tabIndex: Int)
     }

+ 0 - 2
app/src/main/java/com/sambath/kunkhmer/screen/fighter/FighterViewModel.kt

@@ -15,8 +15,6 @@ import io.reactivex.schedulers.Schedulers
 import okhttp3.ResponseBody
 import java.util.concurrent.TimeUnit
 
-
-
 class FighterViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
     private val _state = MutableLiveData<FighterViewState>(FighterViewState())
     val state: LiveData<FighterViewState> = _state

+ 68 - 67
app/src/main/java/com/sambath/kunkhmer/screen/news/NewsFragment.kt

@@ -5,6 +5,7 @@ import androidx.fragment.app.Fragment
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import androidx.lifecycle.Observer
 import androidx.recyclerview.widget.LinearLayoutManager
 import com.google.android.material.tabs.TabLayout
 import com.sambath.kunkhmer.R
@@ -12,19 +13,21 @@ import com.sambath.kunkhmer.adapter.HighlightNewsAdapter
 import com.sambath.kunkhmer.adapter.TopNewsAdapter
 import com.sambath.kunkhmer.adapter.TopNewsCardItem
 import com.sambath.kunkhmer.adapter.TopNewsHeaderAdapter
+import com.sambath.kunkhmer.app.App
+import com.sambath.kunkhmer.remote.HighlightData
+import com.sambath.kunkhmer.remote.News
+import com.sambath.kunkhmer.remote.NewsData
 import kotlinx.android.synthetic.main.fragment_news.view.recyclerViewNews
 import kotlinx.android.synthetic.main.fragment_news.view.recyclerViewNewsHeader
 import kotlinx.android.synthetic.main.fragment_news.view.selectedTabTextView
 import kotlinx.android.synthetic.main.fragment_news.view.tabLayout
 
-/**
- * A simple [Fragment] subclass.
- * Use the [NewsFragment.newInstance] factory method to
- * create an instance of this fragment.
- */
 class NewsFragment : Fragment() {
     private var _root: View? = null
     private val binding get() = _root!!
+    private lateinit var newsViewModel: NewsViewModel
+    private lateinit var topNewsAdapter: TopNewsAdapter
+    private lateinit var highlightNewsAdapter: HighlightNewsAdapter
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
@@ -36,13 +39,52 @@ class NewsFragment : Fragment() {
         // Inflate the layout for this fragment
         _root = inflater.inflate(R.layout.fragment_news, container, false)
 
+        topNewsAdapter = TopNewsAdapter(mutableListOf())
+        highlightNewsAdapter = HighlightNewsAdapter(mutableListOf())
+
+        // Set adapters
+        binding.recyclerViewNews.apply {
+            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
+            adapter = topNewsAdapter
+        }
+
+        newsViewModel = NewsViewModel(App.injectApiService(), App.injectPrefHelper())
+//        newsViewModel.getNews()
+//        newsViewModel.getHighlight()
+        newsViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
+            render(it)
+        })
+
         setTab()
 
         return binding
     }
 
+    private fun render(state: NewsViewState) {
+        if (state.isLoginSuccess) {
+            if(state.newsData != null) {
+                updateNewsFeed(state.newsData)
+            }
+
+            if(state.highlightData != null) {
+                updateHighlight(state.highlightData)
+            }
+        }
+        // Other state handling...
+    }
+
+    private fun updateNewsFeed(newsData: NewsData) {
+        topNewsAdapter.cardList = newsData.obj
+        topNewsAdapter.notifyDataSetChanged()
+    }
+
+    private fun updateHighlight(highlightData: HighlightData) {
+        highlightNewsAdapter.cardList = highlightData.obj
+        highlightNewsAdapter.notifyDataSetChanged()
+    }
+
     private fun setTab() {
-        val tabTitles = listOf("TOP NEWS", "POPULAR", "LAST", "HIGHLIGHT")
+        val tabTitles = listOf("TOP NEWS", "HIGHLIGHT")
 
         // Populate TabLayout with dynamic tabs
         for (title in tabTitles) {
@@ -56,26 +98,22 @@ class NewsFragment : Fragment() {
                 tab?.let {
                     val selectedTitle = it.text.toString()
                     binding.selectedTabTextView.text = "Selected Tab: $selectedTitle"
-                    val selectedIndex = it.position
-                    when (selectedIndex) {
+                    when (it.position) {
                         0 -> {
                             //Top News
-                            setNewsHeaderFeed()
-                            setNewsFeed()
+                            binding.recyclerViewNews.apply {
+                                layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
+                                adapter = topNewsAdapter
+                            }
+                            fetchTopNewsAndUpdateAdapter()
                         }
-
                         1 -> {
-                            // Popular
-                        }
-
-                        2 -> {
-                            // Last
-                        }
-
-                        3 -> {
+                            binding.recyclerViewNews.apply {
+                                layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
+                                adapter = highlightNewsAdapter
+                            }
                             // Highlight
-                            removeNewsHeaderFeed()
-                            setHighlight()
+                            fetchHighlightNewsAndUpdateAdapter()
                         }
                         // Add more cases if needed
                     }
@@ -91,17 +129,18 @@ class NewsFragment : Fragment() {
             }
         })
 
-        setNewsHeaderFeed()
-        setNewsFeed()
+        //setNewsHeaderFeed()
+        fetchTopNewsAndUpdateAdapter()
     }
 
-    private fun setNewsHeaderFeed() {
-        val recyclerView = binding.recyclerViewNewsHeader
-        val cardList = createCardList() // Create your card data list here
-        val cardAdapter = TopNewsHeaderAdapter(cardList)
+    private fun fetchTopNewsAndUpdateAdapter() {
+        // Fetch top news from ViewModel
+        newsViewModel.getNews()
+    }
 
-        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
-        recyclerView.adapter = cardAdapter
+    private fun fetchHighlightNewsAndUpdateAdapter() {
+        // Fetch highlight news from ViewModel
+        newsViewModel.getHighlight()
     }
 
     private fun removeNewsHeaderFeed() {
@@ -109,44 +148,6 @@ class NewsFragment : Fragment() {
         recyclerView.adapter = null
     }
 
-
-    private fun setNewsFeed() {
-        val recyclerView = binding.recyclerViewNews
-        val cardList = createCardList() // Create your card data list here
-        val cardAdapter = TopNewsAdapter(cardList)
-
-        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
-        recyclerView.adapter = cardAdapter
-    }
-
-    private fun setHighlight() {
-        val recyclerView = binding.recyclerViewNews
-        val cardList = createCardList() // Create your card data list here
-        val cardAdapter = HighlightNewsAdapter(cardList)
-
-        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
-        recyclerView.adapter = cardAdapter
-    }
-
-    private fun createCardList(): List<TopNewsCardItem> {
-        // Create and return your list of CardItems
-        return listOf(
-            TopNewsCardItem(
-                "https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA",
-                "ច្បាស់ការ គូពិសេសដែលបងប្អូនរង់ចាំ",
-                "ជំនួបរវាង ដាវ លីឌុក និងព្រំសំណាងនឹងធ្វើឡើងនៅថ្ងៃអាទិត្យ ទី០៥ ខែសីហាខាងមុខនេះ...!! ជាលក្ខណៈ MAS FIGHT ១ទឹក ៩នាទីនឹងដោយវាយការរំអំបោះឆៅលក្ខណៈគុនខ្មែរ",
-                "10mn ago"
-            ),
-            TopNewsCardItem(
-                "https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA", "ច្បាស់ការ គូពិសេសដែលបងប្អូនរង់ចាំ", "ជំនួបរវាង ដាវ លីឌុក និងព្រំសំណាងនឹងធ្វើឡើងនៅថ្ងៃអាទិត្យ ទី០៥ ខែសីហាខាងមុខនេះ...!!", "10mn ago"
-            ),
-            TopNewsCardItem(
-                "https://i.ytimg.com/vi/Lh5O00zo_pc/hq720.jpg?sqp=-oaymwEcCOgCEMoBSFXyq4qpAw4IARUAAIhCGAFwAcABBg==&rs=AOn4CLCEQ100buCWUuyBKWe_96UbevbIEA", "ច្បាស់ការ គូពិសេសដែលបងប្អូនរង់ចាំ", "ជំនួបរវាង ដាវ លីឌុក និងព្រំសំណាងនឹងធ្វើឡើងនៅថ្ងៃអាទិត្យ ទី០៥ ខែសីហាខាងមុខនេះ...!!", "10mn ago"
-            ),
-            // Add more card items as needed
-        )
-    }
-
     interface TabSelectionListener {
         fun onTabSelected(tabIndex: Int)
     }

+ 74 - 0
app/src/main/java/com/sambath/kunkhmer/screen/news/NewsViewModel.kt

@@ -0,0 +1,74 @@
+package com.sambath.kunkhmer.screen.news
+
+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.HighlightData
+import com.sambath.kunkhmer.remote.NewsData
+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
+
+data class NewsViewState(
+    val initial: Boolean = false,
+    val isProgress: Boolean = false,
+    val isLoginSuccess: Boolean = false,
+    val error: String? = null,
+    val newsData: NewsData? = null,
+    val highlightData: HighlightData? = null
+)
+
+class NewsViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel()  {
+    private val _state = MutableLiveData(NewsViewState())
+    val state: LiveData<NewsViewState> = _state
+    private fun prev() = _state.value!!
+
+    fun getNews(){
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+
+        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)
+                })
+        )
+    }
+
+    fun getHighlight(){
+        if (_state.value!!.isProgress) return
+        _state.value = prev().copy(isProgress = true)
+
+        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)
+                })
+        )
+    }
+}