fix: separate upvotes and downvotes in inbox

This commit is contained in:
Diego Beraldin 2023-10-20 08:38:34 +02:00
parent c406ff7b7f
commit cb0f030295
13 changed files with 82 additions and 3 deletions

View File

@ -38,6 +38,7 @@ fun InboxCard(
mention: PersonMentionModel,
type: InboxCardType,
autoLoadImages: Boolean = true,
separateUpAndDownVotes: Boolean = true,
postLayout: PostLayout = PostLayout.Card,
onOpenPost: (PostModel) -> Unit,
onOpenCreator: (UserModel) -> Unit,
@ -91,6 +92,9 @@ fun InboxCard(
autoLoadImages = autoLoadImages,
date = mention.publishDate,
score = mention.score,
upvotes = mention.upvotes,
downvotes = mention.downvotes,
separateUpAndDownVotes = separateUpAndDownVotes,
upVoted = mention.myVote > 0,
downVoted = mention.myVote < 0,
onOpenCommunity = onOpenCommunity,

View File

@ -22,6 +22,8 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.github.diegoberaldin.raccoonforlemmy.core.appearance.theme.Spacing
@ -40,7 +42,10 @@ fun InboxReplySubtitle(
community: CommunityModel? = null,
iconSize: Dp = 20.dp,
date: String? = null,
score: Int,
score: Int = 0,
upvotes: Int = 0,
downvotes: Int = 0,
separateUpAndDownVotes: Boolean = false,
upVoted: Boolean = false,
downVoted: Boolean = false,
onOpenCommunity: ((CommunityModel) -> Unit)? = null,
@ -198,7 +203,47 @@ fun InboxReplySubtitle(
),
)
Text(
text = "$score",
text = buildAnnotatedString {
if (separateUpAndDownVotes) {
val upvoteText = upvotes.toString()
append(upvoteText)
if (upVoted) {
addStyle(
style = SpanStyle(color = MaterialTheme.colorScheme.surfaceTint),
start = 0,
end = upvoteText.length
)
}
append(" / ")
val downvoteText = downvotes.toString()
append(downvoteText)
if (downVoted) {
addStyle(
style = SpanStyle(color = MaterialTheme.colorScheme.tertiary),
start = upvoteText.length + 3,
end = upvoteText.length + 3 + downvoteText.length
)
}
} else {
val text = score.toString()
append(text)
if (upVoted) {
addStyle(
style = SpanStyle(color = MaterialTheme.colorScheme.surfaceTint),
start = 0,
end = text.length
)
} else if (downVoted) {
addStyle(
style = SpanStyle(color = MaterialTheme.colorScheme.tertiary),
start = 0,
end = length
)
}
}
},
style = MaterialTheme.typography.labelLarge,
color = MaterialTheme.colorScheme.onBackground,
)
Image(
modifier = buttonModifier

View File

@ -387,6 +387,8 @@ class PostDetailScreen(
modifier = Modifier.padding(
top = Spacing.xxs,
bottom = Spacing.s,
start = Spacing.s,
end = Spacing.s,
),
horizontalArrangement = Arrangement.spacedBy(Spacing.xxs),
) {

View File

@ -77,7 +77,7 @@ internal class DefaultSettingsRepository(
keyStore[KeyStoreKeys.UiTheme, 0]
} else null,
uiFontScale = keyStore[KeyStoreKeys.UiFontScale, 1f],
uiFontFamily = keyStore[KeyStoreKeys.UiFontFamily, 1],
uiFontFamily = keyStore[KeyStoreKeys.UiFontFamily, 0],
contentFontScale = keyStore[KeyStoreKeys.ContentFontScale, 1f],
locale = keyStore[KeyStoreKeys.Locale, ""].takeIf { it.isNotEmpty() },
defaultListingType = keyStore[KeyStoreKeys.DefaultListingType, 0],

View File

@ -9,6 +9,8 @@ data class PersonMentionModel(
val comment: CommentModel,
val community: CommunityModel,
val score: Int,
val upvotes: Int,
val downvotes: Int,
val myVote: Int,
val saved: Boolean,
val isOwnPost: Boolean = false,

View File

@ -126,6 +126,14 @@ class CommentRepository(
!voted -> mention.score - 1
else -> mention.score
},
upvotes = when {
voted -> mention.upvotes + 1
else -> mention.upvotes - 1
},
downvotes = when {
mention.myVote < 0 -> mention.downvotes - 1
else -> mention.downvotes
}
)
suspend fun upVote(comment: CommentModel, auth: String, voted: Boolean) {
@ -163,6 +171,14 @@ class CommentRepository(
!downVoted -> mention.score + 1
else -> mention.score
},
downvotes = when {
downVoted -> mention.downvotes + 1
else -> mention.downvotes - 1
},
upvotes = when {
mention.myVote > 0 -> mention.upvotes - 1
else -> mention.upvotes
}
)
suspend fun downVote(comment: CommentModel, auth: String, downVoted: Boolean) = runCatching {

View File

@ -172,6 +172,8 @@ internal fun PersonMentionView.toModel() = PersonMentionModel(
myVote = myVote ?: 0,
saved = saved,
publishDate = personMention.published,
upvotes = counts.upvotes,
downvotes = counts.downvotes,
)
internal fun CommentReplyView.toModel() = PersonMentionModel(
@ -206,6 +208,8 @@ internal fun CommentReplyView.toModel() = PersonMentionModel(
myVote = myVote ?: 0,
saved = saved,
publishDate = commentReply.published,
upvotes = counts.upvotes,
downvotes = counts.downvotes,
)
internal fun PrivateMessageView.toModel() = PrivateMessageModel(

View File

@ -28,6 +28,7 @@ interface InboxMentionsMviModel :
val swipeActionsEnabled: Boolean = true,
val postLayout: PostLayout = PostLayout.Card,
val autoLoadImages: Boolean = true,
val separateUpAndDownVotes: Boolean = true,
)
sealed interface Effect {

View File

@ -171,6 +171,7 @@ class InboxMentionsScreen : Tab {
postLayout = uiState.postLayout,
type = InboxCardType.Mention,
autoLoadImages = uiState.autoLoadImages,
separateUpAndDownVotes = uiState.separateUpAndDownVotes,
onOpenPost = { post ->
navigator?.push(
PostDetailScreen(

View File

@ -67,6 +67,7 @@ class InboxMentionsViewModel(
it.copy(
swipeActionsEnabled = settings.enableSwipeActions,
autoLoadImages = settings.autoLoadImages,
separateUpAndDownVotes = settings.separateUpAndDownVotes,
)
}
}.launchIn(this)

View File

@ -28,6 +28,7 @@ interface InboxRepliesMviModel :
val postLayout: PostLayout = PostLayout.Card,
val swipeActionsEnabled: Boolean = true,
val autoLoadImages: Boolean = true,
val separateUpAndDownVotes: Boolean = true,
)
sealed interface Effect {

View File

@ -200,6 +200,7 @@ class InboxRepliesScreen : Tab {
postLayout = uiState.postLayout,
type = InboxCardType.Reply,
autoLoadImages = uiState.autoLoadImages,
separateUpAndDownVotes = uiState.separateUpAndDownVotes,
onOpenPost = { post ->
navigator?.push(
PostDetailScreen(

View File

@ -70,6 +70,7 @@ class InboxRepliesViewModel(
it.copy(
swipeActionsEnabled = settings.enableSwipeActions,
autoLoadImages = settings.autoLoadImages,
separateUpAndDownVotes = settings.separateUpAndDownVotes,
)
}
}.launchIn(this)