|
@@ -5,13 +5,20 @@ import androidx.fragment.app.Fragment
|
|
import android.view.LayoutInflater
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.view.ViewGroup
|
|
|
|
+import androidx.recyclerview.widget.LinearLayoutManager
|
|
import com.sambath.kunkhmer.R
|
|
import com.sambath.kunkhmer.R
|
|
|
|
+import com.sambath.kunkhmer.adapter.TopNewsDetailAdapter
|
|
|
|
+import com.sambath.kunkhmer.app.App
|
|
import com.sambath.kunkhmer.remote.News
|
|
import com.sambath.kunkhmer.remote.News
|
|
|
|
+import com.sambath.kunkhmer.remote.NewsDetail
|
|
|
|
+import kotlinx.android.synthetic.main.fragment_top_news_detail.view.recyclerViewTopNewDetail
|
|
|
|
|
|
class TopNewsDetailFragment : Fragment() {
|
|
class TopNewsDetailFragment : Fragment() {
|
|
private var _root: View? = null
|
|
private var _root: View? = null
|
|
private val binding get() = _root!!
|
|
private val binding get() = _root!!
|
|
private var newsItem: News? = null
|
|
private var newsItem: News? = null
|
|
|
|
+ private lateinit var newsDetailViewModel: NewsDetailViewModel
|
|
|
|
+ private lateinit var topNewsDetailAdapter: TopNewsDetailAdapter
|
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
@@ -21,6 +28,35 @@ class TopNewsDetailFragment : Fragment() {
|
|
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_top_news_detail, container, false)
|
|
_root = inflater.inflate(R.layout.fragment_top_news_detail, container, false)
|
|
val newsId = arguments?.getString("news_id")
|
|
val newsId = arguments?.getString("news_id")
|
|
|
|
+
|
|
|
|
+ topNewsDetailAdapter = TopNewsDetailAdapter(NewsDetail())
|
|
|
|
+
|
|
|
|
+ // Set adapters
|
|
|
|
+ binding.recyclerViewTopNewDetail.apply {
|
|
|
|
+ layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
|
|
|
|
+ adapter = topNewsDetailAdapter
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ newsDetailViewModel = NewsDetailViewModel(App.injectApiService(), App.injectPrefHelper())
|
|
|
|
+ newsDetailViewModel.getNewsDetail(newsId!!)
|
|
|
|
+ newsDetailViewModel.state.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
|
|
|
|
+ render(it)
|
|
|
|
+ })
|
|
|
|
+
|
|
return _root
|
|
return _root
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private fun render(state: NewsDetailViewState) {
|
|
|
|
+ if (state.isLoginSuccess) {
|
|
|
|
+ if(state.newsDetailData != null) {
|
|
|
|
+ updateNewsDetailFeed(state.newsDetailData)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // Other state handling...
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private fun updateNewsDetailFeed(newsDetailData: NewsDetail) {
|
|
|
|
+ topNewsDetailAdapter.newsDetail = newsDetailData
|
|
|
|
+ topNewsDetailAdapter.notifyDataSetChanged()
|
|
|
|
+ }
|
|
}
|
|
}
|