@@ -7,18 +7,24 @@ import android.view.LayoutInflater
77import android.view.View
88import android.view.ViewGroup
99import android.widget.ImageButton
10+ import android.widget.ImageView
11+ import android.widget.ImageView.ScaleType
1012import android.widget.PopupMenu
1113import android.widget.RadioButton
1214import android.widget.TextView
1315import org.wordpress.android.R
14- import org.wordpress.android.R.id
15- import org.wordpress.android.R.layout
1616import org.wordpress.android.ui.ActionableEmptyView
1717import org.wordpress.android.ui.pages.PageItem.Divider
1818import org.wordpress.android.ui.pages.PageItem.Empty
1919import org.wordpress.android.ui.pages.PageItem.Page
2020import org.wordpress.android.ui.pages.PageItem.ParentPage
21+ import org.wordpress.android.ui.reader.utils.ReaderUtils
22+ import org.wordpress.android.util.DateTimeUtils
2123import org.wordpress.android.util.DisplayUtils
24+ import org.wordpress.android.util.ImageUtils
25+ import org.wordpress.android.util.image.ImageManager
26+ import org.wordpress.android.util.image.ImageType
27+ import java.util.Date
2228
2329sealed class PageItemViewHolder (internal val parent : ViewGroup , @LayoutRes layout : Int ) :
2430 RecyclerView .ViewHolder (LayoutInflater .from(parent.context).inflate(layout, parent, false )) {
@@ -27,51 +33,45 @@ sealed class PageItemViewHolder(internal val parent: ViewGroup, @LayoutRes layou
2733 class PageViewHolder (
2834 parentView : ViewGroup ,
2935 private val onMenuAction : (PageItem .Action , Page ) -> Boolean ,
30- private val onItemTapped : (Page ) -> Unit
31- ) : PageItemViewHolder(parentView, layout.page_list_item) {
32- private val pageTitle = itemView.findViewById<TextView >(id.page_title)
33- private val pageMore = itemView.findViewById<ImageButton >(id.page_more)
34- private val firstLabel = itemView.findViewById<TextView >(id.first_label)
35- private val secondLabel = itemView.findViewById<TextView >(id.second_label)
36- private val pageItemContainer = itemView.findViewById<ViewGroup >(id.page_item)
37- private val largeStretcher = itemView.findViewById<View >(id.large_stretcher)
38- private val smallStretcher = itemView.findViewById<View >(id.small_stretcher)
36+ private val onItemTapped : (Page ) -> Unit ,
37+ private val imageManager : ImageManager ,
38+ private val isSitePhotonCapable : Boolean
39+ ) : PageItemViewHolder(parentView, R .layout.page_list_item) {
40+ private val pageTitle = itemView.findViewById<TextView >(R .id.page_title)
41+ private val pageMore = itemView.findViewById<ImageButton >(R .id.page_more)
42+ private val time = itemView.findViewById<TextView >(R .id.time_posted)
43+ private val labels = itemView.findViewById<TextView >(R .id.labels)
44+ private val featuredImage = itemView.findViewById<ImageView >(R .id.featured_image)
45+ private val pageItemContainer = itemView.findViewById<ViewGroup >(R .id.page_item)
46+
47+ companion object {
48+ const val FEATURED_IMAGE_THUMBNAIL_SIZE_DP = 40
49+ }
3950
4051 override fun onBind (pageItem : PageItem ) {
41- (pageItem as Page ).apply {
42- val indentWidth = DisplayUtils .dpToPx(parent.context, 16 * pageItem .indent)
52+ (pageItem as Page ).let { page ->
53+ val indentWidth = DisplayUtils .dpToPx(parent.context, 16 * page .indent)
4354 val marginLayoutParams = pageItemContainer.layoutParams as ViewGroup .MarginLayoutParams
4455 marginLayoutParams.leftMargin = indentWidth
4556 pageItemContainer.layoutParams = marginLayoutParams
4657
47- pageTitle.text = if (pageItem .title.isEmpty())
58+ pageTitle.text = if (page .title.isEmpty())
4859 parent.context.getString(R .string.untitled_in_parentheses)
4960 else
50- pageItem .title
61+ page .title
5162
52- if (pageItem.labels.isEmpty()) {
53- firstLabel.visibility = View .GONE
54- smallStretcher.visibility = View .VISIBLE
55- largeStretcher.visibility = View .GONE
56- } else {
57- firstLabel.text = parent.context.getString(pageItem.labels.first())
58- firstLabel.visibility = View .VISIBLE
59- smallStretcher.visibility = View .GONE
60- largeStretcher.visibility = View .VISIBLE
61- }
63+ val date = if (page.date == Date (0 )) Date () else page.date
64+ time.text = DateTimeUtils .javaDateToTimeSpan(date, parent.context).capitalize()
6265
63- if (pageItem.labels.size <= 1 ) {
64- secondLabel.visibility = View .GONE
65- } else {
66- secondLabel.text = parent.context.getString(pageItem.labels[1 ])
67- secondLabel.visibility = View .VISIBLE
68- }
66+ labels.text = page.labels.map { parent.context.getString(it) }.sorted().joinToString(" · " )
6967
70- itemView.setOnClickListener { onItemTapped(pageItem ) }
68+ itemView.setOnClickListener { onItemTapped(page ) }
7169
72- pageMore.setOnClickListener { moreClick(pageItem, it ) }
70+ pageMore.setOnClickListener { view -> moreClick(page, view ) }
7371 pageMore.visibility =
74- if (pageItem.actions.isNotEmpty() && pageItem.actionsEnabled) View .VISIBLE else View .INVISIBLE
72+ if (page.actions.isNotEmpty() && page.actionsEnabled) View .VISIBLE else View .INVISIBLE
73+
74+ showFeaturedImage(page.imageUrl)
7575 }
7676 }
7777
@@ -87,10 +87,31 @@ sealed class PageItemViewHolder(internal val parent: ViewGroup, @LayoutRes layou
8787 }
8888 popup.show()
8989 }
90+
91+ private fun showFeaturedImage (imageUrl : String? ) {
92+ val imageSize = DisplayUtils .dpToPx(parent.context, FEATURED_IMAGE_THUMBNAIL_SIZE_DP )
93+ if (imageUrl == null ) {
94+ featuredImage.visibility = View .GONE
95+ imageManager.cancelRequestAndClearImageView(featuredImage)
96+ } else if (imageUrl.startsWith(" http" )) {
97+ featuredImage.visibility = View .VISIBLE
98+ val photonUrl = ReaderUtils .getResizedImageUrl(imageUrl, imageSize, imageSize, ! isSitePhotonCapable)
99+ imageManager.load(featuredImage, ImageType .PHOTO , photonUrl, ScaleType .CENTER_CROP )
100+ } else {
101+ val bmp = ImageUtils .getWPImageSpanThumbnailFromFilePath(featuredImage.context, imageUrl, imageSize)
102+ if (bmp != null ) {
103+ featuredImage.visibility = View .VISIBLE
104+ imageManager.load(featuredImage, bmp)
105+ } else {
106+ featuredImage.visibility = View .GONE
107+ imageManager.cancelRequestAndClearImageView(featuredImage)
108+ }
109+ }
110+ }
90111 }
91112
92- class PageDividerViewHolder (parentView : ViewGroup ) : PageItemViewHolder(parentView, layout.page_divider_item) {
93- private val dividerTitle = itemView.findViewById<TextView >(id.divider_text)
113+ class PageDividerViewHolder (parentView : ViewGroup ) : PageItemViewHolder(parentView, R . layout.page_divider_item) {
114+ private val dividerTitle = itemView.findViewById<TextView >(R . id.divider_text)
94115
95116 override fun onBind (pageItem : PageItem ) {
96117 (pageItem as Divider ).apply {
@@ -104,8 +125,8 @@ sealed class PageItemViewHolder(internal val parent: ViewGroup, @LayoutRes layou
104125 private val onParentSelected : (ParentPage ) -> Unit ,
105126 @LayoutRes layout : Int
106127 ) : PageItemViewHolder(parentView, layout) {
107- private val pageTitle = itemView.findViewById<TextView >(id.page_title)
108- private val radioButton = itemView.findViewById<RadioButton >(id.radio_button)
128+ private val pageTitle = itemView.findViewById<TextView >(R . id.page_title)
129+ private val radioButton = itemView.findViewById<RadioButton >(R . id.radio_button)
109130
110131 override fun onBind (pageItem : PageItem ) {
111132 (pageItem as ParentPage ).apply {
@@ -131,12 +152,12 @@ sealed class PageItemViewHolder(internal val parent: ViewGroup, @LayoutRes layou
131152 class EmptyViewHolder (
132153 parentView : ViewGroup ,
133154 private val onActionButtonClicked : () -> Unit
134- ) : PageItemViewHolder(parentView, layout.page_empty_item) {
135- private val emptyView = itemView.findViewById<ActionableEmptyView >(id.actionable_empty_view)
155+ ) : PageItemViewHolder(parentView, R . layout.page_empty_item) {
156+ private val emptyView = itemView.findViewById<ActionableEmptyView >(R . id.actionable_empty_view)
136157
137158 @Suppress(" DEPRECATION" )
138159 override fun onBind (pageItem : PageItem ) {
139- if (pageItem is Empty ) {
160+ (pageItem as Empty ). apply {
140161 emptyView.title.text = emptyView.resources.getString(pageItem.textResource)
141162
142163 if (pageItem.isButtonVisible) {
0 commit comments