Skip to content

Commit

Permalink
Remove instant voting. Fixes #123 (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines authored Mar 13, 2022
1 parent c329c60 commit 3227d9b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 90 deletions.
63 changes: 0 additions & 63 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.util.Patterns
import android.widget.Toast
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.material.ScaffoldState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.Stable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
Expand Down Expand Up @@ -294,68 +293,6 @@ fun unreadCountTotal(unreads: GetUnreadCountResponse): Int {
return unreads.mentions + unreads.private_messages + unreads.replies
}

fun handleInstantUpvote(
myVote: MutableState<Int?>,
score: MutableState<Int>,
upvotes: MutableState<Int>,
downvotes: MutableState<Int>
) {
val newVote = if (myVote.value == 1) {
0
} else {
1
}

when (myVote.value) {
1 -> {
score.value--
upvotes.value--
}
-1 -> {
downvotes.value--
upvotes.value++
score.value += 2
}
else -> {
upvotes.value++
score.value++
}
}

myVote.value = newVote
}

fun handleInstantDownvote(
myVote: MutableState<Int?>,
score: MutableState<Int>,
upvotes: MutableState<Int>,
downvotes: MutableState<Int>
) {
val newVote = if (myVote.value == -1) {
0
} else {
-1
}

when (myVote.value) {
1 -> {
score.value -= 2
upvotes.value--
downvotes.value++
}
-1 -> {
downvotes.value--
score.value++
}
else -> {
downvotes.value++
score.value--
}
}

myVote.value = newVote
}

fun appendMarkdownImage(text: String, url: String): String {
return "$text\n\n![]($url)"
}
Expand Down
28 changes: 13 additions & 15 deletions app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ fun CommentNode(
val commentView = node.commentView

// These are necessary for instant comment voting
val score = remember { mutableStateOf(node.commentView.counts.score) }
val myVote = remember { mutableStateOf(node.commentView.my_vote) }
val upvotes = remember { mutableStateOf(node.commentView.counts.upvotes) }
val downvotes = remember { mutableStateOf(node.commentView.counts.downvotes) }
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) }

Expand Down Expand Up @@ -166,8 +166,8 @@ fun CommentNode(
CommentNodeHeader(
commentView = commentView,
onPersonClick = onPersonClick,
score = score.value,
myVote = myVote.value,
score = score,
myVote = myVote,
isModerator = isModerator(commentView.creator, moderators),
onLongClick = {
expanded = !expanded
Expand All @@ -184,11 +184,9 @@ fun CommentNode(
CommentFooterLine(
commentView = commentView,
onUpvoteClick = {
handleInstantUpvote(myVote, score, upvotes, downvotes)
onUpvoteClick(it)
},
onDownvoteClick = {
handleInstantDownvote(myVote, score, upvotes, downvotes)
onDownvoteClick(it)
},
onViewSourceClick = {
Expand All @@ -201,9 +199,9 @@ fun CommentNode(
onReportClick = onReportClick,
onBlockCreatorClick = onBlockCreatorClick,
showRead = showRead,
myVote = myVote.value,
upvotes = upvotes.value,
downvotes = downvotes.value,
myVote = myVote,
upvotes = upvotes,
downvotes = downvotes,
account = account,
)
}
Expand Down Expand Up @@ -346,7 +344,7 @@ fun CommentFooterLine(
)
if (showRead) {
ActionBarButton(
icon = Icons.Default.Check,
icon = Icons.Filled.Check,
onClick = { onMarkAsReadClick(commentView) },
contentColor = if (commentView.comment.read) {
Color.Green
Expand All @@ -357,7 +355,7 @@ fun CommentFooterLine(
)
}
ActionBarButton(
icon = Icons.Default.StarOutline,
icon = Icons.Filled.Star,
onClick = { onSaveClick(commentView) },
contentColor = if (commentView.saved) {
Color.Yellow
Expand All @@ -369,13 +367,13 @@ fun CommentFooterLine(
// Don't let you respond to your own comment.
if (commentView.creator.id != account?.id) {
ActionBarButton(
icon = Icons.Default.Reply,
icon = Icons.Filled.Reply,
onClick = { onReplyClick(commentView) },
account = account,
)
}
ActionBarButton(
icon = Icons.Default.MoreVert,
icon = Icons.Filled.MoreVert,
account = account,
onClick = { showMoreOptions = !showMoreOptions }
)
Expand Down
19 changes: 8 additions & 11 deletions app/src/main/java/com/jerboa/ui/components/post/PostListing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,10 @@ fun PostFooterLine(
account: Account?,
) {

// These are necessary for instant post voting
val score = remember { mutableStateOf(postView.counts.score) }
val myVote = remember { mutableStateOf(postView.my_vote) }
val upvotes = remember { mutableStateOf(postView.counts.upvotes) }
val downvotes = remember { mutableStateOf(postView.counts.downvotes) }
val score = postView.counts.score
val myVote = postView.my_vote
val upvotes = postView.counts.upvotes
val downvotes = postView.counts.downvotes

var showMoreOptions by remember { mutableStateOf(false) }

Expand Down Expand Up @@ -342,23 +341,21 @@ fun PostFooterLine(
horizontalArrangement = Arrangement.spacedBy(XXL_PADDING)
) {
VoteGeneric(
myVote = myVote.value,
votes = upvotes.value,
myVote = myVote,
votes = upvotes,
item = postView,
type = VoteType.Upvote,
onVoteClick = {
handleInstantUpvote(myVote, score, upvotes, downvotes)
onUpvoteClick(it)
},
account = account,
)
VoteGeneric(
myVote = myVote.value,
votes = downvotes.value,
myVote = myVote,
votes = downvotes,
item = postView,
type = VoteType.Downvote,
onVoteClick = {
handleInstantDownvote(myVote, score, upvotes, downvotes)
onDownvoteClick(it)
},
account = account,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class PostViewModel : ViewModel() {
likeCommentRoutine(
commentView = mutableStateOf(commentView),
voteType = voteType,
// An edge case, but don't pass in comments, otherwise a resort will occur
comments = comments,
account = account,
ctx = ctx,
scope = viewModelScope,
Expand Down

0 comments on commit 3227d9b

Please sign in to comment.