Skip to content

Commit

Permalink
Got comment tree and voting working.
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines committed Jan 7, 2022
1 parent 6e724f7 commit 05fda4a
Show file tree
Hide file tree
Showing 17 changed files with 457 additions and 161 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ android {
dependencies {
def room_version = "2.4.0"

implementation 'com.github.jeziellago:compose-markdown:0.2.6'

// LiveData
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.4.0"
Expand Down
26 changes: 22 additions & 4 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.jerboa.api.API
import com.jerboa.datatypes.api.GetPost
import com.jerboa.datatypes.api.GetPosts
import com.jerboa.db.AccountRepository
import com.jerboa.db.AccountViewModel
import com.jerboa.db.AccountViewModelFactory
Expand Down Expand Up @@ -42,12 +44,13 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)

setContent {
val navController = rememberNavController()
val accounts by accountViewModel.allAccounts.observeAsState()
val currentAccount = getCurrentAccount(accounts)
val account = getCurrentAccount(accounts)

val navController = rememberNavController()

val startRoute = if (currentAccount != null) {
API.changeLemmyInstance(currentAccount.instance)
val startRoute = if (account != null) {
API.changeLemmyInstance(account.instance)
"home"
} else {
"login"
Expand All @@ -68,6 +71,13 @@ class MainActivity : ComponentActivity() {
)
}
composable(route = "home") {

postListingsViewModel.fetchPosts(
GetPosts(
auth = account?.jwt
)
)

HomeActivity(
navController = navController,
postListingsViewModel = postListingsViewModel,
Expand All @@ -78,6 +88,14 @@ class MainActivity : ComponentActivity() {
route = "post/{postId}",
) {
val postId = it.arguments?.getString("postId")!!.toInt()

postViewModel.fetchPost(
GetPost(
id = postId,
auth = account?.jwt,
)
)

PostActivity(
postId = postId,
postViewModel = postViewModel,
Expand Down
120 changes: 117 additions & 3 deletions app/src/main/java/com/jerboa/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,36 @@ package com.jerboa
import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Icon
import androidx.compose.material.LocalContentColor
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowDownward
import androidx.compose.material.icons.filled.ArrowUpward
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.graphics.ColorUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jerboa.datatypes.CommentView
import com.jerboa.db.Account
import com.jerboa.db.AccountViewModel
import com.jerboa.ui.theme.ACTION_BAR_ICON_SIZE
import com.jerboa.ui.theme.MEDIUM_PADDING
import com.jerboa.ui.theme.SMALL_PADDING
import dev.jeziellago.compose.markdowntext.MarkdownText
import org.ocpsoft.prettytime.PrettyTime
import java.util.*

Expand Down Expand Up @@ -75,6 +90,15 @@ fun downvoteColor(myVote: Int?): Color {
}
}

@Composable
fun scoreColor(myVote: Int?): Color {
return when (myVote) {
1 -> MaterialTheme.colors.secondary
-1 -> MaterialTheme.colors.error
else -> LocalContentColor.current
}
}

enum class VoteType {
Upvote,
Downvote,
Expand All @@ -99,7 +123,7 @@ fun newVote(currentVote: Int?, voteType: VoteType): Int {
data class CommentNodeData(
val commentView: CommentView,
val children: MutableList<CommentNodeData>?,
var depth: Number?,
var depth: Int?,
)

fun buildCommentsTree(
Expand Down Expand Up @@ -149,9 +173,99 @@ fun setDepth(node: CommentNodeData, i: Int = 0) {
}

@Composable
fun DotSpacer() {
fun DotSpacer(padding: Dp = MEDIUM_PADDING) {
Text(
text = "·",
modifier = Modifier.padding(horizontal = 8.dp)
modifier = Modifier.padding(horizontal = padding)
)
}

fun colorShade(color: Color, factor: Float): Color {
val hsl = FloatArray(3)
ColorUtils.colorToHSL(color.toArgb(), hsl)
hsl[2] *= factor
return Color(ColorUtils.HSLToColor(hsl))
}

val colorList = listOf(
hsl(0f),
hsl(100f),
hsl(150f),
hsl(200f),
hsl(250f),
hsl(300f),
)

fun hsl(num: Float): Color {
return Color(ColorUtils.HSLToColor(floatArrayOf(num, .35f, .5f)))
}

fun calculateCommentOffset(depth: Int?): Dp {
return if (depth == null) {
0.dp
} else {
((depth + 1) * 2).dp
}
}

@Composable
fun calculateBorderColor(depth: Int?): Color {
return if (depth == null) {
MaterialTheme.colors.background
} else {
colorList[depth.mod(colorList.size)]
}
}

@Composable
fun <T> VoteGeneric(
myVote: Int?,
votes: Int,
item: T,
type: VoteType,
onVoteClick: (item: T) -> Unit = {}
) {
val voteColor =
when (type) {
VoteType.Upvote -> upvoteColor(myVote = myVote)
else -> downvoteColor(myVote = myVote)
}
val voteIcon = when (type) {
VoteType.Upvote -> Icons.Default.ArrowUpward
else -> Icons.Default.ArrowDownward
}

Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.clickable(onClick = { onVoteClick(item) })
) {
Icon(
imageVector = voteIcon,
tint = voteColor,
contentDescription = "TODO",
modifier = Modifier
.size(ACTION_BAR_ICON_SIZE)
.padding(end = SMALL_PADDING)
)
Text(
text = votes.toString(),
style = MaterialTheme.typography.button,
color = voteColor,
)
}
}

@Composable
fun MyMarkdownText(
markdown: String,
modifier: Modifier = Modifier,
) {

// Note, this actually scales down the font size quite a lot, so you need to use a bigger one
MarkdownText(
markdown = markdown,
style = MaterialTheme.typography.body1,
fontSize = MaterialTheme.typography.subtitle1.fontSize,
modifier = modifier,
)
}
33 changes: 27 additions & 6 deletions app/src/main/java/com/jerboa/api/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.jerboa.api

import android.content.Context
import com.jerboa.VoteType
import com.jerboa.datatypes.CommentView
import com.jerboa.datatypes.PostView
import com.jerboa.datatypes.api.*
import com.jerboa.db.Account
Expand Down Expand Up @@ -45,6 +46,12 @@ interface API {
@POST("post/like")
suspend fun likePost(@Body form: CreatePostLike): PostResponse

/**
* Like / vote on a comment.
*/
@POST("comment/like")
suspend fun likeComment(@Body form: CreateCommentLike): CommentResponse

companion object {
private var api: API? = null
private var currentInstance: String = DEFAULT_INSTANCE
Expand Down Expand Up @@ -96,6 +103,26 @@ suspend fun likePostWrapper(
return updatedPost!!
}

suspend fun likeCommentWrapper(
cv: CommentView,
voteType: VoteType,
account: Account,
ctx: Context,
): CommentResponse {
var updatedComment: CommentResponse? = null
val api = API.getInstance()
try {
val newVote = newVote(currentVote = cv.my_vote, voteType = voteType)
val form = CreateCommentLike(
comment_id = cv.comment.id, score = newVote, auth = account.jwt
)
updatedComment = api.likeComment(form)
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
}
return updatedComment!!
}

//
// /**
// * Helps build lemmy HTTP requests.
Expand Down Expand Up @@ -380,12 +407,6 @@ suspend fun likePostWrapper(
// return this.wrapper(HttpType.Post, "/comment/mark_as_read", form);
// }
//
// /**
// * Like / vote on a comment.
// */
// async likeComment(form: CreateCommentLike): Promise<CommentResponse> {
// return this.wrapper(HttpType.Post, "/comment/like", form);
// }
//
// /**
// * Save a comment.
Expand Down
36 changes: 34 additions & 2 deletions app/src/main/java/com/jerboa/datatypes/SampleData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ val sampleComment = Comment(
creator_id = 56450,
post_id = 139549,
parent_id = null,
content = "This looks really cool and similar to Joplin. Having issues getting LaTeX to work.",
content = "This *looks* really cool and similar to Joplin. **Having issues** getting LaTeX to" +
" " +
"work" +
".\n\nIts kind of a long comment\n\nbut I don't want...",
removed = false,
read = false,
published = "2022-01-07T03:12:26.398434",
Expand All @@ -123,7 +126,22 @@ val sampleCommentReply = Comment(
creator_id = 423,
post_id = 139549,
parent_id = 1,
content = "This is a reply comment.",
content = "This is a reply comment.\n\n# This is a header\n\n- list one\n\n- list two",
removed = false,
read = false,
published = "2022-01-07T04:12:26.398434",
updated = "2022-01-07T03:15:37.360888",
deleted = false,
ap_id = "https://midwest.social/comment/24622",
local = false
)

val sampleSecondCommentReply = Comment(
id = 3,
creator_id = 423,
post_id = 139549,
parent_id = 2,
content = "This is a sub-reply comment, mmmmk",
removed = false,
read = false,
published = "2022-01-07T04:12:26.398434",
Expand Down Expand Up @@ -165,3 +183,17 @@ val sampleCommentReplyView = CommentView(
creator_blocked = false,
my_vote = null,
)

val sampleSecondCommentReplyView = CommentView(
comment = sampleSecondCommentReply,
creator = samplePersonSafe,
recipient = null,
post = samplePost,
community = sampleCommunitySafe,
counts = sampleCommentAggregates,
creator_banned_from_community = false,
subscribed = false,
saved = false,
creator_blocked = false,
my_vote = null,
)
1 change: 0 additions & 1 deletion app/src/main/java/com/jerboa/datatypes/api/Post.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.jerboa.datatypes.api

import androidx.compose.runtime.snapshots.SnapshotStateList
import com.jerboa.datatypes.*

data class CreatePost(
Expand Down
Loading

0 comments on commit 05fda4a

Please sign in to comment.