|
@@ -11,9 +11,11 @@ import com.sambath.kunkhmer.config.Config
|
|
|
import com.sambath.kunkhmer.remote.News
|
|
|
import com.sambath.kunkhmer.remote.NewsData
|
|
|
import com.squareup.picasso.Picasso
|
|
|
+import java.text.SimpleDateFormat
|
|
|
+import java.util.Locale
|
|
|
+import java.util.TimeZone
|
|
|
|
|
|
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 {
|
|
|
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_top_news_item_card, parent, false)
|
|
@@ -22,18 +24,22 @@ class TopNewsAdapter(var cardList: MutableList<News>) : RecyclerView.Adapter<Top
|
|
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
|
val currentItem = cardList[position]
|
|
|
-
|
|
|
- Picasso.get()
|
|
|
- .load(Config.BASE_URL + "/v1/new/image/" + currentItem.filename)
|
|
|
- .into(holder.imageView);
|
|
|
- holder.dateTextView.text = currentItem.createdAt
|
|
|
+ if (!currentItem.filename.isNullOrEmpty()) {
|
|
|
+ Picasso.get()
|
|
|
+ .load(Config.BASE_URL + "/v1/new/image/" + currentItem.filename)
|
|
|
+ .into(holder.imageView);
|
|
|
+ } else {
|
|
|
+ Picasso.get()
|
|
|
+ .load(R.drawable.ic_logo_header)
|
|
|
+ .into(holder.imageView);
|
|
|
+ }
|
|
|
+ holder.dateTextView.text = parseAndFormatDate(currentItem.createdAt)
|
|
|
holder.titleTextView.text = currentItem.title
|
|
|
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)
|
|
@@ -41,4 +47,20 @@ class TopNewsAdapter(var cardList: MutableList<News>) : RecyclerView.Adapter<Top
|
|
|
val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
|
|
|
val subTextView: TextView = itemView.findViewById(R.id.descTextView)
|
|
|
}
|
|
|
+
|
|
|
+ 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)
|
|
|
+ }
|
|
|
}
|