Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazycolumn 2 #223

Merged
merged 2 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 82 additions & 81 deletions app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.compose.animation.expandVertically
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.AlertDialog
import androidx.compose.material.Divider
Expand Down Expand Up @@ -102,15 +103,16 @@ fun CommentBodyPreview() {
CommentBody(commentView = sampleCommentView, viewSource = false)
}

@Composable
fun CommentNode(
fun LazyListScope.commentNodeItem(
node: CommentNodeData,
isExpanded: (commentId: Int) -> Boolean,
toggleExpanded: (commentId: Int) -> Unit,
moderators: List<CommunityModeratorView>,
onUpvoteClick: (commentView: CommentView) -> Unit,
onDownvoteClick: (commentView: CommentView) -> Unit,
onReplyClick: (commentView: CommentView) -> Unit,
onSaveClick: (commentView: CommentView) -> Unit,
onMarkAsReadClick: (commentView: CommentView) -> Unit = {},
onMarkAsReadClick: (commentView: CommentView) -> Unit,
onEditCommentClick: (commentView: CommentView) -> Unit,
onDeleteCommentClick: (commentView: CommentView) -> Unit,
onPersonClick: (personId: Int) -> Unit,
Expand All @@ -122,106 +124,106 @@ fun CommentNode(
showRead: Boolean = false,
account: Account?
) {
val offset = calculateCommentOffset(node.depth, 4)

// The ones with a border on the left need a little extra padding
val offset2 = if (node.depth == null) {
LARGE_PADDING
} else {
XXL_PADDING
}
val borderColor = calculateBorderColor(node.depth)
val commentView = node.commentView
val commentId = commentView.comment.id

// These are necessary for instant comment voting
val score = node.commentView.counts.score
val myVote = node.commentView.my_vote
val upvotes = node.commentView.counts.upvotes
val downvotes = node.commentView.counts.downvotes

var expanded by remember { mutableStateOf(true) }
val offset = calculateCommentOffset(node.depth, 4) // The ones with a border on
val offset2 = if (node.depth == null) {
LARGE_PADDING
} else {
XXL_PADDING
}

var viewSource by remember { mutableStateOf(false) }
item {
var viewSource by remember { mutableStateOf(false) }

val border = Border(SMALL_PADDING, borderColor)
val backgroundColor = MaterialTheme.colors.background
val borderColor = calculateBorderColor(backgroundColor, node.depth)
val border = Border(SMALL_PADDING, borderColor)

Column(
modifier = Modifier
.padding(
start = offset
)
) {
Divider()
Column(
modifier = Modifier.border(start = border)
modifier = Modifier
.padding(
start = offset
)
) {
Divider()
Column(
modifier = Modifier.padding(start = offset2, end = LARGE_PADDING)
modifier = Modifier.border(start = border)
) {
if (showPostAndCommunityContext) {
PostAndCommunityContextHeader(
commentView = commentView,
onCommunityClick = onCommunityClick,
onPostClick = onPostClick
)
}
CommentNodeHeader(
commentView = commentView,
onPersonClick = onPersonClick,
score = score,
myVote = myVote,
isModerator = isModerator(commentView.creator, moderators),
onLongClick = {
expanded = !expanded
}
)
AnimatedVisibility(
visible = expanded,
enter = expandVertically(),
exit = shrinkVertically()
Column(
modifier = Modifier.padding(start = offset2, end = LARGE_PADDING)
) {
Column {
CommentBody(
if (showPostAndCommunityContext) {
PostAndCommunityContextHeader(
commentView = commentView,
viewSource = viewSource
)
CommentFooterLine(
commentView = commentView,
onUpvoteClick = {
onUpvoteClick(it)
},
onDownvoteClick = {
onDownvoteClick(it)
},
onViewSourceClick = {
viewSource = !viewSource
},
onEditCommentClick = onEditCommentClick,
onDeleteCommentClick = onDeleteCommentClick,
onReplyClick = onReplyClick,
onSaveClick = onSaveClick,
onMarkAsReadClick = onMarkAsReadClick,
onReportClick = onReportClick,
onBlockCreatorClick = onBlockCreatorClick,
showRead = showRead,
myVote = myVote,
upvotes = upvotes,
downvotes = downvotes,
account = account
onCommunityClick = onCommunityClick,
onPostClick = onPostClick
)
}
CommentNodeHeader(
commentView = commentView,
onPersonClick = onPersonClick,
score = score,
myVote = myVote,
isModerator = isModerator(commentView.creator, moderators),
onLongClick = {
toggleExpanded(commentId)
}
)
AnimatedVisibility(
visible = isExpanded(commentId),
enter = expandVertically(),
exit = shrinkVertically()
) {
Column {
CommentBody(
commentView = commentView,
viewSource = viewSource
)
CommentFooterLine(
commentView = commentView,
onUpvoteClick = {
onUpvoteClick(it)
},
onDownvoteClick = {
onDownvoteClick(it)
},
onViewSourceClick = {
viewSource = !viewSource
},
onEditCommentClick = onEditCommentClick,
onDeleteCommentClick = onDeleteCommentClick,
onReplyClick = onReplyClick,
onSaveClick = onSaveClick,
onMarkAsReadClick = onMarkAsReadClick,
onReportClick = onReportClick,
onBlockCreatorClick = onBlockCreatorClick,
showRead = showRead,
myVote = myVote,
upvotes = upvotes,
downvotes = downvotes,
account = account
)
}
}
}
}
}
}
AnimatedVisibility(
visible = expanded,
enter = expandVertically(),
exit = shrinkVertically()
) {

if (isExpanded(commentId)) {
node.children?.also { nodes ->
CommentNodes(
commentNodeItems(
nodes = nodes,
toggleExpanded = toggleExpanded,
isExpanded = isExpanded,
onUpvoteClick = onUpvoteClick,
onDownvoteClick = onDownvoteClick,
onSaveClick = onSaveClick,
Expand Down Expand Up @@ -510,10 +512,9 @@ fun CommentOptionsDialogPreview() {
)
}

@Composable
fun calculateBorderColor(depth: Int?): Color {
fun calculateBorderColor(defaultBackground: Color, depth: Int?): Color {
return if (depth == null) {
MaterialTheme.colors.background
defaultBackground
} else {
colorList[depth.mod(colorList.size)]
}
Expand Down
104 changes: 81 additions & 23 deletions app/src/main/java/com/jerboa/ui/components/comment/CommentNodes.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.jerboa.ui.components.comment

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import com.jerboa.CommentNodeData
import com.jerboa.datatypes.*
import com.jerboa.db.Account
Expand All @@ -26,27 +29,82 @@ fun CommentNodes(
showPostAndCommunityContext: Boolean = false,
showRead: Boolean = false
) {
Column {
nodes.forEach { node ->
CommentNode(
node = node,
onUpvoteClick = onUpvoteClick,
onDownvoteClick = onDownvoteClick,
onReplyClick = onReplyClick,
onSaveClick = onSaveClick,
account = account,
moderators = moderators,
onMarkAsReadClick = onMarkAsReadClick,
onPersonClick = onPersonClick,
onCommunityClick = onCommunityClick,
onPostClick = onPostClick,
onEditCommentClick = onEditCommentClick,
onDeleteCommentClick = onDeleteCommentClick,
onReportClick = onReportClick,
onBlockCreatorClick = onBlockCreatorClick,
showPostAndCommunityContext = showPostAndCommunityContext,
showRead = showRead
)
}
// Holds the un-expanded comment ids
val unExpandedComments = remember { mutableStateListOf<Int>() }

LazyColumn {
commentNodeItems(
nodes = nodes,
isExpanded = { commentId -> !unExpandedComments.contains(commentId) },
toggleExpanded = { commentId ->
if (unExpandedComments.contains(commentId)) {
unExpandedComments.remove(commentId)
} else {
unExpandedComments.add(commentId)
}
},
onUpvoteClick = onUpvoteClick,
onDownvoteClick = onDownvoteClick,
onReplyClick = onReplyClick,
onSaveClick = onSaveClick,
account = account,
moderators = moderators,
onMarkAsReadClick = onMarkAsReadClick,
onPersonClick = onPersonClick,
onCommunityClick = onCommunityClick,
onPostClick = onPostClick,
onEditCommentClick = onEditCommentClick,
onDeleteCommentClick = onDeleteCommentClick,
onReportClick = onReportClick,
onBlockCreatorClick = onBlockCreatorClick,
showPostAndCommunityContext = showPostAndCommunityContext,
showRead = showRead
)
}
}

fun LazyListScope.commentNodeItems(
nodes: List<CommentNodeData>,
isExpanded: (commentId: Int) -> Boolean,
toggleExpanded: (commentId: Int) -> Unit,
onUpvoteClick: (commentView: CommentView) -> Unit,
onDownvoteClick: (commentView: CommentView) -> Unit,
onReplyClick: (commentView: CommentView) -> Unit,
onSaveClick: (commentView: CommentView) -> Unit,
onMarkAsReadClick: (commentView: CommentView) -> Unit,
onEditCommentClick: (commentView: CommentView) -> Unit,
onDeleteCommentClick: (commentView: CommentView) -> Unit,
onReportClick: (commentView: CommentView) -> Unit,
onPersonClick: (personId: Int) -> Unit,
onCommunityClick: (community: CommunitySafe) -> Unit,
onBlockCreatorClick: (creator: PersonSafe) -> Unit,
onPostClick: (postId: Int) -> Unit,
account: Account? = null,
moderators: List<CommunityModeratorView>,
showPostAndCommunityContext: Boolean = false,
showRead: Boolean = false
) {
nodes.forEach { node ->
commentNodeItem(
node = node,
isExpanded = isExpanded,
toggleExpanded = toggleExpanded,
onUpvoteClick = onUpvoteClick,
onDownvoteClick = onDownvoteClick,
onReplyClick = onReplyClick,
onSaveClick = onSaveClick,
account = account,
moderators = moderators,
onMarkAsReadClick = onMarkAsReadClick,
onPersonClick = onPersonClick,
onCommunityClick = onCommunityClick,
onPostClick = onPostClick,
onEditCommentClick = onEditCommentClick,
onDeleteCommentClick = onDeleteCommentClick,
onReportClick = onReportClick,
onBlockCreatorClick = onBlockCreatorClick,
showPostAndCommunityContext = showPostAndCommunityContext,
showRead = showRead
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ fun Modifier.simpleVerticalScrollbar(
): Modifier {
val targetAlpha = if (state.isScrollInProgress) 0.5f else 0f
val duration = if (state.isScrollInProgress) 150 else 500
val color = MaterialTheme.colors.onBackground

val alpha by animateFloatAsState(
targetValue = targetAlpha,
Expand All @@ -468,7 +469,7 @@ fun Modifier.simpleVerticalScrollbar(
val scrollbarHeight = state.layoutInfo.visibleItemsInfo.size * elementHeight

drawRect(
color = Color.LightGray,
color = color,
topLeft = Offset(this.size.width - width.toPx(), scrollbarOffsetY),
size = Size(width.toPx(), scrollbarHeight),
alpha = alpha
Expand Down
Loading