mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-17 23:47:02 +05:30
Rewrite LobstersItem
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
b229f1b1dc
commit
6f1a488b92
1 changed files with 108 additions and 72 deletions
|
@ -4,12 +4,14 @@ import androidx.compose.animation.Crossfade
|
||||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||||
import androidx.compose.foundation.background
|
import androidx.compose.foundation.background
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
|
||||||
import androidx.compose.foundation.layout.Box
|
import androidx.compose.foundation.layout.Box
|
||||||
import androidx.compose.foundation.layout.Column
|
import androidx.compose.foundation.layout.Column
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.Spacer
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.layout.requiredSize
|
import androidx.compose.foundation.layout.requiredSize
|
||||||
|
import androidx.compose.foundation.layout.width
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
import androidx.compose.foundation.lazy.LazyColumn
|
||||||
import androidx.compose.foundation.lazy.items
|
import androidx.compose.foundation.lazy.items
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
|
@ -55,97 +57,126 @@ fun LobstersItem(
|
||||||
viewPost: () -> Unit,
|
viewPost: () -> Unit,
|
||||||
viewComments: () -> Unit,
|
viewComments: () -> Unit,
|
||||||
toggleSave: () -> Unit,
|
toggleSave: () -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
Surface(
|
Surface(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.clickable { viewPost.invoke() },
|
.clickable { viewPost.invoke() }
|
||||||
|
.then(modifier),
|
||||||
) {
|
) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.padding(horizontal = 12.dp, vertical = 4.dp),
|
||||||
|
) {
|
||||||
|
PostTitle(
|
||||||
|
title = post.title,
|
||||||
|
modifier = Modifier
|
||||||
|
.padding(bottom = 4.dp),
|
||||||
|
)
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier.padding(start = 12.dp),
|
modifier = Modifier
|
||||||
|
.fillMaxWidth(),
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
horizontalArrangement = Arrangement.SpaceBetween,
|
|
||||||
) {
|
) {
|
||||||
Box(
|
TagRow(
|
||||||
modifier = Modifier.weight(0.8f),
|
tags = post.tags,
|
||||||
) {
|
modifier = Modifier.weight(0.65f),
|
||||||
PostDetails(
|
|
||||||
post,
|
|
||||||
)
|
)
|
||||||
}
|
|
||||||
Box(
|
|
||||||
modifier = Modifier.weight(0.1f),
|
|
||||||
) {
|
|
||||||
SaveButton(
|
SaveButton(
|
||||||
isSaved,
|
isSaved = isSaved,
|
||||||
toggleSave,
|
onClick = toggleSave,
|
||||||
|
)
|
||||||
|
Spacer(
|
||||||
|
modifier = Modifier.width(8.dp),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
Box(
|
|
||||||
modifier = Modifier.weight(0.1f),
|
|
||||||
) {
|
|
||||||
CommentsButton(
|
CommentsButton(
|
||||||
onClick = viewComments,
|
onClick = viewComments,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
SubmitterName(
|
||||||
|
name = post.submitterName,
|
||||||
|
avatarUrl = post.submitterAvatarUrl,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun PostDetails(
|
fun PostTitle(
|
||||||
post: SavedPost,
|
title: String,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
Column(
|
|
||||||
modifier = Modifier.padding(top = 8.dp, bottom = 8.dp),
|
|
||||||
) {
|
|
||||||
Text(
|
Text(
|
||||||
text = post.title,
|
text = title,
|
||||||
color = titleColor,
|
color = titleColor,
|
||||||
fontWeight = FontWeight.Bold,
|
fontWeight = FontWeight.Bold,
|
||||||
modifier = Modifier
|
modifier = Modifier.then(modifier),
|
||||||
.padding(bottom = 4.dp),
|
|
||||||
)
|
|
||||||
TagRow(
|
|
||||||
tags = post.tags,
|
|
||||||
modifier = Modifier
|
|
||||||
.padding(bottom = 4.dp),
|
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SubmitterName(
|
||||||
|
name: String,
|
||||||
|
avatarUrl: String,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
) {
|
||||||
Row(
|
Row(
|
||||||
|
modifier = Modifier.then(modifier),
|
||||||
verticalAlignment = Alignment.CenterVertically,
|
verticalAlignment = Alignment.CenterVertically,
|
||||||
) {
|
) {
|
||||||
|
SubmitterAvatar(
|
||||||
|
name = name,
|
||||||
|
avatarUrl = avatarUrl,
|
||||||
|
)
|
||||||
|
SubmitterNameText(
|
||||||
|
name = name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SubmitterAvatar(
|
||||||
|
name: String,
|
||||||
|
avatarUrl: String,
|
||||||
|
) {
|
||||||
CoilImage(
|
CoilImage(
|
||||||
data = "${LobstersApi.BASE_URL}/${post.submitterAvatarUrl}",
|
data = "${LobstersApi.BASE_URL}/$avatarUrl",
|
||||||
contentDescription = stringResource(
|
contentDescription = stringResource(
|
||||||
R.string.avatar_content_description,
|
R.string.avatar_content_description,
|
||||||
post.submitterName
|
name,
|
||||||
),
|
),
|
||||||
fadeIn = true,
|
fadeIn = true,
|
||||||
requestBuilder = {
|
requestBuilder = {
|
||||||
transformations(CircleCropTransformation())
|
transformations(CircleCropTransformation())
|
||||||
},
|
},
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.requiredSize(24.dp)
|
.requiredSize(24.dp),
|
||||||
.padding(bottom = 4.dp),
|
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun SubmitterNameText(
|
||||||
|
name: String,
|
||||||
|
) {
|
||||||
Text(
|
Text(
|
||||||
text = stringResource(id = R.string.submitted_by, post.submitterName),
|
text = stringResource(id = R.string.submitted_by, name),
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(start = 4.dp),
|
.padding(start = 4.dp),
|
||||||
)
|
)
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun SaveButton(
|
fun SaveButton(
|
||||||
isSaved: Boolean,
|
isSaved: Boolean,
|
||||||
onSaveButtonClick: () -> Unit,
|
onClick: () -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
IconToggleButton(
|
IconToggleButton(
|
||||||
checked = isSaved,
|
checked = isSaved,
|
||||||
onCheckedChange = { onSaveButtonClick.invoke() },
|
onCheckedChange = { onClick.invoke() },
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.requiredSize(24.dp),
|
.requiredSize(32.dp)
|
||||||
|
.then(modifier),
|
||||||
) {
|
) {
|
||||||
Crossfade(targetState = isSaved) { saved ->
|
Crossfade(targetState = isSaved) { saved ->
|
||||||
IconResource(
|
IconResource(
|
||||||
|
@ -160,11 +191,13 @@ fun SaveButton(
|
||||||
@Composable
|
@Composable
|
||||||
fun CommentsButton(
|
fun CommentsButton(
|
||||||
onClick: () -> Unit,
|
onClick: () -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
IconButton(
|
IconButton(
|
||||||
onClick = onClick,
|
onClick = onClick,
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.requiredSize(24.dp),
|
.requiredSize(32.dp)
|
||||||
|
.then(modifier),
|
||||||
) {
|
) {
|
||||||
IconResource(
|
IconResource(
|
||||||
resourceId = R.drawable.ic_insert_comment_24px,
|
resourceId = R.drawable.ic_insert_comment_24px,
|
||||||
|
@ -179,8 +212,10 @@ fun TagRow(
|
||||||
tags: List<String>,
|
tags: List<String>,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
) {
|
) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier.then(modifier),
|
||||||
|
) {
|
||||||
FlowLayout(
|
FlowLayout(
|
||||||
modifier = Modifier.then(modifier),
|
|
||||||
horizontalSpacing = 8.dp,
|
horizontalSpacing = 8.dp,
|
||||||
verticalSpacing = 8.dp,
|
verticalSpacing = 8.dp,
|
||||||
) {
|
) {
|
||||||
|
@ -194,6 +229,7 @@ fun TagRow(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue