Skip to content

Commit

Permalink
Don't pass user to avatar composables
Browse files Browse the repository at this point in the history
  • Loading branch information
nbradbury committed Oct 2, 2024
1 parent b528d7c commit 4f54cbc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,32 @@ import coil.compose.AsyncImage
import coil.request.ImageRequest
import org.wordpress.android.R
import org.wordpress.android.ui.compose.theme.M3Theme
import uniffi.wp_api.UserWithEditContext

@Composable
fun UserAvatar(
user: UserWithEditContext,
onUserAvatarClick: ((UserWithEditContext) -> Unit)? = null,
avatarUrl: String?,
onAvatarClick: ((String?) -> Unit)? = null,
) {
val avatarUrl = user.avatarUrls?.values?.firstOrNull() ?: ""
if (avatarUrl.isEmpty()) {
val extraModifier = if (onAvatarClick != null) {
Modifier.clickable {
onAvatarClick(avatarUrl)
}
} else {
Modifier
}

if (avatarUrl.isNullOrEmpty()) {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_user_placeholder_primary_24),
contentDescription = null,
tint = MaterialTheme.colorScheme.onSurface,
modifier = Modifier
.size(48.dp)
.then(
extraModifier
)
)
} else {
val extraModifier = if (onUserAvatarClick != null) {
Modifier.clickable {
onUserAvatarClick(user)
}
} else {
Modifier
}
AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(avatarUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class SelfHostedUsersActivity : LocaleAwareActivity() {
onUserClick = { user ->
viewModel.onUserClick(user)
},
onUserAvatarClick = { user ->
viewModel.onUserAvatarClick(user)
onUserAvatarClick = { avatarUrl ->
viewModel.onUserAvatarClick(avatarUrl)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun SelfHostedUsersScreen(
uiState: StateFlow<SelfHostedUserState>,
onCloseClick: () -> Unit = {},
onUserClick: (UserWithEditContext) -> Unit = {},
onUserAvatarClick: (UserWithEditContext) -> Unit = {},
onUserAvatarClick: (avatarUrl: String?) -> Unit = {},
) {
val state = uiState.collectAsState().value

Expand Down Expand Up @@ -126,7 +126,7 @@ private fun UserLazyRow(
.clickable { onUserClick(user) }
) {
item {
UserAvatar(user)
UserAvatar(user.avatarUrls?.values?.firstOrNull())
}

item {
Expand Down Expand Up @@ -159,7 +159,7 @@ private fun UserLazyRow(
@Composable
private fun UserDetail(
user: UserWithEditContext,
onUserAvatarClick: (UserWithEditContext) -> Unit = {},
onAvatarClick: (String?) -> Unit = {},
) {
Row(
modifier = Modifier
Expand All @@ -168,8 +168,8 @@ private fun UserDetail(
) {
Column {
UserAvatar(
user,
onUserAvatarClick
avatarUrl = user.avatarUrls?.values?.firstOrNull(),
onAvatarClick = onAvatarClick
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class SelfHostedUsersViewModel @Inject constructor(
/**
* Called when the avatar of a user is clicked in the detail screen
*/
fun onUserAvatarClick(user: UserWithEditContext) {
user.avatarUrls?.values?.firstOrNull()?.let { avatarUrl ->
_uiState.value = SelfHostedUserState.UserAvatar(avatarUrl)
fun onUserAvatarClick(avatarUrl: String?) {
avatarUrl?.let {
_uiState.value = SelfHostedUserState.UserAvatar(it)
}
}

Expand Down

0 comments on commit 4f54cbc

Please sign in to comment.