|
@@ -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)
|
|
|
}
|