From 76aa4c3d59d3532f1e61af5dc867c020c9271010 Mon Sep 17 00:00:00 2001 From: Renan Lukas <14964993+RenanLukas@users.noreply.github.com> Date: Mon, 10 Jun 2024 20:30:57 -0300 Subject: [PATCH] Apply PR suggestion: remove old UI logic from ReaderPostAdapter --- .../ui/reader/adapters/ReaderPostAdapter.java | 124 +------ .../ui/reader/discover/ReaderCardUiState.kt | 62 +--- .../reader/discover/ReaderDiscoverAdapter.kt | 11 +- .../discover/ReaderDiscoverViewModel.kt | 10 +- .../discover/ReaderPostUiStateBuilder.kt | 141 +------ .../viewholders/ReaderPostNewViewHolder.kt | 16 +- .../viewholders/ReaderPostViewHolder.kt | 225 ------------ .../tagsfeed/ReaderTagsFeedViewModel.kt | 4 +- .../discover/ReaderDiscoverViewModelTest.kt | 57 ++- .../discover/ReaderPostUiStateBuilderTest.kt | 344 +++--------------- 10 files changed, 114 insertions(+), 880 deletions(-) delete mode 100644 WordPress/src/main/java/org/wordpress/android/ui/reader/discover/viewholders/ReaderPostViewHolder.kt diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java index de82ad15e77c..13667a151df1 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/adapters/ReaderPostAdapter.java @@ -39,13 +39,11 @@ import org.wordpress.android.ui.reader.actions.ReaderActions; import org.wordpress.android.ui.reader.actions.ReaderTagActions; import org.wordpress.android.ui.reader.discover.ReaderCardUiState; -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostNewUiState; import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState; import org.wordpress.android.ui.reader.discover.ReaderPostCardActionType; import org.wordpress.android.ui.reader.discover.ReaderPostMoreButtonUiStateBuilder; import org.wordpress.android.ui.reader.discover.ReaderPostUiStateBuilder; import org.wordpress.android.ui.reader.discover.viewholders.ReaderPostNewViewHolder; -import org.wordpress.android.ui.reader.discover.viewholders.ReaderPostViewHolder; import org.wordpress.android.ui.reader.models.ReaderBlogIdPostId; import org.wordpress.android.ui.reader.utils.ReaderAnnouncementHelper; import org.wordpress.android.ui.reader.tracker.ReaderTab; @@ -292,9 +290,7 @@ public int getItemViewType(int position) { @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { - if (holder instanceof ReaderPostViewHolder) { - renderPost(position, (ReaderPostViewHolder) holder, false); - } else if (holder instanceof ReaderPostNewViewHolder) { + if (holder instanceof ReaderPostNewViewHolder) { renderPostNew(position, (ReaderPostNewViewHolder) holder, false); } else if (holder instanceof ReaderXPostViewHolder) { renderXPost(position, (ReaderXPostViewHolder) holder); @@ -452,116 +448,6 @@ private void undoPostUnbookmarked(final ReaderPost post) { } } - private void renderPost(final int position, final ReaderPostViewHolder holder, boolean showMoreMenu) { - final ReaderPost post = getItem(position); - ReaderPostListType postListType = getPostListType(); - if (post == null) { - return; - } - Context ctx = holder.getViewContext(); - Function3 onButtonClicked = - (postId, blogId, type) -> { - mOnPostListItemButtonListener.onButtonClicked(post, type); - renderPost(position, holder, false); - return Unit.INSTANCE; - }; - Function2 onItemClicked = (postId, blogId) -> { - if (mPostSelectedListener != null) { - mPostSelectedListener.onPostSelected(post); - } - return Unit.INSTANCE; - }; - Function1 onItemRendered = (item) -> { - checkLoadMore(position); - - // if we haven't already rendered this post and it has a "railcar" attached to it, add it - // to the rendered list and record the TrainTracks render event - if (post.hasRailcar() && !mRenderedIds.contains(post.getPseudoId())) { - mRenderedIds.add(post.getPseudoId()); - mReaderTracker.trackRailcar(post.getRailcarJson()); - } - return Unit.INSTANCE; - }; - Function2 onDiscoverSectionClicked = (postId, blogId) -> { - ReaderPostDiscoverData discoverData = post.getDiscoverData(); - switch (discoverData.getDiscoverType()) { - case EDITOR_PICK: - if (mPostSelectedListener != null) { - mPostSelectedListener.onPostSelected(post); - } - break; - case SITE_PICK: - if (discoverData.getBlogId() != 0) { - ReaderActivityLauncher.showReaderBlogPreview( - ctx, - discoverData.getBlogId(), - post.isFollowedByCurrentUser, - mSource, - mReaderTracker - ); - } else if (discoverData.hasBlogUrl()) { - ReaderActivityLauncher.openUrl(ctx, discoverData.getBlogUrl()); - } - break; - case OTHER: - // noop - break; - } - return Unit.INSTANCE; - }; - Function1 onMoreButtonClicked = (uiState) -> { - renderPost(position, holder, true); - return Unit.INSTANCE; - }; - - Function1 onMoreDismissed = (uiState) -> { - renderPost(position, holder, false); - return Unit.INSTANCE; - }; - - Function2 onVideoOverlayClicked = (postId, blogId) -> { - ReaderActivityLauncher.showReaderVideoViewer(ctx, post.getFeaturedVideo()); - return Unit.INSTANCE; - }; - - Function0 onPostHeaderClicked = () -> { - ReaderActivityLauncher.showReaderBlogPreview( - ctx, - post, - mSource, - mReaderTracker - ); - return Unit.INSTANCE; - }; - - Function1 onTagItemClicked = (tagSlug) -> { - // noop - return Unit.INSTANCE; - }; - - ReaderPostUiState uiState = mReaderPostUiStateBuilder - .mapPostToUiStateBlocking( - mSource, - post, - false, - mPhotonWidth, - mPhotonHeight, - postListType, - onButtonClicked, - onItemClicked, - onItemRendered, - onDiscoverSectionClicked, - onMoreButtonClicked, - onMoreDismissed, - onVideoOverlayClicked, - onPostHeaderClicked, - onTagItemClicked, - showMoreMenu ? mReaderPostMoreButtonUiStateBuilder - .buildMoreMenuItemsBlocking(post, false, false, onButtonClicked) : null - ); - holder.onBind(uiState); - } - // TODO update the viewholder to the new one private void renderPostNew(final int position, final ReaderPostNewViewHolder holder, boolean showMoreMenu) { final ReaderPost post = getItem(position); @@ -619,12 +505,12 @@ private void renderPostNew(final int position, final ReaderPostNewViewHolder hol } return Unit.INSTANCE; }; - Function1 onMoreButtonClicked = (uiState) -> { + Function1 onMoreButtonClicked = (uiState) -> { renderPostNew(position, holder, true); return Unit.INSTANCE; }; - Function1 onMoreDismissed = (uiState) -> { + Function1 onMoreDismissed = (uiState) -> { renderPostNew(position, holder, false); return Unit.INSTANCE; }; @@ -644,8 +530,8 @@ private void renderPostNew(final int position, final ReaderPostNewViewHolder hol return Unit.INSTANCE; }; - ReaderPostNewUiState uiState = mReaderPostUiStateBuilder - .mapPostToNewUiStateBlocking( + ReaderPostUiState uiState = mReaderPostUiStateBuilder + .mapPostToUiStateBlocking( mSource, post, mPhotonWidth, diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderCardUiState.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderCardUiState.kt index 6549e88cfe85..a8a9084605cd 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderCardUiState.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderCardUiState.kt @@ -1,16 +1,13 @@ package org.wordpress.android.ui.reader.discover -import android.text.Spanned import androidx.annotation.AttrRes import androidx.annotation.ColorRes import androidx.annotation.DrawableRes import org.wordpress.android.R import org.wordpress.android.ui.reader.discover.ReaderPostCardAction.PrimaryAction import org.wordpress.android.ui.reader.discover.ReaderPostCardActionType.SPACER_NO_ACTION -import org.wordpress.android.ui.reader.discover.interests.TagUiState import org.wordpress.android.ui.reader.models.ReaderImageList import org.wordpress.android.ui.reader.views.compose.ReaderAnnouncementCardItemData -import org.wordpress.android.ui.reader.views.uistates.ReaderBlogSectionUiState import org.wordpress.android.ui.utils.UiDimen import org.wordpress.android.ui.utils.UiString import org.wordpress.android.ui.utils.UiString.UiStringRes @@ -18,51 +15,6 @@ import org.wordpress.android.util.image.ImageType sealed class ReaderCardUiState { data class ReaderPostUiState( - val source: String, - val postId: Long, - val blogId: Long, - val feedId: Long, - val isFollowed: Boolean, - val blogSection: ReaderBlogSectionUiState, - val title: UiString?, - val excerpt: String?, // mTxtText - val tagItems: List, - val photoTitle: String?, - val featuredImageUrl: String?, - val featuredImageCornerRadius: UiDimen, - val fullVideoUrl: String?, - val thumbnailStripSection: GalleryThumbnailStripData?, - val discoverSection: DiscoverLayoutUiState?, - val expandableTagsViewVisibility: Boolean, - val videoOverlayVisibility: Boolean, - val featuredImageVisibility: Boolean, - val moreMenuVisibility: Boolean, - val bookmarkAction: PrimaryAction, - val likeAction: PrimaryAction, - val reblogAction: PrimaryAction, - val commentsAction: PrimaryAction, - val moreMenuItems: List? = null, - val onItemClicked: (Long, Long) -> Unit, - val onItemRendered: (ReaderCardUiState) -> Unit, - val onMoreButtonClicked: (ReaderPostUiState) -> Unit, - val onMoreDismissed: (ReaderPostUiState) -> Unit, - val onVideoOverlayClicked: (Long, Long) -> Unit - ) : ReaderCardUiState() { - data class GalleryThumbnailStripData( - val images: ReaderImageList, - val isPrivate: Boolean, - val content: String // needs to be here as it's required by ReaderThumbnailStrip - ) - - data class DiscoverLayoutUiState( - val discoverText: Spanned, - val discoverAvatarUrl: String, - val imageType: ImageType, - val onDiscoverClicked: ((Long, Long) -> Unit) - ) - } - - data class ReaderPostNewUiState( val source: String, val postId: Long, val blogId: Long, @@ -75,7 +27,7 @@ sealed class ReaderCardUiState { val featuredImageUrl: String?, val featuredImageCornerRadius: UiDimen, val fullVideoUrl: String?, - val thumbnailStripSection: ReaderPostUiState.GalleryThumbnailStripData?, + val thumbnailStripSection: GalleryThumbnailStripData?, val videoOverlayVisibility: Boolean, val featuredImageVisibility: Boolean, val moreMenuVisibility: Boolean, @@ -84,9 +36,9 @@ sealed class ReaderCardUiState { val commentsAction: PrimaryAction, val moreMenuItems: List? = null, val onItemClicked: (Long, Long) -> Unit, - val onItemRendered: (ReaderPostNewUiState) -> Unit, - val onMoreButtonClicked: (ReaderPostNewUiState) -> Unit, - val onMoreDismissed: (ReaderPostNewUiState) -> Unit, + val onItemRendered: (ReaderPostUiState) -> Unit, + val onMoreButtonClicked: (ReaderPostUiState) -> Unit, + val onMoreDismissed: (ReaderPostUiState) -> Unit, val onVideoOverlayClicked: (Long, Long) -> Unit, ) : ReaderCardUiState() { data class CompactBlogSectionData( @@ -105,6 +57,12 @@ sealed class ReaderCardUiState { val likeCount: Int, val commentCount: Int, ) + + data class GalleryThumbnailStripData( + val images: ReaderImageList, + val isPrivate: Boolean, + val content: String // needs to be here as it's required by ReaderThumbnailStrip + ) } data class ReaderInterestsCardUiState(val interest: List) : ReaderCardUiState() { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverAdapter.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverAdapter.kt index c4e76d1bd203..3bd85c385421 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverAdapter.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverAdapter.kt @@ -5,13 +5,11 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView.Adapter import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderAnnouncementCardUiState import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterestsCardUiState -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostNewUiState import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderRecommendedBlogsCardUiState import org.wordpress.android.ui.reader.discover.viewholders.ReaderAnnouncementCardViewHolder import org.wordpress.android.ui.reader.discover.viewholders.ReaderInterestsCardViewHolder import org.wordpress.android.ui.reader.discover.viewholders.ReaderPostNewViewHolder -import org.wordpress.android.ui.reader.discover.viewholders.ReaderPostViewHolder import org.wordpress.android.ui.reader.discover.viewholders.ReaderRecommendedBlogsCardNewViewHolder import org.wordpress.android.ui.reader.discover.viewholders.ReaderViewHolder import org.wordpress.android.ui.reader.tracker.ReaderTracker @@ -20,7 +18,6 @@ import org.wordpress.android.ui.utils.UiHelpers import org.wordpress.android.util.NetworkUtilsWrapper import org.wordpress.android.util.image.ImageManager -private const val POST_VIEW_TYPE: Int = 1 private const val INTEREST_VIEW_TYPE: Int = 2 private const val RECOMMENDED_BLOGS_VIEW_TYPE: Int = 3 private const val POST_NEW_VIEW_TYPE: Int = 4 @@ -35,7 +32,6 @@ class ReaderDiscoverAdapter( private val items = mutableListOf() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReaderViewHolder<*> { return when (viewType) { - POST_VIEW_TYPE -> ReaderPostViewHolder(uiHelpers, imageManager, readerTracker, parent) POST_NEW_VIEW_TYPE -> ReaderPostNewViewHolder( uiHelpers, imageManager, @@ -77,8 +73,7 @@ class ReaderDiscoverAdapter( override fun getItemViewType(position: Int): Int { return when (items[position]) { - is ReaderPostUiState -> POST_VIEW_TYPE - is ReaderPostNewUiState -> POST_NEW_VIEW_TYPE + is ReaderPostUiState -> POST_NEW_VIEW_TYPE is ReaderInterestsCardUiState -> INTEREST_VIEW_TYPE is ReaderRecommendedBlogsCardUiState -> RECOMMENDED_BLOGS_VIEW_TYPE is ReaderAnnouncementCardUiState -> READER_ANNOUNCEMENT_TYPE @@ -105,10 +100,6 @@ class ReaderDiscoverAdapter( oldItem.postId == (newItem as ReaderPostUiState).postId && oldItem.blogId == newItem.blogId } - is ReaderPostNewUiState -> { - oldItem.postId == (newItem as ReaderPostNewUiState).postId && oldItem.blogId == newItem.blogId - } - is ReaderRecommendedBlogsCardUiState -> { val newItemState = newItem as? ReaderRecommendedBlogsCardUiState oldItem.blogs.map { it.blogId to it.feedId } == newItemState?.blogs?.map { it.blogId to it.feedId } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverViewModel.kt index 4c8cc7feb423..4fb2ccc2417b 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverViewModel.kt @@ -19,7 +19,7 @@ import org.wordpress.android.modules.UI_THREAD import org.wordpress.android.ui.bloggingprompts.BloggingPromptsPostTagProvider.Companion.BLOGGING_PROMPT_TAG import org.wordpress.android.ui.pages.SnackbarMessageHolder import org.wordpress.android.ui.reader.ReaderTypes.ReaderPostListType.TAG_FOLLOWED -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostNewUiState +import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderRecommendedBlogsCardUiState.ReaderRecommendedBlogUiState import org.wordpress.android.ui.reader.discover.ReaderNavigationEvents.ShowBlogPreview import org.wordpress.android.ui.reader.discover.ReaderNavigationEvents.ShowPostsByTag @@ -233,7 +233,7 @@ class ReaderDiscoverViewModel @Inject constructor( private suspend fun convertCardsToUiStates(posts: ReaderDiscoverCards): List { return posts.cards.map { card -> when (card) { - is ReaderPostCard -> postUiStateBuilder.mapPostToNewUiState( + is ReaderPostCard -> postUiStateBuilder.mapPostToUiState( source = ReaderTracker.SOURCE_DISCOVER, post = card.post, photonWidth = photonWidth, @@ -422,15 +422,15 @@ class ReaderDiscoverViewModel @Inject constructor( } } - private fun onMoreButtonClicked(postUiState: ReaderPostNewUiState) { + private fun onMoreButtonClicked(postUiState: ReaderPostUiState) { changeMoreMenuVisibility(postUiState, true) } - private fun onMoreMenuDismissed(postUiState: ReaderPostNewUiState) { + private fun onMoreMenuDismissed(postUiState: ReaderPostUiState) { changeMoreMenuVisibility(postUiState, false) } - private fun changeMoreMenuVisibility(currentUiState: ReaderPostNewUiState, show: Boolean) { + private fun changeMoreMenuVisibility(currentUiState: ReaderPostUiState, show: Boolean) { launch { findPost(currentUiState.postId, currentUiState.blogId)?.let { post -> val moreMenuItems = if (show) { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderPostUiStateBuilder.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderPostUiStateBuilder.kt index 9dde480b77a5..579b4755f368 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderPostUiStateBuilder.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/ReaderPostUiStateBuilder.kt @@ -11,10 +11,6 @@ import org.wordpress.android.models.ReaderCardType.GALLERY import org.wordpress.android.models.ReaderCardType.PHOTO import org.wordpress.android.models.ReaderCardType.VIDEO import org.wordpress.android.models.ReaderPost -import org.wordpress.android.models.ReaderPostDiscoverData -import org.wordpress.android.models.ReaderPostDiscoverData.DiscoverType.EDITOR_PICK -import org.wordpress.android.models.ReaderPostDiscoverData.DiscoverType.OTHER -import org.wordpress.android.models.ReaderPostDiscoverData.DiscoverType.SITE_PICK import org.wordpress.android.models.ReaderTag import org.wordpress.android.models.ReaderTagList import org.wordpress.android.modules.BG_THREAD @@ -28,11 +24,8 @@ import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterest import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterestsCardUiState.ChipStyle.ChipStylePurple import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterestsCardUiState.ChipStyle.ChipStyleYellow import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterestsCardUiState.ReaderInterestUiState -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostNewUiState -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostNewUiState.InteractionSectionData import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState.DiscoverLayoutUiState -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState.GalleryThumbnailStripData +import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState.InteractionSectionData import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderRecommendedBlogsCardUiState import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderRecommendedBlogsCardUiState.ReaderRecommendedBlogUiState import org.wordpress.android.ui.reader.discover.ReaderPostCardAction.PrimaryAction @@ -54,7 +47,6 @@ import org.wordpress.android.util.WPAvatarUtilsWrapper import org.wordpress.android.util.SiteUtils import org.wordpress.android.util.UrlUtilsWrapper import org.wordpress.android.util.image.BlavatarShape.CIRCULAR -import org.wordpress.android.util.image.ImageType import javax.inject.Inject import javax.inject.Named import android.R as AndroidR @@ -70,79 +62,26 @@ class ReaderPostUiStateBuilder @Inject constructor( private val dateTimeUtilsWrapper: DateTimeUtilsWrapper, private val readerImageScannerProvider: ReaderImageScannerProvider, private val readerUtilsWrapper: ReaderUtilsWrapper, - private val readerPostTagsUiStateBuilder: ReaderPostTagsUiStateBuilder, @Named(BG_THREAD) private val bgDispatcher: CoroutineDispatcher ) { @Suppress("LongParameterList") - fun mapPostToUiStateBlocking( + suspend fun mapPostToUiState( source: String, post: ReaderPost, - isDiscover: Boolean = false, photonWidth: Int, photonHeight: Int, postListType: ReaderPostListType, onButtonClicked: (Long, Long, ReaderPostCardActionType) -> Unit, onItemClicked: (Long, Long) -> Unit, onItemRendered: (ReaderCardUiState) -> Unit, - onDiscoverSectionClicked: (Long, Long) -> Unit, onMoreButtonClicked: (ReaderPostUiState) -> Unit, onMoreDismissed: (ReaderPostUiState) -> Unit, onVideoOverlayClicked: (Long, Long) -> Unit, - onPostHeaderViewClicked: () -> Unit, - onTagItemClicked: (String) -> Unit, - moreMenuItems: List? = null - ): ReaderPostUiState { - return ReaderPostUiState( - source = source, - postId = post.postId, - blogId = post.blogId, - feedId = post.feedId, - isFollowed = post.isFollowedByCurrentUser, - blogSection = buildBlogSection(post, onPostHeaderViewClicked, postListType, post.isP2orA8C), - excerpt = buildExcerpt(post), - title = buildTitle(post), - tagItems = buildTagItems(post, onTagItemClicked), - photoTitle = buildPhotoTitle(post), - featuredImageUrl = buildFeaturedImageUrl(post, photonWidth, photonHeight), - featuredImageCornerRadius = UIDimenRes(R.dimen.reader_featured_image_corner_radius), - thumbnailStripSection = buildThumbnailStripUrls(post), - expandableTagsViewVisibility = buildExpandedTagsViewVisibility(post, isDiscover), - videoOverlayVisibility = buildVideoOverlayVisibility(post), - featuredImageVisibility = buildFeaturedImageVisibility(post), - moreMenuVisibility = accountStore.hasAccessToken(), - moreMenuItems = moreMenuItems, - fullVideoUrl = buildFullVideoUrl(post), - discoverSection = buildDiscoverSection(post, onDiscoverSectionClicked), - bookmarkAction = buildBookmarkSection(post, onButtonClicked), - likeAction = buildLikeSection(post, onButtonClicked), - reblogAction = buildReblogSection(post, onButtonClicked), - commentsAction = buildCommentsSection(post, onButtonClicked), - onItemClicked = onItemClicked, - onItemRendered = onItemRendered, - onMoreButtonClicked = onMoreButtonClicked, - onMoreDismissed = onMoreDismissed, - onVideoOverlayClicked = onVideoOverlayClicked - ) - } - - @Suppress("LongParameterList") - suspend fun mapPostToNewUiState( - source: String, - post: ReaderPost, - photonWidth: Int, - photonHeight: Int, - postListType: ReaderPostListType, - onButtonClicked: (Long, Long, ReaderPostCardActionType) -> Unit, - onItemClicked: (Long, Long) -> Unit, - onItemRendered: (ReaderCardUiState) -> Unit, - onMoreButtonClicked: (ReaderPostNewUiState) -> Unit, - onMoreDismissed: (ReaderPostNewUiState) -> Unit, - onVideoOverlayClicked: (Long, Long) -> Unit, onPostHeaderViewClicked: (Long, Long) -> Unit, moreMenuItems: List? = null, - ): ReaderPostNewUiState { + ): ReaderPostUiState { return withContext(bgDispatcher) { - mapPostToNewUiStateBlocking( + mapPostToUiStateBlocking( source, post, photonWidth, @@ -161,7 +100,7 @@ class ReaderPostUiStateBuilder @Inject constructor( } @Suppress("LongParameterList") - fun mapPostToNewUiStateBlocking( + fun mapPostToUiStateBlocking( source: String, post: ReaderPost, photonWidth: Int, @@ -170,13 +109,13 @@ class ReaderPostUiStateBuilder @Inject constructor( onButtonClicked: (Long, Long, ReaderPostCardActionType) -> Unit, onItemClicked: (Long, Long) -> Unit, onItemRendered: (ReaderCardUiState) -> Unit, - onMoreButtonClicked: (ReaderPostNewUiState) -> Unit, - onMoreDismissed: (ReaderPostNewUiState) -> Unit, + onMoreButtonClicked: (ReaderPostUiState) -> Unit, + onMoreDismissed: (ReaderPostUiState) -> Unit, onVideoOverlayClicked: (Long, Long) -> Unit, onPostHeaderViewClicked: (Long, Long) -> Unit, moreMenuItems: List? = null, - ): ReaderPostNewUiState { - return ReaderPostNewUiState( + ): ReaderPostUiState { + return ReaderPostUiState( source = source, postId = post.postId, blogId = post.blogId, @@ -184,8 +123,8 @@ class ReaderPostUiStateBuilder @Inject constructor( isFollowed = post.isFollowedByCurrentUser, blogSection = buildCompactBlogSection(post, postListType, onPostHeaderViewClicked, post.isP2orA8C), interactionSection = buildInteractionSection(post), - title = buildTitle(post, forceForPhoto = true, allowEmptyTitle = true), - excerpt = buildExcerpt(post, forceForPhoto = true), + title = buildTitle(post), + excerpt = buildExcerpt(post), featuredImageUrl = buildFeaturedImageUrl(post, photonWidth, photonHeight), featuredImageCornerRadius = UIDimenRes(R.dimen.reader_featured_image_corner_radius_new), fullVideoUrl = buildFullVideoUrl(post), @@ -296,8 +235,8 @@ class ReaderPostUiStateBuilder @Inject constructor( postListType: ReaderPostListType, onBlogSectionClicked: (Long, Long) -> Unit, isP2Post: Boolean = false, - ): ReaderPostNewUiState.CompactBlogSectionData { - return ReaderPostNewUiState.CompactBlogSectionData( + ): ReaderPostUiState.CompactBlogSectionData { + return ReaderPostUiState.CompactBlogSectionData( postId = post.postId, blogId = post.blogId, blogName = buildBlogName(post, isP2Post), @@ -336,20 +275,10 @@ class ReaderPostUiStateBuilder @Inject constructor( ?.blogUrl ?.let { urlUtilsWrapper.removeScheme(it) } - private fun buildDiscoverSection(post: ReaderPost, onDiscoverSectionClicked: (Long, Long) -> Unit) = - post.takeIf { post.isDiscoverPost && post.discoverData.discoverType != OTHER } - ?.let { buildDiscoverSectionUiState(post.discoverData, onDiscoverSectionClicked) } - private fun buildFullVideoUrl(post: ReaderPost) = post.takeIf { post.cardType == VIDEO } ?.let { post.featuredVideo } - private fun buildExpandedTagsViewVisibility(post: ReaderPost, isDiscover: Boolean) = - post.tags.isNotEmpty() && isDiscover - - private fun buildTagItems(post: ReaderPost, onClicked: (String) -> Unit) = - readerPostTagsUiStateBuilder.mapPostTagsToTagUiStates(post, onClicked) - // TODO malinjir show overlay when buildFullVideoUrl != null private fun buildVideoOverlayVisibility(post: ReaderPost) = post.cardType == VIDEO @@ -367,24 +296,11 @@ class ReaderPostUiStateBuilder @Inject constructor( ?.getFeaturedImageForDisplay(photonWidth, photonHeight) } - private fun buildPhotoTitle(post: ReaderPost) = - post.takeIf { it.cardType == PHOTO && it.hasTitle() }?.title - - private fun buildTitle( - post: ReaderPost, - forceForPhoto: Boolean = false, - allowEmptyTitle: Boolean = false - ): UiString? { - return if (post.cardType != PHOTO || forceForPhoto) { - post.takeIf { it.hasTitle() }?.title?.let { UiStringText(it) } - ?: UiStringRes(R.string.untitled_in_parentheses).takeUnless { allowEmptyTitle } - } else { - null - } - } + private fun buildTitle(post: ReaderPost): UiString? = + post.takeIf { it.hasTitle() }?.title?.let { UiStringText(it) } - private fun buildExcerpt(post: ReaderPost, forceForPhoto: Boolean = false) = - post.takeIf { (post.cardType != PHOTO || forceForPhoto) && post.hasExcerpt() }?.excerpt + private fun buildExcerpt(post: ReaderPost) = + post.takeIf { post.hasExcerpt() }?.excerpt private fun buildBlogName(post: ReaderPost, isP2Post: Boolean = false): UiString { val blogName = post.takeIf { it.hasBlogName() }?.blogName?.let { UiStringText(it) } @@ -414,30 +330,11 @@ class ReaderPostUiStateBuilder @Inject constructor( private fun buildDateLine(post: ReaderPost) = dateTimeUtilsWrapper.javaDateToTimeSpan(post.getDisplayDate(dateTimeUtilsWrapper)) - @Suppress("UseCheckOrError") - private fun buildDiscoverSectionUiState( - discoverData: ReaderPostDiscoverData, - onDiscoverSectionClicked: (Long, Long) -> Unit - ): DiscoverLayoutUiState { - val discoverText = discoverData.attributionHtml - val discoverAvatarUrl = avatarUtilsWrapper.rewriteAvatarUrlWithResource( - discoverData.avatarUrl, - R.dimen.avatar_sz_small - ) - @Suppress("DEPRECATION") val discoverAvatarImageType = when (discoverData.discoverType) { - EDITOR_PICK -> ImageType.AVATAR - SITE_PICK -> ImageType.BLAVATAR - OTHER -> throw IllegalStateException("This could should be unreachable.") - else -> ImageType.AVATAR - } - return DiscoverLayoutUiState(discoverText, discoverAvatarUrl, discoverAvatarImageType, onDiscoverSectionClicked) - } - - private fun retrieveGalleryThumbnailUrls(post: ReaderPost): GalleryThumbnailStripData { + private fun retrieveGalleryThumbnailUrls(post: ReaderPost): ReaderPostUiState.GalleryThumbnailStripData { // scan post content for images suitable in a gallery val images = readerImageScannerProvider.createReaderImageScanner(post.text, post.isPrivate) .getImageList(ReaderConstants.THUMBNAIL_STRIP_IMG_COUNT, ReaderConstants.MIN_GALLERY_IMAGE_WIDTH) - return GalleryThumbnailStripData(images, post.isPrivate, post.text) + return ReaderPostUiState.GalleryThumbnailStripData(images, post.isPrivate, post.text) } private fun buildBookmarkSection( diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/viewholders/ReaderPostNewViewHolder.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/viewholders/ReaderPostNewViewHolder.kt index 841e907881d0..4334fe3ba0dc 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/viewholders/ReaderPostNewViewHolder.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/viewholders/ReaderPostNewViewHolder.kt @@ -12,7 +12,7 @@ import org.wordpress.android.databinding.ReaderCardviewPostNewBinding import org.wordpress.android.datasets.ReaderThumbnailTable import org.wordpress.android.ui.reader.adapters.ReaderMenuAdapter import org.wordpress.android.ui.reader.discover.ReaderCardUiState -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostNewUiState +import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState import org.wordpress.android.ui.reader.discover.ReaderPostCardAction import org.wordpress.android.ui.reader.discover.ReaderPostCardAction.PrimaryAction import org.wordpress.android.ui.reader.discover.ReaderPostCardActionType @@ -43,7 +43,7 @@ class ReaderPostNewViewHolder( } override fun onBind(uiState: ReaderCardUiState) = with(binding) { - val state = uiState as ReaderPostNewUiState + val state = uiState as ReaderPostUiState // Blog section updateBlogSection(state) @@ -91,7 +91,7 @@ class ReaderPostNewViewHolder( state.onItemRendered.invoke(uiState) } - private fun updateInteractionCountsSection(state: ReaderPostNewUiState) = with(binding) { + private fun updateInteractionCountsSection(state: ReaderPostUiState) = with(binding) { val likeCount = state.interactionSection.likeCount val commentCount = state.interactionSection.commentCount @@ -103,7 +103,7 @@ class ReaderPostNewViewHolder( readerCardDotSeparator.isVisible = likeLabel != null && commentLabel != null } - private fun updateBlogSection(state: ReaderPostNewUiState) = with(binding.layoutBlogSection) { + private fun updateBlogSection(state: ReaderPostUiState) = with(binding.layoutBlogSection) { updateAvatarOrBlavatar(state) uiHelpers.setTextOrHide(blogSectionTextBlogName, state.blogSection.blogName) uiHelpers.setTextOrHide(blogSectionTextDateline, state.blogSection.dateLine) @@ -118,7 +118,7 @@ class ReaderPostNewViewHolder( } } - private fun updateAvatarOrBlavatar(state: ReaderPostNewUiState) = with(binding.layoutBlogSection) { + private fun updateAvatarOrBlavatar(state: ReaderPostUiState) = with(binding.layoutBlogSection) { var isShowingAnyAvatar = false uiHelpers.updateVisibility(blogSectionImageBlogAvatar, state.blogSection.avatarOrBlavatarUrl != null) @@ -149,7 +149,7 @@ class ReaderPostNewViewHolder( blogSectionAvatarContainer.isVisible = isShowingAnyAvatar } - private fun updateFeaturedImage(state: ReaderPostNewUiState) = with(binding) { + private fun updateFeaturedImage(state: ReaderPostUiState) = with(binding) { uiHelpers.updateVisibility(imageFeatured, state.featuredImageVisibility) if (state.featuredImageUrl == null) { imageManager.cancelRequestAndClearImageView(imageFeatured) @@ -177,7 +177,7 @@ class ReaderPostNewViewHolder( } } - private fun loadVideoThumbnail(state: ReaderPostNewUiState) = with(binding) { + private fun loadVideoThumbnail(state: ReaderPostUiState) = with(binding) { /* TODO ideally, we'd be passing just a thumbnail url in the UiState. However, the code for retrieving thumbnail from full video URL needs to be fully refactored. */ state.fullVideoUrl?.let { videoUrl -> @@ -202,7 +202,7 @@ class ReaderPostNewViewHolder( } } - private fun renderMoreMenu(uiState: ReaderPostNewUiState, actions: List, v: View) { + private fun renderMoreMenu(uiState: ReaderPostUiState, actions: List, v: View) { readerTracker.track(AnalyticsTracker.Stat.POST_CARD_MORE_TAPPED) val listPopup = ListPopupWindow(v.context) listPopup.width = v.context.resources.getDimensionPixelSize(R.dimen.menu_item_width) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/viewholders/ReaderPostViewHolder.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/viewholders/ReaderPostViewHolder.kt deleted file mode 100644 index 7bb3be5dca5d..000000000000 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/discover/viewholders/ReaderPostViewHolder.kt +++ /dev/null @@ -1,225 +0,0 @@ -package org.wordpress.android.ui.reader.discover.viewholders - -import android.view.Gravity -import android.view.View -import android.view.ViewGroup -import androidx.appcompat.widget.ListPopupWindow -import org.wordpress.android.R -import org.wordpress.android.WordPress -import org.wordpress.android.analytics.AnalyticsTracker -import org.wordpress.android.databinding.ReaderCardviewPostBinding -import org.wordpress.android.datasets.ReaderThumbnailTable -import org.wordpress.android.ui.reader.adapters.ReaderMenuAdapter -import org.wordpress.android.ui.reader.discover.ReaderCardUiState -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState -import org.wordpress.android.ui.reader.discover.ReaderPostCardAction -import org.wordpress.android.ui.reader.discover.ReaderPostCardAction.PrimaryAction -import org.wordpress.android.ui.reader.tracker.ReaderTracker -import org.wordpress.android.ui.reader.utils.ReaderVideoUtils -import org.wordpress.android.ui.reader.utils.ReaderVideoUtils.VideoThumbnailUrlListener -import org.wordpress.android.ui.reader.views.ReaderIconCountView -import org.wordpress.android.ui.utils.UiHelpers -import org.wordpress.android.util.extensions.expandTouchTargetArea -import org.wordpress.android.util.extensions.getDrawableResIdFromAttribute -import org.wordpress.android.util.extensions.viewBinding -import org.wordpress.android.util.image.ImageManager -import org.wordpress.android.util.image.ImageType.BLAVATAR_CIRCULAR -import org.wordpress.android.util.image.ImageType.PHOTO_ROUNDED_CORNERS -import org.wordpress.android.util.image.ImageType.VIDEO - -class ReaderPostViewHolder( - private val uiHelpers: UiHelpers, - private val imageManager: ImageManager, - private val readerTracker: ReaderTracker, - parentView: ViewGroup -) : ReaderViewHolder(parentView.viewBinding(ReaderCardviewPostBinding::inflate)) { - init { - with(binding) { - layoutDiscover.expandTouchTargetArea(R.dimen.reader_discover_layout_extra_padding, true) - imageMore.expandTouchTargetArea(R.dimen.reader_more_image_extra_padding, false) - } - } - - override fun onBind(uiState: ReaderCardUiState) = with(binding) { - val state = uiState as ReaderPostUiState - - // Expandable tags section - uiHelpers.updateVisibility(expandableTagsView, state.expandableTagsViewVisibility) - expandableTagsView.updateUi(state.tagItems) - - // Blog section - updateBlogSection(state) - - // More menu - uiHelpers.updateVisibility(imageMore, state.moreMenuVisibility) - imageMore.setOnClickListener { uiState.onMoreButtonClicked.invoke(state) } - - // Featured image section - updateFeaturedImage(state) - uiHelpers.updateVisibility(imageVideoOverlay, state.videoOverlayVisibility) - uiHelpers.setTextOrHide(textPhotoTitle, state.photoTitle) - uiHelpers.updateVisibility(thumbnailStrip, state.thumbnailStripSection != null) - state.thumbnailStripSection?.let { - thumbnailStrip.loadThumbnails(it.images, it.isPrivate, it.content) - } - loadVideoThumbnail(state) - imageVideoOverlay.setOnClickListener { state.onVideoOverlayClicked(uiState.postId, uiState.blogId) } - - // Content section - uiHelpers.setTextOrHide(textTitle, state.title) - uiHelpers.setTextOrHide(textExcerpt, state.excerpt) - postContainer.setOnClickListener { - readerTracker.trackBlog( - AnalyticsTracker.Stat.READER_POST_CARD_TAPPED, - state.blogId, - state.feedId, - state.isFollowed, - state.source - ) - state.onItemClicked(uiState.postId, uiState.blogId) - } - - // Discover section - updateDiscoverSection(state) - - // Action buttons section - updateActionButton(uiState.postId, uiState.blogId, uiState.likeAction, countLikes) - updateActionButton(uiState.postId, uiState.blogId, uiState.reblogAction, reblog) - updateActionButton(uiState.postId, uiState.blogId, uiState.commentsAction, countComments) - updateActionButton(uiState.postId, uiState.blogId, uiState.bookmarkAction, bookmark) - - state.moreMenuItems?.let { - renderMoreMenu(state, state.moreMenuItems, imageMore) - } - - state.onItemRendered.invoke(uiState) - } - - private fun updateBlogSection(state: ReaderPostUiState) = with(binding.layoutBlogSection) { - updateAvatarOrBlavatar(state) - uiHelpers.setTextOrHide(textAuthorAndBlogName, state.blogSection.blogName) - uiHelpers.setTextOrHide(textBlogUrl, state.blogSection.blogUrl) - uiHelpers.updateVisibility(dotSeparator, state.blogSection.dotSeparatorVisibility) - uiHelpers.setTextOrHide(textDateline, state.blogSection.dateLine) - - root.setBackgroundResource( - root.context.getDrawableResIdFromAttribute( - state.blogSection.blogSectionClickData?.background ?: 0 - ) - ) - state.blogSection.blogSectionClickData?.onBlogSectionClicked?.let { - root.setOnClickListener { - state.blogSection.blogSectionClickData.onBlogSectionClicked.invoke() - } - } ?: run { - root.setOnClickListener(null) - root.isClickable = false - } - } - - private fun updateFeaturedImage(state: ReaderPostUiState) = with(binding) { - uiHelpers.updateVisibility(imageFeatured, state.featuredImageVisibility) - if (state.featuredImageUrl == null) { - imageManager.cancelRequestAndClearImageView(imageFeatured) - } else { - imageManager.loadImageWithCorners( - imageFeatured, - PHOTO_ROUNDED_CORNERS, - state.featuredImageUrl, - uiHelpers.getPxOfUiDimen(WordPress.getContext(), state.featuredImageCornerRadius) - ) - } - } - - private fun updateAvatarOrBlavatar(state: ReaderPostUiState) = with(binding.layoutBlogSection) { - uiHelpers.updateVisibility(imageAvatarOrBlavatar, state.blogSection.avatarOrBlavatarUrl != null) - if (state.blogSection.avatarOrBlavatarUrl == null) { - imageManager.cancelRequestAndClearImageView(imageAvatarOrBlavatar) - } else { - imageManager.loadIntoCircle( - imageAvatarOrBlavatar, - state.blogSection.blavatarType, state.blogSection.avatarOrBlavatarUrl - ) - } - - val canShowAuthorsAvatar = state.blogSection.authorAvatarUrl != null && state.blogSection.isAuthorAvatarVisible - uiHelpers.updateVisibility(authorsAvatar, canShowAuthorsAvatar) - - if (!canShowAuthorsAvatar) { - imageManager.cancelRequestAndClearImageView(authorsAvatar) - } else { - imageManager.loadIntoCircle(authorsAvatar, BLAVATAR_CIRCULAR, state.blogSection.authorAvatarUrl!!) - } - } - - private fun updateDiscoverSection(state: ReaderPostUiState) = with(binding) { - uiHelpers.updateVisibility(imageDiscoverAvatar, state.discoverSection?.discoverAvatarUrl != null) - uiHelpers.updateVisibility(layoutDiscover, state.discoverSection != null) - uiHelpers.setTextOrHide(textDiscover, state.discoverSection?.discoverText) - if (state.discoverSection?.discoverAvatarUrl == null) { - imageManager.cancelRequestAndClearImageView(imageDiscoverAvatar) - } else { - // TODO do we need to use `imagemanager.load` for blavatar? - imageManager.loadIntoCircle( - imageDiscoverAvatar, - state.discoverSection.imageType, - state.discoverSection.discoverAvatarUrl - ) - } - layoutDiscover.setOnClickListener { - state.discoverSection?.onDiscoverClicked?.invoke(state.postId, state.blogId) - } - } - - private fun updateActionButton(postId: Long, blogId: Long, state: PrimaryAction, view: View) { - if (view is ReaderIconCountView) { - view.setCount(state.count) - } - view.isEnabled = state.isEnabled - view.isSelected = state.isSelected - view.contentDescription = state.contentDescription?.let { uiHelpers.getTextOfUiString(view.context, it) } - view.setOnClickListener { state.onClicked?.invoke(postId, blogId, state.type) } - } - - private fun loadVideoThumbnail(state: ReaderPostUiState) = with(binding) { - /* TODO ideally, we'd be passing just a thumbnail url in the UiState. However, the code for retrieving - thumbnail from full video URL needs to be fully refactored. */ - state.fullVideoUrl?.let { videoUrl -> - ReaderVideoUtils.retrieveVideoThumbnailUrl(videoUrl, object : VideoThumbnailUrlListener { - override fun showThumbnail(thumbnailUrl: String) { - imageManager.loadImageWithCorners( - imageFeatured, - PHOTO_ROUNDED_CORNERS, - thumbnailUrl, - uiHelpers.getPxOfUiDimen(WordPress.getContext(), state.featuredImageCornerRadius) - ) - } - - override fun showPlaceholder() { - imageManager.load(imageFeatured, VIDEO) - } - - override fun cacheThumbnailUrl(thumbnailUrl: String) { - ReaderThumbnailTable.addThumbnail(state.postId, videoUrl, thumbnailUrl) - } - }) - } - } - - private fun renderMoreMenu(uiState: ReaderPostUiState, actions: List, v: View) { - readerTracker.track(AnalyticsTracker.Stat.POST_CARD_MORE_TAPPED) - val listPopup = ListPopupWindow(v.context) - listPopup.width = v.context.resources.getDimensionPixelSize(R.dimen.menu_item_width) - listPopup.setAdapter(ReaderMenuAdapter(v.context, uiHelpers, actions)) - listPopup.setDropDownGravity(Gravity.END) - listPopup.anchorView = v - listPopup.isModal = true - listPopup.setOnItemClickListener { _, _, position, _ -> - listPopup.dismiss() - val item = actions[position] - item.onClicked?.invoke(uiState.postId, uiState.blogId, item.type) - } - listPopup.setOnDismissListener { uiState.onMoreDismissed.invoke(uiState) } - listPopup.show() - } -} diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/viewmodels/tagsfeed/ReaderTagsFeedViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/reader/viewmodels/tagsfeed/ReaderTagsFeedViewModel.kt index 149db5634938..3f3edc1743f8 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/viewmodels/tagsfeed/ReaderTagsFeedViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/viewmodels/tagsfeed/ReaderTagsFeedViewModel.kt @@ -511,7 +511,7 @@ class ReaderTagsFeedViewModel @Inject constructor( val photonHeight = (photonWidth * FEATURED_IMAGE_HEIGHT_WIDTH_RATION).toInt() _openMoreMenuEvents.postValue( MoreMenuUiState( - readerCardUiState = readerPostUiStateBuilder.mapPostToNewUiState( + readerCardUiState = readerPostUiStateBuilder.mapPostToUiState( source = ReaderTracker.SOURCE_TAGS_FEED, post = post, photonWidth = photonWidth, @@ -625,7 +625,7 @@ class ReaderTagsFeedViewModel @Inject constructor( } data class MoreMenuUiState( - val readerCardUiState: ReaderCardUiState.ReaderPostNewUiState, + val readerCardUiState: ReaderCardUiState.ReaderPostUiState, val readerPostCardActions: List, ) diff --git a/WordPress/src/test/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverViewModelTest.kt index 40d918517bc8..c1fc2ec37b46 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverViewModelTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/reader/discover/ReaderDiscoverViewModelTest.kt @@ -31,7 +31,7 @@ import org.wordpress.android.models.discover.ReaderDiscoverCards import org.wordpress.android.ui.pages.SnackbarMessageHolder import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterestsCardUiState import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterestsCardUiState.ReaderInterestUiState -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostNewUiState +import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderRecommendedBlogsCardUiState import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderRecommendedBlogsCardUiState.ReaderRecommendedBlogUiState import org.wordpress.android.ui.reader.discover.ReaderDiscoverViewModel.DiscoverUiState @@ -154,7 +154,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { whenever(menuUiStateBuilder.buildMoreMenuItems(anyOrNull(), any(), anyOrNull())).thenReturn(mock()) whenever( - uiStateBuilder.mapPostToNewUiState( + uiStateBuilder.mapPostToUiState( source = anyString(), post = anyOrNull(), photonWidth = anyInt(), @@ -170,7 +170,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { moreMenuItems = anyOrNull() ) ).thenAnswer { - createDummyReaderPostNewUiState( + createDummyReaderPostUiState( it.getArgument(POST_PARAM_POSITION_NEW), it.getArgument(ON_ITEM_RENDERED_PARAM_POSITION_NEW), it.getArgument(ON_BUTTON_CLICKED_PARAM_POSITION_NEW), @@ -281,7 +281,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { val closeToEndIndex = NUMBER_OF_ITEMS.toInt() - INITIATE_LOAD_MORE_OFFSET init() // Act - ((viewModel.uiState.value as ContentUiState).cards[closeToEndIndex] as ReaderPostNewUiState).let { + ((viewModel.uiState.value as ContentUiState).cards[closeToEndIndex] as ReaderPostUiState).let { it.onItemRendered.invoke(it) } // Assert @@ -294,7 +294,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { val notCloseToEndIndex = 2 init() // Act - ((viewModel.uiState.value as ContentUiState).cards[notCloseToEndIndex] as ReaderPostNewUiState).let { + ((viewModel.uiState.value as ContentUiState).cards[notCloseToEndIndex] as ReaderPostUiState).let { it.onItemRendered.invoke(it) } // Assert @@ -334,18 +334,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { fakeDiscoverFeed.value = ReaderDiscoverCards(createDummyReaderPostCardList()) // Assert val contentUiState = uiStates[1] as ContentUiState - assertThat(contentUiState.cards.first()).isInstanceOf(ReaderPostNewUiState::class.java) - } - - @Test - fun `if ReaderPostCard exist then ReaderPostNewUiState will be present in the ContentUIState`() = test { - // Arrange - val uiStates = init(autoUpdateFeed = false).uiStates - // Act - fakeDiscoverFeed.value = ReaderDiscoverCards(createDummyReaderPostCardList()) - // Assert - val contentUiState = uiStates[1] as ContentUiState - assertThat(contentUiState.cards.first()).isInstanceOf(ReaderPostNewUiState::class.java) + assertThat(contentUiState.cards.first()).isInstanceOf(ReaderPostUiState::class.java) } @Test @@ -473,7 +462,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { // Arrange val uiStates = init().uiStates // Act - ((uiStates.last() as ContentUiState).cards[2] as ReaderPostNewUiState) + ((uiStates.last() as ContentUiState).cards[2] as ReaderPostUiState) .likeAction.onClicked!!.invoke(2, 200, LIKE) // Assert verify(readerPostCardActionsHandler).onAction( @@ -489,7 +478,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { // Arrange val uiStates = init().uiStates // Act - ((uiStates.last() as ContentUiState).cards[2] as ReaderPostNewUiState).onVideoOverlayClicked(2, 200) + ((uiStates.last() as ContentUiState).cards[2] as ReaderPostUiState).onVideoOverlayClicked(2, 200) // Assert verify(readerPostCardActionsHandler).handleVideoOverlayClicked( eq((fakeDiscoverFeed.value!!.cards[2] as ReaderPostCard).post.featuredVideo) @@ -501,7 +490,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { // Arrange val uiStates = init().uiStates // Act - ((uiStates.last() as ContentUiState).cards[2] as ReaderPostNewUiState) + ((uiStates.last() as ContentUiState).cards[2] as ReaderPostUiState) .onItemClicked.invoke(2, 200) // Assert verify(readerPostCardActionsHandler).handleOnItemClicked( @@ -514,24 +503,24 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { fun `When user clicks on more button menu is shown`() = test { // Arrange val uiStates = init().uiStates - val cardUiState = ((uiStates.last() as ContentUiState).cards[2] as ReaderPostNewUiState) + val cardUiState = ((uiStates.last() as ContentUiState).cards[2] as ReaderPostUiState) // Act cardUiState.onMoreButtonClicked.invoke(cardUiState) // Assert - assertThat(((uiStates.last() as ContentUiState).cards[2] as ReaderPostNewUiState).moreMenuItems).isNotNull + assertThat(((uiStates.last() as ContentUiState).cards[2] as ReaderPostUiState).moreMenuItems).isNotNull } @Test fun `When user dismisses the menu the ui state is updated`() = test { // Arrange val uiStates = init().uiStates - var cardUiState = ((uiStates.last() as ContentUiState).cards[2] as ReaderPostNewUiState) + var cardUiState = ((uiStates.last() as ContentUiState).cards[2] as ReaderPostUiState) // Act cardUiState.onMoreButtonClicked.invoke(cardUiState) // show menu - cardUiState = ((uiStates.last() as ContentUiState).cards[2] as ReaderPostNewUiState) + cardUiState = ((uiStates.last() as ContentUiState).cards[2] as ReaderPostUiState) cardUiState.onMoreDismissed.invoke(cardUiState) // dismiss menu // Assert - assertThat(((uiStates.last() as ContentUiState).cards[2] as ReaderPostNewUiState).moreMenuItems).isNull() + assertThat(((uiStates.last() as ContentUiState).cards[2] as ReaderPostUiState).moreMenuItems).isNull() } @Test @@ -621,7 +610,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { val closeToEndIndex = NUMBER_OF_ITEMS.toInt() - INITIATE_LOAD_MORE_OFFSET init() // Act - ((viewModel.uiState.value as ContentUiState).cards[closeToEndIndex] as ReaderPostNewUiState).let { + ((viewModel.uiState.value as ContentUiState).cards[closeToEndIndex] as ReaderPostUiState).let { it.onItemRendered.invoke(it) } fakeDiscoverFeed.value = createDummyReaderCardsList() @@ -647,7 +636,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { scrollToTopCounter.reset() // Act for load more - ((viewModel.uiState.value as ContentUiState).cards[closeToEndIndex] as ReaderPostNewUiState).let { + ((viewModel.uiState.value as ContentUiState).cards[closeToEndIndex] as ReaderPostUiState).let { it.onItemRendered.invoke(it) } fakeDiscoverFeed.value = createDummyReaderCardsList() @@ -739,23 +728,23 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { } @Suppress("LongParameterList") - private fun createDummyReaderPostNewUiState( + private fun createDummyReaderPostUiState( post: ReaderPost, onItemRendered: (ReaderCardUiState) -> Unit = mock(), onButtonClicked: (Long, Long, ReaderPostCardActionType) -> Unit, onVideoOverlayClicked: (Long, Long) -> Unit, postHeaderClicked: (Long, Long) -> Unit, onItemClicked: (Long, Long) -> Unit, - onMoreMenuClicked: (ReaderPostNewUiState) -> Unit, - onMoreMenuDismissed: (ReaderPostNewUiState) -> Unit - ): ReaderPostNewUiState { - return ReaderPostNewUiState( + onMoreMenuClicked: (ReaderPostUiState) -> Unit, + onMoreMenuDismissed: (ReaderPostUiState) -> Unit + ): ReaderPostUiState { + return ReaderPostUiState( source = "source", postId = post.postId, blogId = post.blogId, feedId = post.feedId, isFollowed = post.isFollowedByCurrentUser, - blogSection = ReaderPostNewUiState.CompactBlogSectionData( + blogSection = ReaderPostUiState.CompactBlogSectionData( post.postId, post.blogId, "", @@ -766,7 +755,7 @@ class ReaderDiscoverViewModelTest : BaseUnitTest() { blavatarType = BLAVATAR_CIRCULAR, postHeaderClicked, ), - interactionSection = ReaderPostNewUiState.InteractionSectionData( + interactionSection = ReaderPostUiState.InteractionSectionData( post.numLikes, post.numReplies, ), diff --git a/WordPress/src/test/java/org/wordpress/android/ui/reader/discover/ReaderPostUiStateBuilderTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/reader/discover/ReaderPostUiStateBuilderTest.kt index 2b907f35d5ae..a4fa6856162e 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/reader/discover/ReaderPostUiStateBuilderTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/reader/discover/ReaderPostUiStateBuilderTest.kt @@ -25,11 +25,6 @@ import org.wordpress.android.models.ReaderCardType.GALLERY import org.wordpress.android.models.ReaderCardType.PHOTO import org.wordpress.android.models.ReaderCardType.VIDEO import org.wordpress.android.models.ReaderPost -import org.wordpress.android.models.ReaderPostDiscoverData -import org.wordpress.android.models.ReaderPostDiscoverData.DiscoverType -import org.wordpress.android.models.ReaderPostDiscoverData.DiscoverType.EDITOR_PICK -import org.wordpress.android.models.ReaderPostDiscoverData.DiscoverType.OTHER -import org.wordpress.android.models.ReaderPostDiscoverData.DiscoverType.SITE_PICK import org.wordpress.android.models.ReaderTag import org.wordpress.android.models.ReaderTagList import org.wordpress.android.ui.Organization.NO_ORGANIZATION @@ -41,9 +36,7 @@ import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterest import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterestsCardUiState.ChipStyle.ChipStyleOrange import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterestsCardUiState.ChipStyle.ChipStylePurple import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderInterestsCardUiState.ChipStyle.ChipStyleYellow -import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostNewUiState import org.wordpress.android.ui.reader.discover.ReaderCardUiState.ReaderPostUiState -import org.wordpress.android.ui.reader.discover.ReaderPostCardActionType.BOOKMARK import org.wordpress.android.ui.reader.discover.ReaderPostCardActionType.LIKE import org.wordpress.android.ui.reader.discover.ReaderPostCardActionType.REBLOG import org.wordpress.android.ui.reader.models.ReaderImageList @@ -56,7 +49,6 @@ import org.wordpress.android.ui.utils.UiString.UiStringText import org.wordpress.android.util.DateTimeUtilsWrapper import org.wordpress.android.util.WPAvatarUtilsWrapper import org.wordpress.android.util.UrlUtilsWrapper -import org.wordpress.android.util.image.ImageType import java.util.Date @Suppress("LargeClass") @@ -84,9 +76,6 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { @Mock lateinit var readerUtilsWrapper: ReaderUtilsWrapper - @Mock - lateinit var readerPostTagsUiStateBuilder: ReaderPostTagsUiStateBuilder - @Before fun setUp() = test { builder = ReaderPostUiStateBuilder( @@ -96,7 +85,6 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { dateTimeUtilsWrapper, readerImageScannerProvider, readerUtilsWrapper, - readerPostTagsUiStateBuilder, testDispatcher() ) whenever(dateTimeUtilsWrapper.javaDateToTimeSpan(anyOrNull())).thenReturn("") @@ -106,7 +94,6 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { .thenReturn(imageScanner) whenever(imageScanner.getImageList(anyInt(), anyInt())).thenReturn(ReaderImageList(false)) whenever(accountStore.hasAccessToken()).thenReturn(true) - whenever(readerUtilsWrapper.getLongLikeLabelText(anyInt(), anyBoolean())).thenReturn("") } // endregion @@ -118,7 +105,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Act val uiState = mapPostToUiState(post, BLOG_PREVIEW) // Assert - assertThat(uiState.blogSection.blogSectionClickData).isNull() + assertThat(uiState.blogSection.onClicked).isNull() } @Test @@ -126,7 +113,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost() // Act - val uiState = mapPostToNewUiState(post, BLOG_PREVIEW) + val uiState = mapPostToUiState(post, BLOG_PREVIEW) // Assert assertThat(uiState.blogSection.onClicked).isNull() } @@ -139,7 +126,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Act val uiState = mapPostToUiState(post, it) // Assert - assertThat(uiState.blogSection.blogSectionClickData).isNotNull + assertThat(uiState.blogSection.onClicked).isNotNull } } @@ -149,7 +136,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { val post = createPost() ReaderPostListType.values().filter { it != BLOG_PREVIEW }.forEach { // Act - val uiState = mapPostToNewUiState(post, it) + val uiState = mapPostToUiState(post, it) // Assert assertThat(uiState.blogSection.onClicked).isNotNull } @@ -174,8 +161,8 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { val p2post = createPost(isp2Post = true) val nonP2Post = createPost(isp2Post = false) // Act - val p2UiState = mapPostToNewUiState(p2post) - val nonP2UiState = mapPostToNewUiState(nonP2Post) + val p2UiState = mapPostToUiState(p2post) + val nonP2UiState = mapPostToUiState(nonP2Post) // Assert assertThat(p2UiState.blogSection.isAuthorAvatarVisible).isTrue assertThat(nonP2UiState.blogSection.isAuthorAvatarVisible).isFalse @@ -183,95 +170,6 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // endregion - // region BLOG URL - @Test - fun `scheme is removed from blog url`() = test { - // Arrange - val post = createPost(blogUrl = "http://dummy.url") - whenever(urlUtilsWrapper.removeScheme("http://dummy.url")).thenReturn("dummy.url") - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.blogSection.blogUrl).isEqualTo("dummy.url") - } - // endregion - - // region DISCOVER SECTION - @Test - fun `discover section is empty when isDiscoverPost is false`() = test { - // Arrange - val post = createPost(isDiscoverPost = false) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.discoverSection).isNull() - } - - @Test - fun `discover section is not empty when isDiscoverPost is true`() = test { - // Arrange - val post = createPost(isDiscoverPost = true) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.discoverSection).isNotNull - } - - @Test - fun `discover section is empty when discoverType is OTHER`() = test { - // Arrange - val post = createPost(isDiscoverPost = true, discoverType = OTHER) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.discoverSection).isNull() - } - - @Test - fun `discover section is not empty when discoverType is other than OTHER`() = test { - // Arrange - DiscoverType.values().filter { it != OTHER }.forEach { - val post = createPost(isDiscoverPost = true, discoverType = it) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.discoverSection).isNotNull - } - } - - @Test - fun `discover uses ImageType AVATAR when EDITOR_PICK`() = test { - // Arrange - val post = createPost(isDiscoverPost = true, discoverType = EDITOR_PICK) - // Act - val uiState = mapPostToUiState(post) - // Assert - @Suppress("DEPRECATION") - assertThat(uiState.discoverSection!!.imageType).isEqualTo(ImageType.AVATAR) - } - - @Test - fun `discover uses ImageType BLAVATAR when SITE_PICK`() = test { - // Arrange - val post = createPost(isDiscoverPost = true, discoverType = SITE_PICK) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.discoverSection!!.imageType).isEqualTo(ImageType.BLAVATAR) - } - - @Test - fun `discover uses fixed avatar URL`() = test { - // Arrange - val post = createPost(isDiscoverPost = true) - whenever(avatarUtilsWrapper.rewriteAvatarUrlWithResource(anyOrNull(), anyInt())).thenReturn("12345") - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.discoverSection!!.discoverAvatarUrl).isEqualTo("12345") - } - // endregion - // region VIDEO @Test fun `videoUrl gets initialized for video cards`() = test { @@ -288,7 +186,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(cardType = VIDEO, featuredVideoUrl = "12345") // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.fullVideoUrl).isEqualTo("12345") } @@ -313,7 +211,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { types.filter { it != VIDEO }.forEach { val post = createPost(cardType = it, featuredVideoUrl = "12345") // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.fullVideoUrl).isNull() } @@ -334,7 +232,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(cardType = VIDEO) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.videoOverlayVisibility).isTrue } @@ -359,7 +257,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { types.filter { it != VIDEO }.forEach { val post = createPost(cardType = it) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.videoOverlayVisibility).isFalse() } @@ -382,7 +280,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(cardType = GALLERY) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.thumbnailStripSection).isNotNull } @@ -405,7 +303,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { ReaderCardType.values().filter { it != GALLERY }.forEach { val post = createPost(cardType = it) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.thumbnailStripSection).isNull() } @@ -433,7 +331,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { ReaderCardType.values().filter { it == PHOTO || it == DEFAULT }.forEach { val post = createPost(cardType = it, hasFeaturedImage = true, featuredImageUrlForDisplay = dummyUrl) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.featuredImageUrl).isEqualTo(dummyUrl) } @@ -457,7 +355,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { ReaderCardType.values().filter { it != PHOTO && it != DEFAULT }.forEach { val post = createPost(cardType = it, hasFeaturedImage = true) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.featuredImageUrl).isNull() } @@ -478,43 +376,9 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(cardType = PHOTO, hasFeaturedImage = false) // Act - val uiState = mapPostToNewUiState(post) - // Assert - assertThat(uiState.featuredImageUrl).isNull() - } - // endregion - - // region PHOTO TITLE - @Test - fun `photo title is displayed for photo card type`() = test { - // Arrange - val post = createPost(cardType = PHOTO) - // Act val uiState = mapPostToUiState(post) // Assert - assertThat(uiState.photoTitle).isNotNull - } - - @Test - fun `photo title is not displayed for other than photo card type`() = test { - // Arrange - ReaderCardType.values().filter { it != PHOTO }.forEach { - val post = createPost(cardType = it) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.photoTitle).isNull() - } - } - - @Test - fun `photo title is not displayed when hasTitle returns false`() = test { - // Arrange - val post = createPost(cardType = PHOTO, hasTitle = false) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.photoTitle).isNull() + assertThat(uiState.featuredImageUrl).isNull() } // endregion @@ -531,44 +395,24 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { } } - @Test - fun `title is not displayed for PHOTO card type`() = test { - // Arrange - val post = createPost(cardType = PHOTO) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.title).isNull() - } - @Test fun `title is displayed for all card types for new UI`() = test { // Arrange ReaderCardType.values().forEach { val post = createPost(cardType = it) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat((uiState.title as UiStringText).text).isEqualTo(post.title) } } - @Test - fun `default title is displayed when the post doesn't have a title`() = test { - // Arrange - val post = createPost(cardType = DEFAULT, hasTitle = false) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat((uiState.title as UiStringRes).stringRes).isEqualTo(R.string.untitled_in_parentheses) - } - @Test fun `title is not displayed when the post doesn't have a title for new UI`() = test { // Arrange val post = createPost(cardType = DEFAULT, hasTitle = false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.title).isNull() } @@ -585,23 +429,13 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { } } - @Test - fun `excerpt is not displayed for PHOTO card type`() = test { - // Arrange - val post = createPost(cardType = PHOTO) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.excerpt).isNull() - } - @Test fun `excerpt is displayed for all card types for new UI`() = test { // Arrange ReaderCardType.values().forEach { val post = createPost(cardType = it) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.excerpt).isNotNull() } @@ -622,7 +456,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(cardType = DEFAULT, hasExcerpt = false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.excerpt).isNull() } @@ -644,7 +478,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost() // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.blogSection.blogName).isNotNull } @@ -664,7 +498,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(hasBlogName = false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat((uiState.blogSection.blogName as UiStringRes).stringRes).isEqualTo(R.string.untitled_in_parentheses) } @@ -717,8 +551,8 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { authorName = "John Smith" ) // Act - val firstNameUiState = mapPostToNewUiState(postWithFirstName) - val fullNameUiState = mapPostToNewUiState(postWithoutFirstName) + val firstNameUiState = mapPostToUiState(postWithFirstName) + val fullNameUiState = mapPostToUiState(postWithoutFirstName) // Assert val firstNameBlog = firstNameUiState.blogSection.blogName as UiStringResWithParams assertThat(firstNameBlog.stringRes).isEqualTo(R.string.reader_author_with_blog_name) @@ -756,73 +590,9 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { whenever(post.getDisplayDate(dateTimeUtilsWrapper)).thenReturn(dummyDate) whenever(dateTimeUtilsWrapper.javaDateToTimeSpan(dummyDate)).thenReturn("success") // Act - val uiState = mapPostToNewUiState(post) - // Assert - assertThat(uiState.blogSection.dateLine).isEqualTo("success") - } - // endregion - - // region BOOKMARK BUTTON - @Test - fun `bookmark button is disabled when postId is empty`() = test { - // Arrange - val post = createPost(postId = 0) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.bookmarkAction.isEnabled).isFalse - } - - @Test - fun `bookmark button is disabled when blogId is empty`() = test { - // Arrange - val post = createPost(blogId = 0) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.bookmarkAction.isEnabled).isFalse - } - - @Test - fun `bookmark button is enabled when blogid and postId is not empty`() = test { - // Arrange - val post = createPost(postId = 1L, blogId = 2L) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.bookmarkAction.isEnabled).isTrue - } - - @Test - fun `bookmark button is selected when the post is bookmarked`() = test { - // Arrange - val post = createPost(isBookmarked = true) - // Act - val uiState = mapPostToUiState(post) - // Assert - assertThat(uiState.bookmarkAction.isSelected).isTrue - } - - @Test - fun `bookmark button is not selected when the post is not bookmarked`() = test { - // Arrange - val post = createPost(isBookmarked = false) - // Act val uiState = mapPostToUiState(post) // Assert - assertThat(uiState.bookmarkAction.isSelected).isFalse - } - - @Test - fun `onButtonClicked listener is correctly assigned to bookmarkAction`() = test { - // Arrange - val post = createPost() - val onButtonClicked: (Long, Long, ReaderPostCardActionType) -> Unit = mock() - val uiState = mapPostToUiState(post, onButtonClicked = onButtonClicked) - // Act - uiState.bookmarkAction.onClicked!!.invoke(1L, 1L, BOOKMARK) - // Assert - verify(onButtonClicked).invoke(1L, 1L, BOOKMARK) + assertThat(uiState.blogSection.dateLine).isEqualTo("success") } // endregion @@ -842,7 +612,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost() // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.likeAction.isEnabled).isTrue } @@ -864,7 +634,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { val post = createPost() whenever(accountStore.hasAccessToken()).thenReturn(false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.likeAction.isEnabled).isFalse } @@ -884,7 +654,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(isCanLikePost = false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.likeAction.isEnabled).isFalse } @@ -905,7 +675,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost() val onButtonClicked: (Long, Long, ReaderPostCardActionType) -> Unit = mock() - val uiState = mapPostToNewUiState(post, onButtonClicked = onButtonClicked) + val uiState = mapPostToUiState(post, onButtonClicked = onButtonClicked) // Act uiState.likeAction.onClicked!!.invoke(1L, 1L, LIKE) // Assert @@ -929,7 +699,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost() // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.reblogAction.isEnabled).isTrue } @@ -949,7 +719,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(isPrivate = true) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.reblogAction.isEnabled).isFalse } @@ -971,7 +741,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { val post = createPost() whenever(accountStore.hasAccessToken()).thenReturn(false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.reblogAction.isEnabled).isFalse } @@ -992,7 +762,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost() val onButtonClicked: (Long, Long, ReaderPostCardActionType) -> Unit = mock() - val uiState = mapPostToNewUiState(post, onButtonClicked = onButtonClicked) + val uiState = mapPostToUiState(post, onButtonClicked = onButtonClicked) // Act uiState.reblogAction.onClicked!!.invoke(1L, 1L, REBLOG) // Assert @@ -1016,7 +786,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost() // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.commentsAction.isEnabled).isTrue } @@ -1036,7 +806,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(isCommentsOpen = false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.commentsAction.isEnabled).isFalse } @@ -1056,7 +826,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(isWPCom = false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.commentsAction.isEnabled).isFalse } @@ -1079,7 +849,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { val post = createPost(numOfReplies = 0) whenever(accountStore.hasAccessToken()).thenReturn(false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.commentsAction.isEnabled).isFalse } @@ -1101,7 +871,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { val post = createPost(numOfReplies = 1) whenever(accountStore.hasAccessToken()).thenReturn(false) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.commentsAction.isEnabled).isTrue } @@ -1121,7 +891,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { // Arrange val post = createPost(isDiscoverPost = true) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.commentsAction.isEnabled).isFalse } @@ -1145,7 +915,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { val numReplies = 15 val post = createPost(numOfReplies = numReplies) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.interactionSection.commentCount).isEqualTo(numReplies) } @@ -1156,7 +926,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { val numLikes = 15 val post = createPost(numOfLikes = numLikes) // Act - val uiState = mapPostToNewUiState(post) + val uiState = mapPostToUiState(post) // Assert assertThat(uiState.interactionSection.likeCount).isEqualTo(numLikes) } @@ -1281,31 +1051,7 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { postListType: ReaderPostListType = TAG_FOLLOWED, onButtonClicked: (Long, Long, ReaderPostCardActionType) -> Unit = mock() ): ReaderPostUiState { - return builder.mapPostToUiStateBlocking( - source = "source", - post = post, - isDiscover = false, - photonWidth = 0, - photonHeight = 0, - postListType = postListType, - onButtonClicked = onButtonClicked, - onItemClicked = mock(), - onItemRendered = mock(), - onDiscoverSectionClicked = mock(), - onMoreButtonClicked = mock(), - onVideoOverlayClicked = mock(), - onPostHeaderViewClicked = mock(), - onTagItemClicked = mock(), - onMoreDismissed = mock() - ) - } - - private suspend fun mapPostToNewUiState( - post: ReaderPost, - postListType: ReaderPostListType = TAG_FOLLOWED, - onButtonClicked: (Long, Long, ReaderPostCardActionType) -> Unit = mock() - ): ReaderPostNewUiState { - return builder.mapPostToNewUiState( + return builder.mapPostToUiState( source = "source", post = post, photonWidth = 0, @@ -1327,7 +1073,6 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { hasBlogName: Boolean = true, blogUrl: String = "", isDiscoverPost: Boolean = false, - discoverType: DiscoverType = SITE_PICK, cardType: ReaderCardType = DEFAULT, featuredVideoUrl: String? = null, postId: Long = 1L, @@ -1357,13 +1102,6 @@ class ReaderPostUiStateBuilderTest : BaseUnitTest() { }) // The ReaderPost contains business logic and accesses static classes. Using spy() allows us to use it in tests. whenever(post.isDiscoverPost).thenReturn(isDiscoverPost) - if (isDiscoverPost) { - val mockedDiscoverData: ReaderPostDiscoverData = mock() - whenever(post.discoverData).thenReturn(mockedDiscoverData) - whenever(mockedDiscoverData.discoverType).thenReturn(discoverType) - whenever(mockedDiscoverData.attributionHtml).thenReturn(mock()) - whenever(mockedDiscoverData.avatarUrl).thenReturn("dummyUrl") - } post.numReplies = numOfReplies post.numLikes = numOfLikes post.isPrivate = isPrivate