Skip to content
This repository has been archived by the owner on Feb 3, 2024. It is now read-only.

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MMauro94 committed Jul 24, 2023
1 parent 788e82d commit 33fd5d7
Show file tree
Hide file tree
Showing 16 changed files with 457 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ suspend inline fun <R : Any> ApiResult(
return try {
Result.Success(block())
} catch (e: ClientRequestException) {
System.err.println(e)
handleError(e, errorHandler)
} catch (e: Exception) {
System.err.println(e)
Result.Error(e)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package io.github.mmauro94.common.client.api

import io.github.mmauro94.common.client.ApiResult
import io.github.mmauro94.common.client.LemmyClient
import io.github.mmauro94.common.client.entities.CommentSortType
import io.github.mmauro94.common.client.entities.CommentView
import io.github.mmauro94.common.client.entities.ListingType
import io.github.mmauro94.common.client.entities.GetCommentsForm
import io.github.mmauro94.common.client.entities.GetCommentsFormContextualInfo
import io.github.mmauro94.common.utils.serialName
import io.ktor.client.call.body
import io.ktor.client.request.HttpRequestBuilder
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import kotlinx.serialization.Serializable
Expand All @@ -16,30 +17,32 @@ private data class GetCommentsBody(
val comments: List<CommentView>,
)

suspend fun LemmyClient.getComments(
communityId: Long? = null,
communityName: String? = null,
page: Int? = null,
limit: Int? = null,
maxDepth: Int? = null,
parentId: Long? = null,
postId: Long? = null,
savedOnly: Boolean? = null,
sort: CommentSortType? = null,
type: ListingType? = null,
): ApiResult<List<CommentView>> {
const val DEFAULT_COMMENTS_LIMIT = 50

suspend fun LemmyClient.getComments(form: GetCommentsForm): ApiResult<List<CommentView>> {
return ApiResult {
ktorClient.get("comment/list") {
parameter("community_id", communityId)
parameter("community_name", communityName)
parameter("page", page)
parameter("limit", limit)
parameter("max_depth", maxDepth)
parameter("parent_id", parentId)
parameter("post_id", postId)
parameter("saved_only", savedOnly)
parameter("sort", sort?.serialName())
parameter("type_", type?.serialName())
fill(form)
}.body<GetCommentsBody>().comments
}
}

private fun HttpRequestBuilder.fill(form: GetCommentsForm) {
when (form.context) {
is GetCommentsFormContextualInfo.CommunityId -> parameter("community_id", form.context.communityId)
is GetCommentsFormContextualInfo.CommunityName -> parameter("community_name", form.context.communityName)
is GetCommentsFormContextualInfo.ParentId -> parameter("parent_id", form.context.commentId)
is GetCommentsFormContextualInfo.PostId -> parameter("post_id", form.context.postId)
null -> {}
}
parameter("max_depth", form.maxDepth)

if (form.page != null) {
parameter("page", form.page.page)
parameter("limit", form.page.limit)
}

parameter("saved_only", form.userPreferences.savedOnly)
parameter("sort", form.userPreferences.sort?.serialName())
parameter("type_", form.userPreferences.type?.serialName())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.mmauro94.common.client.entities

data class GetCommentsForm(
val userPreferences: GetCommentsUserPreferenceInfo,
val context: GetCommentsFormContextualInfo? = null,
val page: GetCommentsFormPageInfo? = null,
val maxDepth: Int? = null,
) {

init {
require(maxDepth == null || maxDepth > 0) { "Invalid max depth: $maxDepth" }
}

fun withIncreasedPage(): GetCommentsForm {
check(page != null)
return copy(page = page.increased())
}
}

sealed interface GetCommentsFormContextualInfo {
data class CommunityId(
val communityId: Long,
) : GetCommentsFormContextualInfo

data class CommunityName(
val communityName: String,
) : GetCommentsFormContextualInfo

data class PostId(
val postId: Long,
) : GetCommentsFormContextualInfo

data class ParentId(
val commentId: Long,
) : GetCommentsFormContextualInfo
}

data class GetCommentsFormPageInfo(
val page: Int,
val limit: Int,
) {
init {
require(page > 0) { "Invalid page: $page" }
require(limit > 0) { "Invalid limit: $limit" }
}

fun increased(): GetCommentsFormPageInfo {
return copy(page = page + 1)
}
}

data class GetCommentsUserPreferenceInfo(
val savedOnly: Boolean? = null,
val sort: CommentSortType? = null,
val type: ListingType? = null,
)
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package io.github.mmauro94.common.ui.components
package io.github.mmauro94.common.ui.components.comments

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.BookmarkBorder
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.outlined.ModeComment
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -31,31 +25,26 @@ import io.github.mmauro94.common.MR
import io.github.mmauro94.common.client.entities.Comment
import io.github.mmauro94.common.client.entities.CommentView
import io.github.mmauro94.common.markdown.ReadOnlyMarkdown
import io.github.mmauro94.common.ui.components.FooterIcon
import io.github.mmauro94.common.ui.components.FooterLabeledIcon
import io.github.mmauro94.common.ui.components.FooterVoteIcons
import io.github.mmauro94.common.ui.onSurfaceLowlighted
import io.github.mmauro94.common.utils.LikeStatus
import io.github.mmauro94.common.utils.comments.CommentTree
import io.github.mmauro94.common.utils.relativeTimeString

@Composable
fun Comment(commentTree: CommentTree, depth: Int) {
val comment = commentTree.commentView.comment

Row(
modifier = Modifier
.height(IntrinsicSize.Max)
.fillMaxWidth()
.clickable { commentTree.collapsed = !commentTree.collapsed },
CommentContainer(
modifier = Modifier.clickable { commentTree.collapsed = !commentTree.collapsed },
depth = depth,
) {
Box(Modifier.width(4.dp * depth).fillMaxHeight().background(MaterialTheme.colorScheme.primary))
Box(Modifier.padding(top = 8.dp)) {
Column {
CommentHeader(commentTree.commentView)
CommentBody(commentTree.commentView.comment)
CommentFooter(commentTree.commentView)
}
Column(Modifier.padding(top = 8.dp)) {
CommentHeader(commentTree.commentView)
CommentBody(commentTree.commentView.comment)
CommentFooter(commentTree.commentView)
}
}
Divider()
}

@Composable
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.github.mmauro94.common.ui.components.comments

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

private val DEPTH_WIDTH = 4.dp

@Composable
fun CommentContainer(
depth: Int,
modifier: Modifier = Modifier,
content: @Composable () -> Unit,
) {
Row(
modifier = Modifier
.height(IntrinsicSize.Max)
.fillMaxWidth()
.then(modifier),
) {
Spacer(Modifier.width(DEPTH_WIDTH * (depth - 1)))
if (depth > 0) {
Box(Modifier.width(DEPTH_WIDTH).fillMaxHeight().background(MaterialTheme.colorScheme.primary))
}
content()
}
Divider()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.mmauro94.common.ui.components.comments

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import dev.icerock.moko.resources.compose.stringResource
import io.github.mmauro94.common.MR
import io.github.mmauro94.common.utils.LocalLemmyContext
import io.github.mmauro94.common.utils.comments.CommentList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@Composable
fun LoadMoreComments(
coroutineScope: CoroutineScope,
commentList: CommentList<*>,
depth: Int,
) {
val client = LocalLemmyContext.current.client
CommentContainer(
modifier = Modifier.clickable {
coroutineScope.launch { commentList.loadNext(client) }
},
depth = depth,
) {
Text(
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
text = stringResource(MR.strings.load_more_comments),
style = MaterialTheme.typography.bodyMedium,
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.mmauro94.common.ui.components.comments

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import dev.icerock.moko.resources.compose.stringResource
import io.github.mmauro94.common.MR

@Composable
fun LoadingComments(depth: Int) {
CommentContainer(depth = depth) {
Row(
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
verticalAlignment = Alignment.CenterVertically,
) {
CircularProgressIndicator(Modifier.size(16.dp), strokeWidth = 2.dp)
Text(
modifier = Modifier.padding(start = 8.dp),
text = stringResource(MR.strings.loading___),
style = MaterialTheme.typography.bodyMedium,
)
}
}
}
Loading

0 comments on commit 33fd5d7

Please sign in to comment.