Skip to content

Commit

Permalink
Apply PR suggestion: remove old UI logic from ReaderPostAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
RenanLukas committed Jun 10, 2024
1 parent bba2471 commit 76aa4c3
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 880 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Long, Long, ReaderPostCardActionType, Unit> onButtonClicked =
(postId, blogId, type) -> {
mOnPostListItemButtonListener.onButtonClicked(post, type);
renderPost(position, holder, false);
return Unit.INSTANCE;
};
Function2<Long, Long, Unit> onItemClicked = (postId, blogId) -> {
if (mPostSelectedListener != null) {
mPostSelectedListener.onPostSelected(post);
}
return Unit.INSTANCE;
};
Function1<ReaderCardUiState, Unit> 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<Long, Long, Unit> 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<ReaderPostUiState, Unit> onMoreButtonClicked = (uiState) -> {
renderPost(position, holder, true);
return Unit.INSTANCE;
};

Function1<ReaderPostUiState, Unit> onMoreDismissed = (uiState) -> {
renderPost(position, holder, false);
return Unit.INSTANCE;
};

Function2<Long, Long, Unit> onVideoOverlayClicked = (postId, blogId) -> {
ReaderActivityLauncher.showReaderVideoViewer(ctx, post.getFeaturedVideo());
return Unit.INSTANCE;
};

Function0<Unit> onPostHeaderClicked = () -> {
ReaderActivityLauncher.showReaderBlogPreview(
ctx,
post,
mSource,
mReaderTracker
);
return Unit.INSTANCE;
};

Function1<String, Unit> 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);
Expand Down Expand Up @@ -619,12 +505,12 @@ private void renderPostNew(final int position, final ReaderPostNewViewHolder hol
}
return Unit.INSTANCE;
};
Function1<ReaderPostNewUiState, Unit> onMoreButtonClicked = (uiState) -> {
Function1<ReaderPostUiState, Unit> onMoreButtonClicked = (uiState) -> {
renderPostNew(position, holder, true);
return Unit.INSTANCE;
};

Function1<ReaderPostNewUiState, Unit> onMoreDismissed = (uiState) -> {
Function1<ReaderPostUiState, Unit> onMoreDismissed = (uiState) -> {
renderPostNew(position, holder, false);
return Unit.INSTANCE;
};
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,68 +1,20 @@
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
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<TagUiState>,
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<ReaderPostCardAction>? = 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,
Expand All @@ -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,
Expand All @@ -84,9 +36,9 @@ sealed class ReaderCardUiState {
val commentsAction: PrimaryAction,
val moreMenuItems: List<ReaderPostCardAction>? = 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(
Expand All @@ -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<ReaderInterestUiState>) : ReaderCardUiState() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -35,7 +32,6 @@ class ReaderDiscoverAdapter(
private val items = mutableListOf<ReaderCardUiState>()
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,
Expand Down Expand Up @@ -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
Expand All @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -233,7 +233,7 @@ class ReaderDiscoverViewModel @Inject constructor(
private suspend fun convertCardsToUiStates(posts: ReaderDiscoverCards): List<ReaderCardUiState> {
return posts.cards.map { card ->
when (card) {
is ReaderPostCard -> postUiStateBuilder.mapPostToNewUiState(
is ReaderPostCard -> postUiStateBuilder.mapPostToUiState(
source = ReaderTracker.SOURCE_DISCOVER,
post = card.post,
photonWidth = photonWidth,
Expand Down Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit 76aa4c3

Please sign in to comment.