Skip to content

Commit

Permalink
Adding additional vote display modes (#1378)
Browse files Browse the repository at this point in the history
* Adding additional vote display modes

- Also removing redundant score counts from comment and post footers.
- Context: #1372

* Adding a threshold.

* Using label for comment count.

* Refactor to pass in instantscores.

* Fixing format percent.
  • Loading branch information
dessalines authored Feb 23, 2024
1 parent 1927d66 commit 61e384b
Show file tree
Hide file tree
Showing 23 changed files with 434 additions and 230 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/com/jerboa/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ package com.jerboa

const val DEBOUNCE_DELAY = 1000L
const val MAX_POST_TITLE_LENGTH = 200

/**
* Hides the downvote or percentage, if below this threshold
*/
const val SHOW_UPVOTE_PCT_THRESHOLD = 0.9F
const val VIEW_VOTES_LIMIT = 40L
9 changes: 9 additions & 0 deletions app/src/main/java/com/jerboa/datatypes/Others.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ data class PostFeatureData(
val featured: Boolean,
)

// TODO this should be got rid of after https://github.com/LemmyNet/lemmy/issues/4449
enum class VoteDisplayMode {
Full,
ScoreAndUpvotePercentage,
UpvotePercentage,
Score,
HideAll,
}

/**
* Says which type of users can view which bottom app bar tabs.
*/
Expand Down
38 changes: 37 additions & 1 deletion app/src/main/java/com/jerboa/feat/Voting.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jerboa.feat

import com.jerboa.datatypes.VoteDisplayMode

enum class VoteType(val value: Int) {
Upvote(1),
Downvote(-1),
Expand All @@ -19,7 +21,8 @@ data class InstantScores(
val newVote = newVote(this.myVote, voteAction)
// get original (up/down)votes, add (up/down)vote if (up/down)voted
val upvotes = this.upvotes - (if (this.myVote == 1) 1 else 0) + (if (newVote == 1) 1 else 0)
val downvotes = this.downvotes - (if (this.myVote == -1) 1 else 0) + (if (newVote == -1) 1 else 0)
val downvotes =
this.downvotes - (if (this.myVote == -1) 1 else 0) + (if (newVote == -1) 1 else 0)

return InstantScores(
myVote = newVote,
Expand All @@ -28,10 +31,43 @@ data class InstantScores(
score = upvotes - downvotes,
)
}

fun scoreOrPctStr(voteDisplayMode: VoteDisplayMode): String? {
return scoreOrPctStr(
score = score,
upvotes = upvotes,
downvotes = downvotes,
voteDisplayMode = voteDisplayMode,
)
}
}

// Set myVote to given action unless it was already set to that action, in which case we reset to 0
fun newVote(
oldVote: Int,
voteAction: VoteType,
): Int = if (voteAction.value == oldVote) 0 else voteAction.value

fun upvotePercent(
upvotes: Long,
downvotes: Long,
): Float {
return (upvotes.toFloat() / (upvotes + downvotes))
}

fun formatPercent(pct: Float): String {
return "%.0f%%".format(pct * 100F)
}

private fun scoreOrPctStr(
score: Long,
upvotes: Long,
downvotes: Long,
voteDisplayMode: VoteDisplayMode,
): String? {
return when (voteDisplayMode) {
VoteDisplayMode.UpvotePercentage -> formatPercent(upvotePercent(upvotes, downvotes))
VoteDisplayMode.HideAll -> null
else -> score.toString()
}
}
27 changes: 22 additions & 5 deletions app/src/main/java/com/jerboa/model/SiteViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.jerboa.api.API
import com.jerboa.api.ApiState
import com.jerboa.api.DEFAULT_INSTANCE
import com.jerboa.api.toApiState
import com.jerboa.datatypes.VoteDisplayMode
import com.jerboa.db.entity.AnonAccount
import com.jerboa.db.entity.isAnon
import com.jerboa.db.repository.AccountRepository
Expand Down Expand Up @@ -130,7 +131,8 @@ class SiteViewModel(private val accountRepository: AccountRepository) : ViewMode
viewModelScope.launch {
viewModelScope.launch {
unreadAppCountRes = ApiState.Loading
unreadAppCountRes = API.getInstance().getUnreadRegistrationApplicationCount().toApiState()
unreadAppCountRes =
API.getInstance().getUnreadRegistrationApplicationCount().toApiState()
}
}
}
Expand Down Expand Up @@ -199,7 +201,10 @@ class SiteViewModel(private val accountRepository: AccountRepository) : ViewMode

fun showAvatar(): Boolean {
return when (val res = siteRes) {
is ApiState.Success -> res.data.my_user?.local_user_view?.local_user?.show_avatars ?: true
is ApiState.Success ->
res.data.my_user?.local_user_view?.local_user?.show_avatars
?: true

else -> true
}
}
Expand All @@ -211,10 +216,22 @@ class SiteViewModel(private val accountRepository: AccountRepository) : ViewMode
}
}

fun showScores(): Boolean {
// TODO this should probably be persisted rather than waited for
// For the current default, just use FullScores
fun voteDisplayMode(): VoteDisplayMode {
val defaultMode = VoteDisplayMode.Full
return when (val res = siteRes) {
is ApiState.Success -> res.data.my_user?.local_user_view?.local_user?.show_scores ?: true
else -> true
is ApiState.Success ->
res.data.my_user?.let { mui ->
if (mui.local_user_view.local_user.show_scores) {
defaultMode
} else {
VoteDisplayMode.HideAll
}
} ?: run {
defaultMode
}
else -> defaultMode
}
}

Expand Down
42 changes: 19 additions & 23 deletions app/src/main/java/com/jerboa/ui/components/comment/CommentNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import com.jerboa.border
import com.jerboa.buildCommentsTree
import com.jerboa.calculateCommentOffset
import com.jerboa.datatypes.BanFromCommunityData
import com.jerboa.datatypes.VoteDisplayMode
import com.jerboa.datatypes.getContent
import com.jerboa.datatypes.sampleCommentView
import com.jerboa.datatypes.sampleCommunity
Expand Down Expand Up @@ -98,19 +99,18 @@ import it.vercruysse.lemmyapi.v0x19.datatypes.PostId
fun CommentNodeHeader(
commentView: CommentView,
onPersonClick: (personId: PersonId) -> Unit,
score: Long,
myVote: Int,
instantScores: InstantScores,
collapsedCommentsCount: Long,
isExpanded: Boolean,
onClick: () -> Unit,
onLongClick: () -> Unit,
showAvatar: Boolean,
showScores: Boolean,
voteDisplayMode: VoteDisplayMode,
) {
CommentOrPostNodeHeader(
creator = commentView.creator,
score = score,
myVote = myVote,
instantScores = instantScores,
voteDisplayMode = voteDisplayMode,
published = commentView.comment.published,
updated = commentView.comment.updated,
deleted = commentView.comment.deleted,
Expand All @@ -122,7 +122,6 @@ fun CommentNodeHeader(
onClick = onClick,
onLongCLick = onLongClick,
showAvatar = showAvatar,
showScores = showScores,
isDistinguished = commentView.comment.distinguished,
)
}
Expand All @@ -132,15 +131,19 @@ fun CommentNodeHeader(
fun CommentNodeHeaderPreview() {
CommentNodeHeader(
commentView = sampleCommentView,
score = 23,
myVote = 26,
instantScores = InstantScores(
score = 23,
upvotes = 21,
downvotes = 2,
myVote = 26,
),
voteDisplayMode = VoteDisplayMode.Full,
onPersonClick = {},
onClick = {},
onLongClick = {},
collapsedCommentsCount = 5,
isExpanded = false,
showAvatar = true,
showScores = true,
)
}

Expand Down Expand Up @@ -220,7 +223,7 @@ fun LazyListScope.commentNodeItem(
enableDownVotes: Boolean,
showAvatar: Boolean,
blurNSFW: BlurNSFW,
showScores: Boolean,
voteDisplayMode: VoteDisplayMode,
swipeToActionPreset: SwipeToActionPreset,
) {
val commentView = node.commentView
Expand Down Expand Up @@ -331,8 +334,8 @@ fun LazyListScope.commentNodeItem(
CommentNodeHeader(
commentView = commentView,
onPersonClick = onPersonClick,
score = instantScores.score,
myVote = instantScores.myVote,
instantScores = instantScores,
voteDisplayMode = voteDisplayMode,
onClick = {
onHeaderClick(commentView)
},
Expand All @@ -342,7 +345,6 @@ fun LazyListScope.commentNodeItem(
collapsedCommentsCount = commentView.counts.child_count,
isExpanded = isExpanded(commentId),
showAvatar = showAvatar,
showScores = showScores,
)
AnimatedVisibility(
visible = isExpanded(commentId) || showCollapsedCommentContent,
Expand Down Expand Up @@ -409,7 +411,6 @@ fun LazyListScope.commentNodeItem(
},
account = account,
enableDownVotes = enableDownVotes,
showScores = showScores,
viewSource = viewSource,
)
}
Expand Down Expand Up @@ -484,7 +485,7 @@ fun LazyListScope.commentNodeItem(
enableDownVotes = enableDownVotes,
showAvatar = showAvatar,
blurNSFW = blurNSFW,
showScores = showScores,
voteDisplayMode = voteDisplayMode,
admins = admins,
moderators = moderators,
swipeToActionPreset = swipeToActionPreset,
Expand Down Expand Up @@ -531,7 +532,7 @@ fun LazyListScope.missingCommentNodeItem(
enableDownVotes: Boolean,
showAvatar: Boolean,
blurNSFW: BlurNSFW,
showScores: Boolean,
voteDisplayMode: VoteDisplayMode,
swipeToActionPreset: SwipeToActionPreset,
) {
val commentId = node.missingCommentView.commentId
Expand Down Expand Up @@ -638,7 +639,7 @@ fun LazyListScope.missingCommentNodeItem(
enableDownVotes = enableDownVotes,
showAvatar = showAvatar,
blurNSFW = blurNSFW,
showScores = showScores,
voteDisplayMode = voteDisplayMode,
swipeToActionPreset = swipeToActionPreset,
)
}
Expand Down Expand Up @@ -765,7 +766,6 @@ fun CommentFooterLine(
onClick: () -> Unit,
onLongClick: () -> Unit,
account: Account,
showScores: Boolean,
viewSource: Boolean,
) {
var showMoreOptions by remember { mutableStateOf(false) }
Expand Down Expand Up @@ -838,18 +838,14 @@ fun CommentFooterLine(
) {
VoteGeneric(
myVote = instantScores.myVote,
votes = instantScores.upvotes,
type = VoteType.Upvote,
onVoteClick = onUpvoteClick,
showNumber = (instantScores.downvotes != 0L) && showScores,
account = account,
)
if (enableDownVotes) {
VoteGeneric(
myVote = instantScores.myVote,
votes = instantScores.downvotes,
type = VoteType.Downvote,
showNumber = showScores,
onVoteClick = onDownvoteClick,
account = account,
)
Expand Down Expand Up @@ -944,7 +940,7 @@ fun CommentNodesPreview() {
showAvatar = true,
blurNSFW = BlurNSFW.NSFW,
account = AnonAccount,
showScores = true,
voteDisplayMode = VoteDisplayMode.Full,
swipeToActionPreset = SwipeToActionPreset.DEFAULT,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.jerboa.CommentNode
import com.jerboa.CommentNodeData
import com.jerboa.MissingCommentNode
import com.jerboa.datatypes.BanFromCommunityData
import com.jerboa.datatypes.VoteDisplayMode
import com.jerboa.db.entity.Account
import com.jerboa.feat.BlurNSFW
import com.jerboa.feat.SwipeToActionPreset
Expand Down Expand Up @@ -66,7 +67,7 @@ fun CommentNodes(
enableDownVotes: Boolean,
showAvatar: Boolean,
blurNSFW: BlurNSFW,
showScores: Boolean,
voteDisplayMode: VoteDisplayMode,
swipeToActionPreset: SwipeToActionPreset,
) {
LazyColumn(state = listState) {
Expand Down Expand Up @@ -110,7 +111,7 @@ fun CommentNodes(
enableDownVotes = enableDownVotes,
showAvatar = showAvatar,
blurNSFW = blurNSFW,
showScores = showScores,
voteDisplayMode = voteDisplayMode,
swipeToActionPreset = swipeToActionPreset,
)
item {
Expand Down Expand Up @@ -159,7 +160,7 @@ fun LazyListScope.commentNodeItems(
enableDownVotes: Boolean,
showAvatar: Boolean,
blurNSFW: BlurNSFW,
showScores: Boolean,
voteDisplayMode: VoteDisplayMode,
swipeToActionPreset: SwipeToActionPreset,
) {
nodes.forEach { node ->
Expand Down Expand Up @@ -205,7 +206,7 @@ fun LazyListScope.commentNodeItems(
enableDownVotes = enableDownVotes,
showAvatar = showAvatar,
blurNSFW = blurNSFW,
showScores = showScores,
voteDisplayMode = voteDisplayMode,
swipeToActionPreset = swipeToActionPreset,
)

Expand Down Expand Up @@ -250,7 +251,7 @@ fun LazyListScope.commentNodeItems(
enableDownVotes = enableDownVotes,
showAvatar = showAvatar,
blurNSFW = blurNSFW,
showScores = showScores,
voteDisplayMode = voteDisplayMode,
swipeToActionPreset = swipeToActionPreset,
)
}
Expand Down
Loading

0 comments on commit 61e384b

Please sign in to comment.